RSI trigger – simplified coding?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #20413 quote
    jonjon
    Participant
    Average

    Happy New Year all. Wow, the holiday passed quickly.

    My brain may be being a bit slow. See the code below. Is there a more efficient way of coding this simple trigger? I was thinking it could also be useful when used with a market scanner to find setups. Eg: scan to find anything with a RSI passing below 70, then have the automated code to trigger when it passes below 70 again.

    Apologies if this has been covered in another topic. I had a quick search and couldn’t find anything.

    Example product: Platinum. Code PLXXXX. Just end of day data (I think everyone will therefore have access).

    Date: Look back to Aug 2016.

    Trigger: RSI passing under 70 level to sell 1 lot. Easy so far. However, I’ve seen on a few occasions that the trigger should not be the first “passing under” of the 70 level, instead it is better to wait and see if the break below 70 is real, let it retest the overbought level by returning above 70, and then waiting for it to pass under 70 again (within a specified time period). I hope this makes sense. Just look back at the RSI for Platinum for last Aug and you will see. There is a nice selloff when it passes under 70 the second time. Also note that there is a divergence on 10th Aug: up candle with RSI increasing above 70.

    5th Aug: this is the first break under 70 level. Wait and see.

    10th Aug: RSI goes back above 70

    11th Aug: passes under 70 again. Trigger to sell 1 lot on open of 12th Aug.

     

    Question: can the code be made more efficient instead of manually having to stipulate each indicator[1], indicator[2], indicator….[x]? E.g. if I wanted to look at 5min bars for another product (I’ve nothing in mind at the moment), is it easy to stipulate that instead of looking for RSI breaks 70 level twice within last (say) 5 bars it can be changed to 20 bars easily. Or 50 bars? I hope you get what I mean.

    I can’t attach screenshots or files hence this long request.

    Any thoughts appreciated. Thanks

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter short positions
    indicator1 = RSI[14](close)
    c1 = (indicator1 CROSSES UNDER 70)
    indicator2 = RSI[14](close)
    c2 = (indicator2[1] CROSSES UNDER 70)
    indicator3 = RSI[14](close)
    c3 = (indicator3[2] CROSSES UNDER 70)
    indicator4 = RSI[14](close)
    c4 = (indicator4[3] CROSSES UNDER 70)
    indicator5 = RSI[14](close)
    c5 = (indicator5[4] CROSSES UNDER 70)
    indicator6 = RSI[14](close)
    c6 = (indicator6[5] CROSSES UNDER 70)
    
    IF c1 AND (c2 OR c3 OR c4 OR c5 OR c6) THEN
    SELLSHORT 1 SHARES AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    indicator7 = RSI[14](close)
    c7 = (indicator7 CROSSES UNDER 30)
    
    IF c7 THEN
    EXITSHORT AT MARKET
    ENDIF
    #20561 quote
    Nicolas
    Keymaster
    Master

    You can easily make a summation of how many times the RSI cross below the 70 level in the past X candlesticks:

    X = 10 //lookback periods to check the RSI cross
    count = summation[X](RSI[14](close) crosses under 70)
    
    RETURN count

    In this example, the ‘count’ variable will give you the sum of the boolean condition under parenthesis that return true.

    jonjon thanked this post
    #20673 quote
    jonjon
    Participant
    Average

    Thanks Nicolas.

    Easy and neat solution.

    However I’m having a problem and keep getting a syntax error message.  I’ve tried in vain many variations of the code below. I just want it to trigger on occasions where, over the last 5 bars, the 70 level is passed twice. Any chance to you take a look?

    I’m preparing to shoot myself for a stupid error 😉

     

    X = 5 //lookback periods to check the RSI cross
    count = summation[X](RSI[14](close) crosses under 70)
    
    RETURN count
    
    IF count = 2
    SELLSHORT 1 SHARES AT MARKET
    ENDIF
    #20676 quote
    JC_Bywan
    Moderator
    Master

    Hi,

    Your syntax error is because you used “return” (last line of an Indicator in Probuilder module) and “sellshort” (from Probacktest/proorder module to build trading strategies rather than indicators) in the same piece of code.

    You can’t mix the 2, you have to choose, either you build an indicator ending with “return” and no buy or sell order, or you build a strategy buying and selling but without “return” line.

    But please don’t shoot yourself, we enjoy our forum members staying alive… 🙂

    jonjon thanked this post
    #20677 quote
    jonjon
    Participant
    Average

    Ah, I get it now. I kinda knew it / I had seen that before. Got it to work now through your help and luck. Perhaps just a graze to a toe?

    I hope you don’t mind me putting down my steps here for others to follow:

    1. Create a new indicator (I’ve named it “# RSI crosses”)
    // Conditions to enter short positions
    X = 5 //lookback periods to check the RSI cross
    count = summation[X](RSI[14](close) crosses under 70)
    
    RETURN count

    This then adds a new indicator at the bottom of the chart.

     

    2. Create a new strategy and get it to reference the “# RSI crosses” indicator. Got the sell trigger where the indicator = 2

     

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Conditions to enter short positions
    indicator1 = CALL "# RSI crosses"
    c1 = (indicator1 = 2)
    
    IF c1 THEN
    SELLSHORT 1 SHARES AT MARKET
    ENDIF
    
    // Conditions to exit short positions
    indicator2 = RSI[14](close)
    c2 = (indicator2 CROSSES UNDER 30)
    
    IF c2 THEN
    EXITSHORT AT MARKET
    ENDIF
    #20805 quote
    Big Hug
    Participant
    Average

    Nice indicator JonJon – I’ve already named it after you…:-)

    “Long” Version

    // Conditions to enter short positions
    X = 5 //lookback periods to check the RSI cross
    count = summation[X](RSI[14](close) crosses over 30)

    RETURN count

    jonjon thanked this post
    #20806 quote
    Big Hug
    Participant
    Average

    Edited “Short” to “Long”

    Nice indicator JonJon – I’ve already named it after you…:-)

    “Long” Version

    // Conditions to enter Long positions

    X = 5 //lookback periods to check the RSI cross

    count = summation[X](RSI[14](close) crosses over 30)

    RETURN count

    jonjon thanked this post
    #20827 quote
    jonjon
    Participant
    Average

    Thanks, although can’t really take credit. Perhaps rename it the Nicolas_Noobywan indicator??

     

    Thanks to all for their help

    #20831 quote
    JC_Bywan
    Moderator
    Master

    Thanks Jonjon, that’s very kind of you, especially considering sometimes we answer questions not always easy to solve, and don’t even get a thank you. So you’re very kind and I appreciate it and I wish all forum members were as kind as you all the time, but in this case I didn’t contribute to the logic of the indicator, I only helped quickly with the syntax error. That’s far from enough to put my name on it.

    Jonjon or Jonjon-Nicolas indicator sounds good to me if you want a name with names. RSI trigger sounded good too. Maybe Nicolas would agree that “Jonjon RSI trigger” could be best name choice. To be honest after you’ve developped a few, you’ll soon find out naming an indicator with a hint on its function rather than a hint on every single person who contributed to it will go a long way to find it faster in your PRT personal indicators list.

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

RSI trigger – simplified coding?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
jonjon @jonjon Participant
Summary

This topic contains 8 replies,
has 4 voices, and was last updated by JC_Bywan
9 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/10/2017
Status: Active
Attachments: No files
Logo Logo
Loading...