- count all open positions
- skip unwanted positions (filter)
some common filters :- positions with specific magic number will be considered for setting a trailing stop
- positions that are in profit (or have reached at least %X of max profit) are considered
- positions that are open more than N minutes are qualified for trailing stop
- etc.
- calculate a new stop-loss price for the position in question
common methods :- using Parabolic-SAR value of the previous bars
- using Moving Averages
- considering ATR indicator
- following current Bid( or Ask ) with a constant distance (e.g. 50 points)
- etc.
- check the suggested stop-loss :
- distance between open price of position and the suggested SL, should be checked against stops level and/or freeze levels f symbol
- if Long position : suggested SL price should be higher than current SL of positions, but lower than current Bid.
- if Short position: suggested SL price should be lower than current SL of positions, but higher than current Ask.
- set the calculated SL price to the position ( Position Modify part of code)
- and then check the operation success and probably report the failures....
No but also
-Start - when to start walking, it is only so logical when you want to start trailing from a specific point for example break even or > $0
-Stop - How much you want to walk behind, this can also be dynamic, like anti stop hunt, spread razor etc.
-Step - The size of the trailing steps, the distance between your feet when you walk.
look the original source and to understand how use it..
I put the peaces of code that you have use too..
original code:
https://www.mql5.com/pt/code/viewcode/19546/178536/ema_6.12.mq5
peace of code for trailing stop with step. //--- #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> CPositionInfo m_position; // trade position object CTrade m_trade; // trading object CSymbolInfo m_symbol; // symbol info object //--- input parameters input ushort InpTrailingStop = 50; // Trailing Stop (in pips) input ushort InpTrailingStep = 5; // Trailing Step (in pips) double ExtTrailingStop=0.0; double ExtTrailingStep=0.0; -----in the OnInit---- (....) int digits_adjust=1; if(m_symbol.Digits()==3 || m_symbol.Digits()==5) digits_adjust=10; m_adjusted_point=m_symbol.Point()*digits_adjust; ExtTakeProfit=InpTakeProfit*m_adjusted_point; ExtTrailingStop=InpTrailingStop*m_adjusted_point; ExtTrailingStep=InpTrailingStep*m_adjusted_point; (....) -----in the OnTick---- (....) Trailing(); (....) ----------- //+------------------------------------------------------------------+ //| Trailing | //+------------------------------------------------------------------+ void Trailing() { if(ExtTrailingStop==0) return; for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions if(m_position.SelectByIndex(i)) if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic) { if(m_position.PositionType()==POSITION_TYPE_BUY) { if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingStop+ExtTrailingStep) if(m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingStop+ExtTrailingStep)) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTrailingStop), m_position.TakeProfit())) Print("Modify ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); continue; } } else { if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep) if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) || (m_position.StopLoss()==0)) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingStop), m_position.TakeProfit())) Print("Modify ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); } } } }
look the original source and to understand how use it..
I put the peaces of code that you have use too..
original code:
https://www.mql5.com/pt/code/viewcode/19546/178536/ema_6.12.mq5
Great thank you very much, I will look into it!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey guys,
I am struggling with coding a trailing stop loss in my EA in MQL5.
Could anyone please help me?
I would be extremely grateful.
Thanks in advance,
Martin