Download MetaTrader 5
To add comments, please log in or register
Find out how to use widgets at MQL5.community
Mohamed
66
Mohamed 2014.04.06 17:30 

Hello, I am Trying to calculate my prievious trades losses using a function of the open lots. But it seems there is something wrong because every time I try to read Print ( OrderLots()); I got the initial lot which is 0.01.

 void lossCalculation()
 {
 
   int total=OrdersTotal();
 
   for (int cnt=total;cnt>=0;cnt--)

    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

 double Loss1=Step*OrderLots();
 double Loss2=Step*OrderLots()+Loss1;
 double Loss3=Step*OrderLots()+Loss2;
 double Loss4=Step*OrderLots()+Loss3;
 double Loss5=Step*OrderLots()+Loss4;
 double Loss6=Step*OrderLots()+Loss5;
 double Loss7=Step*OrderLots()+Loss6;
 double Loss8=Step*OrderLots()+Loss7;
 double Loss9=Step*OrderLots()+Loss8;
 double Loss10=Step*OrderLots()+Loss9;

 
 if (OrderLots()==0.01) {Loss=Loss1;}
  if (OrderLots()==0.02) {Loss=Loss2;}
   if (OrderLots()==0.04) {Loss=Loss3;}
    if (OrderLots()==0.08) {Loss=Loss4;}
     if (OrderLots()==0.16) {Loss=Loss5;}
      if (OrderLots()==0.32) {Loss=Loss6;}
       if (OrderLots()==0.64) {Loss=Loss7;}
        if (OrderLots()==1.28) {Loss=Loss8;}
         if (OrderLots()==2.56) {Loss=Loss9;}
          if (OrderLots()==5.12) {Loss=Loss10;}
          
  Print ( OrderLots());    


 
 }
Tjipke de Vries
6276
Tjipke de Vries 2014.04.06 18:05  

This is what happens

void lossCalculation()
  { 
   int total=OrdersTotal();  // Example OrdersTotal == 5
   OrderSelect(5,SELECT_BY_POS,MODE_TRADES);      //there is no number 5
   OrderSelect(4,SELECT_BY_POS,MODE_TRADES);      //OrderLots()==0.16
   OrderSelect(3,SELECT_BY_POS,MODE_TRADES);      //OrderLots()==0.08
   OrderSelect(2,SELECT_BY_POS,MODE_TRADES);      //OrderLots()==0.04
   OrderSelect(1,SELECT_BY_POS,MODE_TRADES);      //OrderLots()==0.02
   OrderSelect(0,SELECT_BY_POS,MODE_TRADES);      //OrderLots()==0.01    This trade is now selected


 double Loss1=Step*OrderLots();
 double Loss2=Step*OrderLots()+Loss1;
 double Loss3=Step*OrderLots()+Loss2;
 double Loss4=Step*OrderLots()+Loss3;
 double Loss5=Step*OrderLots()+Loss4;
 double Loss6=Step*OrderLots()+Loss5;
 double Loss7=Step*OrderLots()+Loss6;
 double Loss8=Step*OrderLots()+Loss7;
 double Loss9=Step*OrderLots()+Loss8;
 double Loss10=Step*OrderLots()+Loss9;

 
 if (OrderLots()==0.01) {Loss=Loss1;}                   //true
  if (OrderLots()==0.02) {Loss=Loss2;}
   if (OrderLots()==0.04) {Loss=Loss3;}
    if (OrderLots()==0.08) {Loss=Loss4;}
     if (OrderLots()==0.16) {Loss=Loss5;}
      if (OrderLots()==0.32) {Loss=Loss6;}
       if (OrderLots()==0.64) {Loss=Loss7;}
        if (OrderLots()==1.28) {Loss=Loss8;}
         if (OrderLots()==2.56) {Loss=Loss9;}
          if (OrderLots()==5.12) {Loss=Loss10;}
          
  Print ( 0.01);                                        //Print ( OrderLots());    


 
 }

look to what you coded

Mohamed
66
Mohamed 2014.04.07 18:27  
Thanks deVries
Daniel Petrovai
440
Daniel Petrovai 2014.04.09 09:56  
meroo_basha:
Thanks deVries


First step to improve your code is to get an understanding of the OrderSelect function, what you can do with it and how to do it.

Second step is to understand the "pool" of orders notion.

Closed orders are kept in a "pool" of closed orders and open orders are in a pool of open orders.

If an open order is closed, get's passed into the history pool or the closed orders pool.

So, if you look for the profit / loss of a closed order, you need to select the order from the history pool.

You can read here about it and if you have any questions or need some more examples, no problem, just ask.

/
To add comments, please log in or register