Last order information

 

Hi all,

What I want to get is;

If last order minus 1 has its profit above or equal to 0 then close all open orders.

If last order is equal to MaxOrders and if profit is less than 0.00002 then close all open orders.

I already have put to the forum a quite similar issue with  the way that this function must to be written, but still confused, for that my apologies (to be so idiot....). 

Here is the code that I'm using to get the above.

Is this code right ?

void GetLastOrderProfit()
       {//0
       int LastOrderTime = 0;
       double LastOrderProfit = 0;
       
       for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
              {//3
              if(OrderOpenTime() > LastOrderTime)
                {//4
                 LastOrderProfit = OrderProfit() + OrderCommission()+ OrderSwap(); 
                 LastOrderTime = OrderOpenTime();        
                }//4      
             }//3          
          }//2
       }//1
      if( OTCurrentTick == MaxOrders - 1 && LastOrderProfit >= 0)CloseAll(); 
      if( OTCurrentTick == MaxOrders && LastOrderProfit <= - 0.00002)CloseAll();               
    }//0 

 

Any help will be welcome

Luis 

 
luisneves:

Hi all,

What I want to get is;

If last order minus 1 has its profit above or equal to 0 then close all open orders.

If last order is equal to MaxOrders and if profit is less than 0.00002 then close all open orders.

I already have put to the forum a quite similar issue with  the way that this function must to be written, but still confused, for that my apologies (to be so idiot....). 

Here is the code that I'm using to get the above.

Is this code right ?

No,  it isn't, and I'm sure we have discussed part of what makes it wrong before.

 

You want to compare the Profit to 2 points but the profit is in currency not points of pips,  so if your account currency were Euro your order profit if positive is at least    €0.01  which will always be greater than 2 points.  

Don't try to compare an apple with an orange . . .

I also don't see from your code how you are attempting to find the "last order minus 1"  it seems that you are looking for the most recently opened order.

 
RaptorUK:

No,  it isn't, and I'm sure we have discussed part of what makes it wrong before.

 

You want to compare the Profit to 2 points but the profit is in currency not points of pips,  so if your account currency were Euro your order profit if positive is at least    €0.01  which will always be greater than 2 points.  

Don't try to compare an apple with an orange . . .

I also don't see from your code how you are attempting to find the "last order minus 1"  it seems that you are looking for the most recently opened order.


Hi RaptorUK,

Thank you for your attention to my issue.

Regarding the two points where I should concentrate my attention to get what I'm looking for the follow code is updated to get the amount of open orders by the ea and the compare that number with MaxOrders. So if my MaxOrders  is 5 then MaxOrders - 1 is 4 or is this approach wrong ?

Regarding the Order Profit, please could you help me because am not see how to cope with ?

void GetLastOrderProfit()
       {//0
       int LastOrderTime = 0;
       int TotalOrders;
       LastOrderProfit = 0;
       
       for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
              {//3
              if(OrderOpenTime() > LastOrderTime)
                {//4
                 LastOrderProfit = OrderProfit() + OrderCommission()+ OrderSwap(); 
                 LastOrderTime = OrderOpenTime();
                 TotalOrders = OrdersTotal();        
                }//4      
             }//3          
          }//2
       }//1     
      if( TotalOrders == MaxOrders - 1 && LastOrderProfit >= 0)CloseAll(); 
      if( TotalOrders == MaxOrders && LastOrderProfit <= - 0.00002)CloseAll();               
    }//0 

 

Thank you again for your support

 Luis 

 
luisneves:


Hi RaptorUK,

Thank you for your attention to my issue.

Regarding the two points where I should concentrate my attention to get what I'm looking for the follow code is updated to get the amount of open orders by the ea and the compare that number with MaxOrders. So if my MaxOrders  is 5 then MaxOrders - 1 is 4 or is this approach wrong ?

I can only try to help based on what you write . . .  if what you write is not what you want then my help will be of no use,  so it is very important to express exactly what you are trying to achieve.

You said . . .  

"If last order minus 1 has its profit above or equal to 0 then close all open orders.

If last order is equal to MaxOrders and if profit is less than 0.00002 then close all open orders.

so if you have 3 orders open you need to establish which order is  " last order minus 1 " . . .  perhaps this isn't what you meant ?  looking at your code I suspect it isn't,  but I can't help you based on my best guess of what you are looking to do.

 

If you meant . . .

"If my EA's order count is equal to MaxOrders - 1 and the last order has its profit above or equal to 0 then close all open orders.

If my EA's order count is equal to MaxOrders and the last order has its profit less than 0.00002 then close all open orders."  

then I can see how your code makes sense . . . 

I would do something like this first and test it . . .

void GetLastOrderProfit()
       {//0
       datetime LastOrderTime = 0;   //  times are datetimes not ints
       int TotalOrders = 0;          // do all thee open orders belong to this EA ? ?
       LastOrderProfit = 0;
       
       for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)   //  is the order from my EA ?
              {//3
              TotalOrders++;                              //  count the orders that belong to this EA  
              if(OrderOpenTime() > LastOrderTime)
                 {//4
                 LastOrderProfit = OrderProfit() + OrderCommission()+ OrderSwap(); 
                 LastOrderTime = OrderOpenTime();
                 }//4      
             }//3          
          }//2
       }//1     
      //  does this EA have MaxOrders - 1 open orders and last profit BE or +ve  OR
      //  does this EA have MaxOrders open orders and last profit less than or equal to -2 points 
      //  if either condition applies call CloseAll()
      if( ( TotalOrders == MaxOrders - 1 && LastOrderProfit >= 0 ) || 
          ( TotalOrders == MaxOrders && LastOrderProfit <= (-2 * MarketInfor(Symbol(), MODE_TICKVALUE)) ) ) CloseAll();               
    }//0 
 
RaptorUK:

I can only try to help based on what you write . . .  if what you write is not what you want then my help will be of no use,  so it is very important to express exactly what you are trying to achieve.

You said . . .  

"If last order minus 1 has its profit above or equal to 0 then close all open orders.

If last order is equal to MaxOrders and if profit is less than 0.00002 then close all open orders.

so if you have 3 orders open you need to establish which order is  " last order minus 1 " . . .  perhaps this isn't what you meant ?  looking at your code I suspect it isn't,  but I can't help you based on my best guess of what you are looking to do.

 

If you meant . . .

"If my EA's order count is equal to MaxOrders - 1 and the last order has its profit above or equal to 0 then close all open orders.

If my EA's order count is equal to MaxOrders and the last order has its profit less than 0.00002 then close all open orders."  

then I can see how your code makes sense . . . 

I would do something like this first and test it . . .

 Hi RaptorUK,

Thank you for your support, Seems that is working fine  and I was far away from get the right function.

Best regards

Luis 

Reason: