Download MetaTrader 5

Adjust takeprift and stoploss dynamically

To add comments, please log in or register
BigDub
24
BigDub  

Hi all,

I‌ am running into an issue which I hope you guys can enlighten me.

I am trying to adjust my takeprofit and stoploss dynamically such that increasing takeprofit and stoploss by 20 pips whenever the market price is within 10 pips of the original takeprofit. 

T‌he following is a part of my code so far:

 if(OrderSelect(0, SELECT_BY_POS, MODE_TRADES)){
      if(OrderMagicNumber()==121){
        if(OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            if(Ask >= (OrderTakeProfit() - 10*pips)){
               if(OrderOpenPrice()>OrderStopLoss()){
                  OrderModify(OrderTicket(), OrderOpenPrice(), Ask - 2*sizeOfAdjustment*pips, Ask + 2*sizeOfAdjustment*pips, 0, CLR_NONE);
               }
            }
         }
        }
      }
   }

‌I understand it might be incorrect but I thought under such conditions, all trades will be closed as SL (as takeprofit increases all the time). However, it does not appear so in my back testing, and I am wondering why.

T‌his piece of code is a part of my adjustment function which is called in the OnTick().

T‌hanks.

Achmad Wijaya
771
Achmad Wijaya  
BigDub:

Hi all,

I‌ am running into an issue which I hope you guys can enlighten me.

I am trying to adjust my takeprofit and stoploss dynamically such that increasing takeprofit and stoploss by 20 pips whenever the market price is within 10 pips of the original takeprofit. 

T‌he following is a part of my code so far:

‌I understand it might be incorrect but I thought under such conditions, all trades will be closed as SL (as takeprofit increases all the time). However, it does not appear so in my back testing, and I am wondering why.

T‌his piece of code is a part of my adjustment function which is called in the OnTick().

T‌hanks.

Hello, I hope this can help you

 if(OrderSelect(0, SELECT_BY_POS, MODE_TRADES)){
      if(OrderMagicNumber()==121){
        if(OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            if(Ask >= (OrderTakeProfit() - 10*pips)){
               if(OrderOpenPrice()>OrderStopLoss()){
                  OrderModify(OrderTicket(), OrderOpenPrice(), Ask - 2*NormalizeDouble(sizeOfAdjustment,Digits)*pips, Ask + 2*NormalizeDouble(sizeOfAdjustment,Digits)*pips, 0, CLR_NONE);
               }
            }
         }
        }
      }
   }

Best Regards

Achmad Wijaya‌

Roberto Jacobs
48621
Roberto Jacobs  
BigDub:

Hi all,

I‌ am running into an issue which I hope you guys can enlighten me.

I am trying to adjust my takeprofit and stoploss dynamically such that increasing takeprofit and stoploss by 20 pips whenever the market price is within 10 pips of the original takeprofit. 

T‌he following is a part of my code so far:

‌I understand it might be incorrect but I thought under such conditions, all trades will be closed as SL (as takeprofit increases all the time). However, it does not appear so in my back testing, and I am wondering why.

T‌his piece of code is a part of my adjustment function which is called in the OnTick().

T‌hanks.

Modifications are not performed because:
modifications take profit has been triggered by the freeze level.
OrderModify (), must be preceded by a bool variable, eg. bool buymodify = Ordermodify

BigDub
24
BigDub  
Achmad Wijaya:

Hello, I hope this can help you

Best Regards

Achmad Wijaya‌


Hi Achmad,

T‌hanks very much for the reply, it is sort of working after I made a couple of changes. However, I am running into issue No.2 now:D

  if(OrderSelect(0, SELECT_BY_POS, MODE_TRADES)){
      if(OrderMagicNumber()==121){
        if(OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            if(Ask >= (OrderTakeProfit() + 0.00100*adjustmentMade - slAdjustment)){
                 OrderModify(OrderTicket(), OrderOpenPrice(), Ask - slAdjustment, Ask + tpAdjustment, 0, CLR_NONE);
                 adjustmentMade = adjustmentMade + 1;
             }
          }
        }
      }
   }

The code above could adjust takeprofit() as well as stoploss first time when the Ask price is 10 pips below OrderOpenPrice(), which is exactly what I want. 

