//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
long pos_id_array[]; // массив для хранения идентификаторов позиций
//--- запрашиваем всю историю
if(!HistorySelect(0, TimeCurrent()))
{
Print("HistorySelect() failed. Error ", GetLastError());
return;
}
//--- соберём в массив все Position ID, возникшие только от отложенных ордеров
int total=HistoryOrdersTotal();
for(int i=0; i<total; i++)
{
ulong ticket=HistoryOrderGetTicket(i);
if(ticket==0)
continue;
ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticket, ORDER_TYPE);
long pos_id=HistoryOrderGetInteger(ticket, ORDER_POSITION_ID);
if(type<=ORDER_TYPE_SELL || pos_id==0)
continue;
int size=ArraySize(pos_id_array);
if(ArrayResize(pos_id_array, size+1)==size+1)
pos_id_array[size]=pos_id;
}
//--- по списку идентификаторов позиций в массиве
total=ArraySize(pos_id_array);
for(int i=0; i<total; i++)
{
//--- распечатываем в журнале заголовок и список ордеров и сделок позиции
long position_id=pos_id_array[i];
Print("List of orders and deals for position with ID: ", position_id);
HistorySelectByPositionProcess(position_id);
}
/*
результат:
List of orders and deals for position with ID: 1819629924
[0] Order Sell Limit #1819629924
[1] Order Buy #1819633194
[0] Entry In Deal Sell #1794972472
[1] Entry Out Deal Buy #1794975589
List of orders and deals for position with ID: 1841753970
[0] Order Sell Stop #1841753970
[1] Order Buy #1842322160
[0] Entry In Deal Sell #1817242142
[1] Entry Out Deal Buy #1817765341
*/
}
//+------------------------------------------------------------------+
//| Выбирает историю ордеров и сделок по идентификатору позиции и |
//| распечатывает в журнале список ордеров и сделок этой позиции |
//+------------------------------------------------------------------+
bool HistorySelectByPositionProcess(const long position_id)
{
//--- запрашиваем историю сделок и ордеров, имеющих указанный идентификатор позиции
if(!HistorySelectByPosition(position_id))
{
PrintFormat("HistorySelectByPosition(%I64d) failed. Error %d", position_id, GetLastError());
return(false);
}
//--- распечатаем в журнале список ордеров позиции
int orders_total=HistoryOrdersTotal();
for(int i=0; i<orders_total; i++)
{
ulong ticket=HistoryOrderGetTicket(i);
if(ticket==0)
continue;
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)HistoryOrderGetInteger(ticket, ORDER_TYPE);
PrintFormat(" [%d] Order %s #%I64u", i, OrderTypeDescription(order_type), ticket);
}
//--- распечатаем в журнале список сделок позиции
int deals_total =HistoryDealsTotal();
for(int i=0; i<deals_total; i++)
{
ulong ticket=HistoryDealGetTicket(i);
if(ticket==0)
continue;
ENUM_DEAL_ENTRY deal_entry=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY);
ENUM_DEAL_TYPE deal_type= (ENUM_DEAL_TYPE)HistoryDealGetInteger(ticket, DEAL_TYPE);
if(deal_type!=DEAL_TYPE_BUY && deal_type!=DEAL_TYPE_SELL)
continue;
PrintFormat(" [%d] Entry %s Deal %s #%I64u", i, DealEntryDescription(deal_entry), DealTypeDescription(deal_type), ticket);
}
return(true);
}
//+------------------------------------------------------------------+
//| Возвращает описание типа ордера |
//+------------------------------------------------------------------+
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: "+(string)type);
}
}
//+------------------------------------------------------------------+
//| Возвращает описание типа сделки позиции |
//+------------------------------------------------------------------+
string DealTypeDescription(const ENUM_DEAL_TYPE type)
{
switch(type)
{
//--- вернём описание типов сделок только Buy и Sell,
//--- так как все остальные типы к позиции не относятся
case DEAL_TYPE_BUY : return("Buy");
case DEAL_TYPE_SELL : return("Sell");
default : return("Unknown deal type: "+(string)type);
}
}
//+------------------------------------------------------------------+
//| Возвращает описание способа изменения позиции |
//+------------------------------------------------------------------+
string DealEntryDescription(const ENUM_DEAL_ENTRY entry)
{
switch(entry)
{
case DEAL_ENTRY_IN : return("In");
case DEAL_ENTRY_OUT : return("Out");
case DEAL_ENTRY_INOUT : return("InOut");
case DEAL_ENTRY_OUT_BY : return("Out by");
case DEAL_ENTRY_STATE : return("Status record");
default : return("Unknown deal entry: "+(string)entry);
}
}
|