Hi everyone,
Wondered if anyone can help me. I’m trying to build a strategy based around Heikin Ashi Candles and Bollinger bands. To enter a buy position a red heikin ashi candle must close below the lower Bollinger band and then on the next green heikin candle open a buy of 0.5 contracts. If the price goes against the position, if the entry conditions are met again with a minimum of a 10 point distance, enter another position of 0.1 etc. Close all positions when the price meets the weighted position price + 5 points (or whatever set). It should do the opposite for short positions. This is what I have so far, apologies for my code, I’m a novice…
DEFPARAM CumulateOrders = True
//Calculate Bollinger Bands
BollMid = Average[20](close)
BollUpper = BollingerUp[20](close)
BollLower = BollingerDown[20](close)
HaClose = (Open + High + Low + Close) / 4
if(barindex>2) then
HaOpen = (HaOpen[1] + HaClose[1]) / 2
endif
//Fixing HaHigh calculation
TempHigh = max(HaOpen, HaClose)
HaHigh = max(High, TempHigh)
//Fixing HaLow calculation
TempLow = min(HaOpen, HaClose)
HaLow = min(Low, TempLow)
//Define Heikin-Ashi color change
HaGreen = (HaClose > HaOpen)
HaRed = (HaClose < HaOpen)
//Track entry prices
ONCE EntryPrice = 0
ONCE PositionSize = 0
//Entry conditions
LongCondition = (HaGreen AND HaClose > BollLower AND HaClose[1] <= BollLower)
ShortCondition = (HaRed AND HaClose < BollUpper AND HaClose[1] >= BollUpper)
//Re-entry conditions (add positions if price moves 10 points against the trade)
MinMove = 10
AddLongCondition = (LongCondition AND abs(close - EntryPrice) >= MinMove)
AddShortCondition = (ShortCondition AND abs(close - EntryPrice) >= MinMove)
//Exit condition (close all positions at weighted average + 5 points)
IF PositionSize > 0 THEN
WeightedExitPrice = POSITIONPRICE
ExitCondition = (close >= WeightedExitPrice)
ELSE
ExitCondition = 0
ENDIF
//Execute trades
IF LongCondition THEN
BUY 0.5 CONTRACT AT MARKET
EntryPrice = close
PositionSize = PositionSize + 0.5
ENDIF
IF ShortCondition THEN
SELLSHORT 0.5 CONTRACT AT MARKET
EntryPrice = close
PositionSize = PositionSize + 0.5
ENDIF
//Add positions if price moves against initial trade
IF AddLongCondition THEN
BUY 0.1 CONTRACT AT MARKET
PositionSize = PositionSize + 0.1
ENDIF
IF AddShortCondition THEN
SELLSHORT 0.1 CONTRACT AT MARKET
PositionSize = PositionSize + 0.1
ENDIF
//Close all positions when reaching WeightedExitPrice
IF ExitCondition THEN
IF PositionSize > 0 THEN
IF LONGONMARKET THEN
SELL AT MARKET
ELSE
IF SHORTONMARKET THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
ENDIF
ENDIF
Bonjour
Sorry, I replied in French, surely it is better in english as this is the english forum
Some remarks
Hope this will be helpful, let us exchange about the modified code
Hi,
Thanks for replying. I’ve made some modifications to the code but I still can’t get it to open any positions. Would anyone be able to help?
DEFPARAM CumulateOrders = True
//Calculate Bollinger Bands
BollMid = BollingerUp[20](close) + BollingerDown[20](close) / 2
BollUpper = BollingerUp[20](close)
BollLower = BollingerDown[20](close)
PositionSize = 0.5
PositionIncrease = 0.5
Pointsgain = 5
heikinlow = 0
heikinhigh = 0
// Calculate Heikin-Ashi candles
HaClose = (Open + High + Low + Close) / 4
HaOpen = (HaOpen[1] + HaClose[1]) / 2
HaHigh = max(HaOpen, HaClose)
HaLow = min(HaOpen, HaClose)
//Define Heikin-Ashi color change
HaGreen = (HaClose > HaOpen)
HaRed = (HaClose < HaOpen)
//Heikin cross low
If HaClose < BollLower then
heikinlow = 1
ENDIF
//Heikin cross High
If HaClose > BollUpper then
heikinhigh = 1
ENDIF
//Buy entry conditions
IF heikinlow = 1 and HaGreen THEN
buy PositionSize CONTRACTS AT MARKET
ENDIF
//Sell entry conditions
IF heikinhigh = 1 and HaRed THEN
SELLSHORT PositionSize CONTRACTS AT MARKET
ENDIF
//Re-entry conditions (add positions if price moves 10 points against the trade)
MinMove = 10
AddLongCondition = (LONGONMARKET AND abs(HaClose - TRADEPRICE) >= MinMove)
AddShortCondition = (SHORTONMARKET AND abs(HaClose - TRADEPRICE) >= MinMove)
//Exit conditions (close all positions at weighted average + points gain)
IF PositionSize <> 0 THEN
ExitConditionlong = (close >= (POSITIONPRICE + Pointsgain))
ENDIF
IF PositionSize <> 0 THEN
ExitConditionshort = (close <= (POSITIONPRICE - Pointsgain))
ENDIF
//Add positions if price moves against initial trade
IF AddLongCondition and heikinlow = 1 and HaGreen THEN
BUY PositionIncrease CONTRACTS AT MARKET
ENDIF
IF AddShortCondition and heikinhigh = 1 and HaRed THEN
SELLSHORT PositionIncrease CONTRACTS AT MARKET
ENDIF
//Close all positions when reaching WeightedExitPrice
IF ExitConditionlong THEN
SELL AT MARKET
heikinlow = 0
ELSE
IF SHORTONMARKET and ExitConditionshort then
EXITSHORT AT MARKET
heikinhigh = 0
ENDIF
ENDIF
Assuming you are testing in your Demo Account, try changing position sizes to a minimum of 1.
Thanks for your reply. I just tried changing the size to 1 but unfortunately still no positions opened.
You need below instead of your Line 17
if(barindex>2) then
HaOpen = (HaOpen[1] + HaClose[1]) / 2
endif