Coding for RSI Reversal with divergence
Forums › ProRealTime English forum › ProOrder support › Coding for RSI Reversal with divergence
- This topic has 15 replies, 6 voices, and was last updated 2 years ago by WillAuto.
Tagged: Divergence, DivergenceRSI, RSI
-
-
01/10/2022 at 9:57 AM #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- RSI < 33
- Price > 200 EMA
- Recent Price Low (for example a range of 5 periods ago) < Prior Price Low (for example a range of 10 periods ago)
- Recent RSI Low (for example a range of 5 periods ago) > Prior Price Low (for example a range of 10 periods ago)
- Current Price crosses over Recent Price High (for example the low of the last 10 periods)
for sell/short
- RSI > 65
- Price < 200 EMA
- Recent Price High (for example 5 periods ago) > Prior Price high (for example 10 periods ago)
- Recent RSI High (for example 5 periods ago) < Prior Price high (for example 10 periods ago)
- 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 LOSSIf Long
Stop Loss: at Prior Price Low (within last 10 periods)
Target: 3x the STOP LOSS12345678910111213141516171819202122232425262728293031323334// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated// Conditions to enter long positionsindicator1 = RSI[14](close)c1 = (indicator1 < 33)indicator2 = ExponentialAverage[200](close)c2 = (close > indicator2)c3 = (high[5] < high[10])indicator3 = RSI[14](close)c4 = (indicator3[5] > indicator3[10])c5 = (close CROSSES OVER high[10])IF c1 AND c2 AND c3 AND c4 AND c5 THENBUY 1 SHARES AT MARKETENDIF// Conditions to enter short positionsindicator4 = RSI[14](close)c6 = (indicator4 > 65)indicator5 = ExponentialAverage[200](close)c7 = (close < indicator5)c8 = (high[5] > high[10])indicator6 = RSI[14](close)c9 = (indicator6[5] < indicator6[10])c10 = (close CROSSES UNDER low[10])IF c6 AND c7 AND c8 AND c9 AND c10 THENSELLSHORT 1 SHARES AT MARKETENDIF// Stops and targetsSET STOP pLOSS 500 pTRAILING 250SET TARGET pPROFIT 100001/13/2022 at 6:04 AM #185313Haven’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?
1c3 = (close>highest[5]) < (highest[10])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)
01/14/2022 at 9:24 AM #18540601/17/2022 at 11:56 AM #185673Hi @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 rangesAlso 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,
01/18/2022 at 10:56 AM #1858015 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.
01/19/2022 at 6:48 AM #185896Very good idea – I’ve found what I feel is hopefully a clear example of a bullish set up. In this chart you can see:
- Price reaching a lower low
- RSI <30 with the higher low situation emerging
- Price above 200 EMA (as indicated with the constantly purple line)
- 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! 🙂
01/19/2022 at 9:16 AM #185899is 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.
01/19/2022 at 10:41 AM #185904Do 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):
1234567891011121314151617181920212223242526272829303132Defparam CumulateOrders = falseOnce LookBack = 30 //30 periods to scanOnce RSIperiods = 14Once OBlevel = 70Once OSlevel = 100 - OBlevelOnce BreakLevel = 0Once EntryFlag = 0Bullish = close > openBearish = close < openEma200 = average[200,1](close)c1 = close > Ema200If OnMarket thenBreakLevel = 0EntryFlag = 0EndifUPswing = (max(high,high[1]) = highest[LookBack](high)) and Bearish and Bullish[1]If UPswing and Not EntryFlag thenBreakLevel = max(high,high[1])EndifDOWNswing = (min(low,low[1]) = lowest[LookBack](low)) and Bullish and Bearish[1]If DOWNswing and BreakLevel and summation[2](DivergenceRSI[RSIperiods,OSlevel, OBlevel, LookBack](close)) thenEntryFlag = min(low,low[1])Endifif low < EntryFlag thenEntryFlag = 0EndifIf EntryFlag and c1 and close CROSSES OVER BreakLevel thenBuy 1 contract at MarketEndif// Stops and targetsSET STOP pLOSS 500 pTRAILING 250SET TARGET pPROFIT 100001/19/2022 at 10:53 AM #18590501/19/2022 at 10:58 AM #185907I am on my mobile now, I’ll make it later today.
01/20/2022 at 5:08 AM #186003Roberto, 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.
01/20/2022 at 12:13 PM #186050There you go. I also added Nicolas’trailing stop:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697//https://www.prorealcode.com/topic/coding-for-rsi-reversal-with-divergence///Defparam CumulateOrders = falseOnce LookBack = 30 //30 periods to scanOnce RSIperiods = 14Once OBlevel = 70Once OSlevel = 100 - OBlevelOnce BreakLevelL = 0Once BreakLevelS = 0Once EntryFlagL = 0Once EntryFlagS = 0Bullish = close > openBearish = close < openEma200 = average[200,1](close)L1 = close > Ema200S1 = close < Ema200If OnMarket thenBreakLevelL = 0BreakLevelS = 0EntryFlagL = 0EntryFlagS = 0Endif//UPswing = (max(high,high[1]) = highest[LookBack](high)) and Bearish and Bullish[1]DOWNswing = (min(low,low[1]) = lowest[LookBack](low)) and Bullish and Bearish[1]//If UPswing and Not EntryFlagL thenBreakLevelL = max(high,high[1])EndifIf DOWNswing and Not EntryFlagS thenBreakLevelS = min(low,low[1])Endif//If DOWNswing and BreakLevelL and summation[2](DivergenceRSI[RSIperiods,OSlevel, OBlevel, LookBack](close)) thenEntryFlagL = min(low,low[1])EndifIf UPswing and BreakLevelS and summation[2](DivergenceRSI[RSIperiods,OSlevel, OBlevel, LookBack](close)= -1) thenEntryFlagS = max(high,high[1])Endif//if low < EntryFlagL thenEntryFlagL = 0Endifif high > EntryFlagS thenEntryFlagS = 0Endif//If EntryFlagL and L1 and close CROSSES OVER BreakLevelL thenBuy 1 contract at MarketEndifIf EntryFlagS and S1 and close CROSSES UNDER BreakLevelS thenSELLSHORT 1 contract at MarketEndif// Stops and targetsSET STOP pLOSS 100SET TARGET pPROFIT 300////************************************************************************//trailing stop functiontrailingstart = 30 //trailing will start @trailinstart points profittrailingstep = 10 //trailing step to move the "stoploss"//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THENnewSL = tradeprice(1)+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THENnewSL = tradeprice(1)-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//************************************************************************1 user thanked author for this post.
01/21/2022 at 6:55 AM #186134Hi 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
01/21/2022 at 8:28 AM #186135ONCE 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.
01/31/2022 at 7:50 PM #187164Glad I found this post, great example, code and explanation. I learnt a lot from this.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on