Help with a code please.

 

Hi all, i have only one problem remaining to be able to finish my EA. I wrote the following code in the beginning of the start function to show me the profit of the last closed Sell trade, the problem is that it brings me the first closed Sell trade profit and remains on that number and never changes, no matter how much the ea opens and closes other Sell trades, can you help me? i will leave you with the code:

double FindSellProfit;
   for(FindSellProfit = OrdersHistoryTotal(); FindSellProfit >= 0; FindSellProfit--)
            {
                  if(OrderSelect(FindSellProfit, SELECT_BY_POS,MODE_HISTORY))
                     {
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumberSell)
                           {
                              if(OrderType()==OP_SELL)   // long position is opened
                                 {
                                   
                                  double SellProfit= OrderProfit();
                                        
      
                                 }
                           }
                     } 
            }

Waiting for your help and thanks in advance.

 
Abdull1996:

Hi all, i have only one problem remaining to be able to finish my EA. I wrote the following code in the beginning of the start function to show me the profit of the last closed Sell trade, the problem is that it brings me the first closed Sell trade profit and remains on that number and never changes, no matter how much the ea opens and closes other Sell trades, can you help me? i will leave you with the code:

Waiting for your help and thanks in advance.


double FindSellProfit,SellProfit;

double SellProfit= OrderProfit(); change to SellProfit+=OrderProfit();

untested ...... but looks like can be one of the errors

 
EADeveloper:


double FindSellProfit,SellProfit;

double SellProfit= OrderProfit(); change to SellProfit+=OrderProfit();

untested ...... but looks like can be one of the errors


Thank You Very Very Much. Solved! :)
 


EADeveloper:


double FindSellProfit,SellProfit;

double SellProfit= OrderProfit(); change to SellProfit+=OrderProfit();

untested ...... but looks like can be one of the errors


Sorry but it didnt work, it just add the profit of the buy trades up!

 

Please try this Abdull

double   FindSellProfit, SellProfit;
datetime t;
for (FindSellProfit = OrdersHistoryTotal(); FindSellProfit >= 0; FindSellProfit--) {
   if (OrderSelect(FindSellProfit, SELECT_BY_POS,MODE_HISTORY)) {
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumberSell) {
         if (OrderType()==OP_SELL) { // long position is opened
            if (t<OrderCloseTime()) {
               t=OrderCloseTime();
               SellProfit= OrderProfit();
            }
         }
      }
   }
}
 
sxTed:

Please try this Abdull


Thanks a lot man, it worked, and this time i am sure. :) Thanks again.
 
FindSellProfit = OrdersHistoryTotal(); 
   if (OrderSelect(FindSellProfit
is wrong. Index is zero based, Select(total) does not exist. Simplified:
double   SellProfit;
datetime last;
for (int pos = OrdersHistoryTotal()-1; pos >= 0; pos--) if(
   OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
&& last              <  OrderCloseTime()
&& OrderSymbol()     == Symbol() 
&& OrderMagicNumber()== MagicNumberSell
&& OrderType()       == OP_SELL) {
               last=OrderCloseTime();
               SellProfit= OrderProfit();
}
 

WHRoeder, thank you for noting the index error.


Abdull, i have corrected the code into a generic function, i think you will find it much more versatile.

//+------------------------------------------------------------------+
//| Function..: LastClosedOrderPos                                   |
//| Purpose...: Locate position number of order that was last closed |
//|             in the history pool (closed and canceled orders).    |
//| Parameters: iOrderType        - Order type, pass -1 for any type.|
//|             iOrderMagicNumber - Magic number of the expert or the|
//|                                 script, pass -1 for all orders to|
//|                                 be searched.                     |
//|             sSymbol           - Symbol/Instrument, defaults to   |
//|                                 all symbols to be searched for.  |
//| Returns...: iOrderPosition or -1 (not found).                    |
//| Sample....: int iPos=LastClosedOrderPos(OP_SELL,MagicNumberSell);|
//|             if (OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY))  |
//|                Print("Profit for last order ",OrderProfit());    |
//|             else                                                 |
//|                Print("OrderSelect error ",GetLastError());       |
//+------------------------------------------------------------------+
int LastClosedOrderPos(int iOrderType=-1, int iOrderMagicNumber=-1, string sSymbol="") {
  datetime tOrderCloseTime;
  int      i, iOrderPosition=-1, iOrdersTotal=OrdersHistoryTotal();
 
  for (i=0; i<iOrdersTotal; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (sSymbol=="" || OrderSymbol()==sSymbol) {
        if (iOrderType<0 || OrderType()==iOrderType) {
          if (iOrderMagicNumber<0 || OrderMagicNumber()==iOrderMagicNumber) {
            if (tOrderCloseTime<OrderCloseTime()) {
              tOrderCloseTime=OrderCloseTime();
              iOrderPosition=i;
            }
          }
        }
      }
    }
  }
  return(iOrderPosition);
}