OPEN-SOURCE SCRIPT

ES Stoch RSI [Krypt] Strategy

257
//version=6
strategy("ES Stoch RSI [Krypt] Strategy", overlay=false, precision=1)

// Constants
PI = 3.14159265359

// Functions
drop1st(src) =>
var float x = na
x := na(src[1]) ? na : src

xlowest(src, len) =>
var float x = src
for i = 1 to len - 1
v = src
if na(v)
break
x := math.min(x, v)
x

xhighest(src, len) =>
var float x = src
for i = 1 to len - 1
v = src
if na(v)
break
x := math.max(x, v)
x

xstoch(c, h, l, len) =>
float xlow = xlowest(l, len)
float xhigh = xhighest(h, len)
100 * (c - xlow) / (xhigh - xlow)

Stochastic(c, h, l, length) =>
float rawsig = xstoch(c, h, l, length)
math.min(math.max(rawsig, 0.0), 100.0)

xrma(src, len) =>
var float sum = na
sum := (src + (len - 1) * nz(sum[1], src)) / len

xrsi(src, len) =>
msig = nz(ta.change(src), 0.0)
up = xrma(math.max(msig, 0.0), len)
dn = xrma(math.max(-msig, 0.0), len)
rs = up / dn
100.0 - 100.0 / (1.0 + rs)

EhlersSuperSmoother(src, lower) =>
a1 = math.exp(-PI * math.sqrt(2) / lower)
coeff2 = 2 * a1 * math.cos(math.sqrt(2) * PI / lower)
coeff3 = -math.pow(a1, 2)
coeff1 = (1 - coeff2 - coeff3) / 2
var float filt = na
filt := nz(coeff1 * (src + nz(src[1], src)) + coeff2 * nz(filt[1], src) + coeff3 * nz(filt[2], src))

// Inputs
smoothK = input.int(10, minval=1, title="K")
smoothD = input.int(3, minval=1, title="D")
lengthRSI = input.int(14, minval=1, title="RSI Length")
lengthStoch = input.int(14, minval=1, title="Stochastic Length")
showsignals = input.bool(true, title="Buy/Sell Signals")
src = input.source(close, title="Source")

ob = 80
os = 20
midpoint = 50

// Indicator Calculations
price = math.log(drop1st(src))
rsi1 = xrsi(price, lengthRSI)
rawsig = Stochastic(rsi1, rsi1, rsi1, lengthStoch)
sig = EhlersSuperSmoother(rawsig, smoothK)
ma = ta.sma(sig, smoothD)

// Strategy Logic
mm1 = ta.change(ta.change(ma))
mm2 = ta.change(ta.change(ma[1]))
ms1 = ta.change(ta.change(sig))
ms2 = ta.change(ta.change(sig[1]))

sellsignals = showsignals and (mm1 + ms1 < 0 and mm2 + ms2 < 0) and ta.crossunder(sig, ma) and sig[1] > midpoint
buysignals = showsignals and (mm1 + ms1 > 0 and mm2 + ms2 > 0) and ta.crossover(sig, ma) and sig[1] < midpoint

if (buysignals)
strategy.close("Short")
strategy.entry("Long", strategy.long)

if (sellsignals)
strategy.close("Long")
strategy.entry("Short", strategy.short)

// Exits
if (sellsignals)
strategy.close("Long")
if (buysignals)
strategy.close("Short")

// Plots
plot(sig, color=color.blue, title="K")
plot(ma, color=color.orange, title="D")

lineOB = hline(ob, title="Upper Band", color=color.gray)
lineOS = hline(os, title="Lower Band", color=color.gray)
fill(lineOB, lineOS, color=color.new(color.purple, 90))

ploff = 4
plot(buysignals ? sig[1] - ploff : na, style=plot.style_circles, color=color.new(color.blue, 0), linewidth=3, title="Buy Signal")
plot(sellsignals ? sig[1] + ploff : na, style=plot.style_circles, color=color.new(color.red, 0), linewidth=3, title="Sell Signal")

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.