Limit Ladder Planner [Zen]

ChartZenFX · study · 208 行 · 点赞 19 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartZenFX
//@version=6
indicator("Limit Ladder Planner [Zen]", overlay = true, max_lines_count = 20, max_labels_count = 20, max_boxes_count = 2)
 
// Limit Ladder Planner [Zen]
// A plan for 1–5 equal limit orders spread across a POI zone, a stop and a target.
// The total size is set so that the loss with every order filled and the stop hit equals the risk, fees included.
 
// ---------- inputs ----------
gP = "Plan"
zA     = input.price(0.0, "Zone edge 1", confirm = true, group = gP, display = display.none, tooltip = "One edge of the entry zone. Click the chart when the script is added, type a price, or drag the line. The order of the two edges does not matter.")
zB     = input.price(0.0, "Zone edge 2", confirm = true, group = gP, display = display.none, tooltip = "The other edge of the entry zone.")
stopP  = input.price(0.0, "Stop loss", confirm = true, group = gP, display = display.none, tooltip = "Below the zone = long plan, above the zone = short plan.")
tgtP   = input.price(0.0, "Take profit", confirm = true, group = gP, display = display.none, tooltip = "Must be beyond the zone on the other side from the stop.")
nOrd   = input.int(3, "Limit orders", minval = 1, maxval = 5, group = gP, display = display.none, tooltip = "Equal-size orders spread evenly from the near edge of the zone to the far edge. One order sits in the middle of the zone.")
gR = "Risk"
bal    = input.float(1000.0, "Account balance", minval = 0.0, group = gR, display = display.none)
riskPc = input.float(1.0, "Risk per trade %", minval = 0.01, maxval = 100.0, step = 0.1, group = gR, display = display.none, tooltip = "The loss if every order fills and the stop is hit, fees included.")
lev    = input.float(10.0, "Leverage", minval = 1.0, maxval = 200.0, group = gR, display = display.none, tooltip = "Used only to show the margin: position value / leverage. It does not change the size or the risk.")
feeMk  = input.float(0.02, "Limit order fee %", minval = 0.0, step = 0.01, group = gR, display = display.none, tooltip = "Charged on the entries and on the take profit (limit orders).")
feeTk  = input.float(0.06, "Stop (market) fee %", minval = 0.0, step = 0.01, group = gR, display = display.none, tooltip = "Charged on the stop exit (market order).")
tblPosIn = input.string("Bottom right", "Table position", options = ["Bottom right", "Bottom center", "Top right", "Bottom left", "Top left"], group = "Display", display = display.none, tooltip = "If the table covers part of the plan, move it here. The prices of all levels stay on the price scale.")
 
// ---------- ChartZenFX palette (auto light / dark) ----------
bool isLight = (color.r(chart.bg_color) + color.g(chart.bg_color) + color.b(chart.bg_color)) / 3 > 128
color cHI     = isLight ? #AD4A12 : #D9703A
color cLO     = isLight ? #1B46B4 : #6F88EE
color cMUTED  = isLight ? #505C69 : #9AA3B2
color cINK    = isLight ? #1F2937 : #D1D4DC
color cPANEL  = isLight ? #F4F5F7 : #1E222D
color cHEAD   = #2A2E39
color cHEADTX = isLight ? #FFFFFF : #D1D4DC
color cBORDER = isLight ? #C4C8CE : #363A45
color cRISK   = isLight ? #B8404C : #E5707B
color cREW    = isLight ? #2E8A57 : #4FC27F
 
// ---------- plan ----------
float zTop = math.max(zA, zB)
float zBot = math.min(zA, zB)
bool isLong  = stopP < zBot
bool isShort = stopP > zTop
bool okStop  = (isLong or isShort) and zBot > 0 and stopP > 0
bool okTgt   = okStop and tgtP > 0 and (isLong ? tgtP > zTop : tgtP < zBot)
float mk = feeMk / 100.0
float tk = feeTk / 100.0
 
// limit prices: nearest edge first, evenly spread to the far edge (one order = middle of the zone), rounded to the price step
f_entry(int i) =>
    float near = isLong ? zTop : zBot
    float far  = isLong ? zBot : zTop
    math.round_to_mintick(nOrd == 1 ? (zTop + zBot) / 2 : near + (far - near) * i / (nOrd - 1))
 
// total size: loss with all orders filled and the stop hit = risk (entry fees + stop fee included)
float riskAmt = bal * riskPc / 100.0
float sumDist = 0.0
float sumE    = 0.0
for i = 0 to nOrd - 1
    float e = f_entry(i)
    sumDist += math.abs(e - stopP)
    sumE    += e
float lossPerUnit = (sumDist + mk * sumE) / nOrd + tk * stopP
float qTot = okStop and lossPerUnit > 0 ? riskAmt / lossPerUnit : na
float qOne = qTot / nOrd
 
// scenario k = the first k orders filled
f_scen(int k) =>
    float sE = 0.0
    float sD = 0.0
    float sG = 0.0
    for i = 0 to k - 1
        float e = f_entry(i)
        sE += e
        sD += math.abs(e - stopP)
        sG += math.abs(tgtP - e)
    float q = qOne * k
    float avg = sE / k
    float loss = qOne * sD + qOne * sE * mk + q * stopP * tk
    float gain = qOne * sG - qOne * sE * mk - q * tgtP * mk
    [avg, q, loss, gain, loss > 0 ? gain / loss : na, qOne * sE]
 
// ---------- drawing ----------
f_tf() => timeframe.isminutes ? (timeframe.multiplier % 60 == 0 ? str.tostring(int(timeframe.multiplier / 60)) + "H" : str.tostring(timeframe.multiplier) + "m") : timeframe.period
f_px(float x) => na(x) ? "—" : str.tostring(x, format.mintick)
f_qty(float x) => na(x) ? "—" : x < 0.01 ? str.tostring(x, "0.########") : str.tostring(x, "#,##0.####")
f_usd(float x) => na(x) ? "—" : str.tostring(x, "#,##0.00")
 
// the price of every level sits on the price scale: never cut by the chart edge, never under candles, buttons or the table
plot(okStop ? f_entry(0) : na, "L1", color = cINK, display = display.price_scale)
plot(okStop and nOrd >= 2 ? f_entry(1) : na, "L2", color = cINK, display = display.price_scale)
plot(okStop and nOrd >= 3 ? f_entry(2) : na, "L3", color = cINK, display = display.price_scale)
plot(okStop and nOrd >= 4 ? f_entry(3) : na, "L4", color = cINK, display = display.price_scale)
plot(okStop and nOrd >= 5 ? f_entry(4) : na, "L5", color = cINK, display = display.price_scale)
plot(okStop ? stopP : na, "SL", color = cRISK, display = display.price_scale)
plot(okTgt ? tgtP : na, "TP", color = cREW, display = display.price_scale)
 
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
var box zoneBox = na
 
if barstate.islast
    for l in lns
        line.delete(l)
    for b in lbs
        label.delete(b)
    array.clear(lns)
    array.clear(lbs)
    box.delete(zoneBox)
    if okStop
        int x1 = math.max(0, bar_index - 20)
        int x2 = bar_index + 1
        color cS = isLong ? cLO : cHI
        // zone: fill only, no border, so the L1 line and the last order line are the zone edges
        zoneBox := box.new(x1, zTop, x2, zBot, border_color = color.new(cS, 100), border_width = 0, bgcolor = color.new(cS, 85))
        // short tags right of the last candle (no candles there); prices are on the price scale
        for i = 0 to nOrd - 1
            float e = f_entry(i)
            array.push(lns, line.new(x1, e, x2, e, color = cINK, width = 1, style = line.style_dashed))
            array.push(lbs, label.new(x2, e, "L" + str.tostring(i + 1), style = label.style_label_left, color = cPANEL, textcolor = cINK, size = size.small))
        array.push(lns, line.new(x1, stopP, x2, stopP, color = cRISK, width = 2))
        array.push(lbs, label.new(x2, stopP, "SL", style = label.style_label_left, color = cPANEL, textcolor = cRISK, size = size.small))
        if okTgt
            array.push(lns, line.new(x1, tgtP, x2, tgtP, color = cREW, width = 2))
            array.push(lbs, label.new(x2, tgtP, "TP", style = label.style_label_left, color = cPANEL, textcolor = cREW, size = size.small))
 
// ---------- alerts: price reaches the zone (L1), the stop or the target ----------
float nearP = okStop ? f_entry(0) : na
bool evZone = okStop and (isLong ? low <= nearP and low[1] > nearP : high >= nearP and high[1] < nearP)
bool evStop = okStop and (isLong ? low <= stopP and low[1] > stopP : high >= stopP and high[1] < stopP)
bool evTgt  = okTgt and (isLong ? high >= tgtP and high[1] < tgtP : low <= tgtP and low[1] > tgtP)
string pre = syminfo.ticker + " " + f_tf() + ": "
// each event is sent once per bar; events of the same update go in one message
varip int aZone = -1
varip int aStop = -1
varip int aTgt  = -1
string msg = ""
if evZone and aZone != bar_index
    aZone := bar_index
    msg := "price reached the zone, L1 " + f_px(nearP)
