SAAD FX ORB PRO v2.5

SAAD_FX · study · 534 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("SAAD FX ORB PRO v2.5", shorttitle="SAAD FX ORB", overlay=true, max_labels_count=500)
 
// ============================================================
// SAAD FX ORB PRO v2.5
// COMPLETE DAILY HISTORY + SESSION DIAGNOSTICS
// ============================================================
 
// ================= INPUTS =================
 
gA = "01 - ASIA SESSION"
enableAsia = input.bool(true, "Enable Asia", group=gA)
asiaORB = input.session("2000-2030", "Opening Range - NY Time", group=gA)
asiaTrade = input.session("2030-0300", "Trading Window - NY Time", group=gA)
asiaColor = input.color(color.aqua, "Range Color", group=gA)
 
gE = "02 - EUROPE SESSION"
enableEurope = input.bool(true, "Enable Europe", group=gE)
europeORB = input.session("0300-0330", "Opening Range - NY Time", group=gE)
europeTrade = input.session("0330-0930", "Trading Window - NY Time", group=gE)
europeColor = input.color(color.orange, "Range Color", group=gE)
 
gN = "03 - NEW YORK SESSION"
enableNY = input.bool(true, "Enable New York", group=gN)
nyORB = input.session("0930-1000", "Opening Range - NY Time", group=gN)
nyTrade = input.session("1000-1600", "Trading Window - NY Time", group=gN)
nyColor = input.color(color.yellow, "Range Color", group=gN)
 
gS = "04 - SIGNAL SETTINGS"
direction = input.string("Both", "Trade Direction", options=["Both", "BUY Only", "SELL Only"], group=gS)
breakoutMode = input.string("First Confirmed Close Outside", "Breakout Mode", options=["First Confirmed Close Outside", "Fresh Crossing Only"], group=gS)
buffer = input.float(0, "Breakout Buffer - Points", minval=0, step=1, group=gS)
useBody = input.bool(true, "Require Strong Candle", group=gS)
minBody = input.float(50, "Minimum Candle Body %", minval=0, maxval=100, group=gS)
 
gR = "05 - TP / SL"
tpPoints = input.float(50, "Take Profit - Index Points", minval=0.01, step=1, group=gR)
dollarPP = input.float(1, "Dollar Value Per Point", minval=0.0001, step=0.1, group=gR)
showTradeLevels = input.bool(true, "Show Active TP / SL", group=gR)
showPnL = input.bool(true, "Show Closed Trade P&L", group=gR)
closeAtWindowEnd = input.bool(false, "Close At Session Window End", group=gR)
 
gV = "06 - VISUAL SETTINGS"
showORB = input.bool(true, "Show ORB Lines", group=gV)
showFill = input.bool(true, "Show ORB Fill", group=gV)
showSignals = input.bool(true, "Show BUY / SELL Labels", group=gV)
showExitMarkers = input.bool(true, "Show Exit Markers", group=gV)
 
gD = "07 - DASHBOARDS"
showMain = input.bool(true, "Show Main Dashboard", group=gD)
mainPosition = input.string("Top Right", "Main Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group=gD)
showHistory = input.bool(true, "Show SAAD FX Daily History", group=gD)
historyPosition = input.string("Bottom Left", "History Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group=gD)
historyPeriod = input.int(90, "Performance Period - Trading Days", options=[10, 30, 60, 90, 180, 365], group=gD)
historyRows = input.int(10, "Visible Daily History Rows", minval=1, maxval=30, group=gD)
tableSizeInput = input.string("Normal", "Table Text Size", options=["Tiny", "Small", "Normal", "Large"], group=gD)
 
// ================= HELPERS =================
 
string tz = "America/New_York"
 
f_position(string p) =>
    switch p
        "Top Right" => position.top_right
        "Top Left" => position.top_left
        "Bottom Right" => position.bottom_right
        "Bottom Left" => position.bottom_left
        "Middle Right" => position.middle_right
        "Middle Left" => position.middle_left
        => position.top_right
 
f_size(string s) =>
    switch s
        "Tiny" => size.tiny
        "Small" => size.small
        "Large" => size.large
        => size.normal
 
f_money(float v) =>
    (v < 0 ? "-$" : "$") + str.tostring(math.abs(v), "#,##0.00")
 
f_date(int k) =>
    int mm = int(math.floor(k % 10000 / 100))
    int dd = k % 100
    str.tostring(mm, "00") + "/" + str.tostring(dd, "00")
 
f_name(int id) =>
    switch id
        0 => "ASIA"
        1 => "EUROPE"
        => "NEW YORK"
 
// ================= TRADING DAY =================
// 18:00 NY to 17:59 NY belongs to the next
// trading date. Evening Asia is grouped with
// the following Europe and New York sessions.
 
int nyHour = hour(time, tz)
int tradingKey = na
 
if nyHour >= 18
    int tomorrow = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz) + 1, 12, 0)
    tradingKey := year(tomorrow, tz) * 10000 + month(tomorrow, tz) * 100 + dayofmonth(tomorrow, tz)
