Multiple take profits.

 

Hello, I want to add in my code multiple tp function.

total = OrdersTotal(); 
for( i=total-1;i>=0;i--)   
{     
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1)
{    
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
}

If I am opened 0.03 lot, this function closes all my opened lots every 0.01 lots one after another.

So I need cycle, that it would do only once.

So I did this in that way, but nothing change:

int j=1;
total = OrdersTotal(); 
for( i=total-1;i>=0;i--)   
{     
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1)
{    
for(j=0;j<=2;j++)
{
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
j=5;
}
}

What do I wrong?

Thank you.

 
Eggo:

If I am opened 0.03 lot, this function closes all my opened lots every 0.01 lots one after another.

So I need cycle, that it would do only once.

What do I wrong?

Because that is what you wrote. Even if you return after closing one order, it will close the next order on the next tick.

You need to define when you want to close each and be able to differentiate each order (3 magic number)

Why don't you set the TP where you want when you create each order.

 

I don't understand why I need 3 magic number. Maybe I wrote something not understandable.

Situation is :

For example, it is opened 0.03 lot EURUSD, and I want to close order 0.01 lot, when price reaches tp1, then close other 0.01 lot, when price reaches tp2 and finally, close 0.01 when price reaches tp3.

Magic number is needed for order identification if there is more than one open position with the same symbol, in this situation there is only one opened order.

In this code I only write how to do this with tp1, if I do, what I want with tp1, then I will do with other tp's too.

My problem is, that I need that this line would be done only once, and then go to check if Bid>tp2 and so on.

if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1)
{    
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
}
 
Eggo:

I don't understand why I need 3 magic number. Maybe I wrote something not understandable.

Situation is :

For example, it is opened 0.03 lot EURUSD, and I want to close order 0.01 lot, when price reaches tp1, then close other 0.01 lot, when price reaches tp2 and finally, close 0.01 when price reaches tp3.

Magic number is needed for order identification if there is more than one open position with the same symbol, in this situation there is only one opened order.

In this code I only write how to do this with tp1, if I do, what I want with tp1, then I will do with other tp's too.

My problem is, that I need that this line would be done only once, and then go to check if Bid>tp2 and so on.


\

You are not using any MagicNumber ....

Do you understand why atleast one MagicNumber is needed ????

 
Eggo:

In this code I only write how to do this with tp1, if I do, what I want with tp1, then I will do with other tp's too.

My problem is, that I need that this line would be done only once, and then go to check if Bid>tp2 and so on.

So you need to keep track of the fact that TP1 has been hit so you don't do it again, how are you going to keep track of that ? you could look at the order history and see that a recent trade has been closed at TP1 . . .
 
deVries:


\

You are not using any MagicNumber ....

Do you understand why atleast one MagicNumber is needed ????


Magic number is needed for order identification if there is more than one open position with the same symbol.
 
Eggo:

Magic number is needed for order identification if there is more than one open position with the same symbol.


If your OrderLots() is a fixed lotsize at opening trade you can check by closing tp1 if orderlots() is still that lotsize

if it is smaller tp1 is done

for tp2 you have then to check........ ( Answer yourself )

 
deVries:


If your OrderLots() is a fixed lotsize at opening trade you can check by closing tp1 if orderlots() is still that lotsize

if it is smaller tp1 is done

for tp2 you have then to check........ ( Answer yourself )


I like this idea, I will try it in this way, thank you.
 
RaptorUK:
So you need to keep track of the fact that TP1 has been hit so you don't do it again, how are you going to keep track of that ? you could look at the order history and see that a recent trade has been closed at TP1 . . .


Yes, this idea good too, but the previews one is simpler for me, thank you.
 

I tried to do it in this way, but when BID>= tp1, then EA closes all my lots one after another.

At the beginning is 0,03 lot. After first successful close lot is 0,02 and the first if condition is false and even so it do the first if condition, instead of waiting while BID will be >=tp2.


total = OrdersTotal(); 
for( i=total-1;i>=0;i--)  
{    
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1 && OrderLots()==0.03) // error, close 3x 0,01.
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
    
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp2 && OrderLots()==0.02)       
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 
      
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp3 && OrderLots()==0.01)      
OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 
}   
 
Eggo:

I tried to do it in this way, but when BID>= tp1, then EA closes all my lots one after another.

At the beginning is 0,03 lot. After first successful close lot is 0,02 and the first if condition is false and even so it do the first if condition, instead of waiting while BID will be >=tp2.

I think that if you do a partial close you should exit the loop and look for the next TP hit on a subsequent tick . . .

total = OrdersTotal(); 
for( i=total-1;i>=0;i--)  
{    
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp1 && OrderLots()==0.03) // error, close 3x 0,01.
   {
   OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits);    
   break;
   } 
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp2 && OrderLots()==0.02)       
   {
   OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 
   break;
   }   
if(OrderSymbol() == Symbol() && type==OP_BUY && Bid>=tp3 && OrderLots()==0.01)      
   {   
   OrderClose(OrderTicket(),0.01,MarketInfo(OrderSymbol(),MODE_BID),Digits); 
   break;
   }
}   

you also need to add code to check that the OrderClose has worked and report any errors.

Reason: