TradeSystem [InsightEdge]

InsightEdge · library · 1077 行 · 点赞 0 · 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/
// © InsightEdge
 
//@version=6
//@description Limit-order tracker for any signal. Create a trade, monitor the fill and the exit, and update the stats table. This library does not generate signals.
library("TradeSystem", overlay = true)
 
// Publication title is Trade System. Import it as username/Trade_System/1.
// Inputs live in the chart script. This library only creates trades, monitors them, and draws the table.
// Drawings use force_overlay so they stay on the price scale.
// Call updateTable() once per bar. The table is stored for that call site.
 
export const string VERSION = "1.0.0"
 
//@enum Lifecycle of one created trade.
//@field WAITING Limit is working. Fill and expiry are still open.
//@field OPEN Entry was touched. Stop, targets, and break-even are live.
//@field CLOSED Stop, break-even, or the last enabled target closed it.
//@field EXPIRED Waiting trade was invalidated or aged out before a fill.
//@field SKIPPED Stop was wider than the max, or the prices were invalid. Not monitored.
//@field NONE Cancelled on the bar it was created. The slot is kept so that bar is not created again.
export enum TradeState
    WAITING
    OPEN
    CLOSED
    EXPIRED
    SKIPPED
    NONE
 
//@enum Trade direction.
//@field BUY Limit buy. Price must trade down to the entry.
//@field SELL Limit sell. Price must trade up to the entry.
//@field NONE Ignored by create().
export enum TradeSide
    BUY
    SELL
    NONE
 
//@type One limit trade and its drawings.
//@field t_s Left time of the PNL drawings. Moves to the fill bar when the trade opens.
//@field t_e Close time once the trade is finished.
//@field signal_bar Bar where create() ran. That bar cannot fill the trade.
//@field signal_time time of create(). Expiry is measured from here.
//@field start_bar Bar of create(), then the fill bar once the trade is open.
//@field entry Working entry after the entry buffer.
//@field entry_init Entry stored at create(). The fill test uses this.
//@field stopAnchor Stop price passed to create(), before the stop buffer.
//@field tp1 1R target after the TP buffer.
//@field tp2 2R target after the TP buffer.
//@field tp3 3R target after the TP buffer.
//@field sl Working stop. Moves to entry when break-even is set.
//@field sl_init Stop stored at create().
//@field sl_hit Original stop was the close, or break-even was booked as a loss.
//@field tp1_hit Price traded through tp1 while the trade was open.
//@field tp2_hit Price traded through tp2 while the trade was open.
//@field tp3_hit Price traded through tp3 while the trade was open.
//@field static_hit MFE reached the static pip target.
//@field be_set Stop has been moved to entry.
//@field be_hit Closed at the break-even stop.
//@field be_set_bar Bar where break-even was set.
//@field mfe_pips Best favorable excursion in pips.
//@field state Current lifecycle.
//@field side Buy or sell.
//@field box_sl Entry-to-stop box.
//@field box_tp Entry-to-target box. Drawn only after the fill.
//@field line_tp1 1R line.
//@field line_tp2 2R line.
//@field line_tp3 3R line.
//@field line_entry Entry line.
//@field line_sl Stop line.
export type Trade
    int   t_s = na
    int   t_e = na
    int   signal_bar = na
    int   signal_time = na
    int   start_bar = na
    float entry = na
    float entry_init = na
    float stopAnchor = na
    float tp1 = na
    float tp2 = na
    float tp3 = na
    float sl = na
    float sl_init = na
    bool  sl_hit = false
    bool  tp1_hit = false
    bool  tp2_hit = false
    bool  tp3_hit = false
    bool  static_hit = false
    bool  be_set = false
    bool  be_hit = false
    int   be_set_bar = na
    float mfe_pips = 0.0
    TradeState state = TradeState.NONE
    TradeSide side = TradeSide.NONE
    box   box_sl = na
    box   box_tp = na
    line  line_tp1 = na
    line  line_tp2 = na
    line  line_tp3 = na
    line  line_entry = na
    line  line_sl = na
 
//@type All tracker inputs. Build a fresh object every bar from the indicator inputs.
//@field showTable Draw the stats table.
//@field showVisuals Master switch for trade boxes and lines.
//@field showGrayed Gray entry-to-stop preview while a setup is waiting for a fill.
//@field showActive Colored PNL box after the fill.
//@field hideClosed Delete drawings when the trade finishes.
//@field showBoxText Write the side, entry, and stop inside the risk box.
//@field textColor Color of that box text.
//@field textSize Tiny, Small, Normal, Large, or Huge.
//@field grayFill Waiting-box fill.
//@field grayBorder Waiting-box border and lines.
//@field hideBuy Hide buy drawings. Long stats still update.
//@field hideSell Hide sell drawings. Short stats still update.
//@field staticTpPips MFE pip target counted when a trade later closes at the original stop. 0 disables it. Break-even closes are excluded.
//@field slBufferPips Added beyond the raw stop anchor.
//@field entryBufferPips Shifts the raw entry away from price. Buy adds, sell subtracts.
//@field tpBufferPips Pulls each target back toward entry.
//@field maxSlPips Skip the trade when the stop is wider than this. 0 means no limit.
//@field expireByAge Expire an unfilled waiting trade after expireHours.
//@field expireHours Age limit from create().
//@field beEnabled Move the stop to entry after bePips of favorable travel.
//@field beNotSl Book that close on the BE row and leave it out of wins, losses, and win %.
//@field bePips Favorable pips required before the stop moves.
//@field lotSize Dollar scale. 0.01 lot means a 1.0 price move is $1.
//@field tablePos Top Left, Top Center, Top Right, Middle Left, Middle Right, Bottom Left, Bottom Center, or Bottom Right.
//@field tableSize Tiny, Small, or Normal.
//@field tableTitle First header cell. A second line shows how long tracking has been running.
//@field showSignals Signal row.
//@field showActivated Filled-trade row.
//@field showFill Fill percent row.
//@field showOpen Still-open row.
//@field showWinLoss Win and loss rows.
//@field showWinPct Win percent row. Break-even is excluded when beNotSl is on.
//@field showBe Break-even row.
//@field showTp1 1:1 row. If 1:2 and 1:3 are off, the open box and the win close stop at 1:1.
//@field showTp2 1:2 row. If 1:3 is off, the open box and the win close stop at 1:2.
//@field showTp3 1:3 row. The open box and the win close extend to 1:3.
//@field showPnlUsd Dollar rows under each visible target.
//@field showPf Profit-factor row under each visible target.
//@field showExpect Expectancy row under each visible target.
//@field showStatic Static pip target rows.
//@field showAvgPips Average MFE row.
//@field showExpired Expired row.
//@field showSkipped Skipped row.
//@field showEntered Draw the fill label.
//@field enteredBull Fill-label text for buys.
//@field enteredBear Fill-label text for sells.
//@field enteredSize Tiny, Small, Normal, Large, or Huge.
//@field enteredOffset ATR multiple for the fill label.
//@field filledLabels Filled label background. Off draws plain text.
export type TradeSettings
    bool  showTable = true
    bool  showVisuals = true
    bool  showGrayed = true
    bool  showActive = true
    bool  hideClosed = true
    bool  showBoxText = true
    color textColor = #FFFFFFD9
    string textSize = "Small"
    color grayFill = #787B862E
    color grayBorder = #787B8673
    bool  hideBuy = false
    bool  hideSell = false
    float staticTpPips = 0.0
    float slBufferPips = 0.0
    float entryBufferPips = 0.0
    float tpBufferPips = 0.0
    float maxSlPips = 0.0
    bool  expireByAge = false
    float expireHours = 24.0
    bool  beEnabled = false
    bool  beNotSl = true
    float bePips = 10.0
    float lotSize = 0.01
    string tablePos = "Bottom Right"
    string tableSize = "Normal"
    string tableTitle = "Trades"
    bool  showSignals = true
    bool  showActivated = true
    bool  showFill = false
    bool  showOpen = false
    bool  showWinLoss = true
    bool  showWinPct = true
    bool  showBe = false
    bool  showTp1 = true
    bool  showTp2 = true
    bool  showTp3 = false
    bool  showPnlUsd = true
    bool  showPf = false
    bool  showExpect = false
    bool  showStatic = false
    bool  showAvgPips = false
    bool  showExpired = false
    bool  showSkipped = false
    bool  showEntered = true
    string enteredBull = "Buy"
    string enteredBear = "Sell"
    string enteredSize = "Small"
    float enteredOffset = 1.0
    bool  filledLabels = true
 
//@type Persistent stats, the trade list, and the tracking clock. Create it with newManager() and store it with var.
//@field trades Every created trade, including skipped and cancelled slots.
//@field clockStart time of the first monitor() call. The table uses it as the tracking window.
//@field long_signals Buy orders accepted at creation.
//@field long_count Buy fills.
//@field long_win Buy closes booked as wins.
//@field long_loss Buy closes booked as losses.
//@field long_tp1 Buys that traded through 1R.
//@field long_tp2 Buys that traded through 2R.
//@field long_tp3 Buys that traded through 3R.
//@field long_expired Unfilled buys that expired.
//@field long_skipped Buys rejected by a filter.
//@field long_static Buys whose MFE reached the static target before an original-stop close.
//@field long_static_loss Buys that missed that static target.
//@field long_be Buy break-even closes.
//@field long_mfe_sum Sum of buy MFE pips on finished trades.
//@field long_pnl Net dollars of buy win and loss closes, at 1R. Break-even is not included.
//@field long_static_pnl Net dollars of the static-target model.
//@field long_tp1_profit Gross dollars if every 1R touch paid 1R.
//@field long_tp1_loss Gross dollars if every missed 1R paid a full loss.
//@field long_tp2_profit Gross dollars if every 2R touch paid 2R.
//@field long_tp2_loss Gross dollars if every missed 2R paid a full loss.
//@field long_tp3_profit Gross dollars if every 3R touch paid 3R.
//@field long_tp3_loss Gross dollars if every missed 3R paid a full loss.
//@field short_signals Sell orders accepted at creation.
//@field short_count Sell fills.
//@field short_win Sell closes booked as wins.
//@field short_loss Sell closes booked as losses.
//@field short_tp1 Sells that traded through 1R.
//@field short_tp2 Sells that traded through 2R.
//@field short_tp3 Sells that traded through 3R.
//@field short_expired Unfilled sells that expired.
//@field short_skipped Sells rejected by a filter.
//@field short_static Sells whose MFE reached the static target before an original-stop close.
//@field short_static_loss Sells that missed that static target.
//@field short_be Sell break-even closes.
//@field short_mfe_sum Sum of sell MFE pips on finished trades.
//@field short_pnl Net dollars of sell win and loss closes, at 1R.
//@field short_static_pnl Net dollars of the static-target model.
//@field short_tp1_profit Gross dollars if every 1R touch paid 1R.
//@field short_tp1_loss Gross dollars if every missed 1R paid a full loss.
//@field short_tp2_profit Gross dollars if every 2R touch paid 2R.
//@field short_tp2_loss Gross dollars if every missed 2R paid a full loss.
//@field short_tp3_profit Gross dollars if every 3R touch paid 3R.
//@field short_tp3_loss Gross dollars if every missed 3R paid a full loss.
export type TradeManager
    Trade[] trades
    int   clockStart = na
    int   long_signals = 0
    int   long_count = 0
    int   long_win = 0
    int   long_loss = 0
    int   long_tp1 = 0
    int   long_tp2 = 0
    int   long_tp3 = 0
    int   long_expired = 0
    int   long_skipped = 0
    int   long_static = 0
    int   long_static_loss = 0
    int   long_be = 0
    float long_mfe_sum = 0.0
    float long_pnl = 0.0
    float long_static_pnl = 0.0
    float long_tp1_profit = 0.0
    float long_tp1_loss = 0.0
    float long_tp2_profit = 0.0
    float long_tp2_loss = 0.0
    float long_tp3_profit = 0.0
    float long_tp3_loss = 0.0
    int   short_signals = 0
    int   short_count = 0
    int   short_win = 0
    int   short_loss = 0
    int   short_tp1 = 0
    int   short_tp2 = 0
    int   short_tp3 = 0
    int   short_expired = 0
    int   short_skipped = 0
    int   short_static = 0
    int   short_static_loss = 0
    int   short_be = 0
    float short_mfe_sum = 0.0
    float short_pnl = 0.0
    float short_static_pnl = 0.0
    float short_tp1_profit = 0.0
    float short_tp1_loss = 0.0
    float short_tp2_profit = 0.0
    float short_tp2_loss = 0.0
    float short_tp3_profit = 0.0
    float short_tp3_loss = 0.0
 
// Size and position arguments on drawings must be constants.
// A settings string is series, so each call site branches onto a literal.
 
f_pip_size() =>
    syminfo.type == "forex" ? syminfo.mintick * 10.0 : syminfo.mintick
 
f_price_to_pips(float dist) =>
    float pip = f_pip_size()
    pip == 0 ? 0.0 : math.abs(dist) / pip
 
f_usd(TradeSettings cfg, float signedPrice) =>
    signedPrice * (cfg.lotSize / 0.01)
 
f_usd_txt(float v) =>
    string sign = v > 0 ? "+$" : v < 0 ? "-$" : "$"
    sign + str.tostring(math.abs(v), "#.##")
 
f_pnl_col(float v) =>
    v > 0 ? color.lime : v < 0 ? color.red : color.gray
 
f_pct(int num, int den) =>
    den == 0 ? "-" : str.tostring(100.0 * num / den, "#.##") + "%"
 
f_avg(float sum, int den) =>
    den == 0 ? "-" : str.tostring(sum / den, "#.##")
 
f_pf(float profit, float loss) =>
    loss == 0 ? (profit > 0 ? "Inf" : "-") : str.tostring(profit / loss, "#.##")
 
f_dur_txt(TradeManager tm) =>
    int endT = barstate.isrealtime ? timenow : time_close
    int startT = na(tm.clockStart) ? time : tm.clockStart
    int ms = math.max(endT - startT, 0)
    int totalH = int(ms / 3600000)
    int d = int(totalH / 24)
    int h = totalH % 24
    d > 0 ? str.tostring(d) + " Day " + str.tostring(h) + "H" : str.tostring(h) + "H"
 