else
    tradingKey := year(time, tz) * 10000 + month(time, tz) * 100 + dayofmonth(time, tz)
 
// ================= SESSION DETECTION =================
 
inAORB = enableAsia and not na(time(timeframe.period, asiaORB, tz))
inEORB = enableEurope and not na(time(timeframe.period, europeORB, tz))
inNORB = enableNY and not na(time(timeframe.period, nyORB, tz))
 
inATrade = enableAsia and not na(time(timeframe.period, asiaTrade, tz))
inETrade = enableEurope and not na(time(timeframe.period, europeTrade, tz))
inNTrade = enableNY and not na(time(timeframe.period, nyTrade, tz))
 
// ================= HISTORY ARRAYS =================
 
var array<int> dayKeys = array.new_int()
var array<int> dayTrades = array.new_int()
var array<float> dayPnL = array.new_float()
var array<int> dayWins = array.new_int()
var array<int> dayLosses = array.new_int()
var array<float> dayAsia = array.new_float()
var array<float> dayEurope = array.new_float()
var array<float> dayNY = array.new_float()
 
// Count entries by session, independently of exits.
var array<int> dayAsiaEntries = array.new_int()
var array<int> dayEuropeEntries = array.new_int()
var array<int> dayNYEntries = array.new_int()
 
f_ensure_day(int key) =>
    int idx = array.indexof(dayKeys, key)
    if idx == -1
        array.push(dayKeys, key)
        array.push(dayTrades, 0)
        array.push(dayPnL, 0.0)
        array.push(dayWins, 0)
        array.push(dayLosses, 0)
        array.push(dayAsia, 0.0)
        array.push(dayEurope, 0.0)
        array.push(dayNY, 0.0)
        array.push(dayAsiaEntries, 0)
        array.push(dayEuropeEntries, 0)
        array.push(dayNYEntries, 0)
        idx := array.size(dayKeys) - 1
    idx
 
// Register each observed weekday even when
// no trade closes on that day.
// This does not fabricate missing market data.
 
int dow = dayofweek(time, tz)
bool weekday = dow >= dayofweek.monday and dow <= dayofweek.friday
 
if weekday or inAORB or inEORB or inNORB
    f_ensure_day(tradingKey)
 
// ================= SESSION STATE =================
 
var array<float> rangeHigh = array.new_float(3, na)
var array<float> rangeLow = array.new_float(3, na)
var array<bool> rangeReady = array.new_bool(3, false)
var array<bool> sessionTaken = array.new_bool(3, false)
var array<bool> sessionClosed = array.new_bool(3, false)
 
var array<int> tradeDir = array.new_int(3, 0)
var array<float> entryPrice = array.new_float(3, na)
var array<float> takeProfit = array.new_float(3, na)
var array<float> stopLine = array.new_float(3, na)
var array<int> entryBar = array.new_int(3, na)
var array<int> entryDay = array.new_int(3, na)
 
var float totalPnL = 0.0
var int closedTrades = 0
var int wins = 0
var int losses = 0
 
// ================= BUILD OPENING RANGES =================
 
