//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 在账户中的所有仓位中循环
int total=PositionsTotal();
for(int i=0; i<total; i++)
{
//--- 通过自动选择仓位取得下一个仓位的编号以访问它的属性
ulong ticket=PositionGetTicket(i);
if(ticket==0)
continue;
//--- 取得仓位类型并显示用于仓位字符串属性列表的标题
string type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE ? "Buy" : "Sell");
PrintFormat("String properties of an open position %s #%I64u:", type, ticket);
//--- 在标题下方打印选定仓位的所有字符串型属性
PositionPropertiesStringPrint(15);
}
/*
结果:
String properties of an open position Buy #2810798881:
Symbol: EURUSD
Comment: Test PositionGetString
External ID:
*/
}
//+------------------------------------------------------------------+
//| 在日志中显示选定仓位的字符串型属性 |
//+------------------------------------------------------------------+
void PositionPropertiesStringPrint(const uint header_width=0)
{
uint w=0;
string header="";
string value="";
//--- 定义标题文字和标题栏位的宽度
//--- 如果传给函数的标题宽度为0, 则宽度为标题文字行的大小+1
header="Symbol:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- 取得并在日志中以指定标题宽度显示仓位的交易品种
if(!PositionGetString(POSITION_SYMBOL, value))
return;
PrintFormat("%-*s%-s", w, header, value);
//--- 在日志中显示仓位注释
header="Comment:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetString(POSITION_COMMENT, value))
return;
PrintFormat("%-*s%-s", w, header, value);
//--- 在日志中显示外部系统中的仓位 ID
header="External ID:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetString(POSITION_EXTERNAL_ID, value))
return;
PrintFormat("%-*s%-s", w, header, value);
}
|