Sometimes it may happen that your target is not reached, but price went in your favour and you want to save some of the profits earned so far, that’s why the Trailing Stop exist!
But in automated trading (at least until MTF will be supported) and mainly on higher TFs, Nicolas’code to support a good Trailing Stop is executed only when a bar closes, leaving room for price to pullback or even reverse, despite your trailing stop may have been triggered while the bar was forming, simply because when the bar closes price has retraced to a lower point < to your trailing stop treshold and the code is fooled into believing the price never reached that treshold!
So, while awaiting MTF to come, I replaced CLOSE with HIGH and LOW, respectively, for Long and Short trades. So the code will compare TRADEPRICE with a higher/lower price than before, the price you would use manually when in front of your charts.
When the bar closes and the code is executed, it will set a new SL according to those new prices in relation with TRADEPRICE and set a new SL where it wouldn’t have been set before, or closing immediately ifa price has fallen below TRADEPRICE!
//************************************************************************
// trailing stop function
trailingstart = 10 //10 trailing will start @trailinstart points profit
trailingstep = 5 //5 trailing step to move the "stoploss"
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND HIGH-tradeprice(1)>=trailingstart*pipsize THEN //close --> HIGH
newSL = tradeprice(1)+trailingstep*pipsize
// new coding
IF newSL > close THEN //if current closing price is < new SL then exit IMMEDIATELY!
SELL AT MARKET
ENDIF
// end new coding
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
// new coding
IF newSL > close THEN //if current closing price is < new SL then exit IMMEDIATELY!
SELL AT MARKET
ENDIF
// end new coding
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-LOW>=trailingstart*pipsize THEN //close --> LOW
newSL = tradeprice(1)-trailingstep*pipsize
// new coding
IF newSL < close THEN //if current closing price is > new SL then exit IMMEDIATELY!
EXITSHORT AT MARKET
ENDIF
// end new coding
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
// new coding
IF newSL < close THEN //if current closing price is > new SL then exit IMMEDIATELY!
EXITSHORT AT MARKET
ENDIF
// end new coding
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
Should anyone take some further risk of a higher loss and aim at closing at breakeven instead of a loss, in case price has fallen below entry point, lines 17 and 26 can be replaced by:
SELL AT newSL LIMIT
and lines 38 and 47 with:
EXITSHORT AT newSL LIMIT
I tested it a bit and found out that it yields slightly poorer performances, but less losing trades and less drawdown.
In a few months this post will, hopefully, be superseded by MTF.