Dear all,
Apologies, first of all, for my ignorance in this subject:
I am trying to simply combine the bullish heikin-ashi (green candle) closure with one moving average overcrossing (for longs) and a bearish heikin-ashi (red candle) clousure with moving average undercrossing (for bearish). The application does not have the variables to back-test on the basis of such condition of a heikin-ashi signal (bullish or bearish signal -first green or red candle) – (case by case). Hence, I would like to ask if any of you could be so kind to let me know how would the variable in the backtesting code should read to that effect. Thank you very much in advance.
Apologies for the second post in a row (unfortunately I could not amend the previous one, sorry for this).
Meanwhile, I found a previous topic dealing partly with my question (pending to be answered; how to combine heikin-ashi with crossing (over and/or under a simple moving average): https://www.prorealcode.com/topic/heikin-ashi-auto-buy-sell-based-on-collor-change/
Based on that information I came up with the below “back test code” for the SP500 but it is not showing any results when I ask the application to generate the report, (I must be missing something due to my ignorance):
// Festlegen der Code-Parameter
DEFPARAM CumulateOrders = False // Kumulieren von Positionen deaktiviert
// Heikin Ashi setup
once xOpen = open
xClose = (open + close + high + low) / 4
if barindex > 0 then
xOpen = (xOpen + xClose[1]) / 2
endif
xLow = min(low,min(xClose,xOpen))
xHigh = max(high,max(xClose,xOpen))
//
Bullish = xClose > xOpen
Bearish = xClose < xOpen
// Bedingungen zum Einstieg in Long-Positionen
indicator1 = CALL Average[50](close)
c1 = (Bullish AND Bearish[1]CROSSES OVER indicator1)
IF C1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// exit LONG trade on a Bearish candlestick
indicator1 = CALL Average[50] (close)
c2 = (Bearish CROSSES UNDER indicator1)
IF c2 THEN
SELL AT MARKET
ENDIF
// Heikin Ashi setup
once xOpen = open
xClose = (open + close + high + low) / 4
if barindex > 0 then
xOpen = (xOpen + xClose[1]) / 2
endif
xLow = min(low,min(xClose,xOpen))
xHigh = max(high,max(xClose,xOpen))
//
Bullish = xClose > xOpen
Bearish = xClose < xOpen
// SHORT trades when switching from bullish to bearish
indicator1 = CALL Average[50] (close)
c3 = (Bearish AND Bullish [1] CROSSES UNDER indicator1)
IF c3 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// exit SHORT trade on a Bullish candlestick
indicator1 = CALL Average[50] (close)
c4 = (Bullish CROSSES UNDER indicator1)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
Thank you for your reply.
May you be so kind to repost the code with the recommended amendments / corrections for my easier guidance. Thank you in advance.
JSParticipant
Senior
Hi
@jonpt88
When you use 1 moving average and you want to go both long and short, your base code is limited to:
(xClose = (Open + Close + High + Low) / 4)
If xClose crosses over Average[50](Close) then
Buy 1 contract at Market
EndIf
If xClose crosses under Average[50](close) then
SellShort 1 contract at Market
EndIf
Is there anyway to add to the back test a third variable? What would be the continuation of the CODE if I may ask? Thank you.
JSParticipant
Senior
Hi @jonpt88
What do you want to do with that third variable?
What feature do you want to add?
Hi, a simple RSI. There must a code to allow back test to be subject to overbought / oversold umm
JSParticipant
Senior
Hi @jonpt88
I have modified your first code, Heikin Ashi + Moving Average
You can now back test the code
For optimization, you can run the period of the Moving Average, for example, between 1 and 100
// Festlegen der Code-Parameter
DEFPARAM CumulateOrders = False // Kumulieren von Positionen deaktiviert
Once n=16
// Heikin Ashi setup
once xOpen = open
xClose = (open + close + high + low) / 4
if barindex > 0 then
xOpen = (xOpen + xClose[1]) / 2
endif
xLow = min(low,min(xClose,xOpen))
xHigh = max(high,max(xClose,xOpen))
//
Bullish = xClose > xOpen
Bearish = xClose < xOpen
// Bedingungen zum Einstieg in Long-Positionen
indicator1 = Average[n](close)
c1 = (Bullish AND Bearish[1]) and xClose Crosses Over indicator1
IF C1 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
// exit LONG trade on a Bearish candlestick
c2 = (Bearish and xClose Crosses Under indicator1)
IF c2 THEN
SELL AT MARKET
ENDIF
// SHORT trades when switching from bullish to bearish
c3 = (Bearish AND Bullish[1]) and xClose Crosses Under indicator1
IF c3 THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
// exit SHORT trade on a Bullish candlestick
c4 = (Bullish and xClose Crosses Over indicator1)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
Looks like a profitable one 🙂
Many algos is profitable without stoploss, but u need to swing a big d**k😉
Do you know how to add a third variable like I ask – an RSI for example?
JSParticipant
Senior
You can add an extra condition with RSI for example:
C1 = (Bullish and Bearish[1]) and xClose Crosses Over indicator 1 and RSI(16) Crosses Over 20
C3 = (Bearish and Bullish[1] and xClose Crosses Under indicator 1 and RSI(16) Crosses Under 80
Nice – would it be possible for you to add that to the code with such variable – regardless of the back test result 🙂 That would be very much appreciated.