Heikin Ashi Strategy

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #244728 quote
    neddyboy
    Participant
    New

    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
    #244735 quote
    JS
    Participant
    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"
    #244824 quote
    neddyboy
    Participant
    New

    Thank-you very much. This works well.

    JS thanked this post
    #244851 quote
    VinzentVega
    Participant
    Veteran

    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?

    Unbenannt.jpg Unbenannt.jpg
    #244973 quote
    neddyboy
    Participant
    New

    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.

    #244974 quote
    neddyboy
    Participant
    New

    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
    #244979 quote
    Niklas87
    Participant
    New

    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.

    #244982 quote
    JS
    Participant
    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
    robertogozzi thanked this post
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Heikin Ashi Strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
neddyboy @neddyboy Participant
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by JS
11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/06/2025
Status: Active
Attachments: 1 files
Logo Logo
Loading...