RSI versus SMA (no repaint)

  • Nothing is perfect, and all decisions by you are on your own head. And stuff.

  • It's RSI versus a Simple Moving Average .. Not sure it really needs much more description.
  • Should not repaint - Automatically offsets by 1 bar if anything other than "open" selected as RSI source.

...sometimes smashing things together just happens to work...
Remove from Favorite Scripts Add to Favorite Scripts

strategy(title = "RSI versus SMA", shorttitle = "RSI vs SMA", overlay = false, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, currency = currency.GBP)

// Revision:        1
// Author:          @JayRogers
// - Nothing is perfect, and all decisions by you are on your own head. And stuff.
// Description:
//  - It's RSI versus a Simple Moving Average.. Not sure it really needs much more description.
//  - Should not repaint - Automatically offsets by 1 bar if anything other than "open" selected as RSI source.

// === INPUTS ===
// rsi
rsiSource   = input(defval = open, title = "RSI Source")
rsiLength   = input(defval = 8, title = "RSI Length", minval = 1)
// sma
maLength    = input(defval = 34, title = "MA Period", minval = 1)
// invert trade direction
tradeInvert = input(defval = false, title = "Invert Trade Direction?")
// risk management
useStop     = input(defval = false, title = "Use Initial Stop Loss?")
slPoints    = input(defval = 25, title = "Initial Stop Loss Points", minval = 1)
useTS       = input(defval = true, title = "Use Trailing Stop?")
tslPoints   = input(defval = 120, title = "Trail Points", minval = 1)
useTSO      = input(defval = false, title = "Use Offset For Trailing Stop?")
tslOffset   = input(defval = 20, title = "Trail Offset Points", minval = 1)
// === /INPUTS ===

// delay for direction change actions
switchDelay(exp, len) =>
    average = len >= 2 ? sum(exp, len) / len : exp[1]
    up      = exp > average
    down    = exp < average
    state   = up ? true : down ? false : up[1]
// === /BASE FUNCTIONS ===

// === SERIES and VAR ===
// rsi
shunt = rsiSource == open ? 0 : 1
rsiUp = rma(max(change(rsiSource[shunt]), 0), rsiLength)
rsiDown = rma(-min(change(rsiSource[shunt]), 0), rsiLength)
rsi = (rsiDown == 0 ? 100 : rsiUp == 0 ? 0 : 100 - (100 / (1 + rsiUp / rsiDown))) - 50 // shifted 50 points to make 0 median
// sma of rsi
rsiMa   = sma(rsi, maLength)
// self explanatory..
tradeDirection = tradeInvert ? 0 <= rsiMa ? true : false : 0 >= rsiMa ? true : false
// === /SERIES ===

// === PLOTTING ===
barcolor(color = tradeDirection ? green : red, title = "Bar Colours")
// hlines
medianLine  = hline(0, title = 'Median', color = #996600, linestyle = dotted, linewidth = 1)
limitUp     = hline(25, title = 'Limit Up', color = silver, linestyle = dotted, linewidth = 1)
limitDown   = hline(-25, title = 'Limit Down', color = silver, linestyle = dotted, linewidth = 1)
// rsi and ma
rsiLine     = plot(rsi, title = 'RSI', color = purple, linewidth = 2, style = line, transp = 50)
areaLine    = plot(rsiMa, title = 'Area MA', color = silver, linewidth = 1, style = area, transp = 70)
// === /PLOTTING ===

goLong() => not tradeDirection[1] and tradeDirection
killLong() => tradeDirection[1] and not tradeDirection
strategy.entry(id = "Buy", long = true, when = goLong())
strategy.close(id = "Buy", when = killLong())

goShort() => tradeDirection[1] and not tradeDirection
killShort() => not tradeDirection[1] and tradeDirection
strategy.entry(id = "Sell", long = false, when = goShort())
strategy.close(id = "Sell", when = killShort())

if (useStop)
    strategy.exit("XSL", from_entry = "Buy", loss = slPoints)
    strategy.exit("XSS", from_entry = "Sell", loss = slPoints)
// if we're using the trailing stop
if (useTS and useTSO) // with offset
    strategy.exit("XSL", from_entry = "Buy", trail_points = tslPoints, trail_offset = tslOffset)
    strategy.exit("XSS", from_entry = "Sell", trail_points = tslPoints, trail_offset = tslOffset)
if (useTS and not useTSO) // without offset
    strategy.exit("XSL", from_entry = "Buy", trail_points = tslPoints)
    strategy.exit("XSS", from_entry = "Sell", trail_points = tslPoints)
it seems really good. very well done.
+1 Reply
JayRogers jackvmk
Thanks! My time seems to get divided up equally between developing actual ideas and concepts that form in my head - and just taking a break and "facerolling" random things together.. This was a happy little accident of the latter lol.
jackvmk JayRogers
i wish for you a more accidents as this one lol. it is best win rate strategy which is works in lower time charts too. best strategy now i ever seen in Tradingview.
+1 Reply
JayRogers jackvmk
There are a lot of very good strategies developed by the community here - but they often get presented in a way that's less than optimal. The trick is being able to see their real potential and trade based on what you see, rather than necessarily the default settings supplied in the publication. If you get to that point, there's a whole world of opportunity lurking just beneath the surface here ;)
+1 Reply
Hi JayRogers. I believe it's a nice job but could you please explain it a bit more? i couldn't figure out how to take a trade with it and when would be the expiry time? should the time frame be 5 min?
Firstly you would need to have a play with the inputs and try to find a reasonable profit result on your chart. Next you would wait for it to make a trade entry, and copy that. There is no "target", I make use of trailing stops in the example to lock in any profit gained - so you would need to do the same. Any further explanation required here I'm afraid would indicate you are not ready to be live trading - if thats the case, please be careful and research how to find good/reasonable initial stop loss values and how to trail stops effectively.
haji JayRogers
Tnx JayRogers, I wanted to use it in BO indeed. Do you recommend it just for Forex?
As a general rule of thumb around here; unless the publisher states that an indicator or strategy is targeted at Binary Options, it's fairly safe to assume it's not intended for it. And to answer your question directly - yes I only develop ideas for forex personally, so that is all I would recommend it for ;)
This looks very impressive and so far I cannot pick up any repainting. This is a simple yet very effective strategy, well done mate! It is a bit crazy to manual trade on a 5m time frame however it seems to be working well on a 4hr and daily time frame across multiple pairs.

Why do most of the exits seem to occur at the exact high/low of the candles? This doesn't seem right? Where can I adjust it so that it closes the trade at the close of the "exit" candle?
+1 Reply
I just recently wrote up a risk management example script which contains much better strategy.exit() execution code and properly sets up the inputs. If you swap out the strategy.exit blocks (everything below and including if(useStop) ) for the two in the example and also the relevant inputs you will be able to get much finer control over the way the risk management is - or is not - handled.
JayRogers JayRogers
Strategy Code Example - Risk Management
Entity PRO JayRogers
Thank you! I shall take a look at it. Thanks for your continued work and input
charterm8 PRO JayRogers
@JayRogers, Can't get it working when I change the code. Could you help to modify this by adding the stop-loss controls from the other script, please?
this script works excellent with 5 min chart scripts as well, gives avg 75% success, really appreciate if someone provide includes alerting mechanism as well, I am new bee for pine editor and also to trading
Hi, JayRogers
I need to know how to use it in mt4
I don't know about script but i need your system for setup in mt4, can you help me?
Thank you in advance
United States
United Kingdom
Home Stock Screener Forex Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Account and Billing Sign Out