Trade Plan [Gold Line Traders]

robbob1224 · study · 193 行 · 点赞 39 · TradingView 原页

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

Pine Script

//@version=6
indicator("Trade Plan [Gold Line Traders]", "Trade Plan [Gold Line Traders]", overlay = true, max_boxes_count = 20, max_lines_count = 20, max_labels_count = 20)
 
// ───── Trade inputs (change these for every new setup) ─────
gTrade = "Your trade"
dirIn  = input.string("BUY", "Direction", options = ["BUY", "SELL"], group = gTrade)
entry  = input.price(0.0, "Entry", group = gTrade)
sl     = input.price(0.0, "Stop loss", group = gTrade)
tp1    = input.price(0.0, "TP1", group = gTrade)
tp2    = input.price(0.0, "TP2", group = gTrade)
tp3    = input.price(0.0, "TP3", group = gTrade)
tp4    = input.price(0.0, "TP4", group = gTrade, tooltip = "Leave any TP at 0 to hide it")
filled  = input.bool(false, "Already filled (market order)", group = gTrade, tooltip = "Tick this if you're already in the trade, so it shows ACTIVE straight away instead of waiting for price to touch the entry")
placedT = input.time(0, "Trade placed at", confirm = true, group = gTrade, tooltip = "Click the candle you placed the trade on. Entry, SL and TP hits are only checked after this point, so older candles are ignored.")
 
// ───── Display ─────
gDisp     = "Display"
pip       = input.float(0.10, "Pip size", step = 0.01, group = gDisp, tooltip = "Gold = 0.10")
barsBack  = input.int(20, "Zone starts this many bars back (if no placed time)", minval = 1, maxval = 4000, group = gDisp)
extRight  = input.int(15, "Extend right (bars)", minval = 0, maxval = 500, group = gDisp)
showPanel = input.bool(true, "Show panel", group = gDisp)
panelPos  = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gDisp)
lblSize   = input.string("Normal", "Label size", options = ["Small", "Normal", "Large"], group = gDisp)
 
gCol    = "Colours"
slFill  = input.color(color.new(color.red, 80), "SL zone", group = gCol)
tpFill  = input.color(color.new(color.green, 80), "TP zone", group = gCol)
slLineC = input.color(color.red, "SL line", group = gCol)
tpLineC = input.color(color.lime, "TP lines", group = gCol)
enLineC = input.color(color.white, "Entry line", group = gCol)
 
// ───── Helpers ─────
isBuy = dirIn == "BUY"
fmt(float p) => str.tostring(p, format.mintick)
pipsTo(float p) => math.round(math.abs(p - entry) / pip, 1)
risk = math.abs(entry - sl)
rr(float p) => risk > 0 ? math.round(math.abs(p - entry) / risk, 2) : na
tpOn(float p) => p > 0
lblSz = lblSize == "Small" ? size.small : lblSize == "Large" ? size.large : size.normal
 
hasEntry = entry > 0
hasSL    = sl > 0
tps      = array.from(tp1, tp2, tp3, tp4)
 
// farthest TP that is set
farTP = 0.0
for p in tps
    if tpOn(p)
        farTP := farTP == 0.0 ? p : (isBuy ? math.max(farTP, p) : math.min(farTP, p))
 
// sanity check
okSide = true
if hasEntry and hasSL
    okSide := isBuy ? sl < entry : sl > entry
for p in tps
    if hasEntry and tpOn(p)
        okSide := okSide and (isBuy ? p > entry : p < entry)
 
// ───── Trade tracking (only after the trade was placed) ─────
var int placeBar = na
if na(placeBar) and placedT > 0 and time_close > placedT
    placeBar := bar_index
afterPlace = not na(placeBar) and bar_index > placeBar
onPlaceBar = not na(placeBar) and bar_index == placeBar
 
