//version=5

indicator('RSI Lạc Quan Tếu 2.2', 'R 2.2', format=format.price, max_bars_back=252)

// Input

group_rsi1 = 'Thông số RSI "tăng động"'
group_rsi2 = 'Thông số RSI "bình tĩnh"'
group_tinhieu = 'Thông số tín hiệu quán tính'
rsic=input.source(close, title="Nguồn giá trị RSI")

//RSI "tăng động"
lenrsi = input.int(14, minval=1, title='RSI "tăng động"', group = group_rsi1)
rsi = ta.rsi(rsic, lenrsi)
lenwma = input.int(45, minval=1, title='WMA-Trung bình chậm', group = group_rsi1)
wma_45 = ta.wma(rsi, lenwma)
lenema = input.int(9, minval=1, title='EMA-Trung bình nhanh', group = group_rsi1)
ema_9 = ta.ema(rsi, lenema)

//RSI "bình tĩnh"
lenma = input.int(3, minval=1, title='RSI "bình tĩnh"', group = group_rsi2)
smooth = input.int(4, 'Hệ số làm mượt', minval=2, group = group_rsi2)
rsic2= ta.sma(rsi, lenma)
rsi2 = ta.linreg(rsic2, smooth, 0)
displacement = input.int(2, minval=1, title='Mức độ "bình tĩnh"', group = group_rsi2)

//tin hieu

//////////////////////////////////////////////////
nguontinhieu = input.string('RSI tăng động', 'Nguồn tín hiệu', options=['RSI tăng động', 'RSI bình tĩnh'], group = group_tinhieu)

float rsix = switch nguontinhieu
'RSI tăng động' => ta.rsi(rsic, lenrsi)
'RSI bình tĩnh' => ta.linreg(rsic2, smooth, 0)



///////////////////////////////////////////////////////////////////
overboughtlevel = input.int(75 ,title='Ngưỡng quá mua', group = group_tinhieu)
oversoldlevel = input.int(25 ,title='Ngưỡng quá bán', group = group_tinhieu)
heso = input.int(3, title = 'Độ chính xác', group = group_tinhieu)
upBar = close > open
downBar = close < open
threeUpBars = math.sum(upBar ? 1 : 0, heso) == heso
threeDownBars = math.sum(downBar ? 1 : 0, heso) == heso
overbought = rsix > overboughtlevel and threeUpBars
oversold = rsix < oversoldlevel and threeDownBars

// isup = (ta.crossover(rsi2, overboughtlevel))
// isdn = (ta.crossunder(rsi2, oversoldlevel))
// overboughtcolor = isup ? #ffd90050 :na
// oversoldcolor = isdn ? #ffd90050 :na



//bool isup = (ta.crossover(rsi, ema_9) or ta.crossover(rsi, wma_45)) and (rsi > ema_9) and (rsi > wma_45)
//bool isdn = (ta.crossunder(rsi, ema_9) or ta.crossunder(rsi, wma_45)) and (rsi < ema_9) and (rsi < wma_45)
// isup = rsi > ema_9 and rsi > wma_45
// isdn = rsi < ema_9 and rsi < wma_45


// -Plot ======================================================================================== //

// Duong ke ngang

p1 = hline(80, color=color.red, title='Vùng mua mạnh', linestyle=hline.style_dotted, linewidth=1)
p2 = hline(70, color=color.rgb(255, 82, 82, 30), title='Vùng mua yếu', linestyle=hline.style_dotted, linewidth=1)

l50 = hline(50, color=color.rgb(255, 251, 0), title='Line 50', linestyle=hline.style_dashed, linewidth=1)

p3 = hline(30, color=color.rgb(255, 82, 82, 30), title='Vùng bán yếu', linestyle=hline.style_dotted, linewidth=1)
p4 = hline(20, color=color.red, title='Vùng bán mạnh', linestyle=hline.style_dotted, linewidth=1)


