//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 请求交易和订单历史
if(!HistorySelect(0, TimeCurrent()))
{
Print("HistorySelect() failed. Error ", GetLastError());
return;
}
//--- 在账户的全部历史订单中做循环
int total=HistoryOrdersTotal();
for(int i=0; i<total; i++)
{
//--- 根据循环索引取得列表中的订单编号
ulong ticket=HistoryOrderGetTicket(i);
if(ticket==0)
continue;
//--- 取得订单类型并显示用于选中订单实数型属性列表的标题
string type=OrderTypeDescription((ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticket, ORDER_TYPE));
PrintFormat("Double properties of an history order %s #%I64u:", type, ticket);
//--- 在标题下方打印选中订单的全部实数型属性
HistoryOrderPropertiesDoublePrint(ticket, 16);
}
/*
结果:
Double properties of an history order Sell #2810847541:
Volume initial: 0.50
Volume current: 0.00
Price open: 1.10491
StopLoss: 0.00000
TakeProfit: 0.00000
Price current: 1.10491
StopLimit: 0.00000
Double properties of an history order Buy Limit #2811003507:
Volume initial: 1.00
Volume current: 1.00
Price open: 1.10547
StopLoss: 0.00000
TakeProfit: 0.00000
Price current: 1.10591
StopLimit: 0.00000
*/
}
//+------------------------------------------------------------------+
//| 显示实数型属性 |
//| 适用于日志和选中的历史订单 |
//+------------------------------------------------------------------+
void HistoryOrderPropertiesDoublePrint(const long ticket, const uint header_width=0)
{
uint w=0;
string header="";
double value=0;
//--- 取得订单的交易品种和交易品种的小数位数
string symbol = HistoryOrderGetString(ticket, ORDER_SYMBOL);
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- 定义标题文字和标题栏位的宽度
//--- 如果传给函数的标题宽度为0, 则宽度为标题文字行的大小+1
header="Volume initial:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- 取得并在日志中以指定宽度显示下订单时的初始交易量
if(!HistoryOrderGetDouble(ticket, ORDER_VOLUME_INITIAL, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 在日志中显示未执行的订单交易量
header="Volume current:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_VOLUME_CURRENT, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- 在日志中显示订单中指定的价格
header="Price open:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_PRICE_OPEN, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示止损水平
header="StopLoss:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_SL, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示止盈水平
header="TakeProfit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_TP, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示订单交易品种的当前价格
header="Price current:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_PRICE_CURRENT, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- 在日志中显示限价止损订单的订单限价
header="StopLimit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!HistoryOrderGetDouble(ticket, ORDER_PRICE_STOPLIMIT, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
}
//+------------------------------------------------------------------+
//| 返回订单类型的描述 |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE type)
{
switch(type)
{
case ORDER_TYPE_BUY : return("Buy");
case ORDER_TYPE_SELL : return("Sell");
case ORDER_TYPE_BUY_LIMIT : return("Buy Limit");
case ORDER_TYPE_SELL_LIMIT : return("Sell Limit");
case ORDER_TYPE_BUY_STOP : return("Buy Stop");
case ORDER_TYPE_SELL_STOP : return("Sell Stop");
case ORDER_TYPE_BUY_STOP_LIMIT : return("Buy Stop Limit");
case ORDER_TYPE_SELL_STOP_LIMIT : return("Sell Stop Limit");
default : return("Unknown order type");
}
}
|