var bool entered = filled
var bool slHit   = false
var bool tpHit1  = false
var bool tpHit2  = false
var bool tpHit3  = false
var bool tpHit4  = false
// live-tick flags for the candle the trade was placed on (its earlier wicks are ignored)
varip bool vEntered = filled
varip bool vSL      = false
varip bool vT1      = false
varip bool vT2      = false
varip bool vT3      = false
varip bool vT4      = false
 
hitTP(float p, float hi, float lo) => tpOn(p) and (isBuy ? hi >= p : lo <= p)
 
if hasEntry and afterPlace and not slHit
    if not entered
        entered := low <= entry and high >= entry
    else
        if hasSL and (isBuy ? low <= sl : high >= sl)
            slHit := true
        tpHit1 := tpHit1 or hitTP(tp1, high, low)
        tpHit2 := tpHit2 or hitTP(tp2, high, low)
        tpHit3 := tpHit3 or hitTP(tp3, high, low)
        tpHit4 := tpHit4 or hitTP(tp4, high, low)
 
// placed on the live candle, or no time picked: follow live ticks only
liveOnly = (onPlaceBar or placedT == 0) and barstate.isrealtime
if hasEntry and liveOnly and not vSL
    if not vEntered
        vEntered := isBuy ? close <= entry : close >= entry
    else
        vSL := hasSL and (isBuy ? close <= sl : close >= sl)
        vT1 := vT1 or hitTP(tp1, close, close)
        vT2 := vT2 or hitTP(tp2, close, close)
        vT3 := vT3 or hitTP(tp3, close, close)
        vT4 := vT4 or hitTP(tp4, close, close)
if liveOnly
    entered := entered or vEntered
    slHit   := slHit or vSL
    tpHit1  := tpHit1 or vT1
    tpHit2  := tpHit2 or vT2
    tpHit3  := tpHit3 or vT3
    tpHit4  := tpHit4 or vT4
 
// ───── Drawing ─────
var box   slBox = na
var box   tpBox = na
var array<line>  lns  = array.new<line>()
var array<label> lbls = array.new<label>()
 
mkLine(float p, color c, string st, int w, int xl, int xr) =>
    line.new(xl, p, xr, p, xloc = xloc.bar_time, color = c, style = st, width = w)
 
mkLabel(float p, string t, color c, int xr) =>
    label.new(xr, p, t, xloc = xloc.bar_time, style = label.style_label_left, color = color.new(c, 15), textcolor = color.white, size = lblSz)
 
if barstate.islast
    box.delete(slBox)
    box.delete(tpBox)
    for l in lns
        line.delete(l)
    for b in lbls
        label.delete(b)
    array.clear(lns)
    array.clear(lbls)
 
    if hasEntry
        tfMs  = timeframe.in_seconds() * 1000
        xL    = placedT > 0 ? math.min(placedT, time) : time[math.min(barsBack, bar_index)]
        xR    = time + extRight * tfMs
        if hasSL
            slBox := box.new(xL, math.max(entry, sl), xR, math.min(entry, sl), xloc = xloc.bar_time, bgcolor = slFill, border_color = na)
            array.push(lns, mkLine(sl, slLineC, line.style_solid, 2, xL, xR))
            array.push(lbls, mkLabel(sl, "SL " + fmt(sl) + "  (-" + str.tostring(pipsTo(sl)) + " pips)" + (slHit ? "  ❌ HIT" : ""), slLineC, xR))
        if farTP != 0.0
            tpBox := box.new(xL, math.max(entry, farTP), xR, math.min(entry, farTP), xloc = xloc.bar_time, bgcolor = tpFill, border_color = na)
        hits = array.from(tpHit1, tpHit2, tpHit3, tpHit4)
        for i = 0 to 3
            p = array.get(tps, i)
            if tpOn(p)
                array.push(lns, mkLine(p, tpLineC, line.style_dashed, 1, xL, xR))
                rrTxt = na(rr(p)) ? "" : " | " + str.tostring(rr(p)) + "R"
                array.push(lbls, mkLabel(p, "TP" + str.tostring(i + 1) + " " + fmt(p) + "  (+" + str.tostring(pipsTo(p)) + " pips" + rrTxt + ")" + (array.get(hits, i) ? "  ✅" : ""), color.green, xR))
        array.push(lns, mkLine(entry, enLineC, line.style_solid, 2, xL, xR))
        array.push(lbls, mkLabel(entry, dirIn + " ENTRY " + fmt(entry) + (entered ? "  ▶ ACTIVE" : "  ⏳ waiting"), color.gray, xR))
 