f_bump_skipped(TradeManager tm, bool isBuy) =>
    if isBuy
        tm.long_skipped := tm.long_skipped + 1
    else
        tm.short_skipped := tm.short_skipped + 1
    true
 
f_acc_tp_pnl(TradeManager tm, bool isBuy, bool h1, bool h2, bool h3, float rUsd) =>
    if isBuy
        tm.long_tp1_profit := tm.long_tp1_profit + (h1 ? rUsd : 0.0)
        tm.long_tp1_loss := tm.long_tp1_loss + (h1 ? 0.0 : rUsd)
        tm.long_tp2_profit := tm.long_tp2_profit + (h2 ? 2.0 * rUsd : 0.0)
        tm.long_tp2_loss := tm.long_tp2_loss + (h2 ? 0.0 : rUsd)
        tm.long_tp3_profit := tm.long_tp3_profit + (h3 ? 3.0 * rUsd : 0.0)
        tm.long_tp3_loss := tm.long_tp3_loss + (h3 ? 0.0 : rUsd)
    else
        tm.short_tp1_profit := tm.short_tp1_profit + (h1 ? rUsd : 0.0)
        tm.short_tp1_loss := tm.short_tp1_loss + (h1 ? 0.0 : rUsd)
        tm.short_tp2_profit := tm.short_tp2_profit + (h2 ? 2.0 * rUsd : 0.0)
        tm.short_tp2_loss := tm.short_tp2_loss + (h2 ? 0.0 : rUsd)
        tm.short_tp3_profit := tm.short_tp3_profit + (h3 ? 3.0 * rUsd : 0.0)
        tm.short_tp3_loss := tm.short_tp3_loss + (h3 ? 0.0 : rUsd)
    true
 
f_tp_extra_rows(TradeSettings cfg) =>
    (cfg.showPnlUsd ? 3 : 0) + (cfg.showPf ? 1 : 0) + (cfg.showExpect ? 1 : 0)
 
f_table_row_count(TradeSettings cfg) =>
    int n = 1
    if cfg.showSignals
        n += 1
    if cfg.showActivated
        n += 1
    if cfg.showFill
        n += 1
    if cfg.showOpen
        n += 1
    if cfg.showWinLoss
        n += 2
    if cfg.showBe
        n += 1
    if cfg.showWinPct
        n += 1
    if cfg.showTp1
        n += 1 + f_tp_extra_rows(cfg)
    if cfg.showTp2
        n += 1 + f_tp_extra_rows(cfg)
    if cfg.showTp3
        n += 1 + f_tp_extra_rows(cfg)
    if cfg.showStatic
        n += cfg.showPnlUsd ? 4 : 3
    if cfg.showAvgPips
        n += 1
    if cfg.showExpired
        n += 1
    if cfg.showSkipped
        n += 1
    math.max(n, 1)
 
f_cell(table tbl, int col, int row, string txt, color tc, string szName) =>
    if szName == "Tiny"
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.tiny)
    else if szName == "Normal"
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.normal)
    else
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.small)
    true
 
f_cell_bg(table tbl, int col, int row, string txt, color tc, string szName, color bg) =>
    if szName == "Tiny"
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.tiny, bgcolor = bg)
    else if szName == "Normal"
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.normal, bgcolor = bg)
    else
        table.cell(tbl, col, row, txt, text_color = tc, text_size = size.small, bgcolor = bg)
    true
 
f_tbl(table tbl, int row, string lab, string v1, string v2, string v3, color cl, color c1, color c2, color c3, string szName) =>
    f_cell(tbl, 0, row, lab, cl, szName)
    f_cell(tbl, 1, row, v1, c1, szName)
    f_cell(tbl, 2, row, v2, c2, szName)
    f_cell(tbl, 3, row, v3, c3, szName)
    row + 1
 
f_make_panel(string pos, int nrows) =>
    table tbl = na
    color bg = color.new(color.black, 15)
    if pos == "Top Left"
        tbl := table.new(position.top_left, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Top Center"
        tbl := table.new(position.top_center, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Top Right"
        tbl := table.new(position.top_right, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Middle Left"
        tbl := table.new(position.middle_left, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Middle Right"
        tbl := table.new(position.middle_right, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Bottom Left"
        tbl := table.new(position.bottom_left, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else if pos == "Bottom Center"
        tbl := table.new(position.bottom_center, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    else
        tbl := table.new(position.bottom_right, 4, nrows, bgcolor = bg, border_width = 1, force_overlay = true)
    tbl
 
f_apply_pos(table tbl, string pos) =>
    if pos == "Top Left"
        table.set_position(tbl, position.top_left)
    else if pos == "Top Center"
        table.set_position(tbl, position.top_center)
    else if pos == "Top Right"
        table.set_position(tbl, position.top_right)
    else if pos == "Middle Left"
        table.set_position(tbl, position.middle_left)
    else if pos == "Middle Right"
        table.set_position(tbl, position.middle_right)
    else if pos == "Bottom Left"
        table.set_position(tbl, position.bottom_left)
    else if pos == "Bottom Center"
        table.set_position(tbl, position.bottom_center)
    else
        table.set_position(tbl, position.bottom_right)
    true
 
f_risk_box(int left, float top, int right, float bottom, color bg, color border, string txt, string szName, color txtCol) =>
    box b = na
    if szName == "Tiny"
        b := box.new(left, top, right, bottom, xloc = xloc.bar_time, bgcolor = bg, border_color = border, text = txt, text_size = size.tiny, text_color = txtCol, text_halign = text.align_center, text_valign = text.align_center, text_wrap = text.wrap_auto, force_overlay = true)
    else if szName == "Normal"
        b := box.new(left, top, right, bottom, xloc = xloc.bar_time, bgcolor = bg, border_color = border, text = txt, text_size = size.normal, text_color = txtCol, text_halign = text.align_center, text_valign = text.align_center, text_wrap = text.wrap_auto, force_overlay = true)
    else if szName == "Large"
        b := box.new(left, top, right, bottom, xloc = xloc.bar_time, bgcolor = bg, border_color = border, text = txt, text_size = size.large, text_color = txtCol, text_halign = text.align_center, text_valign = text.align_center, text_wrap = text.wrap_auto, force_overlay = true)
    else if szName == "Huge"
        b := box.new(left, top, right, bottom, xloc = xloc.bar_time, bgcolor = bg, border_color = border, text = txt, text_size = size.huge, text_color = txtCol, text_halign = text.align_center, text_valign = text.align_center, text_wrap = text.wrap_auto, force_overlay = true)
    else
        b := box.new(left, top, right, bottom, xloc = xloc.bar_time, bgcolor = bg, border_color = border, text = txt, text_size = size.small, text_color = txtCol, text_halign = text.align_center, text_valign = text.align_center, text_wrap = text.wrap_auto, force_overlay = true)
    b
 
f_mark_sized(int x, float y, string txt, color bg, color tc, string szName, bool up, bool filled) =>
    if up and filled and szName == "Tiny"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_up, textcolor = tc, size = size.tiny, force_overlay = true)
    else if up and filled and szName == "Normal"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_up, textcolor = tc, size = size.normal, force_overlay = true)
    else if up and filled and szName == "Large"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_up, textcolor = tc, size = size.large, force_overlay = true)
    else if up and filled and szName == "Huge"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_up, textcolor = tc, size = size.huge, force_overlay = true)
    else if up and filled
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_up, textcolor = tc, size = size.small, force_overlay = true)
    else if up and szName == "Tiny"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.tiny, force_overlay = true)
    else if up and szName == "Normal"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.normal, force_overlay = true)
    else if up and szName == "Large"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.large, force_overlay = true)
    else if up and szName == "Huge"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.huge, force_overlay = true)
    else if up
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.small, force_overlay = true)
    else if filled and szName == "Tiny"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_down, textcolor = tc, size = size.tiny, force_overlay = true)
    else if filled and szName == "Normal"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_down, textcolor = tc, size = size.normal, force_overlay = true)
    else if filled and szName == "Large"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_down, textcolor = tc, size = size.large, force_overlay = true)
    else if filled and szName == "Huge"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_down, textcolor = tc, size = size.huge, force_overlay = true)
    else if filled
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_label_down, textcolor = tc, size = size.small, force_overlay = true)
    else if szName == "Tiny"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.tiny, force_overlay = true)
    else if szName == "Normal"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.normal, force_overlay = true)
    else if szName == "Large"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.large, force_overlay = true)
    else if szName == "Huge"
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.huge, force_overlay = true)
    else
        label.new(x, y, txt, xloc = xloc.bar_time, color = bg, style = label.style_none, textcolor = tc, size = size.small, force_overlay = true)
    true
 
