How To Check The Sum Of Serveral Back Order Results.

 

Hi,

I’m looking for any suggestion about this specific code.

This is the original code to check the result of the last closed order.

void CheckLastOrderResult() {
    double profit = 0;
    int orderId = -1;
    datetime lastCloseTime = 0;
    int cnt = OrdersHistoryTotal();
    for (int i=0; i < cnt; i++)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
        orderId = OrderMagicNumber();
        if (OrderSymbol() == Symbol() && lastCloseTime < OrderCloseTime() && orderId == 1)
        {
            lastCloseTime = OrderCloseTime();
            profit = OrderProfit();
            
        }
    }
    if (profit > Amount)
    {
        CloseAll();
        
    }
}

I don't need the result of the last order, but rather a sum of the OrderProfit() of several closed trades from a predetermined date taken from the open orders. Something like that.

OrderCloseTime() > OrderOpenTime()

 In where the OrderCloseTime come from the HistoryCloseOrder, and the OrderOpenTime, from the order that still open.


Any suggestion will be appreciated. Thanks.

 
Julio Guerra:

Hi,

I’m looking for any suggestion about this specific code.

This is the original code to check the result of the last closed order.

I don't need the result of the last order, but rather a sum of the OrderProfit() of several closed trades from a predetermined date taken from the open orders. Something like that.

 In where the OrderCloseTime come from the HistoryCloseOrder, and the OrderOpenTime, from the order that still open.


Any suggestion will be appreciated. Thanks.

Hi 

So in simple terms a function that returns the p+l of closed orders that have closed after the opening of an order ?

 
Lorentzos Roussos #:

Hi 

So in simple terms a function that returns the p+l of closed orders that have closed after the opening of an order ?

Basically yes.

 
Julio Guerra #:

Basically yes.

Aha , you can use this function then and call it to calculate the p+l after the open time of an order . 

double pl_of_closed_after(datetime after_time,//the time after which the order must have closed to be accounted for
                          string _by_symbol_or_null,//the symbol of the order or null (for all symbols)
                          int _by_magic_or_minus_1,//the magic # of the order or -1 for all)
                          int _by_type_or_minus_1,//by order type or -1 for all types 
                          bool _add_commissions,
                          bool _add_swaps){
double pl=0.0;
int t=OrdersHistoryTotal();
for(int i=0;i<t;i++){
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
//if it closed after this time 
  if(OrderCloseTime()>=after_time){
  //and its of the symbol specified or we don't care about the symbol
    if(OrderSymbol()==_by_symbol_or_null||_by_symbol_or_null==NULL){
    //and it's magic number is one we are interested in or we don't care about the magic number 
      if(OrderMagicNumber()==_by_magic_or_minus_1||_by_magic_or_minus_1==-1){
      //and it's type is one we want or we don't care 
        if(OrderType()==_by_type_or_minus_1||_by_type_or_minus_1==-1){  
        //then add to the p+l
          pl+=OrderProfit();
          if(_add_commissions){pl+=OrderCommission();}
          if(_add_swaps){pl+=OrderSwap();}
}}}}}
}
return(pl);
}
 
Lorentzos Roussos #:

Aha , you can use this function then and call it to calculate the p+l after the open time of an order . 

Thank you for the reply. I'm check it to see if the code works for me and will let you know later.

 
Lorentzos Roussos #:

Aha , you can use this function then and call it to calculate the p+l after the open time of an order . 

Worked just fine. Thank you.

 
Julio Guerra #:

Worked just fine. Thank you.

You are welcome