Code: Select all
//------------------------------------------------------------------------------------------
// inputs
src0 = input(title = "Source Input" , defval = close)
ch = input(title = "Oscillator Choice" , defval="Fisher", options=["Fisher", "IFT", "RSI", "Stoch", "RSI Stoch", "TSI", "CCI", "CMO", "MFI", "MOM", "WPR"])
period = input(title = "Main Period" , defval = 13)
variab = input(title = "% Variability around Main Period", defval = 15)
//------------------------------------------------------------------------------------------
// calculation of 4 periods around the main one, considering the variability input
p1 = int(period * (100 - variab ) / 100) < period - 2 ? period - 2 : int(period * (100 - variab ) / 100)
p2 = int(period * (100 - 2*variab / 3) / 100) < period - 1 ? period - 1 : int(period * (100 - 2*variab / 3) / 100)
p3 = int(period * (100 - 2*variab / 3) / 100) < period + 1 ? period + 1 : int(period * (100 - 2*variab / 3) / 100)
p4 = int(period * (100 + variab ) / 100) < period + 2 ? period + 2 : int(period * (100 - variab ) / 100)
//------------------------------------------------------------------------------------------
// functions
// Fisher transform
fsh(_src, _p) =>
high_ = highest(_src, _p)
low_ = lowest (_src, _p)
value = 0.0
value := (.66 * ((_src - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1])) > .99 ? .999 : (.66 * ((_src - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1])) < -.99 ? -.999 : (.66 * ((_src - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1]))
value1 = 0.0
value1 := .5 * log((1 + value) / max(1 - value, .001)) + .5 * nz(value1[1])
// Inverse Fisher transform of RSI
IFTRSI(_src, _p) =>
v2 = ema(0.1 * (rsi(_src, _p) - 50), 3)
IFT_rsi = 100* (exp(2 * v2) - 1) / (exp(2 * v2) + 1)
// RSI Stoch
rst(_src, _p) =>
_r = rsi(_src, _p)
rsistoch = 100*(_r - lowest(_r, _p)) / (highest(_r, _p) - lowest(_r, _p))
// Applying the oscillator of your choice
Choose_Osc(_src, _p) =>
ch == "Fisher" ? fsh(_src,_p) :
ch == "IFT" ? IFTRSI(_src, _p) :
ch == "RSI" ? rsi(_src,_p) :
ch == "Stoch"? stoch(_src,_src,_src,_p) :
ch == "RSI Stoch" ? rst(_src,_p) :
ch == "TSI"? tsi(_src,_p,_p) :
ch == "CCI" ? cci(_src,_p) :
ch == "CMO" ? cmo(_src,_p) :
ch == "MFI" ? mfi(_src,_p) :
ch == "MOM" ? mom(_src,_p) :
ch == "WPR" ? wpr(_p) :
0
// Calculate upper/lower/midlines to plot dependind on the chosen oscillator
Choose_Lns(_ch) =>
_mid = _ch == "Fisher" or _ch == "CMO" or _ch == "TSI" or _ch == "MOM" or _ch == "CCI" or _ch == "IFT" ? 0 : _ch == "RSI" or _ch == "Stoch" or _ch == "RSI Stoch" or _ch == "MFI" ? 50 : _ch == "WPR" ? -50 : 0
_lwlev = _ch == "Fisher" ? -2 : _ch == "CCI" ? -100 : _ch == "RSI" ? 30 : _ch == "Stoch" or _ch == "RSI Stoch" or _ch == "MFI" ? 20 : _ch == "WPR" ? -80 : 0
_uplev = _ch == "Fisher" ? 2 : _ch == "CCI" ? 100 : _ch == "RSI" ? 70 : _ch == "Stoch" or _ch == "RSI Stoch" or _ch == "MFI" ? 80 : _ch == "WPR" ? -20 : 0
_lwlim = _ch == "RSI" or _ch == "Stoch" or _ch == "RSI Stoch" or _ch == "MFI" ? 0 : _ch == "WPR" or _ch == "IFT" ? -100 : 0
_uplim = _ch == "RSI" or _ch == "Stoch" or _ch == "RSI Stoch" or _ch == "MFI" ? 100 : _ch == "WPR" or _ch == "IFT" ? 100 : 0
[_mid, _lwlev, _uplev, _lwlim, _uplim]
// Heikin Ashi function. All credits to allanster: https://www.tradingview.com/script/HtNYMuBO-Heikin-Ashi-Source-Function/, slightly modified to allow for different inputs
HA(_o, _h, _l, _c, _src) =>
Close = (_o + _h + _l + _c) / 4
Open = float(na)
Open := na(Open[1]) ? (_o + _c) / 2 : (nz(Open[1]) + nz(Close[1])) / 2
High = max(_h, max(Open, Close))
Low = min(_l, min(Open, Close))
HL2 = avg(High, Low)
HLC3 = avg(High, Low, Close)
OHLC4 = avg(Open, High, Low, Close)
Price = _src == 'close' ? Close : _src == 'open' ? Open : _src == 'high' ? High : _src == 'low' ? Low : _src == 'hl2' ? HL2 : _src == 'hlc3' ? HLC3 : OHLC4
//------------------------------------------------------------------------------------------
// HeikinAshi calcs
heikinarray = array.new_float(0,0)
array.push(heikinarray, Choose_Osc(src0, p1))
array.push(heikinarray, Choose_Osc(src0, p2))
array.push(heikinarray, Choose_Osc(src0, p3))
array.push(heikinarray, Choose_Osc(src0, p4))
array.sort(heikinarray, order.descending)
//ohlc
h = array.get(heikinarray,0)
c = array.get(heikinarray,1)
o = array.get(heikinarray,2)
l = array.get(heikinarray,3)
hi = HA(o, h, l, c, 'high' )
cl = HA(o, h, l, c, 'close')
op = HA(o, h, l, c, 'open' )
lo = HA(o, h, l, c, 'low' )
//------------------------------------------------------------------------------------------
// Plots
barColor = cl > op ? #26a69a : #ef5350
plotcandle(op, hi, lo, cl, title = "", color = barColor, wickcolor = barColor, bordercolor = barColor)
[mid, lwlev, uplev, lwlim, uplim] = Choose_Lns(ch)
plot(mid , color = color.silver)
plot(lwlev, color = color.silver)
plot(uplev, color = color.silver)
plot(lwlim, color = color.silver, linewidth = 2)
plot(uplim, color = color.silver, linewidth = 2)
//------------------------------------------------------------------------------------------