Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=2
- strategy(title = "RSI versus SMA R2", shorttitle = "RSISMA R2", overlay = false, pyramiding = 0, calc_on_every_tick = false,
- default_qty_type = strategy.percent_of_equity, default_qty_value = 10, currency = currency.GBP)
- // Revision: 2
- // Author: @JayRogers
- // Revisions by: R2 JustUncleL
- //
- // *** USE AT YOUR OWN RISK ***
- // - 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.
- //
- // Revisions:
- // - Removed redundant function code.
- // - Removed Trailing Stop option (gives false and misleading results in back testing)
- // - Simplified the Risk management code.
- // === 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?")
- // === /INPUTS ===
- // === 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 ===
- // === STRATEGY ===
- // stop loss
- slPoints = input(defval = 0, title = "Initial Stop Loss Points (zero to disable)", minval = 0)
- tpPoints = input(defval = 0, title = "Initial Target Profit Points (zero for disable)", minval = 0)
- // Include bar limiting algorithm
- ebar = input(defval = 0, title="Number of Bars for Back Testing", minval=0)
- dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
- //
- // Calculate how many mars since last bar
- tdays = (timenow-time)/60000.0 // number of minutes since last bar
- tdays := ismonthly? tdays/1440.0/5.0/4.3/interval : isweekly? tdays/1440.0/5.0/interval : isdaily? tdays/1440.0/interval : tdays/interval // number of bars since last bar
- //
- //set up exit parameters
- TP = tpPoints>0?tpPoints:na
- SL = slPoints>0?slPoints:na
- goLong() => not tradeDirection[1] and tradeDirection
- goShort() => tradeDirection[1] and not tradeDirection
- // Make sure we are within the bar range, Set up entries and exit conditions
- if (ebar==0 or tdays<=ebar)
- strategy.entry("Buy", strategy.long, when=goLong())
- strategy.entry("Sell", strategy.short, when=goShort())
- strategy.exit("XL", from_entry = "Buy", profit = TP, loss = SL)
- strategy.exit("XS", from_entry = "Sell", profit = TP, loss = SL)
- // === /STRATEGY ===
- // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement