//+------------------------------------------------------------------+
//| CustomSymbolSetInteger.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;
//--- 读取并在日志中打印自定义交易品种所基于交易品种的属性
//--- (交易模式, 止损单价格水平和交易冻结水平)
ENUM_SYMBOL_TRADE_EXECUTION origin_exe_mode = (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(CUSTOM_SYMBOL_ORIGIN, SYMBOL_TRADE_EXEMODE);
int origin_stops_level = (int)SymbolInfoInteger(CUSTOM_SYMBOL_ORIGIN, SYMBOL_TRADE_STOPS_LEVEL);
int origin_freeze_level= (int)SymbolInfoInteger(CUSTOM_SYMBOL_ORIGIN, SYMBOL_TRADE_FREEZE_LEVEL);
PrintFormat("The '%s' symbol from which the custom '%s' was created:\n"+
" Deal execution mode: %s\n Stops Level: %d\n Freeze Level: %d",
CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME,
StringSubstr(EnumToString(origin_exe_mode), 23), origin_stops_level, origin_freeze_level);
//--- 设置自定义交易品种属性的其它数值
ResetLastError();
bool res=true;
res &=CustomSymbolSetInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_EXEMODE, SYMBOL_TRADE_EXECUTION_MARKET);
res &=CustomSymbolSetInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_STOPS_LEVEL, 10);
res &=CustomSymbolSetInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_FREEZE_LEVEL, 3);
//--- 如果在设置属性时出错,就在日志中显示相应消息
if(!res)
Print("CustomSymbolSetInteger() failed. Error ", GetLastError());
//--- 读取并在日志中打印修改过的自定义交易品种属性
//--- (交易模式, 止损单价格水平和交易冻结水平)
ENUM_SYMBOL_TRADE_EXECUTION custom_exe_mode = (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_EXEMODE);
int custom_stops_level = (int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_STOPS_LEVEL);
int custom_freeze_level= (int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_TRADE_FREEZE_LEVEL);
PrintFormat("Custom symbol '%s' based on '%s':\n"+
" Deal execution mode: %s\n Stops Level: %d\n Freeze Level: %d",
CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_ORIGIN,
StringSubstr(EnumToString(custom_exe_mode), 23), custom_stops_level, custom_freeze_level);
//--- 在图表注释区显示脚本终止键的提示
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:
Deal execution mode: INSTANT
Stops Level: 0
Freeze Level: 0
Custom symbol 'EURUSD.C' based on 'EURUSD':
Deal execution mode: MARKET
Stops Level: 10
Freeze Level: 3
*/
}
//+------------------------------------------------------------------+
//| 创建一个自定义交易品种, 返回错误代码 |
//+------------------------------------------------------------------+
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);
}
|