الرئيسية ← المؤشرات ← Gold M15 ← شيفرة القياس
شيفرة القياس

النسخة البحثية من Gold M15

هذه هي الشيفرة المستخدمة لحساب الأرقام المنشورة في صفحة المنهج. ننشرها كاملةً لتعيد قياس السجل التاريخي بنفسك. وهي للقياس والتعليم، وليست نصيحة استثمارية أو توصية بالتداول.

ما الفرق بينها وبين المؤشر المنشور

محرّك الإشارات واحد. فبالإعدادات الافتراضية تُطلق هذه النسخة الإشارات نفسها التي يُطلقها المؤشر المنشور على TradingView، ولا تزيد عليه إلا ما يلزم للقياس:

كيف تعيد القياس بنفسك

  1. افتح TradingView، ثم محرّر Pine أسفل الشارت، وأنشئ مؤشراً جديداً.
  2. انسخ الشيفرة أدناه كاملةً، والصقها مكان المحتوى الافتراضي، ثم احفظ واضغط «Add to chart».
  3. افتح شارت الذهب XAUUSD على إطار 15 دقيقة.
  4. اترك الإعدادات على قيمها الافتراضية. يبدأ «Count signals from» من 1 كانون الثاني 2026، فيُحصي كل التاريخ الذي يحمّله شارت 15 دقيقة، وهو الإعداد الذي خرجت منه أرقامنا. وغيّره إن أردت قياس فترة أقصر. وتظهر النتيجة في جدول RESEARCH TALLY على الشارت.

قد تختلف أرقامك عن أرقامنا، لأنّها تتوقّف على تاريخ البداية، وعلى عدد الشموع التاريخية التي يحمّلها حسابك في TradingView، وعلى مزوّد السعر. المنطق معروض كاملاً، لكن هذه حسابات تاريخية توضيحية وليست وعداً بأداء مستقبلي.

الشيفرة

تنزيل الملف Pine Script v6 · 873 سطراً
//@version=6
// Gold M15 Signal Engine — research build v3
// © 2026 Consultya · consultya.co
// Published for reading and verification. This is the code that produced
// the measured figures on the Consultya website. It is not a trading system.
//
// Same engine as the published version (v3.1), plus what is needed to measure it:
//   1. Partial exits. A third books at TP1, a third at TP2, the rest at
//      TP3 or the stop - which is how the signals are actually traded.
//      Without this, a trade that reached TP1 and came back was recorded
//      as zero, when in reality a third of it was already banked.
//   2. A switch for the breakeven rule, so the same bars can be
//      measured both ways.
//   3. RSI guards on the breakout entry, adjustable rather than fixed.
//   4. An R-multiple tally. Win rate alone cannot tell you whether a
//      system makes money - a 35% win rate can be excellent and a 60%
//      one can be ruinous. R is the distance to the original stop, so
//      every outcome is expressed in units of what was risked.
//   5. A fixed start date and a timeframe override, so the sample can
//      be held still between two readings.
//   6. Filter D: US release windows from the official calendar, the same
//      dates as Gold News Reaction Map. The default (skip, 60 minutes
//      before and after, all ten releases) is the setting of the
//      published v3.1. The tally is also split into signals entered near
//      a release, entered away from one, and still open when one came out.
//   7. Three experimental filters, off by default: A (range), B (price
//      hugging EMA 200) and C (fixed clock slots). They are kept so every
//      reading we tested can be reproduced.
// Skipped signals are drawn as small grey "skip" tags and counted in the tally.

indicator("Gold M15 — Research Build", overlay=true, max_labels_count=500, max_lines_count=100)

// ══════════════════════════════════════════════════════════════
// INPUTS
// ══════════════════════════════════════════════════════════════
i_e20    = input.int(20,    "EMA 20",              group="Moving averages")
i_e50    = input.int(50,    "EMA 50",              group="Moving averages")
i_e200   = input.int(200,   "EMA 200",             group="Moving averages")
i_swing  = input.int(7,     "Swing Length",        group="Support / resistance")
i_maxS   = input.int(2,     "Max Supports",        group="Support / resistance")
i_maxR   = input.int(2,     "Max Resistances",     group="Support / resistance")
i_minD   = input.float(0.4, "Min Gap %",           group="Support / resistance")
i_rsiP   = input.int(14,    "RSI Period",          group="Signal engine")
i_cool   = input.int(5,     "Cooldown Bars",       group="Signal engine")
i_brk    = input.int(10,    "Breakout Bars",       group="Signal engine")
i_exp    = input.int(40,    "Expire Bars (0=off)", group="Signal engine")
// Breakout entries buy strength and sell weakness, which is exactly what
// an exhausted move looks like right before it reverses. These two guards
// block a breakout entry taken into an already-stretched RSI.
i_brkHi  = input.float(70.0, "Breakout: max RSI for a long",  minval=50, maxval=100, group="Signal engine", tooltip="A long breakout is skipped when RSI is above this. 70 reproduces the published behaviour; try 60 to refuse buying into an overbought high.")
i_brkLo  = input.float(30.0, "Breakout: min RSI for a short", minval=0,  maxval=50,  group="Signal engine", tooltip="A short breakout is skipped when RSI is below this. 30 reproduces the published behaviour; try 40 to refuse selling into an oversold low.")
i_atrP   = input.int(14,    "ATR Period",          group="Targets and stop")
i_tp1x   = input.float(0.8, "TP1 × ATR",          group="Targets and stop")
i_tp2x   = input.float(1.8, "TP2 × ATR fallback", group="Targets and stop")
i_tp3x   = input.float(3.0, "TP3 × ATR",          group="Targets and stop")
i_slPct  = input.float(0.35,"Max SL %",            group="Targets and stop")

i_partial = input.bool(true, "Book a third at each target", group="Stop management", tooltip="Models how the signals are actually traded: a third closes at TP1, a third at TP2, the remainder at TP3 or the stop. Turn it off to treat every signal as one all-or-nothing position, which records a trade that reached TP1 and came back as exactly zero.")
i_beTP1   = input.bool(true, "Move stop to breakeven at TP1", group="Stop management", tooltip="ON reproduces the published behaviour. OFF leaves the stop where it started until TP2.")

i_start  = input.time(1767225600000, "Count signals from", group="Research", tooltip="Signals before this moment are ignored entirely - not drawn and not counted. Pin it to a fixed date so two settings can be compared over exactly the same period.")
i_stats  = input.bool(true, "Show tally", group="Research", tooltip="Counts the outcomes the indicator drew inside the window. Not a backtest: no spread, no slippage, no sizing, and when a single bar touches both the stop and a target the stop is assumed to come first.")
i_anyTF  = input.bool(false, "Allow any timeframe (research only)", group="Research", tooltip="The engine was tuned on 15-minute bars. Turning this on gives a larger sample but measures a differently-behaving system.")
i_tallyPos = input.string("Bottom right", "Tally position", options=["Bottom right", "Bottom left", "Top left", "Middle left", "Middle right"], group="Research", tooltip="Put two copies of this indicator on one chart - one with the filters off, one with them on - and move one tally to the left to compare them side by side.")
i_h4     = input.bool(true, "H4 trend filter", group="Higher-timeframe filter", tooltip="Only allow signals that agree with the 4-hour trend.")

