Error codeing MACD crossover happens with 26 & 52 period EMA

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #140161 quote
    Rajesh DeshpandeRajesh Deshpande
    Participant
    Junior

    Hi ,

    I am new to PRorealtime. Thank you GURU’s for reading the topic.

    I am trying to build a code where in when MACD crossover happens with 26 & 52 period EMA.

    // Conditions to enter long positions
    defparam CUMULATEORDERS = false
    
    //macdline
    c2 = MACDline[ 26 , 52, 12 ](close)
    //signalline
    c3 = ExponentialAverage[12](c2)
    
    //histogram heigt
    histbar = c2 – c3
    c5 = histbar crosses over 0
    c6 = histbar crosses under 0
    
    IF NOT LongOnMarket AND c5 THEN
    BUY 1 CONTRACTS AT MARKET
    sl = lowest[3](low)
    set stop loss sl
    tgt = ( POSITIONPRICE – SL ) * 1.5
    set target $profit tgt
    ENDIF
    
    // Conditions to exit long positions
    
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND c6 THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    sl = highest[3](high)
    set stop loss sl
    tgt = ( sl – POSITIONPRICE ) * 1.5
    set target $profit tgt
    ENDIF

    The above code is not setting up the Stop loss & targets. The stop loss should be lowest of last three candles when going long & highest when shorting.

    Also i notice that order is place after 2 candles are formed delaying the entry of the order.

    Can anyone help what is wrong in the code.

    #140164 quote
    robertogozzirobertogozzi
    Moderator
    Legend
    • Give your topic a meaningful title.Describe your question or your subject in your title. Do not use meaningless titles such as ‘Coding Help Needed’.
    • Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.

    Thank you 🙂

    #140168 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    SET STOP LOSS and SET TARGET PROFIT require a difference between prices (Close – Low), not a price (Low).

    Replace lines 16-19  as follows:

    sl = abs(close-lowest[3](low))
    set stop loss sl
    tgt = SL * 1.5
    set target profit tgt

    do the same for the Short side using HIGHEST.

    Do not use $ for PROFIT, it’s intended for currency values, such as 400 etc…

Viewing 3 posts - 1 through 3 (of 3 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

Error codeing MACD crossover happens with 26 & 52 period EMA


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 2 replies,
has 2 voices, and was last updated by robertogozzirobertogozzi
6 years, 2 months ago.

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