f_trade_visible(TradeSettings cfg, Trade t) =>
    bool hideThis = (t.side == TradeSide.BUY and cfg.hideBuy) or (t.side == TradeSide.SELL and cfg.hideSell)
    cfg.showVisuals and not hideThis
 
f_pnl_box_text(TradeSettings cfg, Trade t) =>
    string sideTxt = t.side == TradeSide.BUY ? "Buy" : "Sell"
    string body = sideTxt + ": " + str.tostring(t.entry, format.mintick) + "\nSL: " + str.tostring(t.sl_init, format.mintick)
    cfg.showBoxText ? body : ""
 
f_tp_box_price(TradeSettings cfg, Trade t) =>
    cfg.showTp3 ? t.tp3 : cfg.showTp2 ? t.tp2 : t.tp1
 
f_final_tp_hit(TradeSettings cfg, Trade t) =>
    cfg.showTp3 ? t.tp3_hit : cfg.showTp2 ? t.tp2_hit : t.tp1_hit
 
f_delete_visuals(Trade t) =>
    Trade out = t
    if not na(out.line_entry)
        line.delete(out.line_entry)
    if not na(out.line_sl)
        line.delete(out.line_sl)
    if not na(out.line_tp1)
        line.delete(out.line_tp1)
    if not na(out.line_tp2)
        line.delete(out.line_tp2)
    if not na(out.line_tp3)
        line.delete(out.line_tp3)
    if not na(out.box_sl)
        box.delete(out.box_sl)
    if not na(out.box_tp)
        box.delete(out.box_tp)
    out.line_entry := na
    out.line_sl := na
    out.line_tp1 := na
    out.line_tp2 := na
    out.line_tp3 := na
    out.box_sl := na
    out.box_tp := na
    out
 
f_has_visuals(Trade t) =>
    not na(t.line_entry) or not na(t.box_sl) or not na(t.box_tp)
 
f_pnl_right(Trade t) =>
    int x2 = time_close
    if t.state == TradeState.WAITING
        int tfMs = int(timeframe.in_seconds() * 1000)
        int age = na(t.t_s) or tfMs <= 0 ? 1 : int((time - t.t_s) / tfMs) + 1
        if age < 5
            x2 := time_close + tfMs
    x2
 
f_draw_entered(TradeSettings cfg, Trade t, float atrNow) =>
    bool hideThis = (t.side == TradeSide.BUY and cfg.hideBuy) or (t.side == TradeSide.SELL and cfg.hideSell)
    if cfg.showEntered and not hideThis
        bool isBuy = t.side == TradeSide.BUY
        float pad = na(atrNow) ? 0.0 : atrNow * cfg.enteredOffset
        float y = isBuy ? low - pad : high + pad
        string txt = isBuy ? cfg.enteredBull : cfg.enteredBear
        color col = isBuy ? color.green : color.red
        color bg = cfg.filledLabels ? col : color.new(col, 100)
        color tc = cfg.filledLabels ? color.white : col
        f_mark_sized(time, y, txt, bg, tc, cfg.enteredSize, isBuy, cfg.filledLabels)
    true
 
f_draw_waiting(TradeSettings cfg, Trade t) =>
    Trade out = t
    if f_trade_visible(cfg, out) and cfg.showGrayed
        int x2 = f_pnl_right(out)
        out.line_entry := line.new(out.t_s, out.entry, x2, out.entry, xloc = xloc.bar_time, color = cfg.grayBorder, style = line.style_solid, width = 1, force_overlay = true)
        out.line_sl := line.new(out.t_s, out.sl, x2, out.sl, xloc = xloc.bar_time, color = cfg.grayBorder, style = line.style_solid, width = 1, force_overlay = true)
        out.box_sl := f_risk_box(out.t_s, out.entry, x2, out.sl, cfg.grayFill, cfg.grayBorder, f_pnl_box_text(cfg, out), cfg.textSize, cfg.textColor)
    out
 
f_draw_open(TradeSettings cfg, Trade t) =>
    Trade out = t
    if f_trade_visible(cfg, out) and cfg.showActive
        color colE = out.side == TradeSide.BUY ? color.new(color.blue, 0) : color.new(color.orange, 0)
        color colRisk = color.new(color.red, 75)
        color colRew = color.new(color.green, 75)
        color colTp = out.side == TradeSide.BUY ? color.new(color.green, 0) : color.new(color.red, 0)
        float tpBox = f_tp_box_price(cfg, out)
        out.line_entry := line.new(out.t_s, out.entry, time_close, out.entry, xloc = xloc.bar_time, color = colE, style = line.style_solid, width = 1, force_overlay = true)
        out.line_sl := line.new(out.t_s, out.sl, time_close, out.sl, xloc = xloc.bar_time, color = color.new(color.red, 0), style = line.style_solid, width = 1, force_overlay = true)
        out.box_sl := f_risk_box(out.t_s, out.entry, time_close, out.sl, colRisk, color.new(color.red, 100), f_pnl_box_text(cfg, out), cfg.textSize, cfg.textColor)
        out.box_tp := box.new(out.t_s, out.entry, time_close, tpBox, xloc = xloc.bar_time, bgcolor = colRew, border_color = color.new(color.green, 100), force_overlay = true)
        out.line_tp1 := line.new(out.t_s, out.tp1, time_close, out.tp1, xloc = xloc.bar_time, color = colTp, style = line.style_dotted, width = 1, force_overlay = true)
        if cfg.showTp2
            out.line_tp2 := line.new(out.t_s, out.tp2, time_close, out.tp2, xloc = xloc.bar_time, color = colTp, style = line.style_dotted, width = 1, force_overlay = true)
        if cfg.showTp3
            out.line_tp3 := line.new(out.t_s, out.tp3, time_close, out.tp3, xloc = xloc.bar_time, color = colTp, style = line.style_dotted, width = 1, force_overlay = true)
    out
 
