//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
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("Double properties of an open position %s #%I64u:", type, ticket);
//--- 在标题下方打印选定仓位的全部实数属性
PositionPropertiesDoublePrint(15);
}
/*
结果:
Double properties of an open position Buy #2807075208:
Volume: 1.00
Price open: 1.10516
StopLoss: 0.00000
TakeProfit: 0.00000
Price current: 1.10518
Swap: 0.00
Profit: 2.00 USD
*/
}
//+------------------------------------------------------------------+
//| 在日志中显示选定仓位的实数属性 |
//+------------------------------------------------------------------+
void PositionPropertiesDoublePrint(const uint header_width=0)
{
uint w=0;
string header="";
double value=0;
//--- 取得账户货币, 仓位的交易品种和交易品种的小数点位数
string currency=AccountInfoString(ACCOUNT_CURRENCY);
string symbol =PositionGetString(POSITION_SYMBOL);
int digits =(int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- 定义标题文字和标题栏位的宽度
//--- 如果传给函数的标题宽度为0, 则宽度为标题文字行的大小+1
header="Volume:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- 取得并在日志中以指定的标题宽度显示仓位交易量
if(!PositionGetDouble(POSITION_VOLUME, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 在日志中显示仓位价格值
header="Price open:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_OPEN, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示止损值
header="StopLoss:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SL, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示止盈值
header="TakeProfit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_TP, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示当前价格值
header="Price current:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_CURRENT, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示累积的库存费
header="Swap:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SWAP, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 在日志中显示当前利润值
header="Profit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PROFIT, value))
return;
PrintFormat("%-*s%-.2f %s", w, header, value, currency);
}
|