for s = 0 to 2
    bool inRange = s == 0 ? inAORB : s == 1 ? inEORB : inNORB
    bool wasRange = s == 0 ? inAORB[1] : s == 1 ? inEORB[1] : inNORB[1]
 
    bool rangeStart = inRange and not wasRange
    bool rangeEnd = not inRange and wasRange
 
    if rangeStart
        array.set(rangeHigh, s, high)
        array.set(rangeLow, s, low)
        array.set(rangeReady, s, false)
        array.set(sessionTaken, s, false)
        array.set(sessionClosed, s, false)
 
    if inRange
        array.set(rangeHigh, s, math.max(nz(array.get(rangeHigh, s), high), high))
        array.set(rangeLow, s, math.min(nz(array.get(rangeLow, s), low), low))
 
    if rangeEnd and not na(array.get(rangeHigh, s))
        array.set(rangeReady, s, true)
 
// ================= CONFIRMATION =================
 
float candleRange = high - low
float bodyPct = candleRange > 0 ? math.abs(close - open) / candleRange * 100 : 0
 
bool bullOK = close > open and (not useBody or bodyPct >= minBody)
bool bearOK = close < open and (not useBody or bodyPct >= minBody)
 
bool buyAllowed = direction != "SELL Only"
bool sellAllowed = direction != "BUY Only"
 
// ================= ALERT FLAGS =================
 
bool asiaBuyAlert = false
bool asiaSellAlert = false
bool europeBuyAlert = false
bool europeSellAlert = false
bool nyBuyAlert = false
bool nySellAlert = false
bool anyTP = false
bool anySL = false
bool anySessionExit = false
 
// ================= INDEPENDENT TRADE ENGINE =================
 
