Hi,
I need help to make this work:
I want to trade when the Heiken changes colour and EMA 2 crosses EMA 6.
DEFPARAM CumulateOrders = False //MACD[12,26,9](close) Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the “FLATBEFORE” time.
DEFPARAM FLATBEFORE = 093000
// Cancel all pending orders and close all positions at the “FLATAFTER” time
DEFPARAM FLATAFTER = 205000
a = EMA[2](close)
b = EMA[6](close)
//heiken ashi
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen= (xOpen[1] + xClose[1])/2
//xHigh= Max(xOpen, xClose)
//xLow= Min(xOpen, xClose)
endif
xBullish = xClose > xOpen
xBearish = xClose < xOpen
ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)
// Conditions to enter long positions
IF NOT LongOnMarket AND a crosses over b THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
IF LongOnMarket AND Colorchange THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND a crosses under b THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
Try below …
a = ExponentialAverage[2](close)
b = ExponentialAverage[6](close)
I made some changes to improve it, though a strategy based solely on EMAs is difficult to be turned into a profitable system:
- I used the HA close (xClose) for the calculations of the two moving averages, replacing the regular close
- I added the short exit
- I changed the CROSSOVER condition with a retracement of the price to the lowest EMA,while the close must still be above both of them
- I added a condition that the HA candle must be bullish for Long trades and Bearish for the short trades (but this should have been automatically the case, nonetheless)
- I changed the periods for the two EMAs
- I added both SL and TP
DEFPARAM CumulateOrders = False //MACD[12,26,9](close) Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the "FLATBEFORE" time.
DEFPARAM FLATBEFORE = 093000
// Cancel all pending orders and close all positions at the "FLATAFTER" time
DEFPARAM FLATAFTER = 205000
//heiken ashi
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen = (xOpen[1] + xClose[1])/2
xHigh = Max(xOpen, xClose)
xLow = Min(xOpen, xClose)
endif
xBullish = xClose > xOpen
xBearish = xClose < xOpen
ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)
a = ExponentialAverage[5](xClose)//(close)
b = ExponentialAverage[20](xClose)//(close)
UP= xBullish AND (a > b) AND (xClose > a) AND (xLow <= b)
DN= xBearish AND (a < b) AND (xClose < a) AND (xHigh >= b)
// Conditions to enter long positions
IF NOT LongOnMarket AND UP THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
IF LongOnMarket AND Colorchange THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND DN THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket AND Colorchange THEN
EXITSHORT AT MARKET
ENDIF
SET STOP pLOSS 100
SET TARGET pPROFIT 300
you may also add a trailing stop (lines 17-56 at this link https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/ are commonly used).
Roberto, ciao.
I’ve just tested your strategy but it’s not giving nice results.
Just a question:
is this line correct xOpen = (xOpen[1] + xClose[1])/2 or it should be xOpen = (Open[1] + Close[1])/2 ??
where do you get the value of xOpen[1] ?
many thanks for your reply
It’s correct.
There should be this line, just before “if (barindex>2) then“:
xOpen = open
but it will work the same after the 3rd candle.
Hi Roberto!
Thank you for your code. How do I change it so it goes long/short after the first change of color of the HA bar and rest the conditions mach?
Best regards Patrick
ColorChange triggers an exit.
What do you mean?
Hi Roberto,
Is it possible to change color after ONE HA bar has changed color not after 2 or 3.
Regards Patrick
That’s exactly what ColorChange does,