Heikin Ashi Bollinger Bands Strategy

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #243965 quote
    neddyboyneddyboy
    Participant
    New

    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
    
    #243974 quote
    plbourseplbourse
    Participant
    New

    Bonjour

    Sorry, I replied in French, surely it is better in english as this is the english forum

    Some remarks

    • The condition in line  40 If PositionSize > 0 is it also valid for short trtades ?
      I would recommend to split Long and Short trades and to create ExitConditionLong and ExitConditionShort

      • If LongonMarket then… ExitConditionLong = (close >= WeightedExitPrice + 5) …
      • and If Shortonmarket then … sortie ExitConditionShort = (close <= WeightedExitPrice – 5)
    • In the block of exit orders, I would rcommend to reset after exit eht PositionSize to 0ne faut-il pas mettre après la sortie une ligne de réactualisation PositionSize = 0 dans la boucle
      IF ExitConditionLong THEN
      IF PositionSize > 0 THEN
      …. ordres
      PositionSize = 0
      endif
      endif
    • and the same for IF  ExitConditionShort

    Hope this will be helpful, let us exchange about the modified code

    neddyboy thanked this post
    #244164 quote
    neddyboyneddyboy
    Participant
    New

    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
    #244165 quote
    GraHalGraHal
    Participant
    Master

    Assuming you are testing in your Demo Account, try changing position sizes to a minimum of 1.

    #244171 quote
    neddyboyneddyboy
    Participant
    New

    Thanks for your reply. I just tried changing the size to 1 but unfortunately still no positions opened.

    #244174 quote
    GraHalGraHal
    Participant
    Master

    You need below instead of your Line 17

    if(barindex>2) then
    HaOpen = (HaOpen[1] + HaClose[1]) / 2
    endif
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Heikin Ashi Bollinger Bands Strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
neddyboy @neddyboy Participant
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by GraHalGraHal
1 year, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/17/2025
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...