if evStop and aStop != bar_index
    aStop := bar_index
    msg := (msg == "" ? "" : msg + "; ") + "price reached the stop " + f_px(stopP)
if evTgt and aTgt != bar_index
    aTgt := bar_index
    msg := (msg == "" ? "" : msg + "; ") + "price reached the target " + f_px(tgtP)
if msg != ""
    alert(pre + msg + ".", alert.freq_all)
alertcondition(evZone, "Zone reached (L1)", "{{exchange}}:{{ticker}} {{interval}} Limit Ladder Planner: price reached the zone (L1)")
alertcondition(evStop, "Stop reached", "{{exchange}}:{{ticker}} {{interval}} Limit Ladder Planner: price reached the stop")
alertcondition(evTgt, "Target reached", "{{exchange}}:{{ticker}} {{interval}} Limit Ladder Planner: price reached the target")
 
// ---------- table ----------
tblPos = switch tblPosIn
    "Bottom center" => position.bottom_center
    "Top right"    => position.top_right
    "Bottom right" => position.bottom_right
    "Bottom left"  => position.bottom_left
    => position.top_left
 
var table tb = table.new(tblPos, 6, 11, bgcolor = cPANEL, frame_color = cBORDER, frame_width = 1, border_width = 0)
 
// table rows (all of them depend only on the inputs, so the layout is fixed for the whole run)
int rWarn = okStop and not okTgt ? 2 : -1
int rHead = rWarn > 0 ? 3 : 2
int rF1   = rHead + nOrd + 1
if barstate.isfirst
    table.merge_cells(tb, 0, 0, 5, 0)
    table.merge_cells(tb, 0, 1, 5, 1)
    if not okStop
        table.merge_cells(tb, 0, 2, 5, 2)
    else
        if rWarn > 0
            table.merge_cells(tb, 0, rWarn, 5, rWarn)
        table.merge_cells(tb, 0, rF1, 5, rF1)
        table.merge_cells(tb, 0, rF1 + 1, 5, rF1 + 1)
 
f_cell(int col, int row, string t, color tc, bool num) =>
    table.cell(tb, col, row, t, text_color = tc, text_size = size.small, text_halign = num ? text.align_right : text.align_left, text_font_family = num ? font.family_monospace : font.family_default)
 
if barstate.islast
    string cur = syminfo.currency
    table.cell(tb, 0, 0, "Limit Ladder Planner [Zen] · " + f_tf(), text_color = cHEADTX, bgcolor = cHEAD, text_size = size.small)
    if not okStop
        bool unset = zBot <= 0 or stopP <= 0
        f_cell(0, 1, unset ? "Set the zone edges, the stop and the target:" : "Set the stop outside the zone:", cRISK, false)
        f_cell(0, 2, unset ? "open the settings and type the prices in Inputs." : "below it for a long, above it for a short.", cRISK, false)
    else
        f_cell(0, 1, (isLong ? "Long" : "Short") + " · risk " + f_usd(riskAmt) + " " + cur + " (" + str.tostring(riskPc, "#.##") + "%) · " + str.tostring(nOrd) + " × " + f_qty(qOne), cINK, false)
        if rWarn > 0
            f_cell(0, rWarn, "Set the target beyond the zone.", cRISK, false)
        f_cell(0, rHead, "Filled", cMUTED, false)
        f_cell(1, rHead, "Avg entry", cMUTED, true)
        f_cell(2, rHead, "Size", cMUTED, true)
        f_cell(3, rHead, "Loss", cMUTED, true)
        f_cell(4, rHead, "Profit", cMUTED, true)
        f_cell(5, rHead, "R", cMUTED, true)
        for k = 1 to nOrd
            int r = rHead + k
            [avg, q, loss, gain, rr, notional] = f_scen(k)
            f_cell(0, r, k == 1 ? "L1" : "L1–L" + str.tostring(k), cINK, false)
            f_cell(1, r, f_px(avg), cINK, true)
            f_cell(2, r, f_qty(q), cINK, true)
            f_cell(3, r, f_usd(loss), cINK, true)
            f_cell(4, r, okTgt ? f_usd(gain) : "—", okTgt ? cINK : cMUTED, true)
            f_cell(5, r, okTgt and not na(rr) ? str.tostring(rr, "0.00") : "—", cINK, true)
        float notAll = qOne * sumE
        f_cell(0, rF1, "All filled: " + f_usd(notAll) + " " + cur + " · margin " + f_usd(notAll / lev) + " (" + str.tostring(lev, "#.#") + "x)", cMUTED, false)
        f_cell(0, rF1 + 1, "Fees " + str.tostring(feeMk, "#.###") + "% limit / " + str.tostring(feeTk, "#.###") + "% stop · estimates only", cMUTED, false)

← 返回列表