Trendline Angle Pro V6 (Degree + Strategy)

dewanpro · strategy · 286 行 · 点赞 187 · TradingView 原页

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

Pine Script

//@version=6
strategy("Trendline Angle Pro V6 (Degree + Strategy)", shorttitle="TL-Angle Pro", overlay=true,
         max_lines_count=100, max_labels_count=100, initial_capital=100000,
         default_qty_type=strategy.percent_of_equity, default_qty_value=10,
         commission_type=strategy.commission.percent, commission_value=0.05,
         pyramiding=0, calc_on_every_tick=false)
 
// ============================================================
//  INPUTS
// ============================================================
string G1 = "Angle Engine (market-independent)"
int   atrLen   = input.int(14,  "ATR Length", minval=1, group=G1)
float k45      = input.float(5.0, "Bars needed for 1 ATR move = 45°", minval=0.5, step=0.5, group=G1,
     tooltip="Normalises angle for ALL markets. 5 means a slope of 0.2 ATR per bar = 45°. Raise for slower markets, lower for fast ones.")
int   regLen   = input.int(20,  "Regression Length (live angle)", minval=5, group=G1)
int   pvLen    = input.int(5,   "Pivot Length (trendlines)", minval=2, group=G1)
 
string G2 = "Angle Zones (as per your slide)"
float steepAng = input.float(45.0, "Steep >= (°)", group=G2)
float modAng   = input.float(20.0, "Moderate >= (°)", group=G2)
 
string G3 = "Strategy"
bool  useLong  = input.bool(true,  "Enable Longs", group=G3)
bool  useShort = input.bool(true,  "Enable Shorts (turn OFF for India cash stocks)", group=G3)
bool  allowSteep = input.bool(true, "Allow entries in Steep zone", group=G3)
float minEntry = input.float(20.0, "Min |angle| to enter (°)", group=G3)
int   fastLen  = input.int(9,   "Fast EMA", group=G3)
int   slowLen  = input.int(50,  "Slow EMA", group=G3)
float slMult   = input.float(1.5, "Stop Loss (ATR x)", step=0.1, group=G3)
float rr       = input.float(2.0, "Reward : Risk", step=0.1, group=G3)
bool  exitFlat = input.bool(true, "Exit when angle turns Flat", group=G3)
 
string G4 = "Display"
bool  showLines = input.bool(true, "Show Trendlines + Degree labels", group=G4)
bool  showTable = input.bool(true, "Show Dashboard", group=G4)
 
string G5 = "Major Swing 45° Lines + Signature"
bool   showGann = input.bool(true, "Show major swing line + angle fan lines", group=G5)
int    majLen   = input.int(10, "Major Swing Pivot Length", minval=3, group=G5)
float  ang1     = input.float(20.0, "Fan Line 1 (Red) Angle", minval=1, maxval=80, group=G5)
float  ang2     = input.float(40.0, "Fan Line 2 (Blue) Angle", minval=1, maxval=80, group=G5)
float  ang3     = input.float(45.0, "Fan Line 3 (Dark Green) Angle", minval=1, maxval=80, group=G5)
string wmText   = input.string("Sanjay Dewan", "Signature Text", group=G5)
string wmPos    = input.string("Bottom Center", "Signature Position", options=["Bottom Center", "Bottom Left", "Bottom Right", "Top Center", "Top Left", "Middle Center"], group=G5)
 