if barstate.isconfirmed
    for s = 0 to 2
        int dir = array.get(tradeDir, s)
        float ep = array.get(entryPrice, s)
        float tp = array.get(takeProfit, s)
        float sl = array.get(stopLine, s)
        int eb = array.get(entryBar, s)
 
        bool inWindow = s == 0 ? inATrade : s == 1 ? inETrade : inNTrade
        bool prevWindow = s == 0 ? inATrade[1] : s == 1 ? inETrade[1] : inNTrade[1]
 
        // ============== EXIT ==============
 
        if dir != 0 and bar_index > eb
            bool hitTP = dir == 1 ? high >= tp : low <= tp
            bool hitSL = dir == 1 ? close < sl : close > sl
            bool sessionExit = closeAtWindowEnd and not inWindow and prevWindow
 
            if hitTP or hitSL or sessionExit
                bool exitSL = hitSL
                bool exitTP = not exitSL and hitTP
                float exitPrice = exitTP ? tp : close
                float pnl = (exitPrice - ep) * dir * dollarPP
 
                totalPnL += pnl
                closedTrades += 1
 
                if pnl > 0
                    wins += 1
                else if pnl < 0
                    losses += 1
 
                int idx = f_ensure_day(array.get(entryDay, s))
 
                array.set(dayTrades, idx, array.get(dayTrades, idx) + 1)
                array.set(dayPnL, idx, array.get(dayPnL, idx) + pnl)
 
                if pnl > 0
                    array.set(dayWins, idx, array.get(dayWins, idx) + 1)
                else if pnl < 0
                    array.set(dayLosses, idx, array.get(dayLosses, idx) + 1)
 
                if s == 0
                    array.set(dayAsia, idx, array.get(dayAsia, idx) + pnl)
                else if s == 1
                    array.set(dayEurope, idx, array.get(dayEurope, idx) + pnl)
                else
                    array.set(dayNY, idx, array.get(dayNY, idx) + pnl)
 
                anyTP := anyTP or exitTP
                anySL := anySL or exitSL
                anySessionExit := anySessionExit or (not exitTP and not exitSL)
 
                if showPnL or showExitMarkers
                    string reason = exitSL ? "SL" : exitTP ? "TP" : "SESSION"
                    string labelText = f_name(s) + " " + reason + (showPnL ? "\n" + f_money(pnl) : "")
                    label.new(bar_index, exitPrice, labelText, style=dir == 1 ? label.style_label_down : label.style_label_up, color=pnl >= 0 ? color.green : color.red, textcolor=color.white, size=size.small)
 
                array.set(tradeDir, s, 0)
                array.set(entryPrice, s, na)
                array.set(takeProfit, s, na)
                array.set(stopLine, s, na)
                array.set(entryBar, s, na)
                array.set(entryDay, s, na)
                array.set(sessionClosed, s, true)
 
        // ============== ENTRY ==============
 
        if array.get(tradeDir, s) == 0 and not array.get(sessionTaken, s)
            float h = array.get(rangeHigh, s)
            float l = array.get(rangeLow, s)
            bool ready = array.get(rangeReady, s)
 
            if ready and inWindow and not na(h) and not na(l)
                float buyLevel = h + buffer
                float sellLevel = l - buffer
 
                bool buyCross = close > buyLevel and close[1] <= buyLevel
                bool sellCross = close < sellLevel and close[1] >= sellLevel
 
                bool buyBreak = breakoutMode == "Fresh Crossing Only" ? buyCross : close > buyLevel
                bool sellBreak = breakoutMode == "Fresh Crossing Only" ? sellCross : close < sellLevel
 
                bool buySetup = buyAllowed and bullOK and buyBreak
                bool sellSetup = sellAllowed and bearOK and sellBreak
 
                int newDir = buySetup ? 1 : sellSetup ? -1 : 0
 
                if newDir != 0
                    array.set(tradeDir, s, newDir)
                    array.set(entryPrice, s, close)
                    array.set(takeProfit, s, newDir == 1 ? close + tpPoints : close - tpPoints)
                    array.set(stopLine, s, newDir == 1 ? l : h)
                    array.set(entryBar, s, bar_index)
                    array.set(entryDay, s, tradingKey)
                    array.set(sessionTaken, s, true)
                    array.set(sessionClosed, s, false)
 
                    int idx = f_ensure_day(tradingKey)
                    if s == 0
                        array.set(dayAsiaEntries, idx, array.get(dayAsiaEntries, idx) + 1)
                        asiaBuyAlert := newDir == 1
                        asiaSellAlert := newDir == -1
                    else if s == 1
                        array.set(dayEuropeEntries, idx, array.get(dayEuropeEntries, idx) + 1)
                        europeBuyAlert := newDir == 1
                        europeSellAlert := newDir == -1
                    else
                        array.set(dayNYEntries, idx, array.get(dayNYEntries, idx) + 1)
                        nyBuyAlert := newDir == 1
                        nySellAlert := newDir == -1
 
                    if showSignals
                        label.new(bar_index, newDir == 1 ? low : high, f_name(s) + "\n" + (newDir == 1 ? "BUY" : "SELL"), style=newDir == 1 ? label.style_label_up : label.style_label_down, color=newDir == 1 ? color.green : color.red, textcolor=color.white, size=size.normal)
 
// ================= ORB PLOTS =================
 
aH = plot(showORB and enableAsia and (inAORB or inATrade) ? array.get(rangeHigh, 0) : na, "ASIA HIGH", color=asiaColor, linewidth=2, style=plot.style_linebr)
aL = plot(showORB and enableAsia and (inAORB or inATrade) ? array.get(rangeLow, 0) : na, "ASIA LOW", color=asiaColor, linewidth=2, style=plot.style_linebr)
 
eH = plot(showORB and enableEurope and (inEORB or inETrade) ? array.get(rangeHigh, 1) : na, "EUROPE HIGH", color=europeColor, linewidth=2, style=plot.style_linebr)
eL = plot(showORB and enableEurope and (inEORB or inETrade) ? array.get(rangeLow, 1) : na, "EUROPE LOW", color=europeColor, linewidth=2, style=plot.style_linebr)
 