// ───── Panel ─────
pos = panelPos == "Top right" ? position.top_right : panelPos == "Bottom right" ? position.bottom_right : panelPos == "Top left" ? position.top_left : position.bottom_left
var table pnl = table.new(pos, 3, 10, bgcolor = color.new(#0b0f19, 10), border_color = color.new(color.gray, 60), border_width = 1)
 
row(int r, string a, string b, string c, color col) =>
    table.cell(pnl, 0, r, a, text_color = col, text_size = size.small, text_halign = text.align_left)
    table.cell(pnl, 1, r, b, text_color = color.white, text_size = size.small)
    table.cell(pnl, 2, r, c, text_color = col, text_size = size.small)
 
if barstate.islast and showPanel
    table.clear(pnl, 0, 0, 2, 9)
    table.cell(pnl, 0, 0, "TRADE PLAN", text_color = color.yellow, text_size = size.small)
    table.merge_cells(pnl, 0, 0, 2, 0)
    if not hasEntry
        table.cell(pnl, 0, 1, "Open settings and type your Entry, SL and TPs", text_color = color.orange, text_size = size.small)
        table.merge_cells(pnl, 0, 1, 2, 1)
    else
        status = slHit ? "❌ SL HIT" : tpHit4 ? "🏆 TP4 HIT" : tpHit3 ? "✅ TP3 HIT" : tpHit2 ? "✅ TP2 HIT" : tpHit1 ? "✅ TP1 HIT" : entered ? "▶ ACTIVE" : "⏳ Waiting for entry"
        row(1, dirIn, "Price " + fmt(close), status, isBuy ? color.lime : color.red)
        row(2, "Entry", fmt(entry), entered ? "▶" : "", color.white)
        row(3, "SL", hasSL ? fmt(sl) : "-", hasSL ? "-" + str.tostring(pipsTo(sl)) + " pips" : "", color.red)
        hits = array.from(tpHit1, tpHit2, tpHit3, tpHit4)
        for i = 0 to 3
            p = array.get(tps, i)
            if tpOn(p)
                row(4 + i, "TP" + str.tostring(i + 1), fmt(p), "+" + str.tostring(pipsTo(p)) + " pips" + (na(rr(p)) ? "" : " " + str.tostring(rr(p)) + "R") + (array.get(hits, i) ? " ✅" : ""), color.lime)
        livePips = (isBuy ? close - entry : entry - close) / pip
        row(8, "Live", entered and not slHit ? str.tostring(math.round(livePips, 1)) + " pips" : "-", "", livePips >= 0 ? color.lime : color.red)
        if not okSide
            table.cell(pnl, 0, 9, "⚠ Check levels: SL/TPs on the wrong side for a " + dirIn, text_color = color.orange, text_size = size.small)
            table.merge_cells(pnl, 0, 9, 2, 9)
 
// ───── Alerts ─────
touch(float p) => p > 0 and low <= p and high >= p
alertcondition(touch(entry), "Entry touched", "Gold Line: price touched your entry")
alertcondition(touch(sl), "SL touched", "Gold Line: price touched your stop loss")
alertcondition(touch(tp1), "TP1 touched", "Gold Line: TP1 touched")
alertcondition(touch(tp2), "TP2 touched", "Gold Line: TP2 touched")
alertcondition(touch(tp3), "TP3 touched", "Gold Line: TP3 touched")
alertcondition(touch(tp4), "TP4 touched", "Gold Line: TP4 touched")

← 返回列表