Hi everyone,
I’m having trouble getting this Heikin Ashi Strategy to work. It’s supposed to enter long when heikin ashi drops below lower Bollinger band but only on the next green heikin candle. Also doing the reverse for short positions. Can anyone help with my code please?:
// Define Bollinger Bands
DEFPARAM CUMULATEORDERS = false
DEFPARAM PreloadBars = 100
bollingerMiddle = Average[20](Close)
bollingerUpper = BollingerUp[20](close)
bollingerLower = BollingerDown[20](close)
// Compute Heikin-Ashi Candles
haClose = (Open + High + Low + Close) / 4
haOpen = (haOpen[1] + haClose[1]) / 2
haHigh = max(haOpen, haClose)
haLow = min(haOpen, haClose)
// Track buy/sell signals
longSignal = 0
shortSignal = 0
// Detect Heikin-Ashi close below lower Bollinger Band
if haClose < bollingerLower then
longSignal = 1 // Mark as potential buy signal
endif
// Detect Heikin-Ashi close above upper Bollinger Band
if haClose > bollingerUpper then
shortSignal = 1 // Mark as potential sell signal
endif
// Confirm Buy: First Green HA Candle After Long Signal
if longSignal = 1 and haClose > haOpen then
buy 1 CONTRACT at market
longSignal = 0 // Reset signal
endif
// Confirm Sell: First Red HA Candle After Short Signal
if shortSignal = 1 and haClose < haOpen then
sellshort 1 contract at market
shortSignal = 0 // Reset signal
endif
JSParticipant
Senior
Hi,
Try this one:
DefParam CumulateOrders=False
DefParam PreLoadBars=1000
ONCE LongSignal = 0
ONCE ShortSignal = 0
BollingerMiddle = Average[20](Close)
BollingerUpper = BollingerUp[20](close)
BollingerLower = BollingerDown[20](close)
haClose = (Open + High + Low + Close) / 4
If barindex=0 then
haOpen=Open
else
haOpen = (haOpen[1] + haClose[1]) / 2
EndIf
haHigh = max(High, max(haOpen, haClose))
haLow = min(Low, min(haOpen, haClose))
// Detect Heikin-Ashi close below lower Bollinger Band
if haClose[1] < BollingerLower[1] then
LongSignal = 1 // Mark as potential buy signal
endif
// Detect Heikin-Ashi close above upper Bollinger Band
if haClose[1] > bollingerUpper[1] then
ShortSignal = 1 // Mark as potential sell signal
endif
// Confirm Buy: First Green HA Candle After Long Signal
if LongSignal = 1 and haClose > haOpen then
Buy 1 Contract at Market
longSignal = 0 // Reset signal after entry
endif
// Confirm Sell: First Red HA Candle After Short Signal
if ShortSignal = 1 and haClose < haOpen then
SellShort 1 Contract at Market
ShortSignal = 0 // Reset signal after entry
endif
GraphOnPrice BollingerUpper as "Upper"
GraphOnPrice BollingerLower as "Lower"
Thank-you very much. This works well.
Thank-you very much. This works well.
I tried it in various underluyings and different timeframes. The results are bad. In which one do you tried it?
It was designed for GBP/USD on a 30 min chart. It’s by no means a final strategy and would reply on other confirmations.
Hi everyone,
So I’ve been playing around with this Heikin Ashi Strategy and made a few adjustments. The strategy is to run on a 30 min GBP/USD chart but I’ve attempted to change the code to exit using the 5 min TF to get more precise exits.
I’m trying to get it to add orders if the price moves against and the initial conditions are met again. I’ve been somewhat successful but it seems to add several positions 5 mins apart and not when the original conditions are met again.
Can anyone adjust the code to get this to work, I’m struggling.
Thanks
Jim
DEFPARAM CUMULATEORDERS = true
DefParam PreLoadBars=1000
TIMEFRAME(30mn)
ONCE LongSignal = 0
ONCE ShortSignal = 0
BollingerMiddle = Average[20](Close)
BollingerUpper = BollingerUp[20](Close)
BollingerLower = BollingerDown[20](close)
//Heikin Ashi Candle calc
haClose = (Open + High + Low + Close) / 4
If BarIndex=0 then
haOpen = Open
else
haOpen = (haOpen[1] + haClose[1]) / 2
EndIf
haHigh = max(High, max(haOpen, haClose))
haLow = min(Low, min(haOpen, haClose))
//Trigger possible long
if haClose[1] < BollingerLower[1] then
LongSignal = 1
endif
//Trigger possible short
if haClose[1] > BollingerUpper[1] then
ShortSignal = 1
endif
//Enter long on first green heikin candle after trigger
if LongSignal = 1 and haClose > haOpen then
Buy 1 Contracts at Market
LongSignal = 0
endif
//Enter short on first red heikin candle after trigger
if ShortSignal = 1 and haClose < haOpen then
SellShort 1 Contracts at Market
ShortSignal = 0
endif
//Add long positions if on the market and conditions met again
IF LONGONMARKET and LongSignal=1 and haClose > haOpen then
Buy 1 Contracts at Market
LongSignal = 0
endif
//Add short positions if on the market and conditions met again
if SHORTONMARKET and ShortSignal = 1 and haClose < haOpen then
SellShort 1 Contracts at Market
ShortSignal = 0
endif
TIMEFRAME(5mn)
//If one position open and close crosses bollinger middle exit market.
If LONGONMARKET and COUNTOFPOSITION=1 and close CROSSES OVER BollingerMiddle THEN
SELL AT MARKET
ENDIF
//If one position open and close crosses bollinger middle exit market.
IF SHORTONMARKET and COUNTOFPOSITION=-1 and close CROSSES UNDER BollingerMiddle THEN
EXITSHORT AT MARKET
ENDIF
//If multiple positions open exit when close is weighted position price + 5 points
If COUNTOFPOSITION > 1 THEN
If Close >= POSITIONPRICE + 5 THEN
SELL AT MARKET
ENDIF
ENDIF
//If multiple positions open exit when close is weighted position price + 5 points
If COUNTOFPOSITION < -1 THEN
If Close <= POSITIONPRICE - 5 THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
If you have CUMULATEORDERS = true it will keep adding to your order when conditions are true.
Shuld add “not onmarket” to your first buy/sell short Conditions on row 34 and 40.
Also, what are you trying to do on the block starting on row 77?
COUNTOFSHORTSHARES is the comand for short postions, and COUNTOFPOSITION cant be a negative value.
JSParticipant
Senior
The strategy can open positions every 5 minutes because the different conditions are out of sync…
Try these…
(CountOfPosition can be both positive and negative)
DEFPARAM CUMULATEORDERS = true
DefParam PreLoadBars=1000
TIMEFRAME(30mn)
ONCE LongSignal = 0
ONCE ShortSignal = 0
BollingerMiddle = Average[20](Close)
BollingerUpper = BollingerUp[20](Close)
BollingerLower = BollingerDown[20](close)
//Heikin Ashi Candle calc
haClose = (Open + High + Low + Close) / 4
If BarIndex=0 then
haOpen = Open
else
haOpen = (haOpen[1] + haClose[1]) / 2
EndIf
haHigh = max(High, max(haOpen, haClose))
haLow = min(Low, min(haOpen, haClose))
//Trigger possible long
if haClose[1] < BollingerLower[1] then
LongSignal = 1
endif
//Trigger possible short
if haClose[1] > BollingerUpper[1] then
ShortSignal = 1
endif
//Enter long on first green heikin candle after trigger
if LongSignal = 1 and haClose > haOpen then
Buy 1 Contracts at Market
LongSignal = 0 // Reset trigger
endif
//Enter short on first red heikin candle after trigger
if ShortSignal = 1 and haClose < haOpen then
SellShort 1 Contracts at Market
ShortSignal = 0 // Reset trigger
endif
//Add long positions only if initial conditions met again
if LONGONMARKET and haClose[1] < BollingerLower[1] and haClose > haOpen then
Buy 1 Contracts at Market
endif
//Add short positions only if initial conditions met again
if SHORTONMARKET and haClose[1] > BollingerUpper[1] and haClose < haOpen then
SellShort 1 Contracts at Market
endif
TIMEFRAME(5mn)
//Exit if single position and close crosses Bollinger middle
If LONGONMARKET and COUNTOFPOSITION=1 and close CROSSES OVER BollingerMiddle THEN
SELL AT MARKET
ENDIF
If SHORTONMARKET and COUNTOFPOSITION=-1 and close CROSSES UNDER BollingerMiddle THEN
EXITSHORT AT MARKET
ENDIF
//Exit if multiple positions when price reaches weighted entry + 5 points
If COUNTOFPOSITION > 1 THEN
If Close >= POSITIONPRICE + 5 THEN
SELL AT MARKET
ENDIF
ENDIF
If COUNTOFPOSITION < -1 THEN
If Close <= POSITIONPRICE - 5 THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
GraphOnPrice BollingerMiddle
GraphOnPrice BollingerUpper
GraphOnPrice BollingerLower
GraphOnPrice PositionPrice