nH = plot(showORB and enableNY and (inNORB or inNTrade) ? array.get(rangeHigh, 2) : na, "NY HIGH", color=nyColor, linewidth=2, style=plot.style_linebr)
nL = plot(showORB and enableNY and (inNORB or inNTrade) ? array.get(rangeLow, 2) : na, "NY LOW", color=nyColor, linewidth=2, style=plot.style_linebr)
 
fill(aH, aL, color=showFill ? color.new(asiaColor, 92) : na)
fill(eH, eL, color=showFill ? color.new(europeColor, 92) : na)
fill(nH, nL, color=showFill ? color.new(nyColor, 92) : na)
 
plot(showTradeLevels and array.get(tradeDir, 0) != 0 ? array.get(takeProfit, 0) : na, "ASIA TP", color=color.lime, linewidth=2, style=plot.style_linebr)
plot(showTradeLevels and array.get(tradeDir, 0) != 0 ? array.get(stopLine, 0) : na, "ASIA SL", color=color.red, linewidth=2, style=plot.style_linebr)
 
plot(showTradeLevels and array.get(tradeDir, 1) != 0 ? array.get(takeProfit, 1) : na, "EUROPE TP", color=color.lime, linewidth=2, style=plot.style_linebr)
plot(showTradeLevels and array.get(tradeDir, 1) != 0 ? array.get(stopLine, 1) : na, "EUROPE SL", color=color.red, linewidth=2, style=plot.style_linebr)
 
plot(showTradeLevels and array.get(tradeDir, 2) != 0 ? array.get(takeProfit, 2) : na, "NY TP", color=color.lime, linewidth=2, style=plot.style_linebr)
plot(showTradeLevels and array.get(tradeDir, 2) != 0 ? array.get(stopLine, 2) : na, "NY SL", color=color.red, linewidth=2, style=plot.style_linebr)
 
// ================= ALERTS =================
 
alertcondition(asiaBuyAlert, "ASIA BUY", "SAAD FX ORB ASIA BUY {{ticker}}")
alertcondition(asiaSellAlert, "ASIA SELL", "SAAD FX ORB ASIA SELL {{ticker}}")
alertcondition(europeBuyAlert, "EUROPE BUY", "SAAD FX ORB EUROPE BUY {{ticker}}")
alertcondition(europeSellAlert, "EUROPE SELL", "SAAD FX ORB EUROPE SELL {{ticker}}")
alertcondition(nyBuyAlert, "NEW YORK BUY", "SAAD FX ORB NEW YORK BUY {{ticker}}")
alertcondition(nySellAlert, "NEW YORK SELL", "SAAD FX ORB NEW YORK SELL {{ticker}}")
alertcondition(anyTP, "ANY SESSION TP", "SAAD FX ORB Take Profit {{ticker}}")
alertcondition(anySL, "ANY SESSION SL", "SAAD FX ORB Stop Loss {{ticker}}")
alertcondition(anySessionExit, "ANY SESSION EXIT", "SAAD FX ORB Session Exit {{ticker}}")
 
// ================= MAIN DASHBOARD =================
 
textSize = f_size(tableSizeInput)
headerBG = color.rgb(25, 55, 90)
rowBG = color.rgb(20, 24, 32)
 
var table mainTable = table.new(f_position(mainPosition), 2, 18, bgcolor=color.black, frame_color=color.gray, frame_width=1)
 
f_row(int r, string name, string value, color valueColor) =>
    table.cell(mainTable, 0, r, name, text_color=color.white, bgcolor=rowBG, text_size=textSize)
    table.cell(mainTable, 1, r, value, text_color=valueColor, bgcolor=rowBG, text_size=textSize)
 
