How:Order Modify Take Profit.

To add comments, please log in or register
amerul.trader
55
amerul.trader  
void OrderTPNModify(int m)
{
   int ticket;
   double fixTP,dtp;
   fixTP=0;dtp=0;
   for (int i = 0; i < OrdersTotal(); i++) 
   { 
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderSymbol() != Symbol()|| OrderMagicNumber() != Magic ||OrderType() != m) continue;        
      fixTP=OrderTakeProfit();   
   }
   
   for (int i=OrdersTotal()-1; i>=0; i--) 
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderSymbol() != Symbol()|| OrderMagicNumber() != Magic||OrderType() != m) continue;        
         dtp=OrderTakeProfit();
         
         if ( fixTP != dtp && OrderType()==m)  
         ticket = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), fixTP, 0, CLR_NONE);     
   }
    
}

void breakeven(int m)
{
   int orders_buy = 0;
   int orders_sell = 0;
   double Overall_BE_Price = 0;
   double Sel_BE_Price = 0;
   double Cons_Sell_Price = 0;
   double Total_Sell_Size = 0;
   double Buy_BE_Price = 0;
   double Cons_Buy_Price = 0;
   double Total_Buy_Size = 0;
   double Overall_Size = 0;
   int buy_count=0,sell_count=0;
   int i;
   double total_buy_profit;
   double total_sell_profit;
   
   for (i = 0; i < OrdersTotal(); i++)
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderSymbol() != Symbol()|| OrderMagicNumber() != Magic ||OrderType() != m) continue; 
      {
         if (OrderType() == m)
         {
            Buy_BE_Price += OrderOpenPrice()*OrderLots();
            Total_Buy_Size += OrderLots();
            buy_count++;
            total_buy_profit += OrderProfit();
            Overall_BE_Price += OrderOpenPrice()*OrderLots();
            Overall_Size += OrderLots();
            orders_buy++;
         }
         if (OrderType() == m)
         {
            Sel_BE_Price += OrderOpenPrice()*OrderLots();
            Total_Sell_Size += OrderLots();
            sell_count++;
            total_sell_profit += OrderProfit();
            Overall_BE_Price += OrderOpenPrice()*OrderLots();
            Overall_Size += OrderLots();
            orders_sell++;
         }
      }
   }
   int orders = orders_buy + orders_sell;
   
   if (Buy_BE_Price > 0)
     {Buy_BE_Price /= Total_Buy_Size;}
     
   if (Sel_BE_Price > 0)
     {Sel_BE_Price /= Total_Sell_Size;}

   int Line_Width = 3;
   int r;  
   
   double Buy_Pips_To_BE  = (Bid - Buy_BE_Price)*pt;
   if (orders_buy > 0 && Buy_Pips_To_BE >= 0) 
      {r=OrderModify(OrderTicket(),OrderOpenPrice(),Buy_Pips_To_BE,OrderTakeProfit(),0,Green); }
       
   double Sell_Pips_To_BE = (Sel_BE_Price - Ask)*pt;
   if (orders_sell > 0 && Sell_Pips_To_BE >= 0) 
      { {r=OrderModify(OrderTicket(), OrderOpenPrice(), Sell_Pips_To_BE, OrderTakeProfit(), 0, Red); }}
   
}
//I understand that this process will modify the fixTP when the OrderTakeProfit become bigger,fixTP will be profit the lotsize and pipstep is well calculated. //I need to make it dynamic when the fixTP become bigger and still close in profit even the pipstep and lot are fixed. Problem that im facing: 1.how to calculate the distance to its breakeven point 2.how to calculate the distance when it will not loss and distance when it will not profit.

please help..im stuck with the order modify for takeprofit.. 

//I understand that this process will modify the fixTP when the OrderTakeProfit become bigger,fixTP will be profit if the lotsize and pipstep is well calculated.

//I need to make it dynamic when the fixTP become bigger and still close in profit even the pipstep and lot are not well calculate.

Problem that im facing:

1.how to calculate the distance to its breakeven point 2.

2.how to calculate the distance when it will not loss and distance when it will not profit. and combine with that code?

