In my opinion the best trailing stop is not the one that just trails the price if moving in the positive price i.e. if the trade is +5 pips sl immediately moves by 5 pips maintaining trailing stop distance from current price that is dangerous. A good implementation is like the one in mt4 by default where it only activates if the price is more than trailing stop distance in the positive direction and only moves in steps i.e. if price is at trailing distance in the positive direction trailing is activated sl is adjusted and then the next adjustment only occurs after the price moves a step of say 10 pips from the previous adjustment point and not with every pip above the previous adjustment point. This to some level atleast allows for small swings before the trend continues and avoids the trade being closed prematurely.
What you are describing here, is exactly what I am looking to make..
Could you help me with some coding?
Here is my trailing stop function. (GreedyTrailing to false makes the SL to only be trailed if the new SL is above the open price=
extern bool GreedyTrailing = true; extern int g_TrailingStop = 10; extern int Slippage = 5; extern int MagicNumber = 12345; double DecimalPip; int init() { DecimalPip = GetDecimalPip(); } void TrailStops() { double TS_price; int Type; double stoplevel = getStopLevelInPips(); for(int i = OrdersTotal()-1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { Type = OrderType(); if(g_TrailingStop < stoplevel) g_TrailingStop = stoplevel; if(Type == OP_BUY) { TS_price = Bid - (g_TrailingStop*DecimalPip); if(TS_price > OrderStopLoss()+stoplevel*DecimalPip && (TS_price > OrderOpenPrice() || GreedyTrailing == true)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink)) Print(ShortName +" (OrderModify Error) "+ ErrorDescription(GetLastError())); } } else if(Type == OP_SELL) { TS_price = Ask + (g_TrailingStop*DecimalPip); if(TS_price < OrderStopLoss()-stoplevel*DecimalPip && (TS_price < OrderOpenPrice() || GreedyTrailing == true)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink)) Print(ShortName +" (OrderModify Error) "+ ErrorDescription(GetLastError())); } } } } } double GetDecimalPip() { switch(Digits) { case 5: return(0.0001); case 4: return(0.0001); case 3: return(0.001); default: return(0.01); } } double MyNormalizeDouble(double price) { return (NormalizeDouble(price, Digits)); } double getStopLevelInPips() { double s = MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0; if(Digits == 5) s = s / 10; return(s); }
Here is my trailing stop function. (GreedyTrailing to false makes the SL to only be trailed if the new SL is above the open price=
Thank you very much for your code..
When adding a similiar code to my EA I get this error:
'(' - function definition unexpected ssover.mq4 (62, 21)
Do you know what I can do with it?
Thank you very much for your code..
When adding a similiar code to my EA I get this error:
Do you know what I can do with it?
You probably have a missing ; somewhere . . .
I have lirerally checker the whole code for missing characters, but I can't find anything.
I even wrote the whole code from scratch. Didn't help.
Well I'd help but I can't see you code . . so not much I can do.
extern string optionFour = "Trailing Stop"; extern bool GreedyTrailing = true; extern int g_TrailingStop = 10; extern int Slippage = 5; double StopLossLevel, TakeProfitLevel; int total, Ticket, cnt; double DecimalPip; void TrailStops() { double TS_price; int Type; double stoplevel = getStopLevelInPips(); for(int i = OrdersTotal()-1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { Type = OrderType(); if(g_TrailingStop < stoplevel) g_TrailingStop = stoplevel; if(Type == OP_BUY) { TS_price = Bid - (g_TrailingStop*DecimalPip); if(TS_price > OrderStopLoss()+ stoplevel*DecimalPip && (TS_price > OrderOpenPrice() || GreedyTrailing == true)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink)) { Print("(OrderModify Error)"); } } } else if(Type == OP_SELL) { TS_price = Ask + (g_TrailingStop*DecimalPip); if(TS_price < OrderStopLoss()-stoplevel*DecimalPip && (TS_price < OrderOpenPrice() || GreedyTrailing == true)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), TS_price, OrderTakeProfit(), 0, Pink)) Print("(OrderModify Error)"); } } } double GetDecimalPip() { switch(Digits) { case 5: return(0.0001); case 4: return(0.0001); case 3: return(0.001); default: return(0.01); } } double MyNormalizeDouble(double price) { return (NormalizeDouble(price, Digits)); } double getStopLevelInPips() { double s = MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0; if(Digits == 5) s = s / 10; return(s); } int init() { DecimalPip = GetDecimalPip(); }
if(GreedyTrailing == true)
{
TrailStops();
}
That should help..
I of course have code for the EA which checks the signals too, but I doubt there is something wrong with that code since that worked good before I put the TrailingStop in.
Thanks for your help, RaptorUK.
- 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!
I have been trying to make my EA change the Stop loss value if the current trade almost have reached it's target take profit level. I have not yet found a way to do this.
Can you guys please help me?
One of the codes I have tested is this, but this code makes the absolutely last trade on a backtest loose everything I have on the account because of stop out. Can you help me find a way to make a more efficient trailing stop?