How to write a Heiken Ashi

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #140342 quote
    Paulchal
    Participant
    Average
    Hi
    I would like to write a code where after a EMA crosses over another I buy a contract and sell when the Heiken Ashi (HA) turns from green to red.
    The EMA element is fine but cannot get the HA bit.
    I have the basics of the below but cannot work out how to write the code to get from green to red, a then vice a versa when going short from red to green
    //heiken ashi
    xClose = (Open+High+Low+Close)/4
    if(barindex>2) then
    xOpen= (xOpen[1] + xClose[1])/2
    xHigh= Max(xOpen, xClose)
    xLow= Min(xOpen, xClose)
    endif

     

    Can anyone help please?
    #140348 quote
    robertogozzi
    Moderator
    Master

    There you go:

    //heiken ashi
    xClose = (Open+High+Low+Close)/4
    if(barindex>2) then
       xOpen= (xOpen[1] + xClose[1])/2
       xHigh= Max(xOpen, xClose)
       xLow= Min(xOpen, xClose)
    endif
    xBullish    = xClose > xOpen
    xBearish    = xClose < xOpen
    ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)

    COLORCHANGE will be true whenever two adjacent HA candlesticks have different colours.

    #140459 quote
    Vonasi
    Moderator
    Master

    Paulchal – I have tidied up your post. Please always use the ‘Insert PRT Code’ button when putting code in your posts as per the forum rules.

    #140517 quote
    Paulchal
    Participant
    Average

    Hi

    I applied the above code but cannot get it to work. I had to //REM xHigh and xLow as the system said it was not included in the code.

    Same idea as before, When the MACD crosses over buy on a green HA. Close that order when the HA turns red (but do not automatically sell to open a new position).

    Any pointers to where I am going wrong would be appreciated.

    Sorry cant see where to “insert PRT code”.

    //————————————————————————-
    // Main code : MACD with HA End
    //————————————————————————-
    
    DEFPARAM CumulateOrders = False //MACD[12,26,9](close) Cumulating positions deactivated
    // The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the “FLATBEFORE” time.
    DEFPARAM FLATBEFORE = 200000
    // Cancel all pending orders and close all positions at the “FLATAFTER” time
    DEFPARAM FLATAFTER = 205000
    
    a = MACDline[12,26,9](close)
    b = MACD[12,26,9](close)
    
    //heiken ashi
    xClose = (Open+High+Low+Close)/4
    if(barindex>2) then
    xOpen= (xOpen[1] + xClose[1])/2
    //xHigh= Max(xOpen, xClose)
    //xLow= Min(xOpen, xClose)
    endif
    xBullish = xClose > xOpen
    xBearish = xClose < xOpen
    ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)
    
    // Conditions to enter long positions
    IF NOT LongOnMarket AND a crosses over b THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    IF LongOnMarket AND Colorchange THEN
    SELL AT MARKET
    ENDIF
    
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND a crosses under b THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    IF ShortOnMarket AND Colorchange THEN
    BUY AT MARKET
    ENDIF
    
    // Stops and targets : Enter your protection stops and profit targets here
    SET TARGET PPROFIT 40
    SET STOP Ptrailing 15
    #140520 quote
    robertogozzi
    Moderator
    Master

    You posted in the wrong forum, this is a strategy and therefore the correct support forum is ProOrder.

    To exit a Short trade you need EXITSHORT, since BUY will open a Long trade.

    #140561 quote
    robertogozzi
    Moderator
    Master

    You do not need to start a new topic, please append your further questions here, if relatedto the same topic.

    #140567 quote
    Paulchal
    Participant
    Average

    Sorry about any confusion I thought you wanted me to open a new post.

    I tried the EXITSHORT and positions still did not open. There was a trade this afternoon that would have made 73 points. MACD crossed under and red HA candle present afterwards. Short position did not open.

    #140613 quote
    robertogozzi
    Moderator
    Master

    Line 11 is correct.

    Replace line 12 with:

    b = ExponentialAverage[9](MACDline[12,26,9](close))  //Signal line

    and use EXITSHORT.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

How to write a Heiken Ashi


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Paulchal @paulchal Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by robertogozzi
5 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 07/28/2020
Status: Active
Attachments: No files
Logo Logo
Loading...