f_extend_visuals(TradeSettings cfg, Trade t) =>
    if f_trade_visible(cfg, t)
        int x2 = f_pnl_right(t)
        if not na(t.line_entry)
            line.set_x2(t.line_entry, x2)
        if not na(t.line_sl)
            line.set_x2(t.line_sl, x2)
        if not na(t.line_tp1)
            line.set_x2(t.line_tp1, x2)
        if not na(t.line_tp2)
            line.set_x2(t.line_tp2, x2)
        if not na(t.line_tp3)
            line.set_x2(t.line_tp3, x2)
        if not na(t.box_sl)
            box.set_right(t.box_sl, x2)
        if not na(t.box_tp)
            box.set_right(t.box_tp, x2)
    true
 
f_ensure_waiting(TradeSettings cfg, Trade t) =>
    Trade out = t
    if out.state == TradeState.WAITING and cfg.showGrayed
        if f_has_visuals(out)
            f_extend_visuals(cfg, out)
        else
            out := f_draw_waiting(cfg, out)
    out
 
f_freeze_visuals(TradeSettings cfg, Trade t) =>
    Trade out = t
    out.t_e := time_close
    if cfg.hideClosed
        out := f_delete_visuals(out)
    else
        f_extend_visuals(cfg, out)
    out
 
f_record_static(TradeManager tm, TradeSettings cfg, Trade t) =>
    if cfg.staticTpPips > 0
        float risk = math.abs(t.entry - t.sl_init)
        bool isBuy = t.side == TradeSide.BUY
        if t.mfe_pips >= cfg.staticTpPips
            float tpUsd = f_usd(cfg, cfg.staticTpPips * f_pip_size())
            if isBuy
                tm.long_static := tm.long_static + 1
                tm.long_static_pnl := tm.long_static_pnl + tpUsd
            else
                tm.short_static := tm.short_static + 1
                tm.short_static_pnl := tm.short_static_pnl + tpUsd
        else
            float lossUsd = f_usd(cfg, risk)
            if isBuy
                tm.long_static_loss := tm.long_static_loss + 1
                tm.long_static_pnl := tm.long_static_pnl - lossUsd
            else
                tm.short_static_loss := tm.short_static_loss + 1
                tm.short_static_pnl := tm.short_static_pnl - lossUsd
    true
 
f_close_trade(TradeManager tm, TradeSettings cfg, Trade t, bool isWin) =>
    Trade out = t
    bool isBuy = out.side == TradeSide.BUY
    float risk = math.abs(out.entry - out.sl_init)
    float rUsd = f_usd(cfg, risk)
    float pnl = isWin ? rUsd : -rUsd
    f_record_static(tm, cfg, out)
    f_acc_tp_pnl(tm, isBuy, out.tp1_hit, out.tp2_hit, out.tp3_hit, rUsd)
    if isBuy
        tm.long_mfe_sum := tm.long_mfe_sum + out.mfe_pips
        tm.long_pnl := tm.long_pnl + pnl
        if isWin
            tm.long_win := tm.long_win + 1
        else
            tm.long_loss := tm.long_loss + 1
    else
        tm.short_mfe_sum := tm.short_mfe_sum + out.mfe_pips
        tm.short_pnl := tm.short_pnl + pnl
        if isWin
            tm.short_win := tm.short_win + 1
        else
            tm.short_loss := tm.short_loss + 1
    out.state := TradeState.CLOSED
    f_freeze_visuals(cfg, out)
 
f_close_be(TradeManager tm, TradeSettings cfg, Trade t) =>
    Trade out = t
    out.be_hit := true
    out.sl_hit := not cfg.beNotSl
    if out.side == TradeSide.BUY
        tm.long_mfe_sum := tm.long_mfe_sum + out.mfe_pips
        tm.long_be := tm.long_be + 1
    else
        tm.short_mfe_sum := tm.short_mfe_sum + out.mfe_pips
        tm.short_be := tm.short_be + 1
    out.state := TradeState.CLOSED
    f_freeze_visuals(cfg, out)
 
f_apply_be(TradeSettings cfg, Trade t) =>
    Trade out = t
    if cfg.beEnabled and cfg.bePips > 0 and not out.be_set and out.mfe_pips >= cfg.bePips
        out.sl := out.entry
        out.be_set := true
        out.be_set_bar := bar_index
        if f_trade_visible(cfg, out)
            if not na(out.line_sl)
                line.set_y1(out.line_sl, out.sl)
                line.set_y2(out.line_sl, out.sl)
                line.set_color(out.line_sl, color.yellow)
            if not na(out.box_sl)
                if out.side == TradeSide.BUY
                    box.set_bottom(out.box_sl, out.sl)
                else
                    box.set_top(out.box_sl, out.sl)
    out
 
f_activate(TradeManager tm, TradeSettings cfg, Trade t, float atrNow) =>
    Trade out = t
    out := f_delete_visuals(out)
    out.t_s := time
    out.start_bar := bar_index
    out.state := TradeState.OPEN
    f_draw_entered(cfg, out, atrNow)
    out := f_draw_open(cfg, out)
    if out.side == TradeSide.BUY
        tm.long_count := tm.long_count + 1
    else
        tm.short_count := tm.short_count + 1
    out
 
f_expire(TradeManager tm, Trade t) =>
    Trade out = t
    out.state := TradeState.EXPIRED
    out.t_e := time_close
    out := f_delete_visuals(out)
    if out.side == TradeSide.BUY
        tm.long_expired := tm.long_expired + 1
    else
        tm.short_expired := tm.short_expired + 1
    out
 
f_skip(TradeManager tm, Trade t) =>
    Trade out = t
    out.state := TradeState.SKIPPED
    out.t_e := time_close
    out := f_delete_visuals(out)
    f_bump_skipped(tm, out.side == TradeSide.BUY)
    out
 
f_update_mfe(TradeSettings cfg, Trade t, float pxHigh, float pxLow) =>
    Trade out = t
    if not na(pxHigh) and not na(pxLow)
        float fav = out.side == TradeSide.BUY ? pxHigh - out.entry : out.entry - pxLow
        out.mfe_pips := math.max(out.mfe_pips, f_price_to_pips(math.max(fav, 0.0)))
        out.static_hit := cfg.staticTpPips > 0 and out.mfe_pips >= cfg.staticTpPips
    out
 
