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 LOSS
If Long
Stop Loss: at Prior Price Low (within last 10 periods)
Target: 3x the STOP LOSS
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Conditions to enter long positions
indicator1 = 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 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Conditions to enter short positions
indicator4 = 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 THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS 500 pTRAILING 250
SET TARGET pPROFIT 1000
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?
c3 = (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)
That conditions look like divergences IMO? There are many topics and useful codes of RSI divergences around, both in forums and in the library 🙂
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,
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.
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:
- 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! 🙂
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.
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):
Defparam CumulateOrders = false
Once LookBack = 30 //30 periods to scan
Once RSIperiods = 14
Once OBlevel = 70
Once OSlevel = 100 - OBlevel
Once BreakLevel = 0
Once EntryFlag = 0
Bullish = close > open
Bearish = close < open
Ema200 = average[200,1](close)
c1 = close > Ema200
If OnMarket then
BreakLevel = 0
EntryFlag = 0
Endif
UPswing = (max(high,high[1]) = highest[LookBack](high)) and Bearish and Bullish[1]
If UPswing and Not EntryFlag then
BreakLevel = max(high,high[1])
Endif
DOWNswing = (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)) then
EntryFlag = min(low,low[1])
Endif
if low < EntryFlag then
EntryFlag = 0
Endif
If EntryFlag and c1 and close CROSSES OVER BreakLevel then
Buy 1 contract at Market
Endif
// Stops and targets
SET STOP pLOSS 500 pTRAILING 250
SET TARGET pPROFIT 1000
Looks good Roberto. How would an inverse look like for entry short?
I am on my mobile now, I’ll make it later today.
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.
There you go. I also added Nicolas’trailing stop:
//https://www.prorealcode.com/topic/coding-for-rsi-reversal-with-divergence/
//
Defparam CumulateOrders = false
Once LookBack = 30 //30 periods to scan
Once RSIperiods = 14
Once OBlevel = 70
Once OSlevel = 100 - OBlevel
Once BreakLevelL = 0
Once BreakLevelS = 0
Once EntryFlagL = 0
Once EntryFlagS = 0
Bullish = close > open
Bearish = close < open
Ema200 = average[200,1](close)
L1 = close > Ema200
S1 = close < Ema200
If OnMarket then
BreakLevelL = 0
BreakLevelS = 0
EntryFlagL = 0
EntryFlagS = 0
Endif
//
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 then
BreakLevelL = max(high,high[1])
Endif
If DOWNswing and Not EntryFlagS then
BreakLevelS = min(low,low[1])
Endif
//
If DOWNswing and BreakLevelL and summation[2](DivergenceRSI[RSIperiods,OSlevel, OBlevel, LookBack](close)) then
EntryFlagL = min(low,low[1])
Endif
If UPswing and BreakLevelS and summation[2](DivergenceRSI[RSIperiods,OSlevel, OBlevel, LookBack](close)= -1) then
EntryFlagS = max(high,high[1])
Endif
//
if low < EntryFlagL then
EntryFlagL = 0
Endif
if high > EntryFlagS then
EntryFlagS = 0
Endif
//
If EntryFlagL and L1 and close CROSSES OVER BreakLevelL then
Buy 1 contract at Market
Endif
If EntryFlagS and S1 and close CROSSES UNDER BreakLevelS then
SELLSHORT 1 contract at Market
Endif
// Stops and targets
SET STOP pLOSS 100
SET TARGET pPROFIT 300
//
//************************************************************************
//trailing stop function
trailingstart = 30 //trailing will start @trailinstart points profit
trailingstep = 10 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
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
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.
Glad I found this post, great example, code and explanation. I learnt a lot from this.