//+------------------------------------------------------------------+
//| CustomSymbolSetDouble.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() // 自定义交易品种所基于的交易品种名称
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 取得创建自定义交易品种时的错误代码
int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);
//--- 如果错误代码不为0 (创建交易品种成功) 也不为 5304 (交易品种已经被创建) - 退出
if(create!=0 && create!=5304)
return;
//--- 读取并在日志中打印自定义交易品种所基于交易品种的属性
//--- (最小交易量,最大交易量,执行交易的最小交易量改变步长)
double origin_vol_min = SymbolInfoDouble(CUSTOM_SYMBOL_ORIGIN, SYMBOL_VOLUME_MIN);
double origin_vol_max = SymbolInfoDouble(CUSTOM_SYMBOL_ORIGIN, SYMBOL_VOLUME_MAX);
double origin_vol_step= SymbolInfoDouble(CUSTOM_SYMBOL_ORIGIN, SYMBOL_VOLUME_STEP);
PrintFormat("The '%s' symbol from which the custom '%s' was created:\n"+
" Volume Min: %.2f\n Volume Max: %.2f\n Volume Step: %.2f",
CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME,
origin_vol_min, origin_vol_max, origin_vol_step);
//--- 设置自定义交易品种属性的其它数值
ResetLastError();
bool res=true;
res &=CustomSymbolSetDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_MIN, 0.1);
res &=CustomSymbolSetDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_MAX, 1000);
res &=CustomSymbolSetDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_STEP, 0.1);
//--- 如果在设置属性时出错,就在日志中显示相应消息
if(!res)
Print("CustomSymbolSetDouble() failed. Error ", GetLastError());
//--- 读取并在日志中打印修改过的自定义交易品种属性
//--- (最小交易量,最大交易量,执行交易的最小交易量改变步长)
double custom_vol_min = SymbolInfoDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_MIN);
double custom_vol_max = SymbolInfoDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_MAX);
double custom_vol_step= SymbolInfoDouble(CUSTOM_SYMBOL_NAME, SYMBOL_VOLUME_STEP);
PrintFormat("Custom symbol '%s' based on '%s':\n"+
" Volume Min: %.2f\n Volume Max: %.2f\n Volume Step: %.2f",
CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME,
custom_vol_min, custom_vol_max, custom_vol_step);
//--- 在图表注释区显示脚本终止键的提示
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)
{
if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
break;
}
}
//--- 退出前清空图表
Comment("");
/*
结果:
The 'EURUSD' symbol from which the custom 'EURUSD.C' was created:
Volume Min: 0.01
Volume Max: 500.00
Volume Step: 0.01
Custom symbol 'EURUSD' based on 'EURUSD.C':
Volume Min: 0.10
Volume Max: 1000.00
Volume Step: 0.10
*/
}
//+------------------------------------------------------------------+
//| 创建一个自定义交易品种, 返回错误代码 |
//+------------------------------------------------------------------+
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);
}
|