Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © ZenAndTheArtOfTrading
- //@version=4
- strategy("TTC RSI Strategy", overlay=true, calc_on_order_fills=true)
- // Get user input
- startDay = input(title="Start Day", type=input.integer, defval=1, minval=1, maxval=31)
- startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
- startYear = input(title="Start Year", type=input.integer, defval=1)
- rsiLen = input(title="RSI Length", type=input.integer, defval=10)
- rsiOB = input(title="RSI OB", type=input.float, defval=70.0)
- rsiOS = input(title="RSI OS", type=input.float, defval=30.0)
- emaLen = input(title="EMA", type=input.integer, defval=20)
- rr = input(title="R:R", type=input.float, defval=1.0, step=0.1)
- stopDistance = input(title="SL Distance", type=input.float, defval=5.0)
- useATR = input(title="Use ATR Stop?", type=input.bool, defval=false)
- // Get indicator values
- rsi = rsi(close, rsiLen)
- atr = atr(14)
- ema = ema(close, emaLen)
- // Custom function to convert whole numbers back into pips
- toPips(number) =>
- return = atr(14) >= 1.0 ? number : (number * syminfo.mintick) * (10 / syminfo.pointvalue)
- return := atr(14) >= 1.0 and atr(14) < 100.0 and syminfo.currency == "JPY" ? return / 100 : return
- // See if this bar's time happened on/after start date
- afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDay, 0, 0))
- // Declare trade variables
- var lookingForTrade = true
- var inLongTrade = false
- var inShortTrade = false
- var tradeStop = 0.0
- var tradeTarget = 0.0
- var lookingForEMA = false
- enterLong = false
- enterShort = false
- // Check filters
- filters = afterStartDate and not lookingForEMA
- // Enter long trades
- if rsi >= rsiOB and not inLongTrade and not inShortTrade and filters
- enterLong := true
- inLongTrade := true
- tradeStop := low - (useATR ? atr : toPips(stopDistance))
- distance = close - tradeStop
- tradeTarget := close + (distance * rr)
- // Enter short trades
- if rsi <= rsiOS and not inLongTrade and not inShortTrade and filters
- enterShort := true
- inShortTrade := true
- tradeStop := high + (useATR ? atr : toPips(stopDistance))
- distance = tradeStop - close
- tradeTarget := close - (distance * rr)
- // Check trade results
- resultColor = color.black
- // Check if long target hit
- if inLongTrade and high >= tradeTarget
- inLongTrade := false
- resultColor := color.lime
- lookingForEMA := true
- // Check if long stop hit
- if inLongTrade and low <= tradeStop
- inLongTrade := false
- resultColor := color.maroon
- lookingForEMA := true
- // Check if short target hit
- if inShortTrade and low <= tradeTarget
- inShortTrade := false
- resultColor := color.lime
- lookingForEMA := true
- // Check if short stop hit
- if inShortTrade and high >= tradeStop
- inShortTrade := false
- resultColor := color.maroon
- lookingForEMA := true
- // Check if we need to check for EMA touch
- if lookingForEMA and ema >= low and ema <= high
- lookingForEMA := false
- // Draw data to chart
- plotshape(enterLong ? 1 : na, style=shape.triangleup, location=location.belowbar, color=color.lime, transp=0)
- plotshape(enterShort ? 1 : na, style=shape.triangledown, color=color.red, transp=0)
- plot(ema, color=color.blue)
- plot(inLongTrade or inShortTrade ? tradeTarget : na, style=plot.style_linebr, color=color.green, transp=0)
- plot(inLongTrade or inShortTrade ? tradeStop : na, style=plot.style_linebr, color=color.red, transp=0)
- // Enter trades whenever a valid setup is detected
- strategy.entry(id="Long", long=strategy.long, when=enterLong)
- strategy.entry(id="Short", long=strategy.short, when=enterShort)
- // Exit trades whenever a valid exit reason is detected
- strategy.exit(id="Long Exit", from_entry="Long", limit=tradeTarget, stop=tradeStop, when=strategy.position_size > 0)
- strategy.exit(id="Short Exit", from_entry="Short", limit=tradeTarget, stop=tradeStop, when=strategy.position_size < 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement