Efficiency-Adaptive Trend Trail [Jayadev Rana]

JayadevRana · study · 101 行 · 点赞 47 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0
// (c) Jayadev Rana
//@version=6
indicator("Efficiency-Adaptive Trend Trail [Jayadev Rana]", shorttitle = "EATT", overlay = true)
 
//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
string GRP_CORE  = "Core"
string GRP_DASH  = "Dashboard"
string GRP_STYLE = "Style"
 
//------------------------------------------------------------------------------
// Inputs
//------------------------------------------------------------------------------
int   atrLen    = input.int(14, "ATR Length", minval = 2, maxval = 200, group = GRP_CORE)
int   erLen     = input.int(20, "Efficiency Length", minval = 5, maxval = 200, group = GRP_CORE, tooltip = "Window of the efficiency ratio: net price move divided by the total bar-to-bar path. 1 is a straight line, 0 is pure rotation.")
float minMult   = input.float(8.0, "Tight Multiplier (Efficient Trend)", minval = 0.5, maxval = 30.0, step = 0.1, group = GRP_CORE, tooltip = "ATR multiple used when the efficiency ratio is 1. A clean trend earns a closer trail.")
float maxMult   = input.float(12.0, "Wide Multiplier (Choppy Market)", minval = 0.5, maxval = 30.0, step = 0.1, group = GRP_CORE, tooltip = "ATR multiple used when the efficiency ratio is 0. A rotating market gets a wider trail so noise causes fewer flips. Keep it above the tight multiplier.")
int   multSmooth = input.int(20, "Multiplier Smoothing", minval = 1, maxval = 50, group = GRP_CORE, tooltip = "EMA length applied to the adaptive multiplier so the trail distance changes gradually.")
bool  colorBars = input.bool(false, "Color Bars by Direction", group = GRP_CORE)
 
bool   showDash = input.bool(true, "Show Dashboard", group = GRP_DASH)
string dashPos  = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = GRP_DASH)
 
color upCol = input.color(#26a69a, "Up", group = GRP_STYLE)
color dnCol = input.color(#ef5350, "Down", group = GRP_STYLE)
 
//------------------------------------------------------------------------------
// Core calculations
//------------------------------------------------------------------------------
float atr  = ta.atr(atrLen)
float unit = math.max(nz(atr, high - low), syminfo.mintick)
float chg  = close - close[erLen]
float path = math.sum(math.abs(close - close[1]), erLen)
float er   = path > 0 ? math.abs(chg) / path : 0.0
float mult = ta.ema(maxMult - (maxMult - minMult) * nz(er), multSmooth)
float dist = mult * unit
 
// Ratcheting trail: only moves in the direction of the trend, flips on a close through it.
var float trail = na
var int   dir   = 1
if na(trail)
    trail := close - dist
else if dir == 1
    trail := math.max(trail, close - dist)
    if close < trail
        dir   := -1
        trail := close + dist
else
    trail := math.min(trail, close + dist)
    if close > trail
        dir   := 1
        trail := close - dist
 
bool warm    = bar_index > math.max(atrLen, erLen) * 3
bool buySig  = warm and barstate.isconfirmed and dir == 1 and dir[1] == -1
bool sellSig = warm and barstate.isconfirmed and dir == -1 and dir[1] == 1
 
//------------------------------------------------------------------------------
// Visuals
//------------------------------------------------------------------------------
color dirCol = dir == 1 ? upCol : dnCol
pPrice = plot(hl2, "Price Anchor", display = display.none, editable = false)
pTrail = plot(warm ? trail : na, "Trend Trail", color = dir != dir[1] ? na : dirCol, linewidth = 2, style = plot.style_linebr)
fill(pPrice, pTrail, color = color.new(dirCol, 90), title = "Trend Fill")
plotshape(buySig, "Buy", shape.labelup, location.belowbar, color = upCol, text = "BUY", textcolor = color.white, size = size.small)
plotshape(sellSig, "Sell", shape.labeldown, location.abovebar, color = dnCol, text = "SELL", textcolor = color.white, size = size.small)
barcolor(colorBars ? color.new(dirCol, 20) : na, title = "Direction Bar Color")
 
plot(er, "Efficiency Ratio", display = display.data_window)
plot(mult, "Adaptive Multiplier", display = display.data_window)
plot(dir, "Direction", display = display.data_window)
 
//------------------------------------------------------------------------------
// Dashboard
//------------------------------------------------------------------------------
f_pos(string p) =>
    switch p
        "Top Right"    => position.top_right
        "Bottom Right" => position.bottom_right
        "Top Left"     => position.top_left
        => position.bottom_left
 
var table dash = table.new(f_pos(dashPos), 2, 4, bgcolor = color.new(#131722, 15), border_color = color.new(color.gray, 70), border_width = 1)
 
f_row(int r, string k, string v, color c) =>
    table.cell(dash, 0, r, k, text_color = color.new(color.white, 30), text_size = size.small, text_halign = text.align_left)
    table.cell(dash, 1, r, v, text_color = c, text_size = size.small, text_halign = text.align_right)
 
if showDash and barstate.islast
    f_row(0, "Direction", dir == 1 ? "Up" : "Down", dirCol)
    f_row(1, "Efficiency", str.tostring(er, "#.00"), color.white)
    f_row(2, "Trail width", str.tostring(mult, "#.00") + " ATR", color.white)
    f_row(3, "Distance to flip", str.tostring(math.abs(close - trail) / unit, "#.0") + " ATR", color.white)
 
//------------------------------------------------------------------------------
// Alerts
//------------------------------------------------------------------------------
alertcondition(buySig, "Buy Signal", "Efficiency Trail: BUY signal on {{ticker}} ({{interval}})")
alertcondition(sellSig, "Sell Signal", "Efficiency Trail: SELL signal on {{ticker}} ({{interval}})")

← 返回列表