//————————————————————————-
// Morgan Pettersson 2017-08-06
//————————————————————————-
defparam preloadbars = 300
DEFPARAM CumulateOrders = true // Cumulating positions activated
mdays = 26
myStoploss = 30
myProfit = 48
myLots = 1
EMA20=ExponentialAverage[20](close)
EMA50=ExponentialAverage[50](close)
EMA200=ExponentialAverage[200](close)
//UpTrend
c1 = summation[mdays](ema20>ema50)=mdays
c2 = summation[mdays](ema50>ema200)=mdays
UpTrend = c1 and c2
//Bullish Dip PinBar ema20
c3 = low < ema20 and close > ema20
c4 = close > open
c5 = abs(open-close)/abs(open-low) <= 0.5
BullishDipCondition = c3 and c4 and c5
//*************************************
//Conditions to enter long positions
//*************************************
IF UpTrend and BullishDipCondition THEN
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
BUY myLots CONTRACT AT MARKET
SET STOP pTRAILING myStopLoss
SET TARGET pPROFIT myProfit
ENDIF
//DownTrend
d1 = summation[mdays](ema20<ema50)=mdays
d2 = summation[mdays](ema50<ema200)=mdays
DownTrend = d1 and d2
//Bearish dip
d3 = high > ema20 and close < ema20
d4 = close < open
d5 = abs(close-open)/abs(high-open) <= 0.5
BearishDipCondition = d3 and d4 and d5
//*************************************
//Conditions to enter short positions
//**************************************
IF DownTrend and (BearishDipCondition) THEN
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
SELLSHORT myLots CONTRACT AT MARKET
SET STOP pTRAILING myStopLoss
SET TARGET pPROFIT myProfit
ENDIF
Hi, thanks for sharing your automated trading strategy on USD/JPY. Because your post do not has any explanation, I only tried to simulate the same period on my own platform. Unfortunately, I do not get the same results as yours. Could you please explain us a bit how it works, the timeframe used, spread and any other useful information you could share. Thanks gain for sharing your code with the community, much appreciated 🙂
Hello!
Is it because i did not write enough explanation that it did not fit in the library?
Should i do another post with more explanation or put my text here?
/Morgan
I need to review the code and obtain the same results as you, because a lot of people will download it and try it, I want to be sure everything is ok. I also need some explanations of coursen thanks in advance 🙂
USA/JPY mini 1 Hour
Trades from 1 jan 2017 08:00 to 4 aug 22:00.
Spread IG 0.7.
I define an uptrend if EMA20 >EMA>50EMA200
downtrend EMA20<EMA50<EMA200
If there is a pullback in an uptrend back to EMA20 and with a bullish pinbar I buy.
And the precise opposite in an downtrend.
I have had some problems with devisionwith zero that I dont really understand.
Some small changes in this code:
// Definition of code parameters
defparam preloadbars = 600
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
mydays = 26
myStoploss = 30
myProfit = 48
myLots = 1
var = 0.2
EMA20=ExponentialAverage[20](close)
EMA50=ExponentialAverage[50](close)
EMA200=ExponentialAverage[200](close)
//UpTrend
c1 = summation[mydays](ema20>ema50)=mydays
c2 = summation[mydays](ema50>ema200)=mydays
UpTrend = c1 and c2
//Bullish Dip PinBar ema20
c3 = low < ema20 and close > ema20
c4 = close > open
if(open-low > 0) then
c5 = abs(open-close)/abs(open-low) <= var
endif
BullishDipCondition = c3 and c4 and c5
//DownTrend
d1 = summation[mydays](ema20<ema50)=mydays
d2 = summation[mydays](ema50<ema200)=mydays
DownTrend = d1 and d2
//Bearish dip
d3 = high > ema20 and close < ema20
d4 = close < open
if(high-open > 0) then
d5 = abs(close-open)/abs(high-open) <= var
endif
BearishDipCondition = d3 and d4 and d5
//*************************************
//Conditions to enter long positions
//*************************************
IF UpTrend and (BullishDipCondition) THEN
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
BUY myLots CONTRACT AT MARKET
SET STOP pTRAILING myStopLoss
//SET STOP ploss myStopLoss
SET TARGET pPROFIT myProfit
ENDIF
//*************************************
//Conditions to enter short positions
//**************************************
IF DownTrend and (BearishDipCondition) THEN
IF LONGONMARKET THEN
SELL AT MARKET
ENDIF
SELLSHORT myLots CONTRACT AT MARKET
SET STOP pTRAILING myStopLoss
//SET STOP ploss myStopLoss
SET TARGET pPROFIT myProfit
ENDIF
Your strategy does great from november 2015 onward. Before it doesn’t work so well. Have you optimized over this period?
Yes that sounds about right. I only have a demo account at IG so i think thats all the data i got.
Which parameters did you optimize?
None. The trend check is some standard values for ema that i use when i manuelly trade.
I developed the strategy and then tested it on different intrument. It worked best on USA/JPY and its my favourite for manuell trading so i used it.
I have changed the size of the pinbar to 0.2 from 0.5. The var varible. But I’m not sure if it’s for the better or not 🙂
/Morgan
I see. Like I said, the last 18 months or so it worked great but before it didn’t. I will have a closer look at it when I have time. Thanks for sharing.
Yesterday i got division by zero again. I dont know why?
Hi, having a quick read at your code, you have c5 and d5 with respectively open-low and high-open at denominator. So just a guess with good probability: your division by zero message probably matches candles for which open=low or open=high
What more can I do to avoid division with zero?
This coding language is really limited. No error handeling ..
c = abs(close-open)
d = abs(high-open)
if d > 0 then
b = (c/d) <= 0.5
endif
” Graph d” to view your variable in the backtest window.