gF = "Research filters (A to C off = published behaviour)"
i_fChop    = input.bool(false, "A. Range filter", group=gF, tooltip="Skip a signal when EMA 20 and EMA 50 are tangled together or ADX shows no trend. Most losing clusters happen in these sideways phases.")
i_chopGap  = input.float(0.3,  "   EMA20-EMA50 gap at least (x ATR)", minval=0.0, step=0.05, group=gF)
i_adxLen   = input.int(14,     "   ADX length", minval=2, group=gF)
i_adxMin   = input.float(20.0, "   ADX at least (0 = off)", minval=0.0, step=1.0, group=gF)
i_f200     = input.bool(false, "B. Skip when price hugs EMA 200", group=gF, tooltip="Skip a signal when the close is closer to EMA 200 than this many ATRs. Price tends to whipsaw around the 200.")
i_e200Dist = input.float(1.0,  "   Distance from EMA 200 at least (x ATR)", minval=0.0, step=0.1, group=gF)
i_fNews    = input.bool(false, "C. Skip US data windows", group=gF, tooltip="Skip signals from 08:00 to 09:15 and from 09:45 to 10:15 New York time, Monday to Friday - the slots of most high-impact US releases.")
D_OFF  = "Off"
D_SKIP = "Skip signals near a release"
D_ONLY = "Only signals near a release"
i_dMode   = input.string(D_SKIP, "D. Release windows (official dates)", options=[D_OFF, D_SKIP, D_ONLY], group=gF, tooltip="Uses the official release calendar of Gold News Reaction Map. Off still splits the tally into near / away / held-through, so one run answers the question; Skip and Only remove the other group from the main tally.")
i_dBefore = input.int(60, "   Minutes before the release", minval=0, maxval=240, group=gF)
i_dAfter  = input.int(60, "   Minutes after the release", minval=0, maxval=240, group=gF)
i_dSet    = input.string("All ten", "   Releases", options=["Major", "All ten"], group=gF, tooltip="Major: FOMC decision, CPI, NFP, PCE, PPI and retail sales. All ten adds FOMC minutes, both ISM reports and weekly jobless claims.")
i_dShade  = input.bool(true, "   Shade release windows on the chart", group=gF)

// ══════════════════════════════════════════════════════════════
// CORE INDICATORS
// ══════════════════════════════════════════════════════════════
ema20  = ta.ema(close, i_e20)
ema50  = ta.ema(close, i_e50)
ema200 = ta.ema(close, i_e200)
rsiVal = ta.rsi(close, i_rsiP)
atrVal = ta.atr(i_atrP)