amerul.trader
55
amerul.trader  
void md(int m){int i,r;double tpnya,dtp,bbep,sbep,ssize,bsize;
 for (i = 0; i < OrdersTotal(); i++) 
 { 
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if (OrderSymbol() != Symbol()) continue;
   //tpnya=OrderTakeProfit(); 
   m=OrderType();
   if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
   if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();}
  }
  if (bbep>0) bbep/=bsize;
  if (sbep>0) sbep/=ssize;
  
  if(m==0)tpnya=bbep+TP*pt;
  if(m==1)tpnya=sbep-TP*pt;
  
  for (i = OrdersTotal() - 1; i >= 0; i--) 
  {
   if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
   if(OrderSymbol() != Symbol()) continue;
   m=OrderType();
   if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
   if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();}
   }
   
   if (bbep>0) bbep/=bsize;
   if (sbep>0) sbep/=ssize;
  
  if(m==0)dtp=bbep+TP*pt;
  if(m==1)dtp=sbep-TP*pt;
  

  if( tpnya!=dtp && OrderType()==m)r=OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tpnya, 0, CLR_NONE);
}

still got me error

Seng Joo Thio
1208
Seng Joo Thio  
amerul.trader:

please help..im stuck with the order modify for takeprofit.. 

//I understand that this process will modify the fixTP when the OrderTakeProfit become bigger,fixTP will be profit if the lotsize and pipstep is well calculated.

//I need to make it dynamic when the fixTP become bigger and still close in profit even the pipstep and lot are not well calculate.

Problem that im facing:

1.how to calculate the distance to its breakeven point 2.

2.how to calculate the distance when it will not loss and distance when it will not profit. and combine with that code?

Based on your codes, you add all the order open prices and divide them by the total lot size before attempting to calculate a new TP from there, which will result in ridiculous tpnya value.

I think the proper steps should be (1) Loop through all orders (divided by buys and sells) and add up their profits and lots, then (2) using total lots, calculate how many more pips/points are required to reach your ideal profit level (separately for buys and sells), offset by the total corresponding profits, and finally (3) add/subtract that pips/points from the current Bid/Ask.

So, assuming at the time of calculation, all your orders already acquired a Profit/Loss of $100, but you want to target $150, so you'll use the total lots and calculate the number of pips/points required to gain another $50, since you already have $100.

