OrderTakeProfit()

 

I need value of the last closed position

   for(int b=0;b<OrdersHistoryTotal();b++)
     if(OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
      if(OrderSymbol()==Symbol())
        {
          Comment("b = ",b," OrderTakeProfit ",OrderTakeProfit());
        }

But it somehow shows zero while 3 closed positions. What is wrong in this code?

AND also I need value of currently open position

 for(int b=0;b<OrdersTotal();b++)
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol())
       Comment("OrderTakeProfit() = ", OrderTakeProfit());

Does not work either. Also zero.

Can somebody help what is wrong in these codes?

Thanks a lot.

 
 

This link is a different stuff but anyway thank you for your time.

Can somebody please tell what is wrong in the codes above?

 

I solved the problems myself.

everything is OK now.

 

As I understand, you're looking for the value of the last closed position. The link I provided shows some examples of how to get values of last closed positions. I taught maybe you could look through the codes and figure it out. However, I'm new to programming just like you. The code below I just wrote and tested and it works for ME. You may want to give it a shot and tell me how it works. If you want to cycle through all orders in history then remove the "break;". Also I'm speculating that the reason your code is not working is because of "for(int b=0;b<OrdersHistoryTotal();b++)". The first order your system ever place is 0. So if you have 10 orders and want the take-profit of order #10 then you're really looking for order 9. 0---9. break means get out of loop after u give me the value of last order. *Edit... woops guess you figured it out :)

 

 

for(int b=OrdersHistoryTotal()-1; b>=0; b--)
{
   if(OrderSelect(b,SELECT_BY_POS,MODE_HISTORY)==true)
   {
      if(OrderSymbol()==Symbol())
      {
         Comment("b = ",b," OrderTakeProfit ",OrderTakeProfit());
         break;
      }
   }
}