本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FX Pip Stats Dashboard", overlay = true, dynamic_requests = true)
// ============ INPUTS ============
// ---- Pairs (each enabled pair uses one data request per timeframe, plus one for the weekday table; TradingView allows 40 in total)
u1 = input.bool(true, "", inline = "p1", group = "Pairs")
pr1 = input.symbol("FX:EURUSD", "Pair 1", inline = "p1", group = "Pairs",
tooltip = "Tick the box to include a pair and pick any symbol. Each enabled pair needs (number of timeframes shown + 1) data requests, and TradingView allows 40 in total. 8 pairs with 4 timeframes uses exactly 40.")
u2 = input.bool(true, "", inline = "p2", group = "Pairs")
pr2 = input.symbol("FX:GBPUSD", "Pair 2", inline = "p2", group = "Pairs")
u3 = input.bool(true, "", inline = "p3", group = "Pairs")
pr3 = input.symbol("FX:USDJPY", "Pair 3", inline = "p3", group = "Pairs")
u4 = input.bool(true, "", inline = "p4", group = "Pairs")
pr4 = input.symbol("FX:AUDUSD", "Pair 4", inline = "p4", group = "Pairs")
u5 = input.bool(true, "", inline = "p5", group = "Pairs")
pr5 = input.symbol("FX:USDCAD", "Pair 5", inline = "p5", group = "Pairs")
u6 = input.bool(true, "", inline = "p6", group = "Pairs")
pr6 = input.symbol("FX:USDCHF", "Pair 6", inline = "p6", group = "Pairs")
u7 = input.bool(true, "", inline = "p7", group = "Pairs")
pr7 = input.symbol("FX:NZDUSD", "Pair 7", inline = "p7", group = "Pairs")
u8 = input.bool(false, "", inline = "p8", group = "Pairs")
pr8 = input.symbol("FX:GBPJPY", "Pair 8", inline = "p8", group = "Pairs")
// ---- Timeframes
tfOn1 = input.bool(true, "", inline = "t1", group = "Timeframes")
tf1 = input.timeframe("D", "Timeframe 1", inline = "t1", group = "Timeframes")
n1 = input.int(20, "Candles", minval = 2, maxval = 500, inline = "t1", group = "Timeframes",
tooltip = "How many completed candles are averaged for each timeframe.")
tfOn2 = input.bool(true, "", inline = "t2", group = "Timeframes")
tf2 = input.timeframe("240", "Timeframe 2", inline = "t2", group = "Timeframes")
n2 = input.int(60, "Candles", minval = 2, maxval = 500, inline = "t2", group = "Timeframes")
tfOn3 = input.bool(true, "", inline = "t3", group = "Timeframes")
tf3 = input.timeframe("60", "Timeframe 3", inline = "t3", group = "Timeframes")
n3 = input.int(120, "Candles", minval = 2, maxval = 500, inline = "t3", group = "Timeframes")
tfOn4 = input.bool(true, "", inline = "t4", group = "Timeframes")
tf4 = input.timeframe("15", "Timeframe 4", inline = "t4", group = "Timeframes")
n4 = input.int(200, "Candles", minval = 2, maxval = 500, inline = "t4", group = "Timeframes")
// ---- Statistics
avgType = input.string("Mean", "Average Type", options = ["Mean", "Median"], group = "Statistics",
tooltip = "Median ignores outliers such as news spikes. It applies to the range and to 'Open to high / low'. The 'Candle body' and 'Candle range' options always use the mean.")
upDownMode = input.string("Open to high / low", "Up / Down Pips Measured As", options = ["Open to high / low", "Candle body", "Candle range"], group = "Statistics",
tooltip = "Open to high / low: up = high minus open and down = open minus low, for every candle. Candle body: up = open to close of the candles that closed up, down = the same for candles that closed down. Candle range: the high-to-low range of up candles and of down candles.")
inclCur = input.bool(false, "Include The Current (Unfinished) Candle In The Averages", group = "Statistics")
pipMult = input.float(10.0, "Pip = Minimum Tick x", minval = 0.1, step = 1.0, group = "Statistics",
tooltip = "10 suits 5-digit forex pairs and 3-digit JPY pairs. Use 1 if your feed already quotes a pip as the last digit. Applies to every pair.")
// ---- Weekday table
wkMetric = input.string("Range", "Weekday Table Measures", options = ["Range", "Up (open to high)", "Down (open to low)"], group = "Weekday Table")
nWk = input.int(130, "Daily Candles To Use", minval = 20, maxval = 1000, group = "Weekday Table",
tooltip = "130 daily candles is about 26 weeks, so roughly 26 samples per weekday.")
minWk = input.int(8, "Minimum Samples Per Weekday", minval = 1, group = "Weekday Table")
tzIn = input.string("America/New_York", "Day Is The Day The Daily Candle Closes In", options = ["America/New_York", "America/Chicago", "America/Los_Angeles", "Europe/London", "Europe/Berlin", "Europe/Zurich", "UTC", "Chart (exchange)"], group = "Weekday Table",
tooltip = "Daily forex candles close in the late afternoon in New York, so a candle is labelled with the weekday it closes on. Keep this in the Americas or Europe, otherwise Monday's candle can be labelled Tuesday.")
// ---- Dashboard
showDash = input.bool(true, "Show Dashboard", group = "Dashboard")
dashPosIn = input.string("Top Right", "Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group = "Dashboard")
dashSizeIn = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal"], group = "Dashboard")
showTfT = input.bool(true, "Table 1: Average Pips By Timeframe", group = "Dashboard")
metricSel = input.string("Average range", "Table 1 Shows", options = ["Average range", "Average up", "Average down", "Up candles %"], group = "Dashboard")
hiLead = input.bool(true, "Highlight The Largest Pair In Each Column", group = "Dashboard")
showDetailT = input.bool(true, "Table 2: Range, Up, Down For One Timeframe", group = "Dashboard")
focusSlot = input.int(1, "Table 2 Timeframe (slot 1 to 4)", minval = 1, maxval = 4, group = "Dashboard")
showWeekT = input.bool(true, "Table 3: Busiest Weekdays", group = "Dashboard")
// ============ SETUP ============
dashPos = dashPosIn == "Top Right" ? position.top_right :
dashPosIn == "Top Left" ? position.top_left :
dashPosIn == "Bottom Right" ? position.bottom_right :
dashPosIn == "Bottom Left" ? position.bottom_left :
dashPosIn == "Middle Right" ? position.middle_right : position.middle_left
dashSize = dashSizeIn == "Tiny" ? size.tiny : dashSizeIn == "Normal" ? size.normal : size.small
tzName = tzIn == "Chart (exchange)" ? syminfo.timezone : tzIn
var array<string> syms = array.from(pr1, pr2, pr3, pr4, pr5, pr6, pr7, pr8)
var array<bool> uses = array.from(u1, u2, u3, u4, u5, u6, u7, u8)
// Stored results. Timeframe stats: index = (pair * 4 + slot) * 5 + metric (0 range, 1 up, 2 down, 3 up %, 4 current candle range)
var array<float> st = array.new<float>(8 * 4 * 5, na)
// Weekday stats: index = (pair * 5 + day) * 2 + (0 average, 1 samples), day 0 = Monday
var array<float> wk = array.new<float>(8 * 5 * 2, na)
// ============ DATA FUNCTIONS (evaluated on each pair's own candles) ============
f_stats(n) =>
float pip = syminfo.mintick * pipMult
float rng = (high - low) / pip
float upx = (high - open) / pip
float dnx = (open - low) / pip
float body = math.abs(close - open) / pip
bool bull = close > open
bool bear = close < open
float avgR = ta.sma(rng, n)
float medR = ta.median(rng, n)
float avgU = ta.sma(upx, n)
float medU = ta.median(upx, n)
float avgD = ta.sma(dnx, n)
float medD = ta.median(dnx, n)
float cBull = math.sum(bull ? 1.0 : 0.0, n)
float cBear = math.sum(bear ? 1.0 : 0.0, n)
float sBodyU = math.sum(bull ? body : 0.0, n)
float sBodyD = math.sum(bear ? body : 0.0, n)
float sRngU = math.sum(bull ? rng : 0.0, n)
float sRngD = math.sum(bear ? rng : 0.0, n)
float upPct = ta.sma(bull ? 100.0 : 0.0, n)
float r = avgType == "Median" ? medR : avgR
float uu = upDownMode == "Candle body" ? (cBull > 0 ? sBodyU / cBull : na) : upDownMode == "Candle range" ? (cBull > 0 ? sRngU / cBull : na) : (avgType == "Median" ? medU : avgU)
float dd = upDownMode == "Candle body" ? (cBear > 0 ? sBodyD / cBear : na) : upDownMode == "Candle range" ? (cBear > 0 ? sRngD / cBear : na) : (avgType == "Median" ? medD : avgD)
[inclCur ? r : r[1], inclCur ? uu : uu[1], inclCur ? dd : dd[1], inclCur ? upPct : upPct[1], rng]
f_week() =>
float pip = syminfo.mintick * pipMult
float rng = (high - low) / pip
float upx = (high - open) / pip
float dnx = (open - low) / pip
float v = wkMetric == "Up (open to high)" ? upx : wkMetric == "Down (open to low)" ? dnx : rng
int dwk = dayofweek(time_close - 1, tzIn == "Chart (exchange)" ? syminfo.timezone : tzIn)
float vS = inclCur ? v : v[1]
int dS = inclCur ? dwk : dwk[1]
float sM = math.sum(dS == dayofweek.monday ? nz(vS) : 0.0, nWk)
float cM = math.sum(dS == dayofweek.monday ? 1.0 : 0.0, nWk)
float sT = math.sum(dS == dayofweek.tuesday ? nz(vS) : 0.0, nWk)
float cT = math.sum(dS == dayofweek.tuesday ? 1.0 : 0.0, nWk)
float sW = math.sum(dS == dayofweek.wednesday ? nz(vS) : 0.0, nWk)
float cW = math.sum(dS == dayofweek.wednesday ? 1.0 : 0.0, nWk)
float sH = math.sum(dS == dayofweek.thursday ? nz(vS) : 0.0, nWk)
float cH = math.sum(dS == dayofweek.thursday ? 1.0 : 0.0, nWk)
float sF = math.sum(dS == dayofweek.friday ? nz(vS) : 0.0, nWk)
float cF = math.sum(dS == dayofweek.friday ? 1.0 : 0.0, nWk)
[cM > 0 ? sM / cM : na, cT > 0 ? sT / cT : na, cW > 0 ? sW / cW : na, cH > 0 ? sH / cH : na, cF > 0 ? sF / cF : na, cM, cT, cW, cH, cF]
f_put(p, t, a, b, c, d, e) =>
int base = (p * 4 + t) * 5
array.set(st, base, a)
array.set(st, base + 1, b)
array.set(st, base + 2, c)
array.set(st, base + 3, d)
array.set(st, base + 4, e)
true
f_putWk(p, a0, a1, a2, a3, a4, c0, c1, c2, c3, c4) =>
int base = p * 10
array.set(wk, base, a0)
array.set(wk, base + 1, c0)
array.set(wk, base + 2, a1)
array.set(wk, base + 3, c1)
array.set(wk, base + 4, a2)
array.set(wk, base + 5, c2)
array.set(wk, base + 6, a3)
array.set(wk, base + 7, c3)
array.set(wk, base + 8, a4)
array.set(wk, base + 9, c4)
true
// ============ DATA REQUESTS ============
for p = 0 to 7
if array.get(uses, p)
string sy = array.get(syms, p)
if tfOn1
[a1, b1, c1, d1, e1] = request.security(sy, tf1, f_stats(n1), lookahead = barmerge.lookahead_off)
f_put(p, 0, a1, b1, c1, d1, e1)
if tfOn2
[a2, b2, c2, d2, e2] = request.security(sy, tf2, f_stats(n2), lookahead = barmerge.lookahead_off)
f_put(p, 1, a2, b2, c2, d2, e2)
if tfOn3
[a3, b3, c3, d3, e3] = request.security(sy, tf3, f_stats(n3), lookahead = barmerge.lookahead_off)
f_put(p, 2, a3, b3, c3, d3, e3)
if tfOn4
[a4, b4, c4, d4, e4] = request.security(sy, tf4, f_stats(n4), lookahead = barmerge.lookahead_off)
f_put(p, 3, a4, b4, c4, d4, e4)
if showWeekT
[m0, m1, m2, m3, m4, k0, k1, k2, k3, k4] = request.security(sy, "D", f_week(), lookahead = barmerge.lookahead_off)
f_putWk(p, m0, m1, m2, m3, m4, k0, k1, k2, k3, k4)
// ============ DASHBOARD ============
var table dash = showDash ? table.new(dashPos, 8, 44, bgcolor = color.new(#12161d, 5), border_width = 1, border_color = color.new(color.gray, 60)) : na
f_cell(c, r, txt, fg, bg, ha) =>
table.cell(dash, c, r, txt, text_color = fg, bgcolor = bg, text_halign = ha, text_size = dashSize)
f_title(r, txt) =>
table.cell(dash, 0, r, txt, text_color = color.white, bgcolor = color.new(#1f4e78, 30), text_halign = text.align_left, text_size = dashSize)
table.merge_cells(dash, 0, r, 7, r)
f_note(r, txt) =>
table.cell(dash, 0, r, txt, text_color = color.new(color.white, 35), bgcolor = #12161d, text_halign = text.align_left, text_size = size.tiny)
table.merge_cells(dash, 0, r, 7, r)
f_hdr(r, h) =>
int m = array.size(h)
for i = 0 to m - 1
f_cell(i, r, array.get(h, i), color.white, #1f4e78, i == 0 ? text.align_left : text.align_center)
f_short(s) =>
array<string> parts = str.split(s, ":")
array.get(parts, array.size(parts) - 1)
f_sgnPct(x) => (x > 0 ? "+" : "") + str.tostring(x, "0") + "%"
f_tfLabel(tf) =>
int sec = timeframe.in_seconds(tf)
string out = ""
if sec >= 2400000
out := "Monthly"
else if sec >= 604800
out := sec == 604800 ? "Weekly" : str.tostring(sec / 604800, "0") + "W"
else if sec >= 86400
out := sec == 86400 ? "Daily" : str.tostring(sec / 86400, "0") + "D"
else if sec % 3600 == 0
out := str.tostring(sec / 3600, "0") + "H"
else
out := str.tostring(sec / 60, "0") + "m"
out
if showDash and barstate.islast
color plainBg = #12161d
color headBg = #1f4e78
color goodC = #00e676
color badC = #ff5252
color warnC = #ffb74d
array<string> dayNm = array.from("Mon", "Tue", "Wed", "Thu", "Fri")
array<bool> slotOn = array.from(tfOn1, tfOn2, tfOn3, tfOn4)
array<int> slotN = array.from(n1, n2, n3, n4)
array<string> slotLbl = array.from(f_tfLabel(tf1), f_tfLabel(tf2), f_tfLabel(tf3), f_tfLabel(tf4))
array<int> usedIx = array.new<int>()
for p2 = 0 to 7
if array.get(uses, p2)
array.push(usedIx, p2)
int nUsed = array.size(usedIx)
int nSlots = 0
for t0 = 0 to 3
if array.get(slotOn, t0)
nSlots += 1
int r = 0
f_title(r, "FX pip statistics | " + avgType + " | pip = tick x " + str.tostring(pipMult, "#.#") + " | " + (inclCur ? "incl. current candle" : "completed candles only"))
r += 1
if nUsed == 0
f_note(r, "Tick at least one pair in the settings.")
r += 1
// ---- Table 1: one metric across timeframes
if showTfT and nUsed > 0
int mIx = metricSel == "Average range" ? 0 : metricSel == "Average up" ? 1 : metricSel == "Average down" ? 2 : 3
f_title(r, "1. " + metricSel + (mIx == 3 ? " (share of candles that closed up)" : " per candle, in pips"))
r += 1
f_cell(0, r, "Pair", color.white, headBg, text.align_left)
int hc = 1
for t = 0 to 3
if array.get(slotOn, t)
f_cell(hc, r, array.get(slotLbl, t) + " (" + str.tostring(array.get(slotN, t)) + ")", color.white, headBg, text.align_center)
hc += 1
r += 1
array<float> colMax = array.new<float>(4, -1000000.0)
array<int> colMaxP = array.new<int>(4, -1)
for ui = 0 to nUsed - 1
int pp = array.get(usedIx, ui)
for t = 0 to 3
if array.get(slotOn, t)
float v = array.get(st, (pp * 4 + t) * 5 + mIx)
if not na(v) and v > array.get(colMax, t)
array.set(colMax, t, v)
array.set(colMaxP, t, pp)
for ui = 0 to nUsed - 1
int pq = array.get(usedIx, ui)
f_cell(0, r, f_short(array.get(syms, pq)), color.white, plainBg, text.align_left)
int cc = 1
for t = 0 to 3
if array.get(slotOn, t)
float v2 = array.get(st, (pq * 4 + t) * 5 + mIx)
string vt = na(v2) ? "-" : (mIx == 3 ? str.tostring(v2, "0") + "%" : str.tostring(v2, "0.0"))
bool lead = hiLead and mIx != 3 and array.get(colMaxP, t) == pq
f_cell(cc, r, vt, lead ? goodC : color.white, lead ? color.new(goodC, 88) : plainBg, text.align_center)
cc += 1
r += 1
f_note(r, "Number in brackets = candles averaged. Green = the pair with the largest figure in that column. '-' means no data (check the symbol or your data access).")
r += 1
// ---- Table 2: range, up and down for one timeframe
if showDetailT and nUsed > 0
int ft = focusSlot - 1
if array.get(slotOn, ft)
f_title(r, "2. " + array.get(slotLbl, ft) + " candles | up / down measured as: " + upDownMode)
r += 1
f_hdr(r, array.from("Pair", "Avg range", "Avg up", "Avg down", "Up %", "Now", "Now / avg"))
r += 1
for ui = 0 to nUsed - 1
int pd = array.get(usedIx, ui)
int bs = (pd * 4 + ft) * 5
float rg = array.get(st, bs)
float upv = array.get(st, bs + 1)
float dnv = array.get(st, bs + 2)
float upp = array.get(st, bs + 3)
float cur = array.get(st, bs + 4)
float pct = not na(rg) and rg > 0 and not na(cur) ? cur / rg * 100.0 : na
color pctC = na(pct) ? color.white : pct >= 100 ? goodC : pct < 50 ? warnC : color.white
f_cell(0, r, f_short(array.get(syms, pd)), color.white, plainBg, text.align_left)
f_cell(1, r, na(rg) ? "-" : str.tostring(rg, "0.0"), color.white, plainBg, text.align_center)
f_cell(2, r, na(upv) ? "-" : str.tostring(upv, "0.0"), goodC, plainBg, text.align_center)
f_cell(3, r, na(dnv) ? "-" : str.tostring(dnv, "0.0"), badC, plainBg, text.align_center)
f_cell(4, r, na(upp) ? "-" : str.tostring(upp, "0") + "%", color.white, plainBg, text.align_center)
f_cell(5, r, na(cur) ? "-" : str.tostring(cur, "0.0"), color.white, plainBg, text.align_center)
f_cell(6, r, na(pct) ? "-" : str.tostring(pct, "0") + "%", pctC, plainBg, text.align_center)
r += 1
f_note(r, "Now = the range of the candle still forming, in pips. Now / avg = how much of the average range it has already used (green = at or above, orange = under half). Up % = share of candles that closed up.")
r += 1
else
f_note(r, "Table 2: turn on timeframe " + str.tostring(focusSlot) + " in the settings, or pick another slot.")
r += 1
// ---- Table 3: busiest weekdays
if showWeekT and nUsed > 0
f_title(r, "3. Average " + wkMetric + " by weekday (pips) | last " + str.tostring(nWk) + " daily candles | day = " + tzName)
r += 1
f_hdr(r, array.from("Pair", "Mon", "Tue", "Wed", "Thu", "Fri", "Most active", "Quietest"))
r += 1
array<float> relSum = array.new<float>(5, 0.0)
array<int> relCnt = array.new<int>(5, 0)
for ui = 0 to nUsed - 1
int pw = array.get(usedIx, ui)
float tot = 0.0
int ne = 0
float mx = -1000000.0
float mn = 1000000.0
int mxD = 0
int mnD = 0
for dd = 0 to 4
float vv = array.get(wk, (pw * 5 + dd) * 2)
float cn = array.get(wk, (pw * 5 + dd) * 2 + 1)
if not na(vv) and cn >= minWk
tot += vv
ne += 1
if vv > mx
mx := vv
mxD := dd
if vv < mn
mn := vv
mnD := dd
float mean = ne > 0 ? tot / ne : na
f_cell(0, r, f_short(array.get(syms, pw)), color.white, plainBg, text.align_left)
for dd2 = 0 to 4
float v3 = array.get(wk, (pw * 5 + dd2) * 2)
float c3 = array.get(wk, (pw * 5 + dd2) * 2 + 1)
bool ok = not na(v3) and c3 >= minWk
bool mark = ok and ne >= 2
color cbg = mark and dd2 == mxD ? color.new(goodC, 80) : mark and dd2 == mnD ? color.new(badC, 80) : plainBg
f_cell(1 + dd2, r, na(v3) ? "-" : str.tostring(v3, "0.0"), ok ? color.white : color.new(color.white, 55), cbg, text.align_center)
if ok and mean > 0
array.set(relSum, dd2, array.get(relSum, dd2) + v3 / mean * 100.0)
array.set(relCnt, dd2, array.get(relCnt, dd2) + 1)
bool two = ne >= 2 and mean > 0
string mxTxt = two ? array.get(dayNm, mxD) + " (" + f_sgnPct(mx / mean * 100.0 - 100.0) + ")" : "-"
string mnTxt = two ? array.get(dayNm, mnD) + " (" + f_sgnPct(mn / mean * 100.0 - 100.0) + ")" : "-"
f_cell(6, r, mxTxt, two ? goodC : color.new(color.white, 55), plainBg, text.align_center)
f_cell(7, r, mnTxt, two ? badC : color.new(color.white, 55), plainBg, text.align_center)
r += 1
// all pairs together, each day as a percentage of that pair's own weekday average
color idxBg = color.new(headBg, 55)
f_cell(0, r, "All pairs (index)", color.white, idxBg, text.align_left)
float bestRel = -1000000.0
float worstRel = 1000000.0
int bestRD = 0
int worstRD = 0
int eligR = 0
for dd3 = 0 to 4
int rcnt = array.get(relCnt, dd3)
if rcnt > 0
eligR += 1
float ri = array.get(relSum, dd3) / rcnt
if ri > bestRel
bestRel := ri
bestRD := dd3
if ri < worstRel
worstRel := ri
worstRD := dd3
for dd4 = 0 to 4
int rcnt2 = array.get(relCnt, dd4)
float ri2 = rcnt2 > 0 ? array.get(relSum, dd4) / rcnt2 : na
bool mk2 = eligR >= 2 and rcnt2 > 0
color rbg = mk2 and dd4 == bestRD ? color.new(goodC, 80) : mk2 and dd4 == worstRD ? color.new(badC, 80) : idxBg
f_cell(1 + dd4, r, na(ri2) ? "-" : str.tostring(ri2, "0") + "%", color.white, rbg, text.align_center)
f_cell(6, r, eligR >= 2 ? array.get(dayNm, bestRD) : "-", goodC, idxBg, text.align_center)
f_cell(7, r, eligR >= 2 ? array.get(dayNm, worstRD) : "-", badC, idxBg, text.align_center)
r += 1
f_note(r, "Green = the busiest weekday, red = the quietest (needs " + str.tostring(minWk) + "+ samples per day). Index = each day as a percentage of that pair's own weekday average, averaged across pairs (100% = typical).")
r += 1
int reqUsed = nUsed * (nSlots + (showWeekT ? 1 : 0))
f_note(r, "Data requests used: " + str.tostring(reqUsed) + " of 40. Statistics describe past movement, not a forecast.")