William Roeder
20207
William Roeder  
  1. Seng Joo Thio: Based on your codes, you add all the order open prices and divide them by the total lot size before attempting to calculate a new TP from there, which will result in ridiculous tpnya value.
    @Seng Joo Thio Look again. He is computing Lots Weighted Average Price (∑  opiloti / ∑  loti) This is the break even price of all orders combined.
              OrderOpenPrice question . - MQL4 programming forum
              looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum

  2. amerul.trader: still got me error
    void md(int m){int i,r;double tpnya,dtp,bbep,sbep,ssize,bsize;
     for (i = 0; i < OrdersTotal(); i++){ 
       if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
       if (OrderSymbol() != Symbol()) continue;
       m=OrderType();
       if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
    1. Zero your sums before starting your loop. Always use strict. Fixing the warnings will save you hours of debugging.

    2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
                Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
                MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

    3.   if(m==0)tpnya=bbep+TP*pt;
        if(m==1)tpnya=sbep-TP*pt;
        if(m==0)  dtp=bbep+TP*pt;
        if(m==1)  dtp=sbep-TP*pt;
        
      
        if( tpnya!=dtp 
      When can that compare ever be true?

    4. Your error is that after computing BE price and then dtp, you need to do an OrderSelect loop to modify all orders.

amerul.trader
55
amerul.trader  
William Roeder:
  1. @Seng Joo Thio Look again. He is computing Lots Weighted Average Price (∑  opiloti / ∑  loti) This is the break even price of all orders combined.
              OrderOpenPrice question . - MQL4 programming forum
              looking for sample code on how to calculate the price BE for a few Buy and Sell orders simultaneously - Pips - MQL4 programming forum

    1. Zero your sums before starting your loop. Always use strict. Fixing the warnings will save you hours of debugging.

    2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
                Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
                MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

    3. When can that compare ever be true?

    4. Your error is that after computing BE price and then dtp, you need to do an OrderSelect loop to modify all orders.

Thank you for you comment.

the main idea actually is to modify OrderTakeProfit() to not close losses when totalOrder is getting greater since the TP and pipstep is fixed.

attach below the code for more understanding.


my understanding in looping still bad..need to learn more.. thank you all for helping.

Seng Joo Thio
1208
Seng Joo Thio  
William Roeder:
  1. @Seng Joo Thio Look again. He is computing Lots Weighted Average Price (∑  opiloti / ∑  loti) This is the break even price of all orders combined.

Oooooops...

amerul.trader:

the main idea actually is to modify OrderTakeProfit() to not close losses when totalOrder is getting greater since the TP and pipstep is fixed.

Nailed the bugs! - highlighted below are changes I made:

void ModifyTP(int m)
{
   int i,r;
   double tpnya,dtp,bbep,sbep,ssize,bsize;
   tpnya=0;dtp=0;bbep=0;sbep=0;ssize=0;bsize=0;
   for (i=0; i<OrdersTotal();i++) 
   { 
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if (OrderSymbol() != Symbol()|| OrderMagicNumber() != MagicNumber || OrderType()!=m) continue;
         tpnya=OrderTakeProfit();
         if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
         if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= ssize+OrderLots();}
               
   }
   if (bbep>0) { bbep/=bsize; tpnya=bbep + TP*pt; }
   if (sbep>0) { sbep/=ssize; tpnya=sbep - TP*pt; }
      
   for (i=OrdersTotal()-1; i>=0; i--) 
   {
      if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderSymbol() != Symbol()|| OrderMagicNumber() != MagicNumber|| OrderType()!=m) continue;
         dtp=OrderTakeProfit();
         //if (m==0) {bbep += OrderOpenPrice()*OrderLots(); bsize= bsize+OrderLots();}
         //if (m==1) {sbep += OrderOpenPrice()*OrderLots(); ssize= bsize+OrderLots();}
         Print("tpnya",tpnya,": ",i);
         Print("dtp",dtp,": ",i);
         if( tpnya!=dtp &&  OrderType()==m)
         r  =  OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), tpnya, 0, CLR_NONE); 
   }
   //if (bbep>0) bbep/=bsize; dtp=bbep;
   //if (sbep>0) sbep/=ssize; dtp=sbep;  
}

And here's the backtest results based on EURUSD M15 from 1st Apr to mid May 🤑

amerul.trader
55
amerul.trader  
Seng Joo Thio:

Oooooops...

Nailed the bugs! - highlighted below are changes I made:

And here's the backtest results based on EURUSD M15 from 1st Apr to mid May 🤑

Yeahh!! thanks man..it works.. but somehow it still showing error OrderModify error 1 though.. anyway thanks again!

amerul.trader
55
amerul.trader  

if (bbep>0) { bbep/=bsize; tpnya=NormalizeDouble(bbep + TP*pt,Digits); }

if (sbep>0) { sbep/=ssize; tpnya=NormalizeDouble(sbep - TP*pt,Digits); }

solved the problem.. thank you!

Seng Joo Thio
1208
Seng Joo Thio  
amerul.trader:

Yeahh!! thanks man..it works.. but somehow it still showing error OrderModify error 1 though.. anyway thanks again!

Oh... that... is because when you check "tpnya!=dtp", a very tiny difference between -pt and pt, except 0, will return true as well, and that'll take you to OrderModify().

But OrderModify() will not be happy with a change of less than pt... that's why you see the error.

To remove that error, just change "tpnya!=dtp" to "MathAbs(tpnya-dtp)>=pt"

William Roeder
20207
William Roeder  
amerul.trader: but somehow it still showing error OrderModify error 1 though
if( tpnya!=dtp

Doubles are rarely equal. In this case, usually compare not equals. Understand the links in:
          The == operand. - MQL4 programming forum

Your new TP should be normalized to tick size. The OrderTakeProfit (dtp) already is. Then the compare will succeed without the absolute value.

To add comments, please log in or register