f_check_tps(TradeManager tm, Trade t, float pxHigh, float pxLow) =>
    Trade out = t
    bool isBuy = out.side == TradeSide.BUY
    bool hit1 = isBuy ? pxHigh >= out.tp1 : pxLow <= out.tp1
    bool hit2 = isBuy ? pxHigh >= out.tp2 : pxLow <= out.tp2
    bool hit3 = isBuy ? pxHigh >= out.tp3 : pxLow <= out.tp3
    if hit1 and not out.tp1_hit
        out.tp1_hit := true
        if isBuy
            tm.long_tp1 := tm.long_tp1 + 1
        else
            tm.short_tp1 := tm.short_tp1 + 1
    if hit2 and not out.tp2_hit
        out.tp2_hit := true
        if isBuy
            tm.long_tp2 := tm.long_tp2 + 1
        else
            tm.short_tp2 := tm.short_tp2 + 1
    if hit3 and not out.tp3_hit
        out.tp3_hit := true
        if isBuy
            tm.long_tp3 := tm.long_tp3 + 1
        else
            tm.short_tp3 := tm.short_tp3 + 1
    out
 
f_manage_open(TradeManager tm, TradeSettings cfg, Trade t, float pxHigh, float pxLow) =>
    Trade out = t
    bool isBuy = out.side == TradeSide.BUY
    if out.state == TradeState.OPEN
        out := f_update_mfe(cfg, out, pxHigh, pxLow)
        out := f_apply_be(cfg, out)
        f_extend_visuals(cfg, out)
        out := f_check_tps(tm, out, pxHigh, pxLow)
        if f_final_tp_hit(cfg, out)
            out := f_close_trade(tm, cfg, out, true)
        else
            bool hitEntry = isBuy ? pxLow <= out.entry : pxHigh >= out.entry
            bool hitInitSl = isBuy ? pxLow <= out.sl_init : pxHigh >= out.sl_init
            bool beArmed = out.be_set and bar_index > out.be_set_bar
            bool countAsBe = out.be_set and (cfg.beNotSl or beArmed) and (hitEntry or (cfg.beNotSl and hitInitSl))
            if countAsBe
                out := f_close_be(tm, cfg, out)
            else if hitInitSl
                out.sl_hit := true
                out := f_close_trade(tm, cfg, out, out.tp1_hit)
    out
 
f_create_trade(TradeManager tm, TradeSettings cfg, TradeSide side, float entryPx, float stopPx) =>
    bool isBuy = side == TradeSide.BUY
    float entBuf = cfg.entryBufferPips * f_pip_size()
    float tpBuf = cfg.tpBufferPips * f_pip_size()
    float entry = isBuy ? entryPx + entBuf : entryPx - entBuf
    float slBuf = cfg.slBufferPips * f_pip_size()
    float sl = isBuy ? stopPx - slBuf : stopPx + slBuf
    float dist = isBuy ? entry - sl : sl - entry
    float slPips = f_price_to_pips(dist)
    if na(entry) or na(sl) or dist <= 0
        f_bump_skipped(tm, isBuy)
        true
    else
        Trade t = Trade.new()
        t.t_s := time
        t.t_e := na
        t.signal_bar := bar_index
        t.signal_time := time
        t.start_bar := bar_index
        t.entry := entry
        t.entry_init := entry
        t.stopAnchor := stopPx
        t.sl := sl
        t.sl_init := sl
        t.tp1 := isBuy ? entry + dist - tpBuf : entry - dist + tpBuf
        t.tp2 := isBuy ? entry + 2.0 * dist - tpBuf : entry - 2.0 * dist + tpBuf
        t.tp3 := isBuy ? entry + 3.0 * dist - tpBuf : entry - 3.0 * dist + tpBuf
        t.mfe_pips := 0.0
        t.state := TradeState.WAITING
        t.side := side
        if cfg.maxSlPips > 0 and slPips > cfg.maxSlPips
            t.state := TradeState.SKIPPED
            t.t_e := time_close
            f_bump_skipped(tm, isBuy)
        else
            if isBuy
                tm.long_signals := tm.long_signals + 1
            else
                tm.short_signals := tm.short_signals + 1
        array.push(tm.trades, t)
        true
 
//@function Empty tracker. Assign it once with var in the indicator.
//@returns Trade manager whose stats survive from bar to bar only if the caller stores it with var.
export newManager() =>
    TradeManager.new(array.new<Trade>())
 
//@function True when this bar already has a live trade of this side. A cancelled slot does not count.
//@param this Tracker from newManager().
//@param side TradeSide.BUY or TradeSide.SELL.
//@returns True when create() should be skipped for this side on this bar.
export method created(TradeManager this, TradeSide side) =>
    bool found = false
    if not na(this) and not na(this.trades)
        int n = array.size(this.trades)
        if n > 0
            for i = 0 to n - 1
                Trade t = array.get(this.trades, i)
                if t.side == side and t.signal_bar == bar_index and t.state != TradeState.NONE
                    found := true
    found
 
//@function Remove a waiting trade created on this bar. Use it when the live bar voids the setup before close.
//@param this Tracker from newManager().
//@param side Side to cancel.
//@returns True. The slot stays in the list with state NONE and is not counted as skipped.
export method cancel(TradeManager this, TradeSide side) =>
    if not na(this) and not na(this.trades)
        int n = array.size(this.trades)
        if n > 0
            for i = 0 to n - 1
                Trade t = array.get(this.trades, i)
                if t.side == side and t.signal_bar == bar_index and t.state == TradeState.WAITING
                    t := f_delete_visuals(t)
                    t.state := TradeState.NONE
                    if side == TradeSide.BUY
                        this.long_signals := math.max(this.long_signals - 1, 0)
                    else
                        this.short_signals := math.max(this.short_signals - 1, 0)
                    array.set(this.trades, i, t)
    true
 
//@function Create a limit. Buffers and the max-stop filter run here. The creation bar cannot fill.
//@param this Tracker from newManager().
//@param cfg Settings for this bar.
//@param side TradeSide.BUY or TradeSide.SELL. NONE is ignored.
//@param entryPx Limit price before the entry buffer. A buy fills on a later bar that trades at or below it. A sell fills on a later bar that trades at or above it.
//@param stopPx Stop price before the stop buffer. A buy stop sits below it. A sell stop sits above it.
//@returns True when the call was handled. A stop wider than maxSlPips is stored as SKIPPED. Invalid prices only add to the skipped count.
export method create(TradeManager this, TradeSettings cfg, TradeSide side, float entryPx, float stopPx) =>
    bool handled = false
    if not na(this) and not na(cfg) and side != TradeSide.NONE
        if na(this.trades)
            this.trades := array.new<Trade>()
        handled := f_create_trade(this, cfg, side, entryPx, stopPx)
    handled
 