plot(ema20,  "EMA 20",  color=color.yellow, linewidth=1)
plot(ema50,  "EMA 50",  color=#1E90FF,      linewidth=2)
plot(ema200, "EMA 200", color=color.red,    linewidth=2)

// ══════════════════════════════════════════════════════════════
// OFFICIAL RELEASE CALENDAR (v3) - same dates as Gold News Reaction Map
// k: 0 FOMC, 1 CPI, 2 NFP, 3 PPI, 4 PCE, 5 Retail, 6 ISM Mfg, 7 ISM Srv,
//    8 Claims, 9 FOMC minutes. "Major" keeps k 0-5.
// ══════════════════════════════════════════════════════════════
var array<int> relT = array.new<int>()
dAll = i_dSet == "All ten"
addT(array<int> days, int h, int mi, int k) =>
    if dAll or k <= 5
        for d in days
            array.push(relT, timestamp("America/New_York", math.floor(d / 10000), math.floor(d / 100) % 100, d % 100, h, mi))
if barstate.isfirst
    addT(array.from(20240131, 20240320, 20240501, 20240612, 20240731, 20240918), 14, 0, 0)
    addT(array.from(20241107, 20241218), 14, 0, 0)
    addT(array.from(20250129, 20250319, 20250507, 20250618, 20250730, 20250917), 14, 0, 0)
    addT(array.from(20251029, 20251210), 14, 0, 0)
    addT(array.from(20260128, 20260318, 20260429, 20260617, 20260729, 20260916), 14, 0, 0)
    addT(array.from(20261028, 20261209), 14, 0, 0)
    addT(array.from(20270127, 20270317, 20270428, 20270609, 20270728, 20270915), 14, 0, 0)
    addT(array.from(20271027, 20271208), 14, 0, 0)
    addT(array.from(20240103, 20240221, 20240410, 20240522, 20240703, 20240821), 14, 0, 9)
    addT(array.from(20241009, 20241126), 14, 0, 9)
    addT(array.from(20250108, 20250219, 20250409, 20250528, 20250709, 20250820), 14, 0, 9)
    addT(array.from(20251008, 20251119, 20251230), 14, 0, 9)
    addT(array.from(20260218, 20260408, 20260520, 20260708, 20260819, 20261007), 14, 0, 9)
    addT(array.from(20261118), 14, 0, 9)
    addT(array.from(20261230), 14, 0, 9)
    addT(array.from(20270217, 20270407, 20270519, 20270630, 20270818, 20271006), 14, 0, 9)
    addT(array.from(20271117, 20271229), 14, 0, 9)
    addT(array.from(20240111, 20240213, 20240312, 20240410, 20240515, 20240612), 8, 30, 1)
    addT(array.from(20240711, 20240814, 20240911, 20241010, 20241113, 20241211), 8, 30, 1)
    addT(array.from(20250115, 20250212, 20250312, 20250410, 20250513, 20250611), 8, 30, 1)
    addT(array.from(20250715, 20250812, 20250911, 20251024, 20251218), 8, 30, 1)
    addT(array.from(20260113, 20260213, 20260311, 20260410, 20260512, 20260610), 8, 30, 1)
    addT(array.from(20260714, 20260812, 20260911, 20261014, 20261110, 20261210), 8, 30, 1)
    addT(array.from(20270113, 20270211, 20270310, 20270413, 20270512, 20270610), 8, 30, 1)
    addT(array.from(20270714, 20270811, 20270914, 20271014, 20271110, 20271210), 8, 30, 1)
    addT(array.from(20240126, 20240229, 20240329, 20240426, 20240531, 20240628), 8, 30, 4)
    addT(array.from(20240726, 20240830, 20240927, 20241031, 20241127, 20241220), 8, 30, 4)
    addT(array.from(20250131, 20250228, 20250328, 20250430, 20250530, 20250627), 8, 30, 4)
    addT(array.from(20250731, 20250829, 20250926), 8, 30, 4)
    addT(array.from(20260220, 20260313, 20260409, 20260430, 20260528, 20260625), 8, 30, 4)
    addT(array.from(20260730, 20260826, 20260930, 20261029, 20261125, 20261223), 8, 30, 4)
    addT(array.from(20270331, 20270429, 20270527, 20270625, 20270729, 20270825), 8, 30, 4)
    addT(array.from(20270930, 20271028, 20271124), 8, 30, 4)
    addT(array.from(20251205), 10, 0, 4)
    addT(array.from(20260122), 10, 0, 4)
    addT(array.from(20240112, 20240216, 20240314, 20240411, 20240514, 20240613), 8, 30, 3)
    addT(array.from(20240712, 20240813, 20240912, 20241011, 20241114, 20241212), 8, 30, 3)
    addT(array.from(20250114, 20250213, 20250313, 20250411, 20250515, 20250612), 8, 30, 3)
    addT(array.from(20250716, 20250814, 20250910, 20251125), 8, 30, 3)
    addT(array.from(20260114, 20260130, 20260227, 20260318, 20260414, 20260513), 8, 30, 3)
    addT(array.from(20260611, 20260715, 20260813, 20260910, 20261015, 20261113), 8, 30, 3)
    addT(array.from(20261215), 8, 30, 3)
    addT(array.from(20270114, 20270212, 20270312, 20270414, 20270513, 20270611), 8, 30, 3)
    addT(array.from(20270715, 20270810, 20270910, 20271015, 20271112, 20271215), 8, 30, 3)
    addT(array.from(20240105, 20240202, 20240308, 20240405, 20240503, 20240607), 8, 30, 2)
    addT(array.from(20240705, 20240802, 20240906, 20241004, 20241101, 20241206), 8, 30, 2)
    addT(array.from(20250110, 20250207, 20250307, 20250404, 20250502, 20250606), 8, 30, 2)
    addT(array.from(20250703, 20250801, 20250905, 20251120, 20251216), 8, 30, 2)
    addT(array.from(20260109, 20260211, 20260306, 20260403, 20260508, 20260605), 8, 30, 2)
    addT(array.from(20260702, 20260807, 20260904, 20261002, 20261106, 20261204), 8, 30, 2)
    addT(array.from(20270108, 20270205, 20270305, 20270402, 20270507, 20270604), 8, 30, 2)
    addT(array.from(20270702, 20270806, 20270903, 20271008, 20271105, 20271203), 8, 30, 2)
    addT(array.from(20240117, 20240215, 20240314, 20240415, 20240515, 20240618), 8, 30, 5)
    addT(array.from(20240716, 20240815, 20240917, 20241017, 20241115, 20241217), 8, 30, 5)
    addT(array.from(20250116, 20250214, 20250317, 20250416, 20250515, 20250617), 8, 30, 5)
    addT(array.from(20250717, 20250815, 20250916, 20251125, 20251216), 8, 30, 5)
    addT(array.from(20260114, 20260210, 20260306, 20260401, 20260421, 20260514), 8, 30, 5)
    addT(array.from(20260617, 20260716, 20260814, 20260916, 20261015, 20261117), 8, 30, 5)
    addT(array.from(20261216), 8, 30, 5)
    addT(array.from(20240103, 20240201, 20240301, 20240401, 20240501, 20240603), 10, 0, 6)
    addT(array.from(20240701, 20240801, 20240903, 20241001, 20241101, 20241202), 10, 0, 6)
    addT(array.from(20250103, 20250203, 20250303, 20250401, 20250501, 20250602), 10, 0, 6)
    addT(array.from(20250701, 20250801, 20250902, 20251001, 20251103, 20251201), 10, 0, 6)
    addT(array.from(20260105, 20260202, 20260302, 20260401, 20260501, 20260601), 10, 0, 6)
    addT(array.from(20260701, 20260803, 20260901, 20261001, 20261102, 20261201), 10, 0, 6)
    addT(array.from(20270104, 20270201, 20270301, 20270401, 20270503, 20270601), 10, 0, 6)
    addT(array.from(20270701, 20270802, 20270901, 20271001, 20271101, 20271201), 10, 0, 6)
    addT(array.from(20240105, 20240205, 20240305, 20240403, 20240503, 20240605), 10, 0, 7)
    addT(array.from(20240703, 20240805, 20240905, 20241003, 20241105, 20241204), 10, 0, 7)
    addT(array.from(20250107, 20250205, 20250305, 20250403, 20250505, 20250604), 10, 0, 7)
    addT(array.from(20250703, 20250805, 20250904, 20251003, 20251105, 20251203), 10, 0, 7)
    addT(array.from(20260107, 20260204, 20260304, 20260406, 20260505, 20260603), 10, 0, 7)
    addT(array.from(20260706, 20260805, 20260903, 20261005, 20261104, 20261203), 10, 0, 7)
    addT(array.from(20270106, 20270203, 20270303, 20270405, 20270505, 20270603), 10, 0, 7)
    addT(array.from(20270706, 20270804, 20270903, 20271005, 20271103, 20271203), 10, 0, 7)
    addT(array.from(20240104, 20240111, 20240118, 20240125, 20240201, 20240208), 8, 30, 8)
    addT(array.from(20240215, 20240222, 20240229, 20240307, 20240314, 20240321), 8, 30, 8)
    addT(array.from(20240328, 20240404, 20240411, 20240418, 20240425, 20240502), 8, 30, 8)
    addT(array.from(20240509, 20240516, 20240523, 20240530, 20240606, 20240613), 8, 30, 8)
    addT(array.from(20240620, 20240627, 20240703, 20240711, 20240718, 20240725), 8, 30, 8)
    addT(array.from(20240801, 20240808, 20240815, 20240822, 20240829, 20240905), 8, 30, 8)
    addT(array.from(20240912, 20240919, 20240926, 20241003, 20241010, 20241017), 8, 30, 8)
    addT(array.from(20241024, 20241031, 20241107, 20241114, 20241121, 20241127), 8, 30, 8)
    addT(array.from(20241205, 20241212, 20241219, 20241226), 8, 30, 8)
    addT(array.from(20250102, 20250108, 20250116, 20250123, 20250130, 20250206), 8, 30, 8)
    addT(array.from(20250213, 20250220, 20250227, 20250306, 20250313, 20250320), 8, 30, 8)
    addT(array.from(20250327, 20250403, 20250410, 20250417, 20250424, 20250501), 8, 30, 8)
    addT(array.from(20250508, 20250515, 20250522, 20250529, 20250605, 20250612), 8, 30, 8)
    addT(array.from(20250618, 20250626, 20250703, 20250710, 20250717, 20250724), 8, 30, 8)
    addT(array.from(20250731, 20250807, 20250814, 20250821, 20250828, 20250904), 8, 30, 8)
    addT(array.from(20250911, 20250918, 20250925, 20251120, 20251126, 20251204), 8, 30, 8)
    addT(array.from(20251211, 20251218, 20251224, 20251231), 8, 30, 8)
    addT(array.from(20260108, 20260115, 20260122, 20260129, 20260205, 20260212), 8, 30, 8)
    addT(array.from(20260219, 20260226, 20260305, 20260312, 20260319, 20260326), 8, 30, 8)
    addT(array.from(20260402, 20260409, 20260416, 20260423, 20260430, 20260507), 8, 30, 8)
    addT(array.from(20260514, 20260521, 20260528, 20260604, 20260611, 20260618), 8, 30, 8)
    addT(array.from(20260625, 20260702, 20260709, 20260716, 20260723, 20260730), 8, 30, 8)
    addT(array.from(20260806, 20260813, 20260820, 20260827, 20260903, 20260910), 8, 30, 8)
    addT(array.from(20260917, 20260924, 20261001, 20261008, 20261015, 20261022), 8, 30, 8)
    addT(array.from(20261029, 20261105, 20261112, 20261119, 20261125, 20261203), 8, 30, 8)
    addT(array.from(20261210, 20261217), 8, 30, 8)
    addT(array.from(20261224, 20261231, 20270107, 20270114, 20270121, 20270128), 8, 30, 8)
    addT(array.from(20270204, 20270211, 20270218, 20270225, 20270304, 20270311), 8, 30, 8)
    addT(array.from(20270318, 20270325, 20270401, 20270408, 20270415, 20270422), 8, 30, 8)
    addT(array.from(20270429, 20270506, 20270513, 20270520, 20270527, 20270603), 8, 30, 8)
    addT(array.from(20270610, 20270617, 20270624, 20270701, 20270708, 20270715), 8, 30, 8)
    addT(array.from(20270722, 20270729, 20270805, 20270812, 20270819, 20270826), 8, 30, 8)
    addT(array.from(20270902, 20270909, 20270916, 20270923, 20270930, 20271007), 8, 30, 8)
    addT(array.from(20271014, 20271021, 20271028, 20271104, 20271111, 20271118), 8, 30, 8)
    addT(array.from(20271124, 20271202, 20271209, 20271216, 20271223, 20271230), 8, 30, 8)
    array.sort(relT, order.ascending)

// A signal is "near" a release when it is entered between the release time
// minus the minutes before and the release time plus the minutes after.
// A trade is "held through" a release when one comes out while it is open.
var int pN = 0
var int pH = 0
nRel  = array.size(relT)
while pN < nRel and array.get(relT, pN) + i_dAfter * 60000 < time_close
    pN += 1
nearNow = pN < nRel and array.get(relT, pN) - i_dBefore * 60000 <= time_close
while pH < nRel and array.get(relT, pH) < time
    pH += 1
relInBar = pH < nRel and array.get(relT, pH) < time_close
bgcolor(i_dShade and nearNow ? color.new(color.orange, 88) : na, title="Release window")

// ══════════════════════════════════════════════════════════════
// RESEARCH FILTERS (v2) - computed on every bar
// ══════════════════════════════════════════════════════════════
[diPlus, diMinus, adxVal] = ta.dmi(i_adxLen, i_adxLen)
nyMin    = hour(time, "America/New_York") * 60 + minute(time, "America/New_York")
nyDow    = dayofweek(time, "America/New_York")
nyWkday  = nyDow >= dayofweek.monday and nyDow <= dayofweek.friday
f_tangle = i_fChop and math.abs(ema20 - ema50) < i_chopGap * atrVal
f_weak   = i_fChop and i_adxMin > 0 and adxVal < i_adxMin
f_hug200 = i_f200 and math.abs(close - ema200) < i_e200Dist * atrVal
f_news   = i_fNews and nyWkday and ((nyMin >= 480 and nyMin < 555) or (nyMin >= 585 and nyMin < 615))
f_dSkip  = i_dMode == D_SKIP and nearNow
f_dOnly  = i_dMode == D_ONLY and not nearNow
filtOk   = not (f_tangle or f_weak or f_hug200 or f_news or f_dSkip or f_dOnly)
skipWhy  = (f_tangle ? "EMA20/50 tangled  " : "") + (f_weak ? "ADX " + str.tostring(adxVal, "#.#") + "  " : "") + (f_hug200 ? "near EMA 200  " : "") + (f_news ? "US data window  " : "") + (f_dSkip ? "near a US release  " : "") + (f_dOnly ? "away from releases" : "")

// ══════════════════════════════════════════════════════════════
// H4 TREND FILTER
// ══════════════════════════════════════════════════════════════
h4_ema200    = request.security(syminfo.tickerid, "240", ta.ema(close, 200), lookahead=barmerge.lookahead_off)
h4_close     = request.security(syminfo.tickerid, "240", close,              lookahead=barmerge.lookahead_off)
h4_uptrend   = h4_close > h4_ema200
h4_downtrend = h4_close < h4_ema200
h4_buy_ok    = not i_h4 or h4_uptrend
h4_sell_ok   = not i_h4 or h4_downtrend
plot(h4_ema200, "H4 EMA 200", color=color.new(color.purple, 50), linewidth=1, style=plot.style_stepline)

// ══════════════════════════════════════════════════════════════
// TIMEFRAME GUARD
// ══════════════════════════════════════════════════════════════
is_m15 = i_anyTF or timeframe.period == "15"
if not is_m15 and barstate.islast
    label.new(bar_index, high, "Consultya: this script runs on the 15-minute timeframe only", color=color.yellow,
              textcolor=color.black, style=label.style_label_down, size=size.normal)

// ══════════════════════════════════════════════════════════════
// SUPPORT / RESISTANCE DETECTION
// ══════════════════════════════════════════════════════════════
var array<float> supArr  = array.new_float(0)
var array<float> resArr  = array.new_float(0)
var array<line>  supLns  = array.new_line(0)
var array<line>  resLns  = array.new_line(0)

f_near(arr, lvl, gap) =>
    found = false
    n = array.size(arr)
    if n > 0
        for j = 0 to n - 1
            if math.abs(array.get(arr, j) - lvl) < gap
                found := true
                break
    found

ph = ta.pivothigh(high, i_swing, i_swing)
pl = ta.pivotlow(low,   i_swing, i_swing)

if not na(ph) and is_m15
    gap = close * i_minD / 100.0
    if ph > close and not f_near(resArr, ph, gap)
        if array.size(resArr) >= i_maxR
            line.delete(array.shift(resLns))
            array.shift(resArr)
        array.push(resArr, ph)
        ln = line.new(bar_index - i_swing, ph, bar_index, ph,
             extend=extend.right, color=color.new(#FF4500, 20), style=line.style_dashed, width=1)
        array.push(resLns, ln)

if not na(pl) and is_m15
    gap = close * i_minD / 100.0
    if pl < close and not f_near(supArr, pl, gap)
        if array.size(supArr) >= i_maxS
            line.delete(array.shift(supLns))
            array.shift(supArr)
        array.push(supArr, pl)
        ln = line.new(bar_index - i_swing, pl, bar_index, pl,
             extend=extend.right, color=color.new(color.lime, 20), style=line.style_solid, width=1)
        array.push(supLns, ln)

// ══════════════════════════════════════════════════════════════
// SIGNAL STATE
// ══════════════════════════════════════════════════════════════
var int   lastSig  = -999
var bool  sigOn    = false
var bool  sigBuy   = true
var float sigEntry = na
var float sigSL    = na
var float sigSL0   = na    // the ORIGINAL stop - this is what defines 1R
var float sigTP1   = na
var float sigTP2   = na
var float sigTP3   = na
var int   sigBar   = -1
var bool  sigDone  = false
var bool  t1Hit    = false
var bool  t2Hit    = false

// Partial-exit bookkeeping for the signal currently running.
var float bookedR = 0.0   // R already banked from closed portions
var float openFr  = 1.0   // fraction of the position still open

// ══════════════════════════════════════════════════════════════
// OUTCOME COUNTERS
// ══════════════════════════════════════════════════════════════
var int   cTotal  = 0
var int   cTP3    = 0
var int   cTP2    = 0
var int   cT1Stop = 0
var int   cLoss   = 0
var int   cExp    = 0
var int   cWin    = 0   // finished above breakeven
var int   cFlat   = 0   // finished at breakeven
var int   cDown   = 0   // finished below breakeven
var float sumR    = 0.0
var float bestR   = 0.0
var float worstR  = 0.0
var float sumPts  = 0.0   // same results, in price points instead of R
var int   cSkip   = 0     // signals removed by the research filters
// v3: split of closed trades by their relation to US releases
var bool  sigNear = false
var bool  sigHeld = false
var int   cNear = 0
var int   cAway = 0
var int   cHeld = 0
var float sNear = 0.0
var float sAway = 0.0
var float sHeld = 0.0

// Return a price expressed in R. Positive means it sits beyond the entry
// in the trade's favour. Pine forbids a function writing to a global, so
// this only computes - the totals are updated at the call site.
f_R(float px) =>
    risk = math.abs(sigEntry - sigSL0)
    risk > 0 and not na(px) ? (sigBuy ? (px - sigEntry) / risk : (sigEntry - px) / risk) : na

// ══════════════════════════════════════════════════════════════
// INFO PANEL
// ══════════════════════════════════════════════════════════════
var table pnl = table.new(position.top_right, 2, 10, bgcolor=#0D1117, border_width=1, border_color=#2A2D4A, frame_width=2, frame_color=#2A2D4A)

// ══════════════════════════════════════════════════════════════
// HELPERS
// ══════════════════════════════════════════════════════════════
f_nearestAbove(tp) =>
    float best = na
    n = array.size(resArr)
    if n > 0
        for j = 0 to n - 1
            v = array.get(resArr, j)
            if v > tp and (na(best) or v < best)
                best := v
    best

f_nearestBelow(tp) =>
    float best = na
    n = array.size(supArr)
    if n > 0
        for j = 0 to n - 1
            v = array.get(supArr, j)
            if v < tp and (na(best) or v > best)
                best := v
    best

f_tps(isBuy, price, base, rng, atr) =>
    maxSL = price * (i_slPct / 100.0)
    sl_   = isBuy ? base - math.min(rng * 0.5, maxSL)
                  : base + math.min(rng * 0.5, maxSL)
    tp1_  = isBuy ? price + atr * i_tp1x
                  : price - atr * i_tp1x
    nr    = f_nearestAbove(tp1_)
    ns    = f_nearestBelow(tp1_)
    tp2_  = isBuy ? (not na(nr) and nr > tp1_ ? nr : price + atr * i_tp2x)
                  : (not na(ns) and ns < tp1_ ? ns : price - atr * i_tp2x)
    tp3r  = isBuy ? price + atr * i_tp3x : price - atr * i_tp3x
    tp3_  = isBuy ? (tp3r <= tp2_ ? tp2_ + (tp2_ - price) * 0.5 : tp3r)
                  : (tp3r >= tp2_ ? tp2_ - (price - tp2_) * 0.5 : tp3r)
    [sl_, tp1_, tp2_, tp3_]

// ══════════════════════════════════════════════════════════════
// MAIN LOGIC — evaluated on confirmed bars only (no repainting)
// ══════════════════════════════════════════════════════════════
if is_m15 and barstate.isconfirmed

    rng = high - low
    third = 1.0 / 3.0

    // Holds this bar's closed-trade result, if a signal closed on it.
    // At most one signal can close per bar, so one slot is enough.
    float barR = na

    // v3: a release came out while this trade was open
    if sigOn and not sigDone and sigBar >= 0 and bar_index > sigBar and relInBar
        sigHeld := true

    // ── Track the outcome of the active signal ───────────────────────────
    if sigOn and not sigDone and sigBar >= 0 and bar_index > sigBar

        if sigBuy
            if low <= sigSL
                label.new(bar_index, sigSL, "✕ SL",
                          color=#C83C3C, textcolor=color.white,
                          style=label.style_label_up, size=size.tiny)
                barR := bookedR + openFr * f_R(sigSL)
                sigOn   := false
                sigDone := true
                if t2Hit
                    cTP2 += 1
                else if t1Hit
                    cT1Stop += 1
                else
                    cLoss += 1

            else if high >= sigTP3
                if not t1Hit
                    label.new(bar_index, sigTP1, ">> TP1",
                              color=#00C8A0, textcolor=color.black,
                              style=label.style_label_down, size=size.tiny)
                    t1Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP1)
                        openFr  -= third
                if not t2Hit
                    label.new(bar_index, sigTP2, ">> TP2",
                              color=#00D0A8, textcolor=color.black,
                              style=label.style_label_down, size=size.tiny)
                    t2Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP2)
                        openFr  -= third
                label.new(bar_index, sigTP3, ">> TP3",
                          color=#00EB78, textcolor=color.black,
                          style=label.style_label_down, size=size.tiny)
                barR := bookedR + openFr * f_R(sigTP3)
                bookedR := barR
                openFr  := 0.0
                sigDone := true
                cTP3 += 1

            else if not t2Hit and high >= sigTP2
                if not t1Hit
                    label.new(bar_index, sigTP1, ">> TP1",
                              color=#00C8A0, textcolor=color.black,
                              style=label.style_label_down, size=size.tiny)
                    t1Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP1)
                        openFr  -= third
                label.new(bar_index, sigTP2, ">> TP2",
                          color=#00D0A8, textcolor=color.black,
                          style=label.style_label_down, size=size.tiny)
                t2Hit := true
                if i_partial
                    bookedR += third * f_R(sigTP2)
                    openFr  -= third
                sigSL := sigTP1

            else if not t1Hit and high >= sigTP1
                label.new(bar_index, sigTP1, ">> TP1",
                          color=#00C8A0, textcolor=color.black,
                          style=label.style_label_down, size=size.tiny)
                t1Hit := true
                if i_partial
                    bookedR += third * f_R(sigTP1)
                    openFr  -= third
                if i_beTP1
                    sigSL := sigEntry

        else  // SELL
            if high >= sigSL
                label.new(bar_index, sigSL, "✕ SL",
                          color=#C83C3C, textcolor=color.white,
                          style=label.style_label_down, size=size.tiny)
                barR := bookedR + openFr * f_R(sigSL)
                sigOn   := false
                sigDone := true
                if t2Hit
                    cTP2 += 1
                else if t1Hit
                    cT1Stop += 1
                else
                    cLoss += 1

            else if low <= sigTP3
                if not t1Hit
                    label.new(bar_index, sigTP1, ">> TP1",
                              color=#00C8A0, textcolor=color.black,
                              style=label.style_label_up, size=size.tiny)
                    t1Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP1)
                        openFr  -= third
                if not t2Hit
                    label.new(bar_index, sigTP2, ">> TP2",
                              color=#00D0A8, textcolor=color.black,
                              style=label.style_label_up, size=size.tiny)
                    t2Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP2)
                        openFr  -= third
                label.new(bar_index, sigTP3, ">> TP3",
                          color=#00EB78, textcolor=color.black,
                          style=label.style_label_up, size=size.tiny)
                barR := bookedR + openFr * f_R(sigTP3)
                bookedR := barR
                openFr  := 0.0
                sigDone := true
                cTP3 += 1

            else if not t2Hit and low <= sigTP2
                if not t1Hit
                    label.new(bar_index, sigTP1, ">> TP1",
                              color=#00C8A0, textcolor=color.black,
                              style=label.style_label_up, size=size.tiny)
                    t1Hit := true
                    if i_partial
                        bookedR += third * f_R(sigTP1)
                        openFr  -= third
                label.new(bar_index, sigTP2, ">> TP2",
                          color=#00D0A8, textcolor=color.black,
                          style=label.style_label_up, size=size.tiny)
                t2Hit := true
                if i_partial
                    bookedR += third * f_R(sigTP2)
                    openFr  -= third
                sigSL := sigTP1

            else if not t1Hit and low <= sigTP1
                label.new(bar_index, sigTP1, ">> TP1",
                          color=#00C8A0, textcolor=color.black,
                          style=label.style_label_up, size=size.tiny)
                t1Hit := true
                if i_partial
                    bookedR += third * f_R(sigTP1)
                    openFr  -= third
                if i_beTP1
                    sigSL := sigEntry

    // ── Close the signal once it has run out of time ─────────────────────
    if sigOn and not sigDone and i_exp > 0 and bar_index - sigBar >= i_exp
        label.new(bar_index, sigBuy ? low : high, "⏳",
                  color=#808080, textcolor=color.white,
                  style=sigBuy ? label.style_label_up : label.style_label_down, size=size.tiny)
        barR := bookedR + openFr * f_R(close)
        sigOn   := false
        sigDone := true
        if t2Hit
            cTP2 += 1
        else if t1Hit
            cT1Stop += 1
        else
            cExp += 1

    // ── Fold this bar's result into the running totals ───────────────────
    if not na(barR)
        sumR   += barR
        sumPts += barR * math.abs(sigEntry - sigSL0)
        bestR  := math.max(bestR, barR)
        worstR := math.min(worstR, barR)
        if sigNear
            cNear += 1
            sNear += barR
        else
            cAway += 1
            sAway += barR
        if sigHeld
            cHeld += 1
            sHeld += barR
        if barR > 0.001
            cWin += 1
        else if barR < -0.001
            cDown += 1
        else
            cFlat += 1

    // ── Signal detection ─────────────────────────────────────────────────
    // Nothing fires before the research start date, so the tally always
    // covers the same window regardless of how many bars loaded.
    inWindow = time >= i_start
    canSig   = inWindow and bar_index - lastSig >= i_cool
    actBuy   = sigOn and not sigDone and     sigBuy
    actSell  = sigOn and not sigDone and not sigBuy

    fire_buy  = false
    fire_sell = false
    buy_base  = low
    sell_base = high
    buy_clr   = color.lime
    sell_clr  = color.orange

    if canSig
        upTrd   = ema20 > ema50
        dnTrd   = ema20 < ema50
        abv200  = close > ema200
        blw200  = close < ema200
        strongU = rng > 0 and (close - low)  >= rng * 0.55
        strongD = rng > 0 and (high - close) >= rng * 0.55
        rsiOk   = rsiVal >= 35.0 and rsiVal <= 65.0

        // ── Signal 1: EMA Bounce ─────────────────────────────────────────
        if upTrd and abv200 and low <= ema20 and close > ema20 and strongU and rsiOk and h4_buy_ok and not actBuy
            fire_buy := true
            buy_base := low
            buy_clr  := color.lime

        else if dnTrd and blw200 and high >= ema20 and close < ema20 and strongD and rsiOk and h4_sell_ok and not actSell
            fire_sell := true
            sell_base := high
            sell_clr  := color.orange

        // ── Signal 2: SR Bounce ──────────────────────────────────────────
        if not fire_buy and not fire_sell
            tol = close * 0.0015

            n_s = array.size(supArr)
            if n_s > 0
                for j = 0 to n_s - 1
                    lvl = array.get(supArr, j)
                    if lvl < close and low <= lvl + tol and low >= lvl - tol * 2.0 and
                         (close - low) >= rng * 0.55 and rsiVal < 60.0 and h4_buy_ok and not actBuy
                        fire_buy := true
                        buy_base := lvl
                        buy_clr  := color.aqua
                        break

            if not fire_buy
                n_r = array.size(resArr)
                if n_r > 0
                    for j = 0 to n_r - 1
                        lvl = array.get(resArr, j)
                        if lvl > close and high >= lvl - tol and high <= lvl + tol * 2.0 and
                             (high - close) >= rng * 0.55 and rsiVal > 40.0 and h4_sell_ok and not actSell
                            fire_sell := true
                            sell_base := lvl
                            sell_clr  := color.yellow
                            break

        // ── Signal 3: N-Bar Breakout ─────────────────────────────────────
        if not fire_buy and not fire_sell and bar_index >= i_brk + 1
            hh = ta.highest(high, i_brk)[1]
            ll = ta.lowest(low,   i_brk)[1]

            if close > hh and rng > 0 and (close - low) >= rng * 0.5 and rsiVal < i_brkHi and h4_buy_ok and not actBuy
                fire_buy  := true
                buy_base  := low
                buy_clr   := color.white
            else if close < ll and rng > 0 and (high - close) >= rng * 0.5 and rsiVal > i_brkLo and h4_sell_ok and not actSell
                fire_sell := true
                sell_base := high
                sell_clr  := color.fuchsia

    // ── Research filters: drop the signal, but show and count it ───────
    if (fire_buy or fire_sell) and not filtOk
        cSkip += 1
        label.new(bar_index, fire_buy ? low - close * 0.002 : high + close * 0.002, "skip",
                  color=color.new(color.gray, 55), textcolor=color.white,
                  style=fire_buy ? label.style_label_up : label.style_label_down, size=size.tiny, tooltip=skipWhy)
        fire_buy  := false
        fire_sell := false

    // ── Emit the signal and store its levels ─────────────────────────────
    if fire_buy
        [sl_, tp1_, tp2_, tp3_] = f_tps(true, close, buy_base, rng, atrVal)
        off = close * 0.002
        tip = "Entry: " + str.tostring(close, format.mintick) +
              "  SL: "  + str.tostring(sl_,   format.mintick) +
              "  TP1: " + str.tostring(tp1_,  format.mintick)
        label.new(bar_index, buy_base - off, "",
                  color=buy_clr, style=label.style_arrowup, size=size.small, tooltip=tip)
        sigOn    := true
        sigBuy   := true
        sigEntry := close
        sigSL    := sl_
        sigSL0   := sl_
        sigTP1   := tp1_
        sigTP2   := tp2_
        sigTP3   := tp3_
        sigBar   := bar_index
        sigDone  := false
        t1Hit    := false
        t2Hit    := false
        bookedR  := 0.0
        openFr   := 1.0
        lastSig  := bar_index
        sigNear  := nearNow
        sigHeld  := false
        cTotal   += 1

    if fire_sell and not fire_buy
        [sl_, tp1_, tp2_, tp3_] = f_tps(false, close, sell_base, rng, atrVal)
        off = close * 0.002
        tip = "Entry: " + str.tostring(close,  format.mintick) +
              "  SL: "  + str.tostring(sl_,    format.mintick) +
              "  TP1: " + str.tostring(tp1_,   format.mintick)
        label.new(bar_index, sell_base + off, "",
                  color=sell_clr, style=label.style_arrowdown, size=size.small, tooltip=tip)
        sigOn    := true
        sigBuy   := false
        sigEntry := close
        sigSL    := sl_
        sigSL0   := sl_
        sigTP1   := tp1_
        sigTP2   := tp2_
        sigTP3   := tp3_
        sigBar   := bar_index
        sigDone  := false
        t1Hit    := false
        t2Hit    := false
        bookedR  := 0.0
        openFr   := 1.0
        lastSig  := bar_index
        sigNear  := nearNow
        sigHeld  := false
        cTotal   += 1

