OPEN-SOURCE SCRIPT
Andra Algo

//version=5
indicator(title="Andra Algo V 1.2", shorttitle="Andra Algo V1.2", overlay=true)
// =====================
// INPUT
// =====================
src = input(defval=close, title="Source")
per = input.int(defval=100, minval=1, title="Sampling Period")
mult = input.float(defval=3.0, minval=0.1, title="Range Multiplier")
// =====================
// COLOR SET
// =====================
buyLineColor = color.white
sellLineColor = color.blue
midColor = #90bff9
buyBgColor = color.new(color.gray, 20)
sellBgColor = color.new(color.blue, 20)
// =====================
// SMOOTH RANGE
// =====================
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
ta.ema(avrng, wper) * m
smrng = smoothrng(src, per, mult)
// =====================
// RANGE FILTER
// =====================
rngfilt(x, r) =>
rf = x
rf := x > nz(rf[1]) ?
(x - r < nz(rf[1]) ? nz(rf[1]) : x - r) :
(x + r > nz(rf[1]) ? nz(rf[1]) : x + r)
rf
filt = rngfilt(src, smrng)
// =====================
// TREND DIRECTION
// =====================
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// =====================
// MID LINE COLOR
// =====================
filtColor = upward > 0 ? buyLineColor : downward > 0 ? sellLineColor : midColor
plot(filt, title="Mid Line", color=filtColor, linewidth=2)
// =====================
// BUY & SELL CONDITIONS
// =====================
longCond = src > filt and upward > 0
shortCond = src < filt and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1
// =====================
// SIGNALS (FIXED BG COLOR)
// =====================
plotshape(longCondition, title="Buy Signal", text="BUY", style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.white, color=buyBgColor)
plotshape(shortCondition, title="Sell Signal", text="SELL", style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.white, color=sellBgColor)
// =====================
// ALERTS
// =====================
alertcondition(longCondition, title="Buy Alert", message="Andra Algo V1.2 BUY")
alertcondition(shortCondition, title="Sell Alert", message="Andra Algo V1.2 SELL")
indicator(title="Andra Algo V 1.2", shorttitle="Andra Algo V1.2", overlay=true)
// =====================
// INPUT
// =====================
src = input(defval=close, title="Source")
per = input.int(defval=100, minval=1, title="Sampling Period")
mult = input.float(defval=3.0, minval=0.1, title="Range Multiplier")
// =====================
// COLOR SET
// =====================
buyLineColor = color.white
sellLineColor = color.blue
midColor = #90bff9
buyBgColor = color.new(color.gray, 20)
sellBgColor = color.new(color.blue, 20)
// =====================
// SMOOTH RANGE
// =====================
smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
ta.ema(avrng, wper) * m
smrng = smoothrng(src, per, mult)
// =====================
// RANGE FILTER
// =====================
rngfilt(x, r) =>
rf = x
rf := x > nz(rf[1]) ?
(x - r < nz(rf[1]) ? nz(rf[1]) : x - r) :
(x + r > nz(rf[1]) ? nz(rf[1]) : x + r)
rf
filt = rngfilt(src, smrng)
// =====================
// TREND DIRECTION
// =====================
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// =====================
// MID LINE COLOR
// =====================
filtColor = upward > 0 ? buyLineColor : downward > 0 ? sellLineColor : midColor
plot(filt, title="Mid Line", color=filtColor, linewidth=2)
// =====================
// BUY & SELL CONDITIONS
// =====================
longCond = src > filt and upward > 0
shortCond = src < filt and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1
// =====================
// SIGNALS (FIXED BG COLOR)
// =====================
plotshape(longCondition, title="Buy Signal", text="BUY", style=shape.labelup, location=location.belowbar, size=size.small, textcolor=color.white, color=buyBgColor)
plotshape(shortCondition, title="Sell Signal", text="SELL", style=shape.labeldown, location=location.abovebar, size=size.small, textcolor=color.white, color=sellBgColor)
// =====================
// ALERTS
// =====================
alertcondition(longCondition, title="Buy Alert", message="Andra Algo V1.2 BUY")
alertcondition(shortCondition, title="Sell Alert", message="Andra Algo V1.2 SELL")
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.