You will want to be using Ordermodify
https://docs.mql4.com/trading/OrderModify
You will need to set up if statements to check that the price has reached your targets, a flag to test that the new level you set is better than the current stop loss and then modify the order... the code below is illustartive only.
I would also like to comment that you may run into trouble if trying to open 3 orders at the same time. It will work fine in back test but forward, there is a time lag to open an order and the price may well have changed by the time you try to open your second or third order. If you are ok with different prices then it can be coded around, otherwise perhaps look at opening a single order for the combined total lots and then scale out partial lots as you reach targets. Anyway, hope that helps
if (Bid > target1) // test if you hit target { new_stoploss= whatever; } if (Bid > target2) // test if you hit target { new_stoploss= whatever; } if (Bid > target3) // test if you hit target { new_stoploss= whatever; } // Select your Order // get the current SL current_SL=OrderStopLoss(); // then test whether the new_stoploss is an improvement // so for a buy if(new_stoploss>currrent_SL) { SL=NormalizeDouble(new_stoploss,Digits); OrderModify(ticket,Price,SL,TP,0); }
HI,
For an EA that I'm developing, I would like to move the StopLoss when a Take Profit is executed.
In this EA, each time i am doing an order, i take 3 differents orders with different take profit (TP1, TP2 and TP3) and the same SL (for example 20 pips
I would like to move the SL when the TP1 is reached and to move it again when TP2 is reached but i have no idea how i can't do it.
I would also like to comment that you may run into trouble if trying to open 3 orders at the same time. It will work fine in back test but forward, there is a time lag to open an order and the price may well have changed by the time you try to open your second or third order. If you are ok with different prices then it can be coded around, otherwise perhaps look at opening a single order for the combined total lots and then scale out partial lots as you reach targets. Anyway, hope that helps
TAAD, to add to what Viffer wrote already (all of which is true tmk) in general one benefits from symmetric entry and exit methods. If you intend to "scale-out" of your open positions then you should also entertain methods of "scaling in" to them as well. Staggering your entry positions by an amount comparable to the gaps you are staggering out of your positions. Just my opinion, but it goes towards dealing with the situation described by Viffer in which you leave your entry methodology up to chance rather than controlling it directly as part of your strategy.
TAAD, to add to what Viffer wrote already (all of which is true tmk) in general one benefits from symmetric entry and exit methods. If you intend to "scale-out" of your open positions then you should also entertain methods of "scaling in" to them as well. Staggering your entry positions by an amount comparable to the gaps you are staggering out of your positions. Just my opinion, but it goes towards dealing with the situation described by Viffer in which you leave your entry methodology up to chance rather than controlling it directly as part of your strategy.
Thanks for your help, i will try this strategy but i think now that a trailing stop should be a better solution.
Do you have a code for a trailing stop ?
TAAD, to add to what Viffer wrote already (all of which is true tmk) in general one benefits from symmetric entry and exit methods. If you intend to "scale-out" of your open positions then you should also entertain methods of "scaling in" to them as well. Staggering your entry positions by an amount comparable to the gaps you are staggering out of your positions. Just my opinion, but it goes towards dealing with the situation described by Viffer in which you leave your entry methodology up to chance rather than controlling it directly as part of your strategy.
That's got me thinking Phillip, could you expand on symmetric entry? I believe I have an optimum solution but am keen to hear alternates. My EA is trend following. I enter full sized trade (fixed %risk lot sizing) at the signal, set 3 targets, Easy, Main and Stretch and scale out 25% at each target with the final 25% allowed to run until taken out by a trailing stop. The actual scale out % is dynamic and based on a probability calculation of past success in hitting each target... my take on Kelly criterion. Once the first target is reached, some profit is taken and SL is set to break even. If the trend looses momentum the remaining lots are taken out by the SL. The SL is wide enough to allow fairly large retracements but I am happy to take profits off the table if the Price spikes to hit a target. Once I'm at break even / zero market exposure, it will look for additional entry signals and enter another full sized trade with appropriate targets. This only happens if the signal confirms entry and would enter anyway if there were no existing trades. This is often at the bottom of a retracement and fairly close to my trailing stop. The stops for all trades are the same.
I'm not a fan of grids to scale in becasue at best, you are opening at less favourable prices from your signal start and at worst open at the peak of a move for an inevitable loss in the retracement. If there is a better solution, I'm all ears!
TBH, I am trying to code the order management for my method at the moment and it is prooving a challenge. Target variables changing when new orders open, ticket numbers changing when you close out partial lots and keeping track if an order has already scaled out at a particular target. It's being tracked in arrays, but they are not my strong point :( I'm hunting for code examples to try and get my head round it all at the moment.
V
Thanks for your help, i will try this strategy but i think now that a trailing stop should be a better solution.
Do you have a code for a trailing stop ?
It's fairly straight forward as per the code above.... simply define what your SL should be and then test if that figure is an improvement on the current SL.
sl=Bid-200*Point // 5 digit broker // get trade stoploss if (sl>=current_sl+(10*Point)) //modify the stop
This will maintain the stoploss a 20 pips (5 digit broker) below bid and only change the sl if the bid is 1pip above previouse highs.
Also worth checking that the stop is allowable, ie not too close to Price..
if (sl>Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point) // check stop is allowed, if not, set to min allowable { sl=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
HI,
For an EA that I'm developing, I would like to move the StopLoss when a Take Profit is executed.
In this EA, each time i am doing an order, i take 3 differents orders with different take profit (TP1, TP2 and TP3) and the same SL (for example 20 pips)
I would like to move the SL when the TP1 is reached and to move it again when TP2 is reached but i have no idea how i can't do it.
Does anyone can help me?
Thanks in advance
TAAD