In addition to this, what I also want is that both takeprofit and stoploss would increases accordingly again when the ask price is within 10 pips of the new takeprofit (OrderTakeProfit() + 10 pips - 10 pips), and again and again.

T‌herefore in theory, all my trades are closed as stoploss.

A‌ny ideas of how to correct my code?

BigDub
24
BigDub  
Roberto Jacobs:

Modifications are not performed because:
modifications take profit has been triggered by the freeze level.
OrderModify (), must be preceded by a bool variable, eg. bool buymodify = Ordermodify


Hi Roberto,

T‌hanks very much for the reply.

I do not quite understand that you said :"modifications take profit has been triggered by the freeze level.", and I hope you can clarify it a bit more.

I‌f I can have a look at my reply to Achmad, I am running into the second issue now. But I ‌‌believe what your said could explain why my code does not do what I want it to do.

Thanks.

Achmad Wijaya
771
Achmad Wijaya  
BigDub:


Hi Achmad,

T‌hanks very much for the reply, it is sort of working after I made a couple of changes. However, I am running into issue No.2 now:D

The code above could adjust takeprofit() as well as stoploss first time when the Ask price is 10 pips below OrderOpenPrice(), which is exactly what I want. 

In addition to this, what I also want is that both takeprofit and stoploss would increases accordingly again when the ask price is within 10 pips of the new takeprofit (OrderTakeProfit() + 10 pips - 10 pips), and again and again.

T‌herefore in theory, all my trades are closed as stoploss.

A‌ny ideas of how to correct my code?

wait a minutes,
if(Ask >= (OrderTakeProfit() 
you set no take profit before ? or that order without take profit set ? so you use another function like Hide TP function ? if use another scenerio for adjsuting from ask line running, so I think you need to make range -10 from ask + 10 from ask, so you can use that, and don't forget about spread

and maybe you can choose which you want, using tick or pips, because I think using

0.00100*adjustmentMade

‌is not effective if you using another pair, like cross pair, or Gold or another.

This is just sample, I hope it can help you

try this into your code, I hope you can explore it

  double pipVal = MarketInfo(Symbol(),MODE_TICKVALUE); if (Digits==3 || Digits==5) pipVal *= 10;
  double pip  = ATR / MarketInfo(Symbol(), MODE_POINT);

Roberto Jacobs
48621
Roberto Jacobs  
BigDub:


Hi Roberto,

T‌hanks very much for the reply.

I do not quite understand that you said :"modifications take profit has been triggered by the freeze level.", and I hope you can clarify it a bit more.

I‌f I can have a look at my reply to Achmad, I am running into the second issue now. But I ‌‌believe what your said could explain why my code does not do what I want it to do.

Thanks.

MQL has a trade freeze level is a distance to freeze trade operations in points, usually between 10 to 15 points.
If you modify the order take profit (ask > order take proft - 10 * pips) then in fact at the time the ask is already in the freeze level zone, and the order is not modifiable.

Please read this:

‌https://www.mql5.com/en/forum/2574

Noprofit
304
Noprofit  
BigDub :

Ciao a tutti,

Sono in Esecuzione in un Problema Che spero voi ragazzi mi può illuminare.

Sto cercando di adattare il mio TakeProfit e StopLoss dinamicamente in modo racconto Che l'Aumento TakeProfit e stoploss da 20 pips Ogni volta Che il prezzo di mercato si TROVA 10 pip della TakeProfit originale. 

Quanto segue e Una parte del mio codice finora:

Capisco Che potrebbe Essere errato, ma ho Pensato Che in QUESTE Condizioni, Tutti i mestieri Saranno chiusi venire SL (vieni TakeProfit Aumenta tutto il tempo). Tuttavia, non SEMBRA Così nel mio back testing, e mi Chiedo Perche.

This pezzo di codice e Una parte della mia REGOLAZIONE Funzione Che E Chiamata nel onTick ().

Grazie.

Ciao
io' m trader. For my opinion is better to see only price. For example if price low or close etc.. Touch Ma or Ema , close order. With take profit fix
i
To add comments, please log in or register