//@function Advance every waiting and open trade. Call this after create() and cancel() on the same bar, and only on bars that should count.
//@param this Tracker from newManager().
//@param cfg Settings for this bar.
//@param atrNow ATR used to offset the fill label. Compute it on every bar in the chart script.
//@param pxHigh High of the monitor candle. The chart script loads this from the monitor timeframe.
//@param pxLow Low of the monitor candle. Fills, stops, targets, and favorable travel use this pair.
//@returns The same manager.
export method monitor(TradeManager this, TradeSettings cfg, float atrNow, float pxHigh, float pxLow) =>
    if not na(this) and not na(cfg)
        if na(this.trades)
            this.trades := array.new<Trade>()
        if na(this.clockStart)
            this.clockStart := time
        int n = array.size(this.trades)
        if n > 0
            for i = 0 to n - 1
                Trade t = array.get(this.trades, i)
                if t.state == TradeState.WAITING
                    bool isBuy = t.side == TradeSide.BUY
                    float limitPx = na(t.entry_init) ? t.entry : t.entry_init
                    bool sweptSl = isBuy ? pxLow <= t.sl : pxHigh >= t.sl
                    bool touched = isBuy ? pxLow <= limitPx : pxHigh >= limitPx
                    if bar_index > t.signal_bar and sweptSl and not touched
                        t := f_expire(this, t)
                    else if bar_index > t.signal_bar and touched
                        t := f_activate(this, cfg, t, atrNow)
                        t := f_manage_open(this, cfg, t, pxHigh, pxLow)
                    if t.state == TradeState.WAITING and cfg.expireByAge and cfg.expireHours > 0
                        int ageFrom = na(t.signal_time) ? t.t_s : t.signal_time
                        if time - ageFrom >= cfg.expireHours * 3600000.0
                            t := f_expire(this, t)
                    if t.state == TradeState.WAITING
                        t := f_ensure_waiting(cfg, t)
                else if t.state == TradeState.OPEN
                    t := f_manage_open(this, cfg, t, pxHigh, pxLow)
                array.set(this.trades, i, t)
    this
 
