//+------------------------------------------------------------------+ //| CustomRatesUpdate.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #define CUSTOM_SYMBOL_NAME Symbol()+".C" // 自定义交易品种名称 #define CUSTOM_SYMBOL_PATH "Forex" // 创建的交易品种所在组的名称 #define CUSTOM_SYMBOL_ORIGIN Symbol() // 自定义交易品种的基础交易品种名称 #define DATARATES_COUNT 4 // 发送到日志的柱形数量 //+------------------------------------------------------------------+ //| 脚本程序起始函数 | //+------------------------------------------------------------------+ void OnStart() { //--- 取得创建自定义交易品种时的错误代码 int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN); //--- 如果错误代码不为0 (创建交易品种成功) 也不为 5304 (交易品种已经被创建) - 退出 if(create!=0 && create!=5304) return; //--- 读取并在日志中打印标准交易品种柱形数量 int bars_origin=Bars(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1); PrintFormat("The symbol '%s' from which the custom '%s' was created has %d bars of minute history.", CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME, bars_origin); //--- 读取并在日志中打印自定义交易品种柱形数量 int bars_custom=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1); PrintFormat("Custom symbol '%s' created from symbol '%s' has %d bars of minute history", CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_ORIGIN, bars_custom); //--- 把标准交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组 MqlRates rates[]={}; ResetLastError(); if(CopyRates(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1, 0, bars_origin, rates)!=bars_origin) { PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars_origin, GetLastError()); return; } //--- 把复制的数据设为自定义交易品种的分钟历史 ResetLastError(); int updated=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates); if(updated<0) { PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError()); return; } //--- 在添加历史后读取并在日志中打印自定义交易品种的柱形数量 bars_custom=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1); PrintFormat("\nAfter CustomRatesUpdate(), the custom symbol '%s' has %d bars of minute history", CUSTOM_SYMBOL_NAME, bars_custom); //--- 把自定义交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组 ResetLastError(); if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars_custom, rates)!=bars_custom) { PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars_custom, GetLastError()); return; } //--- 在日志中打印自定义交易品种分钟历史的最后四个柱形 int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_DIGITS); PrintFormat("Last %d bars of the custom symbol's minute history:", DATARATES_COUNT); ArrayPrint(rates, digits, NULL, bars_custom-DATARATES_COUNT, DATARATES_COUNT); //--- 把 MqlRates 数组中的数据替换成计算后的数据,公式为 1.0 / SymbolName for(int i=0; i<bars_custom; i++) { rates[i].open =(rates[i].open !=0 ? 1.0 / rates[i].open : rates[i].open); rates[i].high =(rates[i].high !=0 ? 1.0 / rates[i].high : rates[i].high); rates[i].low =(rates[i].low !=0 ? 1.0 / rates[i].low : rates[i].low); rates[i].close =(rates[i].close!=0 ? 1.0 / rates[i].close : rates[i].close); } //--- 把修改过的数据设置为自定义交易品种的分钟历史 ResetLastError(); updated=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates); if(updated<0) { PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError()); return; } //--- 再次把自定义交易品种分钟时段的所有柱形数据读取至 MqlRates 数组 ResetLastError(); if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars_custom, rates)!=bars_custom) { PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars_custom, GetLastError()); return; } //--- 在日志中打印更新过的自定义交易品种分钟历史中的最后四个柱形 Print("\nLast %d bars after changing the custom symbol calculation formula:", DATARATES_COUNT); ArrayPrint(rates, digits, NULL, bars_custom-DATARATES_COUNT, DATARATES_COUNT); //--- 在图表注释区显示脚本终止键的提示 Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit", CUSTOM_SYMBOL_NAME)); //--- 在无尽循环中等待按下 Esc 或 Del 键 while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0) { Sleep(16); //--- 当按下 Del 时, 删除所创建的自定义交易品种和它的数据 if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0) { //--- 删除柱形数据 int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX); if(deleted>0) PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_SYMBOL_NAME); //--- 删除报价数据 deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX); if(deleted>0) //--- 删除交易品种 if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME)) PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME); break; } } //--- 退出前清空图表 Comment(""); /* 结果: The symbol 'EURUSD' from which the custom 'EURUSD.C' was created has 250488 bars of minute history. Custom symbol 'EURUSD.C' created from symbol 'EURUSD' has 0 bars of minute history After CustomRatesUpdate(), the custom symbol 'EURUSD.C' has 250488 bars of minute history Last 4 bars of the custom symbol's minute history: [time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume] [0] 2024.06.18 11:14:00 1.07235 1.07239 1.07232 1.07239 24 0 0 [1] 2024.06.18 11:15:00 1.07238 1.07239 1.07232 1.07235 44 0 0 [2] 2024.06.18 11:16:00 1.07234 1.07238 1.07227 1.07234 37 0 0 [3] 2024.06.18 11:17:00 1.07234 1.07234 1.07217 1.07225 41 0 0 Last 4 bars after changing the custom symbol calculation formula: [time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume] [0] 2024.06.18 11:14:00 0.93253 0.93250 0.93256 0.93250 24 0 0 [1] 2024.06.18 11:15:00 0.93251 0.93250 0.93256 0.93253 44 0 0 [2] 2024.06.18 11:16:00 0.93254 0.93251 0.93260 0.93254 37 0 0 [3] 2024.06.18 11:17:00 0.93254 0.93254 0.93269 0.93262 41 0 0 */ } //+------------------------------------------------------------------+ //| 创建一个自定义交易品种, 返回错误代码 | //+------------------------------------------------------------------+ int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL) { //--- 定义自定义交易品种所基于的交易品种名称 string origin=(symbol_origin==NULL ? Symbol() : symbol_origin); //--- 如果创建自定义交易品种失败并且错误代码不是 5304, 在日志中报告 ResetLastError(); int error=0; if(!CustomSymbolCreate(symbol_name, symbol_path, origin)) { error=GetLastError(); if(error!=5304) PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error); } //--- 成功 return(error); } //+------------------------------------------------------------------+ //| 删除一个自定义交易品种 | //+------------------------------------------------------------------+ bool DeleteCustomSymbol(const string symbol_name) { //--- 从市场报价窗口中隐藏交易品种 ResetLastError(); if(!SymbolSelect(symbol_name, false)) { PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError()); return(false); } //--- 如果删除自定义交易品种失败,在日志中报告并返回 'false' ResetLastError(); if(!CustomSymbolDelete(symbol_name)) { PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError()); return(false); } //--- 成功 return(true); } |