fa1 = hline(90, color=color.rgb(255, 255, 255), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 1', display=display.none)
fa2 = hline(66.66, color=color.rgb(0, 89, 255, 30), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 2')
fa3 = hline(60, color=color.rgb(0, 89, 255), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 3')
fa4 = hline(40, color=color.rgb(0, 89, 255), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 4')
fa5 = hline(33.33, color=color.rgb(0, 89, 255, 30), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 5')
fa6 = hline(10, color=color.rgb(255, 255, 255), linestyle=hline.style_dotted, linewidth=1, title='Line tùy chọn 6', display=display.none)

// RSI
prsi = plot(rsi, color=color.new(#a0a0a0, 30), style=plot.style_line, linewidth=1, title='RSI "tăng động"')
prsi2 = plot(rsi2, offset = -displacement +1, color=color.new(#dbb300, 20), style=plot.style_line, linewidth=2, title='RSI "bình tĩnh"')

h_50 = plot(50, color=color.new(#4caf4f, 50), style=plot.style_line, linewidth=1, title='Line 50', display=display.none)
p45 = plot(wma_45, color=color.rgb(179, 0, 0), style=plot.style_line, linewidth=1, title='WMA - Trung bình chậm')
p9 = plot(ema_9, color=color.new(#00b906, 0), style=plot.style_line, linewidth=1, title='EMA - Trung bình nhanh')


//mau tich luy
fill(prsi2, p45, color = rsi2 > wma_45 ? color.rgb(255, 82, 82, 60) : color.rgb(0, 187, 212, 60), title='Màu tích lũy')
fill(p1, p2, color= color.rgb(255, 230, 0, 90) , title='Màu vùng mua')
fill(p3, p4, color= color.rgb(255, 230, 0, 90) , title='Màu vùng bán')

//mau tin hieu
plotshape(series = overbought , style=shape.diamond, location=location.top,
color=color.rgb(0, 255, 115, 20), size=size.tiny, title = 'Tín hiệu tăng')
plotshape(series = oversold , style=shape.diamond, location=location.bottom,
color=color.rgb(255, 0, 43, 20), size=size.tiny, title = 'Tín hiệu giảm')


//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


// POSITION AND SIZE
PosTable = input.string(defval="Middle Left", title="Vị trí bảng",
options=["Top Right", "Middle Right", "Bottom Right", "Top Center", "Middle Center", "Bottom Center", "Top Left", "Middle Left", "Bottom Left"],
group="Thông số bảng RSI")
SizTable = input.string(defval="Small", title="Kích thước", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Thông số bảng RSI")

Pos1Table = PosTable == "Top Right" ? position.top_right : PosTable == "Middle Right" ? position.middle_right : PosTable == "Bottom Right" ? position.bottom_right : PosTable == "Top Center" ? position.top_center : PosTable == "Middle Center" ? position.middle_center : PosTable == "Bottom Center" ? position.bottom_center : PosTable == "Top Left" ? position.top_left : PosTable == "Middle Left" ? position.middle_left : position.bottom_left
Siz1Table = SizTable == "Auto" ? size.auto : SizTable == "Huge" ? size.huge : SizTable == "Large" ? size.large : SizTable == "Normal" ? size.normal : SizTable == "Small" ? size.small : size.tiny

tablecellcolor = input.color (color.rgb(0, 0, 0), title = 'Màu nền bảng')
tableframecolor = input.color (color.rgb(0, 0, 0), title = 'Màu khung bảng')
tableupcolor = input.color (color.rgb(7, 85, 0), title = 'Màu nền tăng')
tabledowncolor = input.color (color.rgb(110, 0, 0), title = 'Màu nền giảm')
textupcolor = input.color (color.rgb(0, 155, 0), title = 'Màu chữ tăng')
textdowncolor = input.color (color.rgb(185, 0, 0), title = 'Màu chữ giảm')



tbl = table.new(Pos1Table, 2, 8, frame_width=2, frame_color = tableframecolor, border_width=1, border_color = tableframecolor)


// TIMEFRAMES OPTIONS
box01 = input.bool(true, "Khung[01]", inline = "01", group="Chon Khung Thoi Gian")
Khung01 = input.timeframe("1", "", inline = "01", group="Chon Khung Thoi Gian")

box02 = input.bool(true, "Khung[02]", inline = "02", group="Chon Khung Thoi Gian")
Khung02 = input.timeframe("5", "", inline = "02", group="Chon Khung Thoi Gian")

box03 = input.bool(true, "Khung[03]", inline = "03", group="Chon Khung Thoi Gian")
Khung03 = input.timeframe("15", "", inline = "03", group="Chon Khung Thoi Gian")

box04 = input.bool(true, "Khung[04]", inline = "04", group="Chon Khung Thoi Gian")
Khung04 = input.timeframe("60", "", inline = "04", group="Chon Khung Thoi Gian")

box05 = input.bool(true, "Khung[05]", inline = "05", group="Chon Khung Thoi Gian")
Khung05 = input.timeframe("240", "", inline = "05", group="Chon Khung Thoi Gian")

box06 = input.bool(true, "Khung[06]", inline = "06", group="Chon Khung Thoi Gian")
Khung06 = input.timeframe("1D", "", inline = "06", group="Chon Khung Thoi Gian")

box07 = input.bool(true, "Khung[07]", inline = "07", group="Chon Khung Thoi Gian")
Khung07 = input.timeframe("1W", "", inline = "07", group="Chon Khung Thoi Gian")


// DEFINITION OF VALUES
symbol = ticker.modify(syminfo.tickerid, syminfo.session)
KhungArr = array.new<string>(na)
rsiArr = array.new<float>(na)
wmaArr = array.new<float>(na)
emaArr = array.new<float>(na)

// DEFINITIONS OF RSI & WMA & WMA APPENDED IN THE TIMEFRAME OPTIONS
rsiNmaFun(Khung, flg) =>
[rsit, wmat, emat] = request.security(symbol, Khung, [rsi, wma_45, ema_9])

// if flg and (barstate.isrealtime ? true : timeframe.in_seconds(timeframe.period) <= timeframe.in_seconds(Khung))
if flg ? true : timeframe.in_seconds(timeframe.period) <= timeframe.in_seconds(Khung)
// array.push(KhungArr, na(Khung) ? timeframe.period : Khung)
array.push(KhungArr, Khung)
array.push(rsiArr, rsit)
array.push(wmaArr, wmat)
array.push(emaArr, emat)

rsiNmaFun(Khung01, box01), rsiNmaFun(Khung02, box02), rsiNmaFun(Khung03, box03), rsiNmaFun(Khung04, box04),
rsiNmaFun(Khung05, box05), rsiNmaFun(Khung06, box06), rsiNmaFun(Khung07, box07)

// TABLE AND CELLS CONFIG
// Post Timeframe in format
KhungTxt(x)=>
out = x
if not str.contains(x, "S") and not str.contains(x, "M") and
not str.contains(x, "W") and not str.contains(x, "D")
if str.tonumber(x)%60 == 0
out := str.tostring(str.tonumber(x)/60)+"H"
else
out := x + "m"
out

if barstate.islast
// table.clear(tbl, 0, 0, 2, 7)

// TITLES
// table.cell(tbl, 0, 0, "⏱", text_color=#FFFFFF, text_size=Siz1Table, bgcolor=#000000)
// table.cell(tbl, 0, 1, "RSI", text_color=#FFFFFF, text_size=Siz1Table, bgcolor=#000000)
// table.cell(tbl, 0, 2, "%K", text_color=#FFFFFF, text_size=Siz1Table, bgcolor=#000000)

j = 1
if array.size(rsiArr) > 0
for i = 0 to array.size(rsiArr) - 1
if not na(array.get(rsiArr, i))

//config values in the cells
Khung_VALUE = array.get(KhungArr,i)
RSI_VALUE = array.get(rsiArr, i)
WMA_VALUE = array.get(wmaArr, i)
EMA_VALUE = array.get(emaArr, i)
// SIGNAL = EMA_VALUE > WMA_VALUE ? "▲" : EMA_VALUE < WMA_VALUE ? "▼" : na

//config colors in the the cells
//rsi
cond1 = (array.get(rsiArr,i)) > EMA_VALUE
cond12 = (array.get(rsiArr,i)) > WMA_VALUE
cond2 = (array.get(rsiArr,i)) < EMA_VALUE
cond22 = (array.get(rsiArr,i)) < WMA_VALUE
cond3 = (array.get(emaArr,i)) > WMA_VALUE
cond4 = (array.get(emaArr,i)) < WMA_VALUE
cond5 = (array.get(emaArr,i)) > 50
cond6 = (array.get(emaArr,i)) < 50
txtRSI = cond1 ? textupcolor : cond2 ? textdowncolor : not cond1 and not cond2 ? #A9A9A9 : na
cellRSI = cond12 ? tableupcolor : cond22 ? tabledowncolor : not cond12 and not cond22 ? #1C1C1C : na
txtKhung = cond3 ? textupcolor : cond4 ? textdowncolor : not cond3 and not cond4 ? #1C1C1C : na
// cellKhung = cond5 ? #093106 : cond6 ? #440101 : not cond5 and not cond6 ? #1C1C1C : na

// table.cell(tbl, 0, j, KhungTxt(Khung_VALUE)+ SIGNAL, text_color=txtKhung, text_halign=text.align_right, text_size=Siz1Table, bgcolor= #0e0e0e)
table.cell(tbl, 0, j, KhungTxt(Khung_VALUE), text_color=txtKhung, text_halign=text.align_right, text_size=Siz1Table, bgcolor= tablecellcolor)
table.cell(tbl, 1, j, str.tostring(RSI_VALUE, "#.#") ,text_color=txtRSI, text_halign=text.align_left, text_size=Siz1Table, bgcolor=cellRSI)

j += 1


//tdmacd
source = close
fastLength = input.int(5, minval=1, group = "TDMACD")
slowLength = input.int(14, minval=1, group = "TDMACD")
signalLength = input.int(1, minval=1, group = "TDMACD")
fastMA = ta.ema(source, fastLength)
slowMA = ta.ema(source, slowLength)
macd = fastMA - slowMA
signal = ta.sma(macd, signalLength)
// plot(macd, color=ta.change(macd) <= 0 ? color.red : color.lime, style=plot.style_histogram)
// plot(macd, color=color.new(color.black, 0), style=plot.style_line)
// plot(signal, color=ta.change(signal) <= 0 ? color.red : color.green, style=plot.style_line)

plotshape(ta.crossover(macd,0), style = shape.triangleup, location = location.belowbar, color = color.green, size = size.tiny, force_overlay = true, title = "tdmacd UP")
plotshape(ta.crossunder(macd,0), style = shape.triangledown, location = location.abovebar, color = color.red, size = size.tiny, force_overlay = true, title = "tdmacd DN")
Harmonic PatternsTechnical IndicatorsTrend Analysis

Penafian