//@function Refresh the stats table. Only the last bar does the work. Call once per bar, after monitor().
//@param this Tracker from newManager().
//@param cfg Settings for this bar. showTable false deletes an existing table.
//@returns The same manager.
export method updateTable(TradeManager this, TradeSettings cfg) =>
    var table panel = na
    var int rowsUsed = 0
    if not na(this) and not na(cfg) and barstate.islast and cfg.showTable
        int nrows = f_table_row_count(cfg)
        if na(panel) or rowsUsed != nrows
            if not na(panel)
                table.delete(panel)
            panel := f_make_panel(cfg.tablePos, nrows)
            rowsUsed := nrows
        f_apply_pos(panel, cfg.tablePos)
        color hdr = color.new(color.gray, 35)
        color wht = color.white
        color grn = color.lime
        color red = color.red
        color gry = color.gray
        color blu = color.aqua
        color yel = color.yellow
        int lClosed = this.long_win + this.long_loss
        int sClosed = this.short_win + this.short_loss
        int tClosed = lClosed + sClosed
        int lDone = lClosed + this.long_be
        int sDone = sClosed + this.short_be
        int tDone = lDone + sDone
        int lExpN = cfg.beNotSl ? lClosed : lDone
        int sExpN = cfg.beNotSl ? sClosed : sDone
        int tExpN = cfg.beNotSl ? tClosed : tDone
        int lOpen = math.max(this.long_count - lDone, 0)
        int sOpen = math.max(this.short_count - sDone, 0)
        int tOpen = lOpen + sOpen
        int lStaticN = this.long_static + this.long_static_loss
        int sStaticN = this.short_static + this.short_static_loss
        int tStaticN = lStaticN + sStaticN
        float lTp1Net = this.long_tp1_profit - this.long_tp1_loss
        float sTp1Net = this.short_tp1_profit - this.short_tp1_loss
        float tTp1Net = lTp1Net + sTp1Net
        float lTp2Net = this.long_tp2_profit - this.long_tp2_loss
        float sTp2Net = this.short_tp2_profit - this.short_tp2_loss
        float tTp2Net = lTp2Net + sTp2Net
        float lTp3Net = this.long_tp3_profit - this.long_tp3_loss
        float sTp3Net = this.short_tp3_profit - this.short_tp3_loss
        float tTp3Net = lTp3Net + sTp3Net
        float tStaticPnl = this.long_static_pnl + this.short_static_pnl
        string staticLbl = "Static W (" + str.tostring(cfg.staticTpPips, "#.#") + "p)"
        string beLbl = "BE (" + str.tostring(cfg.bePips, "#.#") + "p)"
        string szName = cfg.tableSize
        string title = cfg.tableTitle + "\n" + f_dur_txt(this)
        f_cell_bg(panel, 0, 0, title, wht, szName, hdr)
        f_cell_bg(panel, 1, 0, "Long", grn, szName, hdr)
        f_cell_bg(panel, 2, 0, "Short", red, szName, hdr)
        f_cell_bg(panel, 3, 0, "Total", wht, szName, hdr)
        int r = 1
        if cfg.showSignals
            r := f_tbl(panel, r, "Signals", str.tostring(this.long_signals), str.tostring(this.short_signals), str.tostring(this.long_signals + this.short_signals), gry, grn, red, wht, szName)
        if cfg.showActivated
            r := f_tbl(panel, r, "Activated", str.tostring(this.long_count), str.tostring(this.short_count), str.tostring(this.long_count + this.short_count), gry, grn, red, wht, szName)
        if cfg.showFill
            r := f_tbl(panel, r, "Fill %", f_pct(this.long_count, this.long_signals), f_pct(this.short_count, this.short_signals), f_pct(this.long_count + this.short_count, this.long_signals + this.short_signals), gry, grn, red, wht, szName)
        if cfg.showOpen
            r := f_tbl(panel, r, "Open", str.tostring(lOpen), str.tostring(sOpen), str.tostring(tOpen), gry, grn, red, wht, szName)
        if cfg.showWinLoss
            r := f_tbl(panel, r, "Win", str.tostring(this.long_win), str.tostring(this.short_win), str.tostring(this.long_win + this.short_win), gry, grn, red, wht, szName)
            r := f_tbl(panel, r, "Loss", str.tostring(this.long_loss), str.tostring(this.short_loss), str.tostring(this.long_loss + this.short_loss), gry, grn, red, wht, szName)
        if cfg.showBe
            r := f_tbl(panel, r, beLbl, str.tostring(this.long_be), str.tostring(this.short_be), str.tostring(this.long_be + this.short_be), yel, grn, red, wht, szName)
        if cfg.showWinPct
            r := f_tbl(panel, r, "Win %", f_pct(this.long_win, lClosed), f_pct(this.short_win, sClosed), f_pct(this.long_win + this.short_win, tClosed), gry, grn, red, wht, szName)
        if cfg.showTp1
            r := f_tbl(panel, r, "1:1", str.tostring(this.long_tp1), str.tostring(this.short_tp1), str.tostring(this.long_tp1 + this.short_tp1), blu, grn, red, wht, szName)
            if cfg.showPnlUsd
                r := f_tbl(panel, r, "1:1 Profit $", f_usd_txt(this.long_tp1_profit), f_usd_txt(this.short_tp1_profit), f_usd_txt(this.long_tp1_profit + this.short_tp1_profit), yel, grn, red, wht, szName)
                r := f_tbl(panel, r, "1:1 Loss $", f_usd_txt(-this.long_tp1_loss), f_usd_txt(-this.short_tp1_loss), f_usd_txt(-(this.long_tp1_loss + this.short_tp1_loss)), yel, red, red, red, szName)
                r := f_tbl(panel, r, "1:1 P/L $", f_usd_txt(lTp1Net), f_usd_txt(sTp1Net), f_usd_txt(tTp1Net), yel, f_pnl_col(lTp1Net), f_pnl_col(sTp1Net), f_pnl_col(tTp1Net), szName)
            if cfg.showPf
                r := f_tbl(panel, r, "1:1 PF", f_pf(this.long_tp1_profit, this.long_tp1_loss), f_pf(this.short_tp1_profit, this.short_tp1_loss), f_pf(this.long_tp1_profit + this.short_tp1_profit, this.long_tp1_loss + this.short_tp1_loss), blu, grn, red, wht, szName)
            if cfg.showExpect
                string lExp = lExpN == 0 ? "-" : f_usd_txt(lTp1Net / lExpN)
                string sExp = sExpN == 0 ? "-" : f_usd_txt(sTp1Net / sExpN)
                string tExp = tExpN == 0 ? "-" : f_usd_txt(tTp1Net / tExpN)
                r := f_tbl(panel, r, "1:1 Exp $", lExp, sExp, tExp, blu, f_pnl_col(lTp1Net), f_pnl_col(sTp1Net), f_pnl_col(tTp1Net), szName)
        if cfg.showTp2
            r := f_tbl(panel, r, "1:2", str.tostring(this.long_tp2), str.tostring(this.short_tp2), str.tostring(this.long_tp2 + this.short_tp2), blu, grn, red, wht, szName)
            if cfg.showPnlUsd
                r := f_tbl(panel, r, "1:2 Profit $", f_usd_txt(this.long_tp2_profit), f_usd_txt(this.short_tp2_profit), f_usd_txt(this.long_tp2_profit + this.short_tp2_profit), yel, grn, red, wht, szName)
                r := f_tbl(panel, r, "1:2 Loss $", f_usd_txt(-this.long_tp2_loss), f_usd_txt(-this.short_tp2_loss), f_usd_txt(-(this.long_tp2_loss + this.short_tp2_loss)), yel, red, red, red, szName)
                r := f_tbl(panel, r, "1:2 P/L $", f_usd_txt(lTp2Net), f_usd_txt(sTp2Net), f_usd_txt(tTp2Net), yel, f_pnl_col(lTp2Net), f_pnl_col(sTp2Net), f_pnl_col(tTp2Net), szName)
            if cfg.showPf
                r := f_tbl(panel, r, "1:2 PF", f_pf(this.long_tp2_profit, this.long_tp2_loss), f_pf(this.short_tp2_profit, this.short_tp2_loss), f_pf(this.long_tp2_profit + this.short_tp2_profit, this.long_tp2_loss + this.short_tp2_loss), blu, grn, red, wht, szName)
            if cfg.showExpect
                string lExp2 = lExpN == 0 ? "-" : f_usd_txt(lTp2Net / lExpN)
                string sExp2 = sExpN == 0 ? "-" : f_usd_txt(sTp2Net / sExpN)
                string tExp2 = tExpN == 0 ? "-" : f_usd_txt(tTp2Net / tExpN)
                r := f_tbl(panel, r, "1:2 Exp $", lExp2, sExp2, tExp2, blu, f_pnl_col(lTp2Net), f_pnl_col(sTp2Net), f_pnl_col(tTp2Net), szName)
        if cfg.showTp3
            r := f_tbl(panel, r, "1:3", str.tostring(this.long_tp3), str.tostring(this.short_tp3), str.tostring(this.long_tp3 + this.short_tp3), blu, grn, red, wht, szName)
            if cfg.showPnlUsd
                r := f_tbl(panel, r, "1:3 Profit $", f_usd_txt(this.long_tp3_profit), f_usd_txt(this.short_tp3_profit), f_usd_txt(this.long_tp3_profit + this.short_tp3_profit), yel, grn, red, wht, szName)
                r := f_tbl(panel, r, "1:3 Loss $", f_usd_txt(-this.long_tp3_loss), f_usd_txt(-this.short_tp3_loss), f_usd_txt(-(this.long_tp3_loss + this.short_tp3_loss)), yel, red, red, red, szName)
                r := f_tbl(panel, r, "1:3 P/L $", f_usd_txt(lTp3Net), f_usd_txt(sTp3Net), f_usd_txt(tTp3Net), yel, f_pnl_col(lTp3Net), f_pnl_col(sTp3Net), f_pnl_col(tTp3Net), szName)
            if cfg.showPf
                r := f_tbl(panel, r, "1:3 PF", f_pf(this.long_tp3_profit, this.long_tp3_loss), f_pf(this.short_tp3_profit, this.short_tp3_loss), f_pf(this.long_tp3_profit + this.short_tp3_profit, this.long_tp3_loss + this.short_tp3_loss), blu, grn, red, wht, szName)
            if cfg.showExpect
                string lExp3 = lExpN == 0 ? "-" : f_usd_txt(lTp3Net / lExpN)
                string sExp3 = sExpN == 0 ? "-" : f_usd_txt(sTp3Net / sExpN)
                string tExp3 = tExpN == 0 ? "-" : f_usd_txt(tTp3Net / tExpN)
                r := f_tbl(panel, r, "1:3 Exp $", lExp3, sExp3, tExp3, blu, f_pnl_col(lTp3Net), f_pnl_col(sTp3Net), f_pnl_col(tTp3Net), szName)
        if cfg.showStatic
            r := f_tbl(panel, r, staticLbl, str.tostring(this.long_static), str.tostring(this.short_static), str.tostring(this.long_static + this.short_static), yel, grn, red, wht, szName)
            r := f_tbl(panel, r, "Static L", str.tostring(this.long_static_loss), str.tostring(this.short_static_loss), str.tostring(this.long_static_loss + this.short_static_loss), yel, grn, red, wht, szName)
            r := f_tbl(panel, r, "Static %", f_pct(this.long_static, lStaticN), f_pct(this.short_static, sStaticN), f_pct(this.long_static + this.short_static, tStaticN), yel, grn, red, wht, szName)
            if cfg.showPnlUsd
                r := f_tbl(panel, r, "Static $", f_usd_txt(this.long_static_pnl), f_usd_txt(this.short_static_pnl), f_usd_txt(tStaticPnl), yel, f_pnl_col(this.long_static_pnl), f_pnl_col(this.short_static_pnl), f_pnl_col(tStaticPnl), szName)
        if cfg.showAvgPips
            r := f_tbl(panel, r, "Avg Pips", f_avg(this.long_mfe_sum, lDone), f_avg(this.short_mfe_sum, sDone), f_avg(this.long_mfe_sum + this.short_mfe_sum, tDone), yel, grn, red, wht, szName)
        if cfg.showExpired
            r := f_tbl(panel, r, "Expired", str.tostring(this.long_expired), str.tostring(this.short_expired), str.tostring(this.long_expired + this.short_expired), gry, grn, red, wht, szName)
        if cfg.showSkipped
            r := f_tbl(panel, r, "Skipped", str.tostring(this.long_skipped), str.tostring(this.short_skipped), str.tostring(this.long_skipped + this.short_skipped), gry, grn, red, wht, szName)
    else if barstate.islast and not na(panel)
        bool hideTable = na(cfg)
        if not na(cfg)
            hideTable := not cfg.showTable
        if hideTable
            table.delete(panel)
            panel := na
            rowsUsed := 0
    this

← 返回列表