if barstate.islast
    table.clear(mainTable, 0, 0, 1, 17)
 
    if showMain
        int idx = array.indexof(dayKeys, tradingKey)
        float aPnl = idx >= 0 ? array.get(dayAsia, idx) : 0.0
        float ePnl = idx >= 0 ? array.get(dayEurope, idx) : 0.0
        float nPnl = idx >= 0 ? array.get(dayNY, idx) : 0.0
        float todayPnL = idx >= 0 ? array.get(dayPnL, idx) : 0.0
 
        float livePnL = 0.0
        int activeCount = 0
        int takenCount = 0
 
        for s = 0 to 2
            int dir = array.get(tradeDir, s)
            if dir != 0
                activeCount += 1
                livePnL += (close - array.get(entryPrice, s)) * dir * dollarPP
            if array.get(sessionTaken, s)
                takenCount += 1
 
        float winRate = closedTrades > 0 ? wins * 100.0 / closedTrades : 0.0
 
        table.cell(mainTable, 0, 0, "SAAD FX", text_color=color.white, bgcolor=headerBG, text_size=textSize)
        table.cell(mainTable, 1, 0, "ORB PRO v2.5", text_color=color.yellow, bgcolor=headerBG, text_size=textSize)
 
        f_row(1, "TIMEFRAME", timeframe.period, color.aqua)
 
        for s = 0 to 2
            bool enabled = s == 0 ? enableAsia : s == 1 ? enableEurope : enableNY
            bool inRange = s == 0 ? inAORB : s == 1 ? inEORB : inNORB
            bool inWindow = s == 0 ? inATrade : s == 1 ? inETrade : inNTrade
 
            bool ready = array.get(rangeReady, s)
            bool closed = array.get(sessionClosed, s)
            int dir = array.get(tradeDir, s)
            float h = array.get(rangeHigh, s)
            float l = array.get(rangeLow, s)
 
            bool outside = ready and not na(h) and not na(l) and (close > h + buffer or close < l - buffer)
 
            string status = not enabled ? "OFF" : dir != 0 ? "ACTIVE" : closed ? "CLOSED" : inRange ? "BUILDING" : not ready ? "WAITING" : not inWindow ? "WAITING" : outside ? "FILTERED" : "NO BREAKOUT"
 
            float sessionPnl = s == 0 ? aPnl : s == 1 ? ePnl : nPnl
            color sessionColor = s == 0 ? asiaColor : s == 1 ? europeColor : nyColor
 
            int rowStatus = s == 0 ? 2 : s == 1 ? 4 : 6
            int rowPnl = rowStatus + 1
 
            f_row(rowStatus, f_name(s), status, sessionColor)
            f_row(rowPnl, f_name(s) + " P&L", f_money(sessionPnl), sessionPnl >= 0 ? color.lime : color.red)
 
        f_row(8, "SESSION SIGNALS", str.tostring(takenCount) + " / 3", color.aqua)
        f_row(9, "ACTIVE TRADES", str.tostring(activeCount), color.yellow)
        f_row(10, "CURRENT P&L", f_money(livePnL), livePnL >= 0 ? color.lime : color.red)
        f_row(11, "TAKE PROFIT", str.tostring(tpPoints, "#.##") + " pts", color.lime)
        f_row(12, "STOP LOSS", "OPPOSITE ORB CLOSE", color.red)
        f_row(13, "TODAY P&L", f_money(todayPnL), todayPnL >= 0 ? color.lime : color.red)
        f_row(14, "CLOSED TRADES", str.tostring(closedTrades), color.white)
        f_row(15, "WINS / LOSSES", str.tostring(wins) + " / " + str.tostring(losses), color.white)
        f_row(16, "WIN RATE", str.tostring(winRate, "#.0") + "%", winRate >= 50 ? color.lime : color.red)
        f_row(17, "TOTAL P&L", f_money(totalPnL), totalPnL >= 0 ? color.lime : color.red)
 
// ================= EXTENDED DAILY HISTORY =================
 
var table historyTable = table.new(f_position(historyPosition), 3, 35, bgcolor=color.black, frame_color=color.gray, frame_width=1)
 