// ══════════════════════════════════════════════════════════════
// PANEL UPDATE — refreshes on the last bar
// ══════════════════════════════════════════════════════════════
if is_m15 and barstate.islast and sigOn
    barsEl   = bar_index - sigBar
    expired  = not sigDone and i_exp > 0 and barsEl >= i_exp

    stat_txt = expired ? "-- Expired" :
               sigDone ? ">> TP3 hit — signal complete" :
               t2Hit   ? ">> TP2 hit — SL moved to TP1" :
               t1Hit   ? (i_beTP1 ? ">> TP1 hit — SL at entry" : ">> TP1 hit — stop unchanged") :
                         "● Active"
    stat_clr = expired ? #808080 : sigDone ? #00EB78 : t2Hit ? #00D0A0 : t1Hit ? #00C0B0 : #50B450

    dir_txt = sigBuy ? "BUY  +" : "SELL  -"
    dir_clr = sigBuy ? #00D25A  : #DC3C3C

    sl_lbl  = str.tostring(sigSL, format.mintick) + (t2Hit ? " [TP1]" : t1Hit and i_beTP1 ? " [BE]" : "")

    hdr_bg  = #1A1E3A
    row_bg  = #0D1117
    dir_bg  = sigBuy ? #003A18 : #3A0010

    table.cell(pnl, 0, 0, "▣ CONSULTYA", text_color=#8AB4FF, text_size=size.normal, bgcolor=hdr_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 0, "",            text_color=#8AB4FF, text_size=size.normal, bgcolor=hdr_bg)
    table.cell(pnl, 0, 1, dir_txt,       text_color=dir_clr, text_size=size.large, bgcolor=dir_bg, text_halign=text.align_center)
    table.cell(pnl, 1, 1, "",            text_color=dir_clr, text_size=size.large, bgcolor=dir_bg)
    table.cell(pnl, 0, 2, "Entry",       text_color=#BBBBBB, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 2, str.tostring(sigEntry, format.mintick), text_color=color.white, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 3, "SL",          text_color=#FF6060, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 3, sl_lbl,        text_color=#FF8080, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 4, "TP1",         text_color=#BBBBBB, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 4, str.tostring(sigTP1, format.mintick), text_color=#64C8FF, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 5, "TP2",         text_color=#BBBBBB, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 5, str.tostring(sigTP2, format.mintick), text_color=#00E5CC, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 6, "TP3",         text_color=#BBBBBB, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 6, str.tostring(sigTP3, format.mintick), text_color=#00FF88, text_size=size.normal, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 7, "Booked",      text_color=#555577, text_size=size.small,  bgcolor=row_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 7, str.tostring(bookedR, "#.##") + "R  ·  " + str.tostring(openFr * 100, "#") + "% open", text_color=#8899AA, text_size=size.small, bgcolor=row_bg, text_halign=text.align_right)
    table.cell(pnl, 0, 8, stat_txt,      text_color=stat_clr, text_size=size.normal, bgcolor=hdr_bg, text_halign=text.align_left)
    table.cell(pnl, 1, 8, "",            text_color=stat_clr, text_size=size.normal, bgcolor=hdr_bg)
    h4_lbl = not i_h4 ? "H4 OFF" : h4_uptrend ? "H4 uptrend" : "H4 downtrend"
    h4_clr = not i_h4 ? #555555 : h4_uptrend  ? #00C853     : #FF1744
    table.cell(pnl, 0, 9, h4_lbl,        text_color=h4_clr,   text_size=size.small,  bgcolor=#0A0D15, text_halign=text.align_left)
    table.cell(pnl, 1, 9, "",            text_color=h4_clr,   text_size=size.small,  bgcolor=#0A0D15)

// ══════════════════════════════════════════════════════════════
// RESEARCH TALLY
// ══════════════════════════════════════════════════════════════
tallyPos = i_tallyPos == "Bottom left" ? position.bottom_left : i_tallyPos == "Top left" ? position.top_left : i_tallyPos == "Middle left" ? position.middle_left : i_tallyPos == "Middle right" ? position.middle_right : position.bottom_right
var table stats = table.new(tallyPos, 2, 17, bgcolor=#0D1117, border_width=1, border_color=#2A2D4A, frame_width=2, frame_color=#2A2D4A)

// v3 tally helpers (functions must live at the top level in Pine)
grp(int n, float r) =>
    n > 0 ? str.tostring(n) + "  ·  " + str.tostring(r / n, "#.###") + "R  ·  " + str.tostring(r, "#.#") + "R" : "-"
grpClr(int n, float r) =>
    n == 0 ? #9AAABB : r > 0 ? #00E58A : r < 0 ? #FF6060 : #9AAABB

if is_m15 and barstate.islast and i_stats
    closed = cWin + cFlat + cDown
    avgR   = closed > 0 ? sumR / closed : 0.0

    // With partial exits on, "profitable" means the trade finished with
    // money banked - which is what a subscriber actually experiences.
    wrTxt  = closed > 0 ? str.tostring(cWin * 100.0 / closed, "#.#") + "%" : "-"
    t1Txt  = closed > 0 ? str.tostring((cTP3 + cTP2 + cT1Stop) * 100.0 / closed, "#.#") + "%" : "-"

    rClr   = sumR > 0 ? #00E58A : sumR < 0 ? #FF6060 : #9AAABB
    modeTxt = (i_partial ? "thirds" : "all-or-nothing") + (i_beTP1 ? " · BE at TP1" : " · stop to TP2")

    // Under roughly 200 closed trades the numbers are mostly noise, so
    // the sample size is flagged rather than left for the eye to judge.
    nTxt   = closed >= 300 ? "" : closed >= 200 ? "  (thin)" : "  (TOO FEW)"
    nClr   = closed >= 300 ? #8899AA : closed >= 200 ? #F0C040 : #FF6060

    sHdr = #1A1E3A
    sRow = #0D1117

    table.cell(stats, 0, 0, "RESEARCH TALLY", text_color=#8AB4FF, text_size=size.normal, bgcolor=sHdr, text_halign=text.align_left)
    table.cell(stats, 1, 0, str.tostring(cTotal) + " fired", text_color=color.white, text_size=size.normal, bgcolor=sHdr, text_halign=text.align_right)

    table.cell(stats, 0, 1, "Total R", text_color=#BBBBBB, text_size=size.large, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 1, str.tostring(sumR, "#.#") + "R", text_color=rClr, text_size=size.large, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 2, "Avg R per trade", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 2, str.tostring(avgR, "#.###") + "R", text_color=rClr, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 3, "Closed in profit", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 3, wrTxt, text_color=#00E58A, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 4, "Reached TP1", text_color=#BBBBBB, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 4, t1Txt, text_color=#9AAABB, text_size=size.small, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 5, "Best / worst", text_color=#BBBBBB, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 5, str.tostring(bestR, "#.#") + "R / " + str.tostring(worstR, "#.#") + "R", text_color=#9AAABB, text_size=size.small, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 6, "Profit / flat / loss", text_color=#BBBBBB, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 6, str.tostring(cWin) + " / " + str.tostring(cFlat) + " / " + str.tostring(cDown), text_color=#9AAABB, text_size=size.small, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 7, "TP3 reached", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 7, str.tostring(cTP3), text_color=#00FF88, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 8, "TP2, closed at TP1", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 8, str.tostring(cTP2), text_color=#00E5CC, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 9, "TP1, then stopped", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 9, str.tostring(cT1Stop), text_color=#64C8FF, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    table.cell(stats, 0, 10, modeTxt, text_color=#8899AA, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_left)
    table.cell(stats, 1, 10, str.tostring(closed) + " closed" + nTxt, text_color=nClr, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_right)

    table.cell(stats, 0, 11, "Total points", text_color=#BBBBBB, text_size=size.normal, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 11, str.tostring(sumPts, "#.#"), text_color=rClr, text_size=size.normal, bgcolor=sRow, text_halign=text.align_right)

    fTxt = (i_fChop ? "A " : "") + (i_f200 ? "B " : "") + (i_fNews ? "C " : "") + (i_dMode != D_OFF ? "D" : "")
    table.cell(stats, 0, 12, "Filtered out" + (fTxt == "" ? "  (filters off)" : "  (" + str.trim(fTxt) + ")"), text_color=#8899AA, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_left)
    table.cell(stats, 1, 12, str.tostring(cSkip), text_color=#9AAABB, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_right)

    // v3: closed trades split by their relation to US releases
    table.cell(stats, 0, 13, "Entered near a release", text_color=#F0C040, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 13, grp(cNear, sNear), text_color=grpClr(cNear, sNear), text_size=size.small, bgcolor=sRow, text_halign=text.align_right)
    table.cell(stats, 0, 14, "Entered away from releases", text_color=#BBBBBB, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 14, grp(cAway, sAway), text_color=grpClr(cAway, sAway), text_size=size.small, bgcolor=sRow, text_halign=text.align_right)
    table.cell(stats, 0, 15, "Open when a release came out", text_color=#BBBBBB, text_size=size.small, bgcolor=sRow, text_halign=text.align_left)
    table.cell(stats, 1, 15, grp(cHeld, sHeld), text_color=grpClr(cHeld, sHeld), text_size=size.small, bgcolor=sRow, text_halign=text.align_right)
    table.cell(stats, 0, 16, "Window -" + str.tostring(i_dBefore) + " / +" + str.tostring(i_dAfter) + " min · " + i_dSet, text_color=#8899AA, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_left)
    table.cell(stats, 1, 16, "n  ·  avg R  ·  total R", text_color=#8899AA, text_size=size.small, bgcolor=#0A0D15, text_halign=text.align_right)

أداة قياس، لا نظام تداول. لا تحاكي فرق السعر أو الانزلاق أو حجم الصفقة أو تكاليف التداول الأخرى لدى الوسيط.

© 2026 Consultya — منشورة للاطّلاع والتحقّق.

اقرأ منهجية القياس والأرقام بحدودها ←