Untitled

 avatar
unknown
plain_text
2 years ago
36 kB
13
Indexable
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator("RSI • Yata", 
     overlay = false, 
     precision = 3, 
     max_bars_back = 500)

// ---------------------------------
groupRSI = "Relative Strength Index"
// ---------------------------------

len	= input.int	(14, minval=1	, title="Length", inline="RSI0", group=groupRSI)
src	= input		(close			, title="Source", inline="RSI0", group=groupRSI)

up = ta.rma(math.max(ta.change(src), 0), len)
dn = ta.rma(-math.min(ta.change(src), 0), len)
rsi0 = dn == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / dn)

show_smooth	= input.bool(false	, title="Smoothed RSI", inline="RSI0.5", group=groupRSI)
ema_smooth	= input.int	(3		, title="| Smooth Length", inline="RSI0.5", group=groupRSI)
rsi_smooth	= ta.ema	(rsi0	, ema_smooth)

rsi = show_smooth ? rsi_smooth : rsi0

// ---------------------------------

show_bar = input.bool(false, title="Show Bars Colors", inline="RSI1", group=groupRSI)

color_rsi_30 = input.color(color.new(#0B54FE, 0), title="Colors:"	, inline="RSI2", group=groupRSI)
color_rsi_40 = input.color(color.new(#3348f4, 0), title=""			, inline="RSI2", group=groupRSI)
color_rsi_49 = input.color(color.new(#4743EE, 0), title=""			, inline="RSI2", group=groupRSI)
color_rsi_51 = input.color(color.new(#8431DF, 0), title=""			, inline="RSI2", group=groupRSI)
color_rsi_60 = input.color(color.new(#AC26D5, 0), title=""			, inline="RSI2", group=groupRSI)
color_rsi_70 = input.color(color.new(#FC0FC0, 0), title=""			, inline="RSI2", group=groupRSI)
color_rsi_no = input.color(color.new(#7e57c2, 0), title="|"			, inline="RSI2", group=groupRSI)

rsi_color = rsi < 30 ? color_rsi_30 : 
     rsi < 40 ? color_rsi_40 : 
	 rsi < 50 ? color_rsi_49 : 
	 rsi > 70 ? color_rsi_70 : 
	 rsi > 60 ? color_rsi_60 : 
	 rsi > 50 ? color_rsi_51 : 
	 na

barcolor(show_bar ? rsi_color : na)

// ---------------------------------

show_rsiF = input.bool(false, title="Show RSI Background Fill", inline="RSI3", group=groupRSI)

rsiF		= rsi //ta.rsi(src, len)
rsi_plot	= plot(rsiF, title="RSI Fill" , color=na)
middle_plot = plot(50, title="Middle Fill", color=na)

color_f20	= input.color(color.new(#0B54FE, 85) ,title="|", inline="RSI3", group=groupRSI)
color_f49	= input.color(color.new(#4743EE, 95) ,title="", inline="RSI3", group=groupRSI)
color_f51	= input.color(color.new(#8431DF, 95) ,title="", inline="RSI3", group=groupRSI)
color_f80	= input.color(color.new(#FC0FC0, 85) ,title="", inline="RSI3", group=groupRSI)

color_up	= show_rsiF and rsiF > 50 ? color_f80 : show_rsiF ? color_f49 : na
color_dn	= show_rsiF and rsiF > 50 ? color_f51 : show_rsiF ? color_f20 : na

fill(rsi_plot, middle_plot, rsiF > 50 ? 80 : 50, rsiF > 50 ? 50 : 20, color_up, color_dn) 

// ---------------------------------

show_candles = input.bool(false, title="Show RSI Candles", inline="RSI4", group=groupRSI)

ad = true // Advanced RSI
u = math.max(src - src[1], 0)
dm = math.max(src[1] - src, 0)
b = 1 / len

ruh = b * math.max(high - close[1], 0) + (1 - b) * ta.rma(u, len)[1]
rdh = (1 - b) * ta.rma(dm, len)[1]
rul = (1 - b) * ta.rma(u, len)[1]
rdl = b * math.max(close[1] - low, 0) + (1 - b) * ta.rma(dm, len)[1]

function(rsi, len) =>
    f = -math.pow(math.abs(math.abs(rsi - 50) - 50), 1 + math.pow(len / 14, 0.618) - 1) / math.pow(50, math.pow(len / 14, 0.618) - 1) + 50
    rsiadvanced = if rsi > 50
        f + 50
    else
        -f + 50
    rsiadvanced

rsiha = 100 - 100 / (1 + ruh / rdh)
rsila = 100 - 100 / (1 + rul / rdl)
rsia = ta.rsi(src, len)

rsih = if ad
    function(rsiha, len)
else
    rsiha

rsil = if ad
    function(rsila, len)
else
    rsila

uc			= input.color(color.new(color.green	, 50) , title="|", inline="RSI4", group=groupRSI)
dc			= input.color(color.new(color.red	, 50) , title="", inline="RSI4", group=groupRSI)
bordercolor	= input.color(color.new(color.gray	, 75) , title="", inline="RSI4", group=groupRSI)
wickcolor	= input.color(color.new(color.gray	, 75) , title="", inline="RSI4", group=groupRSI)

plotcandle(show_candles ? rsia[1] : na, rsih, rsil, rsia, title="RSI Candles", color=ta.change(rsia) > 0 ? uc : dc, wickcolor=wickcolor, bordercolor=bordercolor)

// ---------------------------------

showDots = input.bool(true, title="Show OB/OS Dots", inline="RSI5", group=groupRSI)

color_Dos	= input.color(color.new(color.green	, 25), title="|", inline="RSI5", group=groupRSI)
color_Dob	= input.color(color.new(color.red	, 25), title="", inline="RSI5", group=groupRSI)

ob = ta.cross(rsi, 70) == 1 and rsi >= 70
os = ta.cross(rsi, 30) == 1 and rsi <= 30

plot(ob and showDots ? rsi : na , linewidth=4, style=plot.style_circles, color=color_Dob, title="Overbought")
plot(os and showDots ? rsi : na , linewidth=4, style=plot.style_circles, color=color_Dos, title="Oversold")

// ---------------------------------

color_OB = input.color(color.new(color.green	, 65) ,title="Background Lines Colors:"	, inline="RSI6", group=groupRSI)
color_NT = input.color(color.new(color.silver	, 65) ,title=""						, inline="RSI6", group=groupRSI)
color_OS = input.color(color.new(color.red		, 65) ,title=""						, inline="RSI6", group=groupRSI)
//color_fOB = input.color(color.new(color.green	, 97) ,title="|"					, inline="RSI6", group=groupRSI)
//color_fOS = input.color(color.new(color.red	, 97) ,title=""						, inline="RSI6", group=groupRSI)

band0 = hline(80, title="80", linewidth=1, linestyle=hline.style_dotted	, color=color_OB)
band1 = hline(70, title="70", linewidth=1, linestyle=hline.style_solid	, color=color_OB)
band2 = hline(60, title="60", linewidth=1, linestyle=hline.style_dotted	, color=color_NT)
band3 = hline(50, title="50", linewidth=1, linestyle=hline.style_solid	, color=color_NT)
band4 = hline(40, title="40", linewidth=1, linestyle=hline.style_dotted	, color=color_NT)
band5 = hline(30, title="30", linewidth=1, linestyle=hline.style_solid	, color=color_OS)
band6 = hline(20, title="20", linewidth=1, linestyle=hline.style_dotted	, color=color_OS)

fill(band1, band0, color=color.new(color.green	, 97), title="Background OB")
fill(band5, band6, color=color.new(color.red	, 97), title="Background OS")

// ---------------------------------

showLRSI = input.bool(true, title="RSI Label", inline="RSI0", group=groupRSI)

var label Label_RSI = na
Label_RSI := label.new(bar_index + 1, rsi, str.tostring(math.round(rsi, 2), "##"), style=label.style_label_left, color= showLRSI ? color.new(color.silver, 95) : na, textcolor= showLRSI ? color.new(color.silver, 15) : na)
label.delete(Label_RSI[1])

// -----------------------
groupMA = "Moving Average"
// -----------------------

ma(source, length, type) =>
    switch type
        "SMA"				=> ta.sma(source, length)
        "BB (Bollinger)"	=> ta.sma(source, length)
        "EMA"				=> ta.ema(source, length)
        "SMMA (RMA)"		=> ta.rma(source, length)
        "WMA"				=> ta.wma(source, length)
        "VWMA"				=> ta.vwma(source, length)
        "VWAP"				=> ta.vwap(source)
		"LSMA"				=> ta.linreg(source, length, offset=0)
		"HMA"				=> ta.hma(source, length)
		"ALMA"				=> ta.alma(source, length, offset=0.85, sigma=6.0)

// -----------------------

show_ma			= input.bool	(false					, title="Show RSI MA"	, inline="MA3", group=groupMA)
bull_ma_color	= input.color	(color.new(#7E9BDE, 50)	, title="| Bullish"		, inline="MA3", group=groupMA)
bear_ma_color	= input.color	(color.new(#FF8B00, 50)	, title="Bearish"		, inline="MA3", group=groupMA)
ma_length		= input.int		(21						, title="Length"		, inline="MA1", group=groupMA)

ma_type = input.string	("SMA"	, title="Type", options=["SMA", "BB (Bollinger)", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"], inline="MA1", group=groupMA)
bb_mult = input.float	(2.0	, title="Bollinger Bands Standard Dev."			, inline="MA2", group=groupMA)

rsi_ma	 = ma(rsi, ma_length, ma_type)
ma_color = rsi_ma > rsi_ma[1] ? bull_ma_color : bear_ma_color
ma_plot	 = plot(show_ma ? rsi_ma : na, color=ma_color, title="Moving Average")

bb_color = input.color(color.new(#000000, 75), title="| Fill", inline="MA2", group=groupMA)

bb_check = ma_type == "BB (Bollinger)"
bb_uband = plot(show_ma and bb_check ? rsi_ma + ta.stdev(rsi, ma_length) * bb_mult : na, color=bb_color, title="Upper Bollinger Band")
bb_lband = plot(show_ma and bb_check ? rsi_ma - ta.stdev(rsi, ma_length) * bb_mult : na, color=bb_color, title="Lower Bollinger Band")

fill(bb_uband, bb_lband, color= bb_check ? bb_color : na, title="BB Background")

basis = rsi_ma //ta.sma(rsi, ma_length)
dev = bb_mult * ta.stdev(rsi, ma_length)

upper_bb = basis + dev
lower_bb = basis - dev

over_bb = rsi > upper_bb
under_bb = rsi < lower_bb

// -----------------------

rsiR_input = input.string("None", options=["None", "RSI Levels", "Middle Line", "Moving Average", "Bollinger Bands"], title="| RSI Line Color Ref.", inline="RSI1", group=groupRSI)

rsi_bb = over_bb ? color_rsi_70 : under_bb ? color_rsi_30 : color_rsi_no

rsiR1 = rsi ? color_rsi_no : na
rsiR2 = rsi > 50 ? color_rsi_70 : rsi < 50 ? color_rsi_30 : na
rsiR3 =	rsi > 70 ? color_rsi_70 : 
     rsi < 30 ? color_rsi_30 : 
	 rsi > rsi_ma and rsi > 50 ? color_rsi_60 : 
	 rsi > rsi_ma and rsi < 50 ? color_rsi_51 : 
	 rsi < rsi_ma and rsi > 50 ? color_rsi_49 : 
	 rsi < rsi_ma and rsi < 50 ? color_rsi_40 : 
	 na

rsiR0 =	rsiR_input == "None" ? rsiR1 : 
     rsiR_input == "RSI Levels" ? rsi_color : 
	 rsiR_input == "Middle Line" ? rsiR2 : 
	 rsiR_input == "Moving Average" ? rsiR3 : 
	 rsiR_input == "Bollinger Bands" ? rsi_bb : 
	 na

plot(rsi, title="RSI", color=rsiR0)

// ------------------
groupH = "Histograms"
// ------------------

Color_Grow_Above = input(#2B4B46, title="Histograms Colors:", inline="H0", group=groupH)
Color_Fall_Above = input(#8CA18D, title=""					, inline="H0", group=groupH)
Color_Grow_Below = input(#B49C79, title=""					, inline="H0", group=groupH)
Color_Fall_Below = input(#8B3D44, title=""					, inline="H0", group=groupH)

// ------------------

sHist_RSI		= input(false, title="Show RSI Histogram |"				, inline="HRSI0", group=groupH)
AbsHistogram	= input(false, title="Zero Line"						, inline="HRSI0", group=groupH)
HistogramStyle	= input(false, title="Columns Style"					, inline="HRSI0", group=groupH)
rsiCustom		= input(false, title="Use Custom RSI and MA Settings:"	, inline="RSI0A", group=groupH)

len_h = input.int	(14, minval=1	, title="RSI Length", inline="HRSI1", group=groupH)
src_h = input		(close			, title="RSI Source", inline="HRSI1", group=groupH)

rsi_h = ta.rsi(src_h, len_h)

ma_length_h	= input.int		(21		, title="MA Length", inline="HRSI2", group=groupH)
ma_type_h	= input.string	("SMA"	, title="MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"], inline="HRSI2", group=groupH)

rsi_ma_h = ma(rsi_h, ma_length_h, ma_type_h)

custom_rsi = rsiCustom ? rsi_h - rsi_ma_h : rsi - rsi_ma

absRSI	= math.abs(custom_rsi)
RSIz	= AbsHistogram ? custom_rsi : absRSI

hColor = custom_rsi >= 0 ? (custom_rsi[1] < custom_rsi ? Color_Grow_Above : Color_Fall_Above) : (custom_rsi[1] < custom_rsi ? Color_Grow_Below : Color_Fall_Below)
pStyle = HistogramStyle ? plot.style_columns : plot.style_histogram

plot(sHist_RSI ? RSIz : na, style=pStyle, color=hColor, title="RSI Histogram")

// ------------------

//sHist_TSI		= input(false	, title="Show TSI Histogram", inline="HTSI1", group=groupH)
//Short_Len		= input(5		, title="Short Period"		, inline="HTSI2", group=groupH)
//Long_Len		= input(20		, title="Long Period"		, inline="HTSI2", group=groupH)
//TSI_Signal_Len	= input(5		, title="| Signal Period"	, inline="HTSI1", group=groupH)
//
//TSI				= ta.tsi(close, Short_Len, Long_Len)
//TSI_Signal		= ta.ema(TSI, TSI_Signal_Len)
//Histogram_TSI	= TSI - TSI_Signal
//
//TSIcolor = Histogram_TSI >= 0 ? (Histogram_TSI[1] < Histogram_TSI ? Color_Grow_Above : Color_Fall_Above) : (Histogram_TSI[1] < Histogram_TSI ? Color_Grow_Below : Color_Fall_Below)
//plotshape(Histogram_TSI and sHist_TSI ? 5 : na, style=shape.square, color=TSIcolor, location=location.top, size=size.tiny, title="TSI Histogram")

// ------------------

sHist_MACD		= input(true, title="Show MACD Histogram", group=groupH)
Fast_Len		= input(12	, title="Fast Length", inline="HMACD1", group=groupH)
Slow_Len		= input(26	, title="Slow Length", inline="HMACD1", group=groupH)

MACD_Source		= input.source(close	, title="MACD Source"		, inline="HMACD2", group=groupH)
MACD_Signal_Len = input.int(9, minval=1	, title="Smoothing"			, inline="HMACD2", group=groupH)
MA_Source		= input.string("EMA"	, title="Oscillator Type"	, options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"], inline="HMACD3", group=groupH)
MA_Signal		= input.string("EMA"	, title="Signal Line"		, options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"], inline="HMACD3", group=groupH)

Fast_MA			= ma(MACD_Source, Fast_Len, MA_Source)
Slow_MA			= ma(MACD_Source, Slow_Len, MA_Source)

MACD			= Fast_MA - Slow_MA
MACD_Signal		= ma(MACD, MACD_Signal_Len, MA_Signal)

Histogram_MACD	= MACD - MACD_Signal

MACDcolor = Histogram_MACD >= 0 ? (Histogram_MACD[1] < Histogram_MACD ? Color_Grow_Above : Color_Fall_Above) : (Histogram_MACD[1] < Histogram_MACD ? Color_Grow_Below : Color_Fall_Below)
plotshape(Histogram_MACD and sHist_MACD ? 5 : na, style=shape.square, color=MACDcolor, location=location.bottom, size=size.tiny, title="MACD Histogram")

// ------------------

showMACDf = input.bool(false, title="Show MACD (Full) |"			, inline="MACD0", group=groupH)

mHistogramStyle	= input(false, title="Columns Style"				, inline="MACD0", group=groupH)
mStyle = mHistogramStyle ? plot.style_columns : plot.style_histogram

Color_MACD = input(#0094ff, title="|"								, inline="MACD0", group=groupH)
Color_Signal = input(#ff6a00, title=""								, inline="MACD0", group=groupH)

fast_length = input.int(12			, title="Fast Length"			, inline="MACD1", group=groupH)
slow_length = input.int(26			, title="Slow Length"			, inline="MACD1", group=groupH)
macd_src	= input.source(close	, title="MACD Source"			, inline="MACD2", group=groupH)
signal_length = input.int(9			, title="Smoothing"				, inline="MACD2", group=groupH)
ma_source   = input.string("EMA"	, title="Oscillator Type"		, inline="MACD3", group=groupH, options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"])
ma_signal   = input.string("EMA"	, title="Signal Line"			, inline="MACD3", group=groupH, options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VWAP", "LSMA", "HMA", "ALMA"])

SetScale = input.int(20				, title="Scaling"				, inline="MACD4", group=groupH, minval=1, maxval=100, step=5)
HisLen = input.int(500				, title="Length"				, inline="MACD4", group=groupH, minval=1, step=20)
ScaleCol = input.bool(true			, title="Colors"				, inline="MACD4", group=groupH, tooltip="Scaling: Set Top of Range. 0 will be bottom. \nLength: StdDev period, causes distortion, avoid lowering. Must be >= highest MACD value. \nColors: Uses corrected center for coloring. Otherwise colors will retain original MACD colors.")

// ------------------

shortMA = ma(macd_src, fast_length, ma_source)
longMA = ma(macd_src, slow_length, ma_source)

macdLine = shortMA - longMA

signalLine = ma(macdLine, signal_length, ma_signal)

hist = macdLine - signalLine

// ------------------

midline = SetScale / 2

sd_hist = ta.stdev(hist, HisLen)
basis_hist = ta.ema(hist, HisLen)
up_hist = basis_hist + sd_hist * 2
dn_hist = basis_hist - sd_hist * 2
re_hist = (hist - dn_hist) / (up_hist - dn_hist) * SetScale

sd_sig = ta.stdev(signalLine, HisLen)
basis_sig = ta.ema(signalLine, HisLen)
up_sig = basis_sig + sd_sig * 2
dn_sig = basis_sig - sd_sig * 2
rescaled_sig = (signalLine - dn_sig) / (up_sig - dn_sig) * SetScale

sd_mac = ta.stdev(macdLine, HisLen)
basis_mac = ta.ema(macdLine, HisLen)
up_mac = basis_mac + sd_mac * 2
dn_mac = basis_mac - sd_mac * 2
rescaled_mac = (macdLine - dn_mac) / (up_mac - dn_mac) * SetScale

// ------------------

histLineCol = hist >= 0 ? hist[1] < hist ? Color_Grow_Above : Color_Fall_Above : hist[1] < hist ? Color_Grow_Below : Color_Fall_Below

re_histLineCol = re_hist >= midline ? re_hist[1] < re_hist ? Color_Grow_Above : Color_Fall_Above : re_hist[1] < re_hist ? Color_Grow_Below : Color_Fall_Below

//midCol = ScaleCol ? re_hist >= midline ? Color_Grow_Above : Color_Grow_Below : hist >= 0 ? Color_Grow_Above : Color_Grow_Below

// ------------------

//plot(midline, title="Center (Color)", color=midCol, style=plot.style_line, linewidth=2, display=display.none)
//hline(midline, title="Center", color=color.gray, linestyle=hline.style_solid, linewidth=1)
//hline(SetScale, title="Top Range", color=color.gray, linestyle=hline.style_dashed, linewidth=1)
//hline(0, title="Low Range", color=color.gray, linestyle=hline.style_dashed, linewidth=1)

plot(showMACDf ? rescaled_mac : na	, title="MACD"		, color=Color_MACD, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDf ? rescaled_sig : na	, title="Signal"	, color=Color_Signal, style=plot.style_line, linewidth=1, histbase=midline)
plot(showMACDf ? re_hist : na		, title="Histogram"	, color=ScaleCol ? re_histLineCol : histLineCol, style=mStyle, linewidth=1, histbase=midline)

// --------------------------
groupI = "Ichimoku Kinko Hyo"
// --------------------------

ichi_sw				= input		(true			, title="Show Ichimoku Cloud"	, inline="ICHI0", group=groupI)
conversionPeriods	= input.int	(13, minval=1	, title="Tenkan-Sen"			, inline="ICHI1", group=groupI)
basePeriods			= input.int	(33, minval=1	, title="Kijun-Sen"				, inline="ICHI1", group=groupI)
laggingSpan2Periods = input.int	(84, minval=1	, title="Chikou Span"			, inline="ICHI2", group=groupI)
displacement		= input.int	(33, minval=1	, title="Displacement"			, inline="ICHI2", group=groupI)

donchian(len)	=> math.avg(ta.lowest(rsi, len), ta.highest(rsi, len))
conversionLine	= donchian(conversionPeriods)
baseLine		= donchian(basePeriods)
leadLine1		= math.avg(conversionLine, baseLine)
leadLine2		= donchian(laggingSpan2Periods)

colorI1 = input(color.new(#1B5E20, 65), title="Senkou: Span A"	, inline="ICHI3", group=groupI)
colorI2 = input(color.new(#801922, 65), title="Span B"			, inline="ICHI3", group=groupI)
colorI3 = input(color.new(#1B5E20, 90), title="Fill A"			, inline="ICHI3", group=groupI)
colorI4 = input(color.new(#801922, 90), title="Fill B"			, inline="ICHI3", group=groupI)

ichiLwidth	= input.int(1, minval=0, title="| Lines Width", inline="ICHI0", group=groupI)

pI1 = plot(ichi_sw ? leadLine1 : na, offset = displacement - 1, linewidth=ichiLwidth, color=colorI1, title="LeadLine A")
pI2 = plot(ichi_sw ? leadLine2 : na, offset = displacement - 1, linewidth=ichiLwidth, color=colorI2, title="LeadLine B")

fill(pI1, pI2, color = leadLine1 > leadLine2 ? colorI3 : colorI4, title="Ichimoku Cloud")

// ----------------------
groupS = "Stochastic RSI"
// ----------------------

show_stoch = input.bool(false, title="Show Stochastic RSI |", inline="STOCH1", group=groupS)

smoothK		= input.int(21, minval=1	, title="K"				, inline="STOCH2", group=groupS)
smoothD		= input.int(5,	minval=1	, title="D"				, inline="STOCH3", group=groupS)
lengthRSI	= input.int(21, minval=1	, title="RSI Length"	, inline="STOCH2", group=groupS)
lengthStoch = input.int(50, minval=1	, title="Stoch. Length"	, inline="STOCH3", group=groupS)

src_stoch = input(close, title="Source", inline="STOCH4", group=groupS)

colorS1 = input(color.new(color.green,	85)	, title="| Bullish"	, inline="STOCH4", group=groupS)
colorS2 = input(color.new(color.red,	85)	, title="Bearish"	, inline="STOCH4", group=groupS)

showSRSI_l = input.bool(false	, title="Lines"	, inline="STOCH1", group=groupS)
showSRSI_f = input.bool(true	, title="Fill"	, inline="STOCH1", group=groupS)

showSRSI_lc	= showSRSI_l ? display.all : display.none
showSRSI_fc	= showSRSI_f ? display.all : display.none

SRSI = ta.rsi(src_stoch, lengthRSI)
kS	 = ta.sma(ta.stoch(SRSI, SRSI, SRSI, lengthStoch), smoothK)
dS	 = ta.sma(kS, smoothD)

pkS = plot(show_stoch ? kS : na, linewidth=1, color=colorS1, display=showSRSI_lc, title="Stochastic K")
pdS = plot(show_stoch ? dS : na, linewidth=1, color=colorS2, display=showSRSI_lc, title="Stochastic D")

fill(plot1 = pkS, plot2 = pdS, color = kS >= dS ? colorS1 : colorS2, display=showSRSI_fc, title="Stochastic RSI")

// -----------------
groupT = "RSI Trend"
// -----------------

//show_trend = input.bool(false, title="Show RSI Trend |", inline="T1", group=groupT)

//rsiLengthInput	= input.int	(14, minval=1	, title="Fast Length"		, inline="T1A", group=groupT)
//rsiLengthInput2 = input.int	(28, minval=1	, title="Slow Length"		, inline="T1A", group=groupT)
show_baseline	= input.bool(false			, title="Show Hull Trend |"	, inline="T2", group=groupT)
trendlen		= input		(30				, title="Length"			, inline="T2A", group=groupT)

//showT_l		= input.bool(false, title="Lines", inline="T1", group=groupT)
//showT_f		= input.bool(true, title="Fill", inline="T1", group=groupT)
//showT_lc	= showT_l ? display.all : display.none
//showT_fc	= showT_f ? display.all : display.none

showHl	= input.bool(false, title="Lines", inline="T2", group=groupT)
showHf	= input.bool(true, title="Fill", inline="T2", group=groupT)

showHlC	= showHl ? display.all : display.none
showHfC	= showHf ? display.all : display.none

colorR1 = input(color.new(#0B54FE, 90), title="| Bullish"	, inline="T2A", group=groupT)
colorR2 = input(color.new(#FC0FC0, 90), title="Bearish"		, inline="T2A", group=groupT)

BBMC	= ta.hma(close, trendlen)
MHULL	= BBMC[0]
SHULL	= BBMC[2]
hmac	= MHULL > SHULL ? colorR1 : colorR2

//frsi	 = ta.hma(ta.rsi(close, rsiLengthInput), 10)
//srsi	 = ta.hma(ta.rsi(close, rsiLengthInput2), 10)

hullrsi1 = ta.rsi(MHULL, len)
hullrsi2 = ta.rsi(SHULL, len)
//RSItrend = frsi > srsi ? colorR1 : colorR2

//trend1	= plot(show_trend ? frsi : na, linewidth=1, color=colorR1, display=showT_lc, title="RSI Trend 1")
//trend2	= plot(show_trend ? srsi : na, linewidth=1, color=colorR2, display=showT_lc, title="RSI Trend 2")

hull1	= plot(show_baseline ? hullrsi1 : na, linewidth=1, color=colorR1, display=showHlC, title="HMA 1")
hull2	= plot(show_baseline ? hullrsi2 : na, linewidth=1, color=colorR2, display=showHlC, title="HMA 2")

//fill(trend1, trend2, color=show_trend ? RSItrend : na, display=showT_fc, title="RSI Trend")
fill(hull1, hull2, color=show_baseline ? hmac : na, display=showHfC, title="RSI Hull Trend")

// -----------------

show_tstate = input.bool(false, title="Show Trend State |", inline="T3", group=groupT)

var state = 0
if ta.crossover(rsi, 66.67)
    state := 1
    state
if ta.crossunder(rsi, 33.33)
    state := 2
    state
if state == 1 and ta.crossunder(rsi, 40)
    state := 3
    state
if state == 2 and ta.crossover(rsi, 60)
    state := 3
    state
state := state

colorST1 = input(color.new(#0B54FE		, 75), title="Bull."	, inline="T3", group=groupT)
colorST3 = input(color.new(color.silver	, 75), title="N."		, inline="T3", group=groupT)
colorST2 = input(color.new(#FC0FC0		, 75), title="Bear."	, inline="T3", group=groupT)

state_col = state == 1 ? colorST1 : state == 2 ? colorST2 : state == 3 ? colorST3 : na
state_col2 = state == 1 ? colorST1 : state == 2 ? colorST2 : state == 3 ? colorST3 : na

mh = plot(show_tstate ? 60 : na,  title="Mid-High Band", color=state == 2 or state == 3 ? state_col2 : na, style=plot.style_circles, linewidth=1)
ml = plot(show_tstate ? 40 : na, title="Mid-Low Band", color=state == 1 or state == 3 ? state_col2 : na, style=plot.style_circles, linewidth=1)

// --------------------------
groupLR = "Linear Regression"
// --------------------------

linreg			= input			(true			, title="Show Linear Regression |"	, inline="LR1", group=groupLR)
periodTrend		= input.int		(89, minval=4	, title="Trend Period"				, inline="LR2", group=groupLR)
deviationsAmnt	= input.float	(1.618, step=0.1, title="Deviation"					, inline="LR2", group=groupLR)
//estimatorType	= input.string	("Unbiased"		, title="Estimator"					, options=["Biased", "Unbiased"], inline="LR3", group=groupLR)
estimatorType	= "Unbiased"
var extendType	= input.string	("Segment"		, title="Lines: Extension"			, options=["Right", "Segment"], inline="LR3", group=groupLR) == "Right" ? extend.right : extend.none
lrLwidth		= input.int		(2, minval=0	, title="Width"				, inline="LR3", group=groupLR)

UPlr	= input(color.new(color.green,	65), title="Lines Colors: Upper", inline="LR4", group=groupLR)
MIDlr	= input(color.new(color.silver, 65), title="Middle"				, inline="LR4", group=groupLR)
LOWlr	= input(color.new(color.red,	65), title="Lower"				, inline="LR4", group=groupLR)

i_line0 = "Solid"
i_line1 = "Dotted"
i_line2 = "Dashed"

f_getLineStyle(_inputStyle) =>
    _return = _inputStyle == i_line1 ? line.style_dotted : _inputStyle == i_line2 ? line.style_dashed : line.style_solid
    _return
	
ulLRstyle = input.string(i_line0, title="Style: Upper/Lower", options=[i_line0, i_line1, i_line2], inline="LR5", group=groupLR)
miLRstyle = input.string(i_line2, title="Middle", options=[i_line0, i_line1, i_line2], inline="LR5", group=groupLR)

// --------------------------

rsdcr2(PeriodMinusOne, Deviations, Estimate) =>
    var period = PeriodMinusOne + 1
    var devDenominator = Estimate == "Unbiased" ? PeriodMinusOne : period
    Ex = 0.0
    Ex2 = 0.0
    Exy = 0.0
    Ey = 0.0
    for i = 0 to PeriodMinusOne by 1
        closeI = nz(rsi[i])
        Ex := Ex + i
        Ex2 := Ex2 + i * i
        Exy := Exy + closeI * i
        Ey := Ey + closeI
        Ey
    ExEx = Ex * Ex
    slope = Ex2 == ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0
    for i = 0 to PeriodMinusOne by 1
        deviation := deviation + math.pow(nz(rsi[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsi, bar_index, period)
    r2 = math.pow(correlate, 2.0)
    [linearRegression, slope, deviation, correlate, r2]

// --------------------------

drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := linreg ? line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, width=lrLwidth) : na
    line.delete(Line[1])
	
periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1
endPointY	= linReg + slope * periodMinusOne

drawLine(endPointBar, endPointY + deviation, bar_index, linReg + deviation, extendType, UPlr, f_getLineStyle(ulLRstyle))
drawLine(endPointBar, endPointY, bar_index, linReg, extendType, MIDlr, f_getLineStyle(miLRstyle))
drawLine(endPointBar, endPointY - deviation, bar_index, linReg - deviation, extendType, LOWlr, f_getLineStyle(ulLRstyle))

// --------------------------

showCor = input(false, title="Correlation", inline="LR1", group=groupLR)

var label Label = na
Label := label.new(math.max(0, endPointBar - 1), endPointY, text=str.tostring(correlate, "#.##"), style=label.style_label_right, color= showCor ? color.new(color.silver, 95) : na, textcolor= showCor ? color.new(color.silver, 15) : na)
label.delete(Label[1])

// ----------------------
groupZ = "Highs & Lows"
// ----------------------

showDiv = input.bool(true	, title="Show Divergences"	, inline="Z1", group=groupZ)
length	= input		(21		, title="| Pivot Length"	, inline="Z1", group=groupZ)

r		= rsi[length]
l		= rsi[14]

ph		= ta.pivothigh(rsi,length,length)
pl		= ta.pivotlow(rsi,length,length)

valH	= ta.valuewhen(ph,r,0)
valL	= ta.valuewhen(pl,r,0)
valpH	= ta.valuewhen(ph,r,1)
valpL	= ta.valuewhen(pl,r,1)

d = math.abs(r)
n = bar_index
label lbl = na

HIH = valH > valpH ? "HH" : na 
HIL = valH < valpH ? "LH" : na
LOL = valL < valpL ? "LL" : na
LOH = valL > valpL ? "HL" : na

// ----------------------

zL1style	= input.string	(i_line1	, title="Lines"				, options=[i_line0, i_line1, i_line2], inline="Z2", group=groupZ)
showHL		= input.bool	(true		, title="Show H/L |"		, inline="Z3", group=groupZ)
showHLlines	= input.bool	(true		, title="Li."				, inline="Z3", group=groupZ)
zL2style	= input.string	(i_line2	, title=""					, options=[i_line0, i_line1, i_line2], inline="Z3", group=groupZ)

styleZ1 = f_getLineStyle(zL1style)
styleZ2 = f_getLineStyle(zL2style)

zL1width	= input.int(1, minval=0, title="Width", inline="Z2", group=groupZ)
zL2width	= input.int(1, minval=0, title="Wi.", inline="Z3", group=groupZ)

colorZUP	= input(color.new(color.green, 25), title="", inline="Z2", group=groupZ)
colorZDN	= input(color.new(color.red, 25), title="", inline="Z2", group=groupZ)

colorHIH = input(color.new(color.red, 75)	, title="H/L Colors: HH", inline="Z4", group=groupZ)
colorHIL = input(color.new(color.orange, 75), title="LH"			, inline="Z4", group=groupZ)
colorLOL = input(color.new(color.green, 75)	, title="LL"			, inline="Z4", group=groupZ)
colorLOH = input(color.new(#2196F3, 75)		, title="HL"			, inline="Z4", group=groupZ)

// ----------------------

if ph and valH > valpH and showHL
    lbl := label.new(n[length], math.max(r,l), HIH, color=colorHIH,
      style=label.style_label_down, textcolor=color.new(color.white, 15), size=size.small)
    label.delete(lbl[1])
    linehh = showHLlines ? line.new(n[length], math.max(r,l), bar_index, math.max(r,l), extend=extend.right, style=styleZ2, color=colorHIH, width=zL2width) : na
    line.delete(linehh[1])
else if ph and valH < valpH and showHL
    lbl := label.new(n[length], math.max(r,l), HIL, color=colorHIL,
      style=label.style_label_down, textcolor=color.new(color.white, 15), size=size.small)
    label.delete(lbl[1])
    linehl = showHLlines ? line.new(n[length], math.max(r,l), bar_index, math.max(r,l), extend=extend.right, style=styleZ2, color=colorHIL, width=zL2width) : na
    line.delete(linehl[1])
else if pl and valL < valpL and showHL
    lbl := label.new(n[length], math.min(r,l), LOL, color=colorLOL,  
      style=label.style_label_up, textcolor=color.new(color.white, 15), size=size.small)
    label.delete(lbl[1]) 
    linell = showHLlines ? line.new(n[length], math.min(r,l), bar_index, math.min(r,l), extend=extend.right, style=styleZ2, color=colorLOL, width=zL2width) : na
    line.delete(linell[1])
else if pl and valL > valpL and showHL
    lbl := label.new(n[length], math.min(r,l), LOH, color=colorLOH,
      style=label.style_label_up, textcolor=color.new(color.white, 15), size=size.small)
    label.delete(lbl[1])
    linelh = showHLlines ? line.new(n[length], math.min(r,l), bar_index, math.min(r,l), extend=extend.right, style=styleZ2, color=colorLOH, width=zL2width) : na
    line.delete(linelh[1])
label.delete(lbl[250])

// ----------------------

showBG = input.bool(true, title="OB/OS Bkg.", inline="LR1", group=groupLR)

bgcolor(rsi > linReg + deviation and ta.pivothigh(rsi, length, 0) and rsi >= 65 and showBG ? color.new(color.red, 95) : rsi < linReg - deviation and ta.pivotlow(rsi, length, 1) and rsi <= 35 and showBG ? color.new(color.green, 95) : na)

// ----------------------

extendOptionUp	= input.string("None", title="Upper Line Ext.", options=["None", "Left", "Right", "Both"], inline="ZUP", group=groupZ)
extendOptionLow = input.string("None", title="Lower Line Ext.", options=["None", "Left", "Right", "Both"], inline="ZLOW", group=groupZ)

lengthdiv	= input.int(21	, minval=0, title="Leftbars Len."				, inline="ZUP", group=groupZ)
lengthright	= input.int(0	, minval=0, title="Rightbars Le."				, inline="ZLOW", group=groupZ)

lengthwg	= lengthright
length2wg	= lengthright

astart		= input.int(1	, minval=0, title="Draw Upper Line from Pivot"	, inline="ZP1", group=groupZ)
aend		= input.int(0	, minval=0, title=">"							, inline="ZP1" , group=groupZ)
bstart		= input.int(1	, minval=0, title="Draw Lower Line from Pivot"	, inline="ZP2", group=groupZ)
bend		= input.int(0	, minval=0, title=">"							, inline="ZP2", group=groupZ)

upwg	= ta.pivothigh(rsi, lengthdiv, lengthright)
dnwg	= ta.pivotlow(rsi, lengthdiv, lengthright)
upchart = ta.pivothigh(close, lengthdiv, lengthright)
dnchart = ta.pivotlow(close, lengthdiv, lengthright)

nw = bar_index
a1 = ta.valuewhen(not na(upwg), nw, astart)
b1 = ta.valuewhen(not na(dnwg), nw, bstart)
a2 = ta.valuewhen(not na(upwg), nw, aend)
b2 = ta.valuewhen(not na(dnwg), nw, bend)

ach1 = ta.valuewhen(not na(upchart), nw, astart)
bch1 = ta.valuewhen(not na(dnchart), nw, bstart)
ach2 = ta.valuewhen(not na(upchart), nw, aend)
bch2 = ta.valuewhen(not na(dnchart), nw, bend)

// ----------------------

lineExtendUp = extendOptionUp == "Left" ? extend.left : 
     extendOptionUp == "Right" ? extend.right : 
     extendOptionUp == "Both" ? extend.both : 
     extend.none
lineExtendLow = extendOptionLow == "Left" ? extend.left : 
     extendOptionLow == "Right" ? extend.right : 
     extendOptionLow == "Both" ? extend.both : 
     extend.none

//line upper = line.new(nw[nw - a1 + lengthwg], upwg[nw - a1 + lengthwg], nw[nw - a2 + lengthwg], upwg[nw - a2 + lengthwg], extend=lineExtendUp, color=colorZDN, width=zL1width, style=styleZ1) 
//line.delete(upper[1])
//line lower = line.new(nw[nw - b1 + length2wg], dnwg[nw - b1 + length2wg], nw[nw - b2 + length2wg], dnwg[nw - b2]		, extend=lineExtendLow, color=colorZUP, width=zL1width, style=styleZ1)
//line.delete(lower[1])

// ----------------------

div1 = upwg[nw - a2] < upwg[nw - a1] and upchart[nw - ach2] > upchart[nw - ach1] and upchart > high[nw - ach1]	// and ta.pivotlow(rsi,length,0)
div2 = dnwg[nw - b2] > dnwg[nw - b1] and dnchart[nw - bch2] < dnchart[nw - bch1] and dnchart < low[nw - bch1]	// and ta.pivothigh(rsi,length,0)

if div1 and showDiv
	line.new(nw[nw - a1 + lengthwg], upwg[nw - a1], nw[nw - a2 + lengthwg], upwg[nw - a2], extend=lineExtendUp, color=colorZDN, width=zL1width, style=styleZ1)
    label1 = label.new(nw[nw - a2 + lengthwg], 70 , text="div.", style=label.style_label_up, color=color.new(color.red, 100))
    label.set_size(label1, size.small)
    label.set_textcolor(label1, color.new(color.red, 15))
    
if div2 and showDiv
	line.new(nw[nw - b1 + length2wg], dnwg[nw - b1], nw[nw - b2 + length2wg], dnwg[nw - b2], extend=lineExtendLow, color=colorZUP, width=zL1width, style=styleZ1)
    label2 = label.new(nw[nw - b2 + length2wg], 30, text="div.", style=label.style_label_down, color=color.new(color.green, 100))
    label.set_size(label2, size.small)
    label.set_textcolor(label2, color.new(color.green, 15))

// ----------------------

enterShort = div1
enterLong = div2
enterPos = div1 or div2

alertcondition(enterLong    , title="Divergence Alert (Long)"	, message="Go Long ▲\n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
alertcondition(enterShort   , title="Divergence Alert (Short)"	, message="Go Short ▼\n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
alertcondition(enterPos		, title="Divergences Alert"			, message="Ticker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
Editor is loading...
Leave a Comment