color cSteep = color.new(#00c853, 0)
color cMod   = color.new(#2962ff, 0)
color cFlat  = color.new(#ff9800, 0)
color cDnSt  = color.new(#d50000, 0)
color cDnMod = color.new(#ff5252, 0)
 
// ============================================================
//  HELPERS
// ============================================================
f_deg(float slopeNorm) =>
    float rad = math.atan(slopeNorm)
    float deg = rad * 180.0 / math.pi
    deg
 
f_angle(float p1, int b1, float p2, int b2, float atrV) =>
    float dx  = math.max(float(b2 - b1), 1.0)
    float dy  = (p2 - p1) / atrV
    float sl  = dy / dx * k45
    f_deg(sl)
 
f_zone(float a) =>
    float x = math.abs(a)
    x >= steepAng ? "Steep" : x >= modAng ? "Moderate" : "Flat"
 
f_meaning(float a) =>
    string z = f_zone(a)
    string r = ""
    if a >= 0
        r := z == "Steep" ? "Strong bullish momentum" : z == "Moderate" ? "Healthy sustainable uptrend" : "Weak momentum / consolidation"
    else
        r := z == "Steep" ? "Aggressive selling / panic" : z == "Moderate" ? "Controlled downtrend" : "Weak sellers / base formation"
    r
 
f_color(float a) =>
    float x = math.abs(a)
    a >= 0 ? (x >= steepAng ? cSteep : x >= modAng ? cMod : cFlat) : (x >= steepAng ? cDnSt : x >= modAng ? cDnMod : cFlat)
 
// ============================================================
//  CORE SERIES
// ============================================================
float atr   = ta.atr(atrLen)
float emaF  = ta.ema(close, fastLen)
float emaS  = ta.ema(close, slowLen)
 
// Live angle: linear-regression slope normalised by ATR
float slope = ta.linreg(close, regLen, 0) - ta.linreg(close, regLen, 1)
float angR  = f_deg(slope / atr * k45)
string zoneR = f_zone(angR)
 
// ============================================================
//  PIVOT TRENDLINES + DEGREE MARKING
// ============================================================
float pl = ta.pivotlow(low,  pvLen, pvLen)
float ph = ta.pivothigh(high, pvLen, pvLen)
 
var float lo1 = na
var float lo2 = na
var int   lo1b = na
var int   lo2b = na
var float hi1 = na
var float hi2 = na
var int   hi1b = na
var int   hi2b = na
var line  upL = na
var line  dnL = na
var label upT = na
var label dnT = na
var float upAng = na
var float dnAng = na
 
if not na(pl)
    lo1 := lo2
    lo1b := lo2b
    lo2 := pl
    lo2b := bar_index - pvLen
    if not na(lo1) and lo2 > lo1
        upAng := f_angle(lo1, lo1b, lo2, lo2b, atr)
        if showLines
            line.delete(upL)
            label.delete(upT)
            upL := line.new(lo1b, lo1, lo2b, lo2, extend=extend.right, color=f_color(upAng), width=2)
            upT := label.new(lo2b, lo2, str.tostring(upAng, "#.0") + "° " + f_zone(upAng) + "\n" + f_meaning(upAng),
                 style=label.style_label_up, color=f_color(upAng), textcolor=color.white, size=size.small)
 
if not na(ph)
    hi1 := hi2
    hi1b := hi2b
    hi2 := ph
    hi2b := bar_index - pvLen
    if not na(hi1) and hi2 < hi1
        dnAng := f_angle(hi1, hi1b, hi2, hi2b, atr)
        if showLines
            line.delete(dnL)
            label.delete(dnT)
            dnL := line.new(hi1b, hi1, hi2b, hi2, extend=extend.right, color=f_color(dnAng), width=2)
            dnT := label.new(hi2b, hi2, str.tostring(dnAng, "#.0") + "° " + f_zone(dnAng) + "\n" + f_meaning(dnAng),
                 style=label.style_label_down, color=f_color(dnAng), textcolor=color.white, size=size.small)
 
// ============================================================
//  MAJOR SWING LINE (degree + points) + ANGLE FAN: 20 Red / 40 Blue / 45 Dark Green
// ============================================================
float pML  = ta.pivotlow(low,  majLen, majLen)
float pMH  = ta.pivothigh(high, majLen, majLen)
float atrP = atr[majLen]
 
var float mHi  = na
var int   mHiB = na
var float mLo  = na
var int   mLoB = na
var line  legL  = na
var label legT  = na
var array<line>  fanLines  = array.new<line>()
var array<label> fanLabels = array.new<label>()
 
color cFan1 = color.red
color cFan2 = color.blue
color cFan3 = #006400
 
array<float> fanAngs = array.from(ang1, ang2, ang3)
array<color> fanCols = array.from(cFan1, cFan2, cFan3)
int lblOff = 25
 
if showGann and not na(pML)
    mLo  := pML
    mLoB := bar_index - majLen
    while array.size(fanLines) > 0
        line.delete(array.pop(fanLines))
    while array.size(fanLabels) > 0
        label.delete(array.pop(fanLabels))
    for i = 0 to 2
        float a_ = array.get(fanAngs, i)
        color  c_ = array.get(fanCols, i)
        float st_ = atrP / k45 * math.tan(a_ * math.pi / 180.0)
        array.push(fanLines, line.new(mLoB, mLo, mLoB + 10, mLo + st_ * 10, extend=extend.right, color=c_, width=2))
        array.push(fanLabels, label.new(mLoB + lblOff, mLo + st_ * lblOff, str.tostring(a_, "#.#") + "°",
             style=label.style_label_down, color=color.new(c_, 80), textcolor=c_, size=size.small))
    if not na(mHi) and mHiB < mLoB
        float legA = f_angle(mHi, mHiB, mLo, mLoB, atrP)
        line.delete(legL)
        label.delete(legT)
        legL := line.new(mHiB, mHi, mLoB, mLo, color=f_color(legA), width=2)
        legT := label.new(math.round((mHiB + mLoB) / 2.0), (mHi + mLo) / 2.0,
             str.tostring(mLo - mHi, format.mintick) + " pts\n" + str.tostring(legA, "#.0") + "°",
             style=label.style_label_left, color=color.new(color.gray, 15), textcolor=color.white, size=size.small)
 
if showGann and not na(pMH)
    mHi  := pMH
    mHiB := bar_index - majLen
    while array.size(fanLines) > 0
        line.delete(array.pop(fanLines))
    while array.size(fanLabels) > 0
        label.delete(array.pop(fanLabels))
    for j = 0 to 2
        float b_ = array.get(fanAngs, j)
        color  d_ = array.get(fanCols, j)
        float sd_ = atrP / k45 * math.tan(b_ * math.pi / 180.0)
        array.push(fanLines, line.new(mHiB, mHi, mHiB + 10, mHi - sd_ * 10, extend=extend.right, color=d_, width=2))
        array.push(fanLabels, label.new(mHiB + lblOff, mHi - sd_ * lblOff, "-" + str.tostring(b_, "#.#") + "°",
             style=label.style_label_up, color=color.new(d_, 80), textcolor=d_, size=size.small))
    if not na(mLo) and mLoB < mHiB
        float legA2 = f_angle(mLo, mLoB, mHi, mHiB, atrP)
        line.delete(legL)
        label.delete(legT)
        legL := line.new(mLoB, mLo, mHiB, mHi, color=f_color(legA2), width=2)
        legT := label.new(math.round((mLoB + mHiB) / 2.0), (mLo + mHi) / 2.0,
             "+" + str.tostring(mHi - mLo, format.mintick) + " pts\n" + str.tostring(legA2, "#.0") + "°",
             style=label.style_label_right, color=color.new(color.gray, 15), textcolor=color.white, size=size.small)
 
// ============================================================
//  SIGNATURE / WATERMARK
// ============================================================
string wmPosition = switch wmPos
    "Bottom Left"   => position.bottom_left
    "Bottom Right"  => position.bottom_right
    "Top Center"    => position.top_center
    "Top Left"      => position.top_left
    "Middle Center" => position.middle_center
    => position.bottom_center
 
var table wm = table.new(wmPosition, 1, 1)
if barstate.islast
    table.cell(wm, 0, 0, wmText, text_color=color.new(color.gray, 35), text_size=size.huge)
 
plot(emaF, "Fast EMA", color=color.new(color.aqua, 30))
plot(emaS, "Slow EMA", color=color.new(color.gray, 20), linewidth=2)
bgcolor(color.new(f_color(angR), 92), title="Angle zone")
 
// ============================================================
//  STRATEGY LOGIC
// ============================================================
bool angUp = angR >= minEntry and (allowSteep or angR < steepAng)
bool angDn = angR <= -minEntry and (allowSteep or angR > -steepAng)
 
bool trendUp = emaF > emaS and close > emaS
bool trendDn = emaF < emaS and close < emaS
 
bool longSig  = useLong  and angUp and trendUp and ta.crossover(close, emaF)
bool shortSig = useShort and angDn and trendDn and ta.crossunder(close, emaF)
 
var float entATR = na
 
if longSig and strategy.position_size <= 0
    entATR := atr
    strategy.entry("Long", strategy.long)
if shortSig and strategy.position_size >= 0
    entATR := atr
    strategy.entry("Short", strategy.short)
 
// Exits: ATR stop, RR target, optional flat-angle exit
if strategy.position_size > 0 and not na(entATR)
    float ep = strategy.position_avg_price
    strategy.exit("L-Exit", "Long", stop=ep - entATR * slMult, limit=ep + entATR * slMult * rr)
    if exitFlat and math.abs(angR) < modAng
        strategy.close("Long", comment="Angle Flat")
 
if strategy.position_size < 0 and not na(entATR)
    float ep = strategy.position_avg_price
    strategy.exit("S-Exit", "Short", stop=ep + entATR * slMult, limit=ep - entATR * slMult * rr)
    if exitFlat and math.abs(angR) < modAng
        strategy.close("Short", comment="Angle Flat")
 
plotshape(longSig,  "Buy",  shape.triangleup,   location.belowbar, color.new(color.green, 0), size=size.small, text="BUY")
plotshape(shortSig, "Sell", shape.triangledown, location.abovebar, color.new(color.red, 0),   size=size.small, text="SELL")
 
// ============================================================
//  DASHBOARD
// ============================================================
var table t = table.new(position.top_right, 2, 6, border_width=1, frame_color=color.gray, frame_width=1)
if showTable and barstate.islast
    table.cell(t, 0, 0, "Live Angle",  text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 0, str.tostring(angR, "#.0") + "°", text_color=color.white, bgcolor=f_color(angR), text_size=size.small)
    table.cell(t, 0, 1, "Zone",        text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 1, zoneR,         text_color=color.white, bgcolor=f_color(angR), text_size=size.small)
    table.cell(t, 0, 2, "Meaning",     text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 2, f_meaning(angR), text_color=color.white, bgcolor=f_color(angR), text_size=size.small)
    table.cell(t, 0, 3, "Support line", text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 3, na(upAng) ? "-" : str.tostring(upAng, "#.0") + "°", text_color=color.white, bgcolor=color.new(color.gray, 50), text_size=size.small)
    table.cell(t, 0, 4, "Resist. line", text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 4, na(dnAng) ? "-" : str.tostring(dnAng, "#.0") + "°", text_color=color.white, bgcolor=color.new(color.gray, 50), text_size=size.small)
    table.cell(t, 0, 5, "ATR",         text_color=color.white, bgcolor=color.new(color.gray, 30), text_size=size.small)
    table.cell(t, 1, 5, str.tostring(atr, format.mintick), text_color=color.white, bgcolor=color.new(color.gray, 50), text_size=size.small)

← 返回列表