Coding for RSI Reversal with divergence

Forums ProRealTime English forum ProOrder support Coding for RSI Reversal with divergence

Viewing 15 posts - 1 through 15 (of 16 total)
  • #184997

    Hi guys,

    I would appreciate some assistance. I’m quite new to using PRT and I am trying to code an RSI reversal strategy with some simple conditions, but I am unable to complete the code correctly.

    I need particular help with period ranges for High/Lows (see rules 3/4/5) – any ideas on how best to code this?

    I’ve written out my strategy rules and also copied in a copy of my current code (see both below).

    Many thanks in advance!

    The rules are:
    for buy/long

    1. RSI < 33
    2. Price > 200 EMA
    3. Recent Price Low (for example  a range of 5 periods ago) < Prior Price Low (for example a range of 10 periods ago)
    4. Recent RSI Low (for example  a range of 5 periods ago) > Prior Price Low (for example  a range of 10 periods ago)
    5. Current Price crosses over Recent Price High (for example the low of the last 10 periods)

    for sell/short

    1. RSI > 65
    2. Price < 200 EMA
    3. Recent Price High (for example 5 periods ago) > Prior Price high (for example 10 periods ago)
    4. Recent RSI High (for example 5 periods ago) < Prior Price high (for example 10 periods ago)
    5. Current Price crosses under Recent Price Low (for example the low of the last 10 periods)

    Stop Loss

    If short
    Stop Loss:  at Prior Price high (within last 10 periods)
    Target: 3x the STOP LOSS

    If Long
    Stop Loss:  at Prior Price Low (within last 10 periods)
    Target: 3x the STOP LOSS

     

     

    #185313

    Haven’t seen any responses yet unfortunately, but I think I have made a little progress at understanding my 3rd condition/rule. Would be thankful if someone could tell me if this looks right?

    I still think this isn’t quite right, but getting close to creating an indicator that identifies a double top that (with the second top being smaller than the first)

    #185406

    That conditions look like divergences IMO? There are many topics and useful codes of RSI divergences around, both in forums and in the library 🙂

    #185673

    Hi @Nicolas,

    Yes, trying to create a divergence criteria (albeit I think this code is not quite right for what I need it to do). My two main concerns are

    1) in my last example (the double top), I’m not confident this truly does identify a double top
    2) range of periods is still an issue (as you see I used 5 periods and 10 periods (for first and second top) – not sure how to amend this to be more flexible to different period ranges

    Also thank you for your suggestion re: looking at the library and forums. Can I ask, is there an efficient way to search for topics keywords here? as I’m not having much luck finding relevant codes or help etc.  (apologies, still very new to PRT and ProRealCode site)

    Of the divergence/ MW/WM codes I have come across so far, it doesn’t seem that many have actually directly addressed it or just don’t seem fit for purpose (best example I have seen was someone trying to use moving averages + LEVMIN/MAX, which can provide too many false signals, which I want to avoid) Simpler the better IMO.

    Many thanks,

    #185801

    5 and 10 periods are indeed strict, so combining all conditions together leads to … no signals 🙂

    It would be better if you could think what a setup should look on the chart, and post it here.

    Try to find first with a bullish setup.

    #185896

    Very good idea – I’ve found what I feel is hopefully a clear example of a bullish set up. In this chart you can see:

    1. Price reaching a lower low
    2. RSI <30 with the higher low situation emerging
    3.  Price above 200 EMA (as indicated with the constantly purple line)
    4. Price starts to break major structure (or prior high) indicated by yellow circles  – this signal being the buy indicator

    Something not on the chart, but quite important (which I don’t know how to code for yet) – I want to set the stop loss at the price low in this example and the target at 3x this.

    Hope this is a clear enough illustration! 🙂

     

    #185899

    is there an efficient way to search for topics keywords here?

    Hover over your photo (top right) and you will see a Search box.

    You can also use google, just enter prorealcode + whatever term you are searching for. Use inverted commas around 2 terms etc … usual google protocol.

    1 user thanked author for this post.
    #185904

    Do not embed files in your post, as this slows down the loading of pages.
    Use the SELECT FILE to attach them, instead.
    Thank you 🙂

    You can easily spot RSI divergences using the keyword DivergenceRSI.

    SET STOP pLOSS 500 pTRAILING 250, only works in backtest. You can’t set two different STOPs in the same line.

    I suggest that you replace the TRAILING STOP with a code snippet. You can find Nicolas’Trailing Stop (lines 17-56), ready-made, at https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/.

    There you go (not tested):

     

    #185905

    Looks good Roberto. How would an inverse look like for entry short?

    #185907

    I am on my mobile now, I’ll make it later today.

    #186003

    Roberto, thanks for this, appreciate you providing this code!

    I’ll keep a look-out for your upcoming update re: SnorreDK asking about the inverse with short entry.

    As for the TRAILING STOP, thanks for the link, I’ll get myself familiar with this and look to build in a different stop loss.

     

    #186050

    There you go. I also added Nicolas’trailing stop:

     

    1 user thanked author for this post.
    #186134

    Hi Roberto,

    I have a question and I apologize ahead of time if this sounds stupid! still learning my way around the code! 🙂

    At the beginning of the code you use a series of ONCE instructions (I assume this is to give first time values for the code to initiate appropriately). Could I ask, what function is provided by the ONCE EntryFlagL = 0 and ONCE EntryFlagS = 0

    I see the criteria for these two EntryFlagL (lines 34-35) and EntryFlagS (lines 37-38) nice and clearly defined, but I’d just like to understand how this interacts with the ONCE function.

    Many thanks,

    Will

    #186135

    ONCE is used in lines 4, 5, 6 and 7 to assign variables a value that never changes (but it could, though, if needed).

    ONCE is also used to initialize variables for the very first time, to make sure they retain a numeric value. I might remove ONCE, but in such case the previous value would be cleared each new bar, which I don’t want.

     

    1 user thanked author for this post.
    #187164

    Glad I found this post, great example, code and explanation. I learnt a lot from this.

    1 user thanked author for this post.
Viewing 15 posts - 1 through 15 (of 16 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login