So I recently got the idea of using Bollinger Bands in conjunction with the 200MA to identify a trend. I found that during choppy periods on when a market is consolidating the 200MA tends to stay within the Bollinger Bands. Once the Bollinger Bands clear the 200MA I enter a position on the first pullback to the 20MA.
Exits are triggered on the first pullback to the 20MA once a close beyond the 50MA is registered.
I also added a plain SL and TP, this can be optimized along with the Deviations variable
The included example is based on EURUSD 15min with a spread of 0.8
Defparam cumulateorders = False
Defparam preloadbars = 300
CurvePeriod = 250
Type = 0
Capital = 10000
Equity = Capital + StrategyProfit
EquityCurve = Average[CurvePeriod,Type](Equity)
possize = 1
MA20 = Average[20](close)
MA50 = Average[50](close)
MA200 = Average[200](close)
Deviations = 2.4
PER = 42//Periods
PRICE = LOG(customclose)
alpha = 2/(PER+1)
if barindex < PER then
EWMA = AVERAGE[3](PRICE)
else
EWMA = alpha * PRICE + (1-alpha)*EWMA
endif
error = PRICE - EWMA
dev = SQUARE(error)
if barindex < PER+1 then
var = dev
else
var = alpha * dev + (1-alpha) * var
endif
ESD = SQRT(var)
BollU = EXP(EWMA + (DEVIATIONS*ESD))
BollL = EXP(EWMA - (DEVIATIONS*ESD))
once LT = 0
once ST = 0
If LT = 1 and BollL < MA200 Then
LT = 0
ElsIf ST = 1 and BollU > MA200 Then
ST = 0
EndIf
If countofposition = 0 and (barindex < curveperiod or (barindex > curveperiod and equity >= equitycurve)) and LT = 0 and BollL > MA200 and close <= MA20 Then
Buy possize contract at market
LT = 1
LE = 0
ElsIf countofposition = 0 and (barindex < curveperiod or (barindex > curveperiod and equity >= equitycurve)) and ST = 0 and BollU < MA200 and close >= MA20 Then
Sellshort possize contract at market
ST = 1
SE = 0
EndIf
If longonmarket and LE = 0 and close > MA20 Then
LE = 1
ElsIf shortonmarket and SE = 0 and close < MA20 Then
SE = 1
EndIf
If longonmarket and LE = 2 and close > MA20 Then
Sell at market
ElsIf shortonmarket and SE = 2 and close < MA20 Then
Exitshort at market
ElsIf longonmarket and LE = 1 and close < MA50 Then
LE = 2
ElsIf shortonmarket and SE = 1 and close > MA50 Then
SE = 2
EndIf
SET STOP $Loss 40*possize
SET TARGET $PROFIT 130*possize