I’ve been playing around with this code for some time and I am asking whether someone (not me at present, since IG customers will have MTF… I don’t know when) could test it with MTF enabled, so it can be tested on a 2-year data history, both for the Daily TF and the 5/10-minute TF using MTF for the sole TRAILING STOP code, to see if any improvements occur:
//************************************************************************
// 3 MAs Pullback DAX daily
//
// (Ema 10 + Ema 20 + Ema 120)
//************************************************************************
DEFPARAM CumulateOrders = False
ONCE nLots = 1
ONCE LookBack = 3 //3 bars to set SL to the highest/lowest
ONCE Multiplier = 6 //6 times the Stop Loss
IF OnMarket THEN
CrossingUP = 0 //Reset flags to zero when...
CrossingDN = 0 //... On Market
ENDIF
ONCE AvgType = 6 //6 = Time Series
FastMA = average[10,AvgType](close) //10
SlowMA = average[20,AvgType](close) //20
LongMA = average[120,AvgType](close) //120
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// I don't care which order they are, it's important the highest MA is CROSSED OVER or
// the lowest one is CROSSED UNDER
//
IF close CROSSES OVER Max(FastMA,Max(SlowMA,LongMA)) THEN
CrossingUP = 1
CrossingDN = 0
ENDIF
IF close CROSSES UNDER Min(FastMA,Min(SlowMA,LongMA)) THEN
CrossingUP = 0
CrossingDN = 1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trailing Stop code
// to be tested on a 5/10-minute TF
ONCE TrailStart = 50 //50 Start trailing profits from this point
ONCE ProfitPerCent = 0.250 //25.0% Profit to keep
IF Not OnMarket THEN
y1 = 0
y2 = 0
ELSIF LongOnMarket AND high > (TradePrice + (y1 * pipsize)) THEN //LONG
x1 = (high - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN //go ahead only if TRAILSTART+ pips
y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
ENDIF
IF y1 THEN
y1 = y1 * pipsize //convert pips to price
IF close < (TradePrice + y1) THEN //IF CLOSE < Tradeprice + Profit...
SELL AT MARKET //... exit immediately
ELSE
SELL AT Tradeprice + y1 STOP //Place pending STOP order when y1>0
ENDIF
ENDIF
ELSIF ShortOnMarket AND low < (TradePrice - (y2 * pipsize)) THEN //SHORT
x2 = (tradeprice - low) / pipsize //convert price to pips
IF x2 >= TrailStart THEN //go ahead only if TRAILSTART+ pips
y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
ENDIF
IF y2 THEN
y2 = y2 * pipsize //convert Pips to Price
IF (TradePrice + y2) > close THEN //IF Tradeprice + Profit > CLOSE ...
EXITSHORT AT MARKET //... exit immediately
ELSE
EXITSHORT AT Tradeprice - y2 STOP //Place pending STOP order when y2>0
ENDIF
ENDIF
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//************************************************************************
// LONG
//************************************************************************
a0 = CrossingUP //There must have been a CROSS OVER
a1 = close[1] < Min(FastMA,SlowMA) //Previous bar must have closed < lowest among FastMA/SlowMA
a2 = close CROSSES OVER Max(FastMA,SlowMA) //a CROSS OVER is occurring this bar
a3 = close > open //a bullish bar is mandatory
ax = a0 AND a1 AND a2 AND a3
IF ax AND Not OnMarket THEN
Sl = ((close - lowest[LookBack](low)) / pipsize) //SL at the lowest low within LOOKBACK bars
Tp = Sl * Multiplier
BUY nLots CONTRACT AT MARKET
ENDIF
//************************************************************************
// SHORT
//************************************************************************
b0 = CrossingDN //There must have been a CROSS UNDER
b1 = close[1] > Max(FastMA,SlowMA) //Previous bar must have closed > lowest among FastMA/SlowMA
b2 = close CROSSES UNDER Min(FastMA,SlowMA)//a CROSS OVER is occurring this bar
b3 = close < open //a bearish bar is mandatory
bx = b0 AND b1 AND b2 AND b3
IF bx AND Not Onmarket THEN
Sl = ((highest[LookBack](high) - close) / pipsize) //SL at the highest high within LOOKBACK bars
Tp = Sl * Multiplier
SELLSHORT nLots CONTRACT AT MARKET
ENDIF
SET TARGET pPROFIT Tp
SET STOP pLOSS Sl
I’m afraid my platform is only giving me daily or longer time frames but here is the daily results with 2 pip spread.
Just noticed that you use STOP orders so ignore those results as there is no tick by tick option either!
I changed the code this way to be compatible with MTF: (trading entries are calculated with the daily chart, while trailing stop is made with the 5 minutes one)
//************************************************************************
// 3 MAs Pullback DAX daily
//
// (Ema 10 + Ema 20 + Ema 120)
//************************************************************************
DEFPARAM CumulateOrders = False
timeframe(daily,updateonclose)
ONCE nLots = 1
ONCE LookBack = 3 //3 bars to set SL to the highest/lowest
ONCE Multiplier = 6 //6 times the Stop Loss
IF OnMarket THEN
CrossingUP = 0 //Reset flags to zero when...
CrossingDN = 0 //... On Market
ENDIF
ONCE AvgType = 6 //6 = Time Series
FastMA = average[10,AvgType](close) //10
SlowMA = average[20,AvgType](close) //20
LongMA = average[120,AvgType](close) //120
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// I don't care which order they are, it's important the highest MA is CROSSED OVER or
// the lowest one is CROSSED UNDER
//
IF close CROSSES OVER Max(FastMA,Max(SlowMA,LongMA)) THEN
CrossingUP = 1
CrossingDN = 0
ENDIF
IF close CROSSES UNDER Min(FastMA,Min(SlowMA,LongMA)) THEN
CrossingUP = 0
CrossingDN = 1
ENDIF
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//************************************************************************
// LONG
//************************************************************************
a0 = CrossingUP //There must have been a CROSS OVER
a1 = close[1] < Min(FastMA,SlowMA) //Previous bar must have closed < lowest among FastMA/SlowMA
a2 = close CROSSES OVER Max(FastMA,SlowMA) //a CROSS OVER is occurring this bar
a3 = close > open //a bullish bar is mandatory
ax = a0 AND a1 AND a2 AND a3
IF ax AND Not OnMarket THEN
Sl = ((close - lowest[LookBack](low)) / pipsize) //SL at the lowest low within LOOKBACK bars
Tp = Sl * Multiplier
BUY nLots CONTRACT AT MARKET
ENDIF
//************************************************************************
// SHORT
//************************************************************************
b0 = CrossingDN //There must have been a CROSS UNDER
b1 = close[1] > Max(FastMA,SlowMA) //Previous bar must have closed > lowest among FastMA/SlowMA
b2 = close CROSSES UNDER Min(FastMA,SlowMA)//a CROSS OVER is occurring this bar
b3 = close < open //a bearish bar is mandatory
bx = b0 AND b1 AND b2 AND b3
IF bx AND Not Onmarket THEN
Sl = ((highest[LookBack](high) - close) / pipsize) //SL at the highest high within LOOKBACK bars
Tp = Sl * Multiplier
SELLSHORT nLots CONTRACT AT MARKET
ENDIF
SET TARGET pPROFIT Tp
SET STOP pLOSS Sl
timeframe(default)
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trailing Stop code
// to be tested on a 5/10-minute TF
ONCE TrailStart = 50 //50 Start trailing profits from this point
ONCE ProfitPerCent = 0.250 //25.0% Profit to keep
IF Not OnMarket THEN
y1 = 0
y2 = 0
ELSIF LongOnMarket AND high > (TradePrice + (y1 * pipsize)) THEN //LONG
x1 = (high - tradeprice) / pipsize //convert price to pips
IF x1 >= TrailStart THEN //go ahead only if TRAILSTART+ pips
y1 = max(x1 * ProfitPerCent, y1) //y = % of max profit
ENDIF
IF y1 THEN
y1 = y1 * pipsize //convert pips to price
IF close < (TradePrice + y1) THEN //IF CLOSE < Tradeprice + Profit...
SELL AT MARKET //... exit immediately
ELSE
SELL AT Tradeprice + y1 STOP //Place pending STOP order when y1>0
ENDIF
ENDIF
ELSIF ShortOnMarket AND low < (TradePrice - (y2 * pipsize)) THEN //SHORT
x2 = (tradeprice - low) / pipsize //convert price to pips
IF x2 >= TrailStart THEN //go ahead only if TRAILSTART+ pips
y2 = max(x2 * ProfitPerCent, y2) //y = % of max profit
ENDIF
IF y2 THEN
y2 = y2 * pipsize //convert Pips to Price
IF (TradePrice + y2) > close THEN //IF Tradeprice + Profit > CLOSE ...
EXITSHORT AT MARKET //... exit immediately
ELSE
EXITSHORT AT Tradeprice - y2 STOP //Place pending STOP order when y2>0
ENDIF
ENDIF
ENDIF
graph fastMA coloured(0,200,0)
graph slowma coloured(200,0,0)
graph longma coloured(0,0,200)
graph close
The performance is not good (made with tick mode and spread=1.5), and as you can see in the attached picture, daily MA are not calculated until there is enough bars in the default timeframe (5-minute TF). This is something we should be aware of when backtesting… a test to verify if the SMA are superior to 0 could be enough..