if barstate.islast
    table.clear(historyTable, 0, 0, 2, 34)
 
    if showHistory
        table.cell(historyTable, 0, 0, "SAAD FX", text_color=color.white, bgcolor=headerBG, text_size=textSize)
        table.cell(historyTable, 1, 0, "DAILY", text_color=color.yellow, bgcolor=headerBG, text_size=textSize)
        table.cell(historyTable, 2, 0, "HISTORY", text_color=color.white, bgcolor=headerBG, text_size=textSize)
 
        table.cell(historyTable, 0, 1, "NY DATE", text_color=color.aqua, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 1, 1, "TRADES", text_color=color.aqua, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 2, 1, "DAILY P&L", text_color=color.aqua, bgcolor=rowBG, text_size=textSize)
 
        int availableDays = array.size(dayKeys)
        int periodCount = math.min(historyPeriod, availableDays)
        int visibleCount = math.min(historyRows, periodCount)
 
        float periodPnL = 0.0
        int periodTrades = 0
        int periodWins = 0
        int periodLosses = 0
        float bestDay = na
        float worstDay = na
 
        if periodCount > 0
            for i = 0 to periodCount - 1
                int idx = availableDays - 1 - i
                float p = array.get(dayPnL, idx)
 
                periodPnL += p
                periodTrades += array.get(dayTrades, idx)
                periodWins += array.get(dayWins, idx)
                periodLosses += array.get(dayLosses, idx)
 
                bestDay := na(bestDay) ? p : math.max(bestDay, p)
                worstDay := na(worstDay) ? p : math.min(worstDay, p)
 
        if visibleCount > 0
            for i = 0 to visibleCount - 1
                int idx = availableDays - 1 - i
                int k = array.get(dayKeys, idx)
                int n = array.get(dayTrades, idx)
                float p = array.get(dayPnL, idx)
 
                int entries = array.get(dayAsiaEntries, idx) + array.get(dayEuropeEntries, idx) + array.get(dayNYEntries, idx)
 
                string dateText = f_date(k)
                string tradesText = str.tostring(n)
 
                // A dot identifies a day with entries but no closed trades.
                if n == 0 and entries > 0
                    tradesText := "0*"
 
                color bg = i % 2 == 0 ? color.rgb(22, 26, 35) : color.rgb(14, 18, 25)
 
                table.cell(historyTable, 0, i + 2, dateText, text_color=color.white, bgcolor=bg, text_size=textSize)
                table.cell(historyTable, 1, i + 2, tradesText, text_color=color.white, bgcolor=bg, text_size=textSize)
                table.cell(historyTable, 2, i + 2, f_money(p), text_color=p >= 0 ? color.lime : color.red, bgcolor=bg, text_size=textSize)
 
        int totalRow = visibleCount + 2
        float periodWinRate = periodTrades > 0 ? periodWins * 100.0 / periodTrades : 0.0
 
        table.cell(historyTable, 0, totalRow, "PERIOD TOTAL", text_color=color.yellow, bgcolor=headerBG, text_size=textSize)
        table.cell(historyTable, 1, totalRow, str.tostring(periodTrades), text_color=color.white, bgcolor=headerBG, text_size=textSize)
        table.cell(historyTable, 2, totalRow, f_money(periodPnL), text_color=periodPnL >= 0 ? color.lime : color.red, bgcolor=headerBG, text_size=textSize)
 
        table.cell(historyTable, 0, totalRow + 1, "PERIOD", text_color=color.white, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 1, totalRow + 1, str.tostring(periodCount) + " days", text_color=color.aqua, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 2, totalRow + 1, str.tostring(periodWinRate, "#.0") + "% win", text_color=periodWinRate >= 50 ? color.lime : color.red, bgcolor=rowBG, text_size=textSize)
 
        table.cell(historyTable, 0, totalRow + 2, "WINS / LOSS", text_color=color.white, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 1, totalRow + 2, str.tostring(periodWins), text_color=color.lime, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 2, totalRow + 2, str.tostring(periodLosses), text_color=color.red, bgcolor=rowBG, text_size=textSize)
 
        table.cell(historyTable, 0, totalRow + 3, "BEST / WORST", text_color=color.white, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 1, totalRow + 3, na(bestDay) ? "--" : f_money(bestDay), text_color=color.lime, bgcolor=rowBG, text_size=textSize)
        table.cell(historyTable, 2, totalRow + 3, na(worstDay) ? "--" : f_money(worstDay), text_color=color.red, bgcolor=rowBG, text_size=textSize)

← 返回列表