//+------------------------------------------------------------------+
//| CustomRatesReplace.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=Bars(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1);
//--- 把标准交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组
MqlRates rates[]={};
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars, GetLastError());
return;
}
//--- 把复制的数据设为自定义交易品种的分钟历史
ResetLastError();
if(CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates)<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- 在更新历史数据之后,读取自定义交易品种的柱形数量
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- 把自定义交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
return;
}
//--- 把自定义交易品种1分钟历史中的最新 DATARATES_COUNT 个柱的数据打印到日志中
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-DATARATES_COUNT, DATARATES_COUNT);
//--- 修改自定义交易品种1分钟历史中从倒数第二个柱开始的两个柱的数据
datetime time_from= rates[bars-3].time;
datetime time_to = rates[bars-2].time;
//--- 使从倒数第二个柱开始的两个柱的所有价格等于这两个柱在 'rates' 数组中的开盘价格
rates[bars-3].high=rates[bars-3].open;
rates[bars-3].low=rates[bars-3].open;
rates[bars-3].close=rates[bars-3].open;
rates[bars-2].high=rates[bars-2].open;
rates[bars-2].low=rates[bars-2].open;
rates[bars-2].close=rates[bars-2].open;
//--- 使用修改过的 'rates' 数组中的数据替换已有柱的数据
ResetLastError();
int replaced=CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates);
if(replaced<0)
{
PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
return;
}
//--- 在修改了两个柱的历史数据后,再次读取自定义交易品种的柱数
bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);
//--- 再次读取自定义交易品种分钟时段柱的全部数据
ResetLastError();
if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
{
PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
return;
}
//--- 在日志中打印更新后的自定义交易品种1分钟历史中的最后 DATARATES_COUNT 个柱
PrintFormat("\nLast %d bars after applying CustomRatesUpdate() with %d replaced bars:", DATARATES_COUNT, replaced);
ArrayPrint(rates, digits, NULL, bars-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("");
/*
结果:
Last 4 bars of the custom symbol's minute history:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390 16 1 0
[1] 2024.07.29 13:38:00 1.08389 1.08400 1.08389 1.08398 35 1 0
[2] 2024.07.29 13:39:00 1.08398 1.08410 1.08394 1.08410 29 1 0
[3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414 14 1 0
Last 4 bars after applying CustomRatesUpdate() with 250820 replaced bars:
[time] [open] [high] [low] [close] [tick_volume] [spread] [real_volume]
[0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390 16 1 0
[1] 2024.07.29 13:38:00 1.08389 1.08389 1.08389 1.08389 35 1 0
[2] 2024.07.29 13:39:00 1.08398 1.08398 1.08398 1.08398 29 1 0
[3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414 14 1 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);
}
|