SymbolSelect
SymbolSelect
在市场观测中选择一个交易品种,或者从窗口中移动一个交易品种。
bool SymbolSelect(
string name, // 交易品种名称
bool select // 添加或者移除
);参量
- 名称
[in] 交易品种名称。
- select
[in]转换,如果值是false,交易品种从市场观测中删除,然后在窗口中选择一个交易品种,如果交易品种共图表是开放式的,交易品种就不能被删除,或者该交易品种有开放定位。
返回值
以防返回失败。
示例:
#define SYMBOL_NAME "GBPHKD"
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 检查列表中交易品种是否存在,如果没有找到,进行报告并完成工作
bool custom = false;
if(!SymbolExist(SYMBOL_NAME, custom))
{
PrintFormat("'%s' symbol not found in the lists", SYMBOL_NAME);
return;
}
//--- 将交易品种添加到市场报价窗口
ResetLastError();
if(!SymbolSelect(SYMBOL_NAME, true))
{
Print("SymbolSelect() failed. Error ", GetLastError());
return;
}
//--- 如果交易品种被成功地添加到列表中,请在市场报价窗口中获取它的索引号,并将结果发送到日志
int index = SymbolIndex(SYMBOL_NAME);
PrintFormat("The '%s' symbol has been added to the MarketWatch list. Symbol index in the list: %d", SYMBOL_NAME, index);
//--- 现在从市场报价窗口移除交易品种
ResetLastError();
if(!SymbolSelect(SYMBOL_NAME, false))
{
Print("SymbolSelect() failed. Error ", GetLastError());
return;
}
//--- 如果交易品种被成功地从列表中移除,则市场报价窗口的索引号-1,并将删除结果发送到日志
index = SymbolIndex(SYMBOL_NAME);
PrintFormat("The '%s' symbol has been removed from the MarketWatch list. Symbol index in the list: %d", SYMBOL_NAME, index);
/*
result:
The 'GBPHKD' symbol has been added to the MarketWatch list. Symbol index in the list: 12
The 'GBPHKD' symbol has been removed from the MarketWatch list. Symbol index in the list: -1
*/
}
//+------------------------------------------------------------------+
//| 返回交易品种在市场报价交易品种列表中的索引号 |
//+------------------------------------------------------------------+
int SymbolIndex(const string symbol)
{
int total = SymbolsTotal(true);
for(int i=0; i<total; i++)
{
string name = SymbolName(i, true);
if(name == symbol)
return i;
}
return(WRONG_VALUE);
}