Real edges, proven on real data, free to copy.

Backtested on 8 years of data and verified on real ticks, fees and slippage included.

1B+ real ticks 32M options rows 2019–2026 $0 MIT license

Shield Battery

01
Open on TradingView ↗

One 1:00 PM ET Nasdaq entry, taken only when options dealers are long gamma.

01
Regime

ES opens above the SPX 14-day gamma flip.

02
Calendar

Skip Fed FOMC decision days.

03
Entry

Buy 1 MNQ if 1:00 PM ET closes above VWAP.

04
Exit

2x 15m ATR stop. Flat at 3:55 PM ET.

Backtest

1 MNQ, 2019–2026. Includes 1-tick slippage and $1.24 round-trip fees.

+$9,711Net profit
57.5%Win rate
-$1,096Max drawdown
1.37Profit factor
7 / 8Green years
Filter ON (+$9,711) Filter OFF (-$1,730)
Period Trades Profit Win Rate Max Drawdown

Real 1-tick replay (2023–2025, 221 trades): +$2,190 vs. +$1,736 on 1m bars.

Date Contract Stop (pts) Profit Total Exit

Code

Copy into TradingView or NinjaTrader 8.

Open on TradingView ↗ GitHub
//@version=6
// ============================================================================
// OpenSourceEdge - Build Order #01: Shield Battery (Long-Gamma Drift, TV v6)
// ============================================================================
// Author:  @OpenSourceEdge
// License: MIT (Free to use, modify, fork, and backtest)
// Chart:   MNQ1! (or NQ1!) on the 1-Minute or 5-Minute timeframe
// Scouted on 1B+ real ticks & 32M options rows. Strictly causal. No map hacks.
//
// QUICK START (BEGINNERS):
// 1. Open MNQ1! on a 1m (live) or 5m (longer free backtest) chart.
// 2. Default mode ("Auto Free Proxy") works immediately with zero setup.
// 3. Phone alerts at 13:00 ET (entry + exact stop) and 15:55 ET (flat):
//    Alerts clock -> Condition: OSE Shield Battery -> Order fills and alert()
//    function calls -> Create.
// 4. Optional Pro Mode ("Live Manual Gamma Flip"): toggle Positive Gamma Today
//    before 09:30 ET using pre-market SPX 14-DTE Gamma Flip data.
// ============================================================================

strategy("OpenSourceEdge: Shield Battery (Long-Gamma 13:00)", shorttitle = "OSE Shield Battery", overlay = true, initial_capital = 50000, default_qty_type = strategy.fixed, default_qty_value = 1, margin_long = 0, margin_short = 0, commission_type = strategy.commission.cash_per_contract, commission_value = 0.62, slippage = 1, calc_on_every_tick = false, process_orders_on_close = false)

// --- Inputs (display.none keeps the chart legend clean) ---
grpReg = "1. Dealer Gamma Regime"
regimeMode     = input.string("Auto Free Proxy (VIX/VIX3M)", "Regime Mode", options = ["Auto Free Proxy (VIX/VIX3M)", "Live Manual Gamma Flip"], group = grpReg, display = display.none)
vixRatioMax    = input.float(0.98, "Proxy: Max Prior-Day VIX/VIX3M", step = 0.01, minval = 0.70, maxval = 1.20, group = grpReg, display = display.none)
useVix9dVeto   = input.bool(true, "Skip VIX9D/VIX >= 1.05 (Short-Term Vol Stress)", group = grpReg, display = display.none)
vix9dMax       = input.float(1.05, "Max Prior-Day VIX9D/VIX", step = 0.01, minval = 0.80, maxval = 1.50, group = grpReg, display = display.none)
manualPosGamma = input.bool(true, "Live Mode: Positive Gamma Today (ES Open > SPX Flip)", group = grpReg, display = display.none)

grpSig = "2. Signal & Risk"
checkHourEt    = input.int(13, "Check Hour (ET)", minval = 12, maxval = 15, group = grpSig, display = display.none)
checkMinEt     = input.int(0,  "Check Minute (ET)", minval = 0, maxval = 59, group = grpSig, display = display.none)
exitHourEt     = input.int(15, "Flat Hour (ET)", minval = 13, maxval = 15, group = grpSig, display = display.none)
exitMinEt      = input.int(55, "Flat Minute (ET)", minval = 0, maxval = 59, group = grpSig, display = display.none)
stopAtrMult    = input.float(2.0, "Stop Loss (ATR 15m Multiple)", step = 0.25, minval = 0.5, maxval = 5.0, group = grpSig, display = display.none)
minStopTicks   = input.int(16, "Min Stop (Ticks)", minval = 4, maxval = 200, group = grpSig, display = display.none)
maxStopPoints  = input.float(200.0, "Max Stop Cap (Points, 0 = Off)", minval = 0.0, group = grpSig, display = display.none)
skipFomc       = input.bool(true, "Skip Scheduled FOMC Days (2024-2026)", group = grpSig, display = display.none)

grpUi = "3. Display"
showTable      = input.bool(true, "Show Plain-English Status Panel", group = grpUi, display = display.none)
showZones      = input.bool(true, "Show Entry & Stop Risk Zone Box", group = grpUi, display = display.none)

// --- Strictly Causal Prior-Day CBOE Term Structure (Zero Lookahead) ---
vixPrev   = request.security("CBOE:VIX",   "D", close[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
vix3mPrev = request.security("CBOE:VIX3M", "D", close[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
vix9dPrev = request.security("CBOE:VIX9D", "D", close[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)

vixRatio   = (not na(vixPrev) and not na(vix3mPrev) and vix3mPrev > 0) ? (vixPrev / vix3mPrev) : na
vix9dRatio = (not na(vix9dPrev) and not na(vixPrev) and vixPrev > 0) ? (vix9dPrev / vixPrev) : na

// Causal 15-minute ATR (14-bar SMA of 15m True Range, using closed 15m bar)
atr15m = request.security(syminfo.tickerid, "15", ta.sma(ta.tr(true), 14)[1], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)

// --- Eastern Time Session Clock ---
etYear   = year(time_close, "America/New_York")
etMonth  = month(time_close, "America/New_York")
etDay    = dayofmonth(time_close, "America/New_York")
etHour   = hour(time_close, "America/New_York")
etMin    = minute(time_close, "America/New_York")
etTotalM = etHour * 60 + etMin
dayKey   = etYear * 10000 + etMonth * 100 + etDay

isValidTf   = timeframe.isintraday and (timeframe.multiplier == 1 or timeframe.multiplier == 5)
isRth       = etTotalM > (9 * 60 + 30) and etTotalM <= (16 * 60)
isNewRthDay = isRth and not isRth[1] and (na(dayKey[1]) or dayKey != dayKey[1] or etTotalM <= 9 * 60 + 35)

// Built-in Scheduled FOMC Decision Dates (2024-2026)
var array<int> fomcDates = array.from(20240131, 20240320, 20240501, 20240612, 20240731, 20240918, 20241107, 20241218, 20250129, 20250319, 20250507, 20250618, 20250730, 20250917, 20251029, 20251210, 20260128, 20260318, 20260429, 20260617, 20260729, 20260916, 20261028, 20261209)
isFomcDay = skipFomc and array.includes(fomcDates, dayKey)

// --- RTH VWAP Calculation (Anchored strictly at 09:30 ET) ---
var float cumPV       = na
var float cumV        = na
var bool  tradedToday = false
var float activeStop  = na

if isNewRthDay
    cumPV       := 0.0
    cumV        := 0.0
    tradedToday := false
    activeStop  := na

if isRth
    tp  = (high + low + close) / 3.0
    vol = math.max(volume, 0.0)
    cumPV += tp * vol
    cumV  += vol

rthVwap = (isRth and cumV > 0) ? (cumPV / cumV) : na

// --- Regime Qualification ---
isManualMode  = regimeMode == "Live Manual Gamma Flip"
proxyPosGamma = not na(vixRatio) and vixRatio < vixRatioMax
isPosGamma    = isManualMode ? manualPosGamma : proxyPosGamma
vix9dBlocked  = useVix9dVeto and not na(vix9dRatio) and vix9dRatio >= vix9dMax
regimeAllowed = isPosGamma and not isFomcDay and not vix9dBlocked

// --- Entry & Exit Logic ---
checkTargetMin = checkHourEt * 60 + checkMinEt
exitTargetMin  = exitHourEt * 60 + exitMinEt

isCheckBar = isValidTf and isRth and etTotalM == checkTargetMin
isExitBar  = isRth and etTotalM >= exitTargetMin

rawStopDist = not na(atr15m) ? (stopAtrMult * atr15m) : na
minStopDist = minStopTicks * syminfo.mintick
stopDist    = na(rawStopDist) ? na : math.max(minStopDist, rawStopDist)
if maxStopPoints > 0 and not na(stopDist)
    stopDist := math.min(stopDist, maxStopPoints)

longSignal = isCheckBar and not tradedToday and strategy.position_size == 0 and regimeAllowed and not na(rthVwap) and close > rthVwap and not na(stopDist)

if longSignal
    stopPrice   = close - stopDist
    activeStop  := stopPrice
    tradedToday := true
    strategy.entry("LONG", strategy.long, comment = "Shield Battery")
    strategy.exit("STOP", from_entry = "LONG", stop = stopPrice, comment = "Stop")
    alert("OpenSourceEdge Shield Battery BUY 13:00: " + syminfo.ticker + " at " + str.tostring(close, format.mintick) + " | Hard Stop: " + str.tostring(stopPrice, format.mintick) + " (" + str.tostring(stopDist, "#.##") + " pts) | Flat 15:55 ET", alert.freq_once_per_bar_close)

if strategy.position_size > 0 and isExitBar
    strategy.close("LONG", comment = "Flat 15:55")
    alert("OpenSourceEdge Shield Battery FLAT 15:55 ET: Close " + syminfo.ticker + " long position.", alert.freq_once_per_bar_close)

if strategy.position_size == 0 and not longSignal and strategy.position_size[1] == 0
    activeStop := na

// --- Clean Position-Box Visuals (Zero Overlapping Labels, Light & Dark Ready) ---
inLong      = strategy.position_size > 0
justClosed  = strategy.position_size == 0 and strategy.position_size[1] > 0
showTrade   = showZones and (inLong or justClosed)
entryPlot   = showTrade ? (inLong ? strategy.position_avg_price : strategy.position_avg_price[1]) : na
stopPlot    = showTrade ? activeStop : na
profitPlot  = (showTrade and not na(entryPlot) and close > entryPlot) ? close : entryPlot

plot(rthVwap, "RTH VWAP", color = color.new(#0D9488, 15), linewidth = 2, style = plot.style_linebr)
pEntry  = plot(entryPlot, "Entry Price", color = color.new(#0D9488, 0), linewidth = 2, style = plot.style_linebr)
pStop   = plot(stopPlot, "Hard Stop (2x ATR15)", color = color.new(#EF4444, 0), linewidth = 2, style = plot.style_linebr)
pProfit = plot(profitPlot, "Open Profit Edge", color = color.new(#10B981, 100), linewidth = 1, style = plot.style_linebr, display = display.pane)

fill(pEntry, pStop, color = color.new(#EF4444, 88), title = "Risk Zone")
fill(pEntry, pProfit, color = color.new(#10B981, 86), title = "Profit Zone")

// --- Dummy-Proof Plain-English Status Panel (Readable Size) ---
var table tbl = table.new(position.top_right, 2, 6, bgcolor = color.new(#0B0E14, 6), border_color = #334155, border_width = 1, frame_color = #334155, frame_width = 1)
if showTable and barstate.islast
    actionTxt = not isValidTf ? "USE 1M OR 5M CHART" : (inLong ? "IN LONG (FLAT 15:55)" : (tradedToday ? "DONE FOR TODAY" : (isFomcDay ? "SKIP TODAY (FOMC)" : (vix9dBlocked ? "SKIP (VOL STRESS)" : (not isPosGamma ? "SKIP (NEG GAMMA)" : (isRth and etTotalM > checkTargetMin ? "NO SIGNAL TODAY" : "WATCH 13:00 ET"))))))
    actionCol = not isValidTf ? #F87171 : (inLong or actionTxt == "WATCH 13:00 ET" ? #2DD4BF : #94A3B8)
    modeTxt   = isManualMode ? "MANUAL FLIP" : (na(vixRatio) ? "N/A" : str.tostring(vixRatio, "#.##") + " (VIX/3M)")
    stopTxt   = (inLong and not na(activeStop)) ? str.tostring(activeStop, format.mintick) : (na(stopDist) ? "N/A" : str.tostring(stopDist, "#.##") + " pts")
    stopLbl   = (inLong and not na(activeStop)) ? "Active Hard Stop" : "Est. Stop (2x ATR)"

    table.cell(tbl, 0, 0, "OpenSourceEdge", text_color = #F3F5F8, text_size = size.large, text_halign = text.align_left)
    table.cell(tbl, 1, 0, "Shield Battery", text_color = #2DD4BF, text_size = size.large, text_halign = text.align_right)
    table.cell(tbl, 0, 1, "Today's Action", text_color = #94A3B8, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 1, actionTxt, text_color = actionCol, text_size = size.normal, text_halign = text.align_right)
    table.cell(tbl, 0, 2, "Dealer Gamma", text_color = #94A3B8, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 2, isPosGamma ? "POSITIVE (SHIELDS ON)" : "NEGATIVE (OFFLINE)", text_color = isPosGamma ? #2DD4BF : #F87171, text_size = size.normal, text_halign = text.align_right)
    table.cell(tbl, 0, 3, "Regime Input", text_color = #94A3B8, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 3, modeTxt, text_color = #F3F5F8, text_size = size.normal, text_halign = text.align_right)
    table.cell(tbl, 0, 4, stopLbl, text_color = #94A3B8, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 4, stopTxt, text_color = (inLong and not na(activeStop)) ? #F87171 : #F3F5F8, text_size = size.normal, text_halign = text.align_right)
    table.cell(tbl, 0, 5, "Rule", text_color = #94A3B8, text_size = size.normal, text_halign = text.align_left)
    table.cell(tbl, 1, 5, "MNQ 1m/5m · > VWAP", text_color = #CBD5E1, text_size = size.normal, text_halign = text.align_right)
MNQ1! 1m or 5m · Alerts: OSE Shield Battery
// ============================================================================
// OpenSourceEdge - Build Order #01: Shield Battery (Long-Gamma Drift, NT8)
// ============================================================================
// Author:  @OpenSourceEdge
// License: MIT (Free to use, modify, fork, and backtest)
// Scouted on 1B+ real ticks & 32M options rows. Strictly causal. No map hacks.
// Disclaimer: Educational and quantitative research software only. Not
// financial advice. Verify your broker or prop firm rules before running
// automated order execution.
//
// MECHANISM:
// When ES opens at 09:30 ET above the causally mapped pre-market SPX 14-DTE
// Gamma Flip (Positive Gamma), options dealers hedge by buying dips and
// selling rips. As daily options decay in the afternoon, volatility stays
// suppressed and equities tend to grind steadily higher into the close.
//
// RULES:
// 1. Pre-market regime: ES 09:30 ET open > mapped SPX 14-DTE Gamma Flip:
//      flip_es = es_prior_close * (d14_gamma_flip / spx_prior_close)
//    (Fallback if flip is blank: d14_net_gex_b > 0).
// 2. Pause on scheduled Fed FOMC decision days (built-in 2019-2026 calendar).
// 3. Optional dual-period regime vetoes (all pass through if column missing):
//    - VIX9D/VIX >= 1.05 (short-term vol inversion)
//    - QQQ 60-day put/call volume percentile <= 0.15 (call frenzy)
// 4. Signal: at 13:00 ET, if MNQ closes strictly above RTH VWAP (09:30+),
//    buy at market on the 1-tick execution series.
// 5. Risk: hard stop at max(16 ticks, round(2.0 x ATR15)), capped by MaxStopPoints.
// 6. Exit: flat at 15:55 ET (hard safety clock at 15:56 ET). Max 1 trade/day.
//
// CHART SETUP:
// - Instrument: MNQ (front contract, e.g. MNQ 12-26)
// - Bars: 1 Minute | Trading Hours: CME US Index Futures ETH | Days to load: 40+
// - CSV Path: defaults to Documents\NinjaTrader 8\OpenSourceEdge\daily_regime.csv
//   (auto-reloads live in real time whenever updated pre-market)
// ============================================================================

#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Strategies.OpenSourceEdgeLgCore;
#endregion

namespace NinjaTrader.NinjaScript.Strategies
{
    public class LongGammaDriftOpenSource : Strategy
    {
        private LgCore core;
        private LgRegimeTable regime;
        private bool refused, runWritten, optimizing;
        private string configProblem, esName = "", resolvedCsvPath = "";
        private TimeZoneInfo eastern;
        private DateTime offsetKey = DateTime.MinValue;
        private TimeSpan offsetToEastern;
        private StreamWriter writer;
        private SessionIterator sessionIterator;
        private System.Timers.Timer clock;
        private int checkMinute, exitMinute;
        private DateTime lastTradeEt = DateTime.MinValue;

        private DateTime railDay = DateTime.MinValue, entryDay = DateTime.MinValue;
        private bool haltedToday, exitPending;
        private double realizedToday;
        private string exitPendingName = "";
        private LgDecision todaySignal;

        private int openQty, closedQty, openStopTicks;
        private DateTime openTime, lastExitTime;
        private double openValue, exitValue, openCommission, exitCommission;
        private string openNote = "", lastExitName = "";
        private int tradesLogged, signalsPlaced;

        private const int TickIndex = 1;
        private const int EsIndex = 2;
        private const string LongName = "OSE LG Long";
        private const string FlatName = "OSE LG Flat";
        private const string SafetyName = "OSE LG Safety";
        private const string DailyLossName = "OSE LG DLL";
        private static readonly TimeSpan HardFlat = new TimeSpan(15, 56, 0);

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = "OpenSourceEdge Build Order #01: Shield Battery (Long-Gamma Afternoon Drift). When ES opens above the causally mapped SPX 14-DTE Gamma Flip (Positive Gamma) and MNQ closes above RTH VWAP at 13:00 ET, buy with a 2x ATR15 hard stop and exit flat at 15:55 ET.";
                Name = "LongGammaDriftOpenSource";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.AllEntries;
                IsExitOnSessionCloseStrategy = true;
                ExitOnSessionCloseSeconds = 30;
                IsFillLimitOnTouch = false;
                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                OrderFillResolution = OrderFillResolution.Standard;
                Slippage = 1;
                StartBehavior = StartBehavior.WaitUntilFlat;
                TimeInForce = TimeInForce.Day;
                TraceOrders = false;
                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                BarsRequiredToTrade = 0;
                IsInstantiatedOnEachOptimizationIteration = true;

                Quantity = 1;
                CheckTimeEt = 1300;
                ExitTimeEt = 1555;
                StopAtrMultiple = 2.0;
                MinStopTicks = 16;
                MaxStopPoints = 200;
                SkipFomcDays = true;
                SkipQqqCallFrenzy = false;
                QqqCallFrenzyMaxPct = 0.15;
                SkipVix9dInverted = false;
                MaxVix9dRatio = 1.05;
                RegimeCsvPath = "";
                EsInstrument = "";
                ExtraFomcDates = "";
                MaxDailyLossDollars = 1000;
                AllowOtherInstruments = false;
                AllowSharedAccountPositions = true;
                BlockOpposingAccountPosition = true;
                WriteCsv = true;
            }
            else if (State == State.Configure)
            {
                AddDataSeries(Data.BarsPeriodType.Tick, 1);
                string suffix = ContractSuffix();
                esName = IsAuto(EsInstrument) ? "ES " + suffix : EsInstrument.Trim();
                if (!InstrumentExists(esName)) esName = "ES ##-##";
                if (!InstrumentExists(esName))
                {
                    configProblem = "ES reference instrument " + esName + " was not found. Set 'ES instrument' explicitly (for example ES 12-26).";
                    return;
                }
                AddDataSeries(esName, Data.BarsPeriodType.Minute, 1);
            }
            else if (State == State.DataLoaded)
            {
                optimizing = Category == Category.Optimize || Category == Category.WalkForward || Category == Category.MultiObjective;
                try { eastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); }
                catch { eastern = TimeZoneInfo.Local; Say("Eastern time zone not found, using local time."); }

                string why = configProblem ?? StartProblem();
                if (why != null)
                {
                    refused = true;
                    Say("REFUSED TO START: " + why + " No orders will be placed.");
                    try { Log("LongGammaDriftOpenSource refused to start: " + why, LogLevel.Error); } catch { }
                    return;
                }

                resolvedCsvPath = string.IsNullOrWhiteSpace(RegimeCsvPath)
                    ? Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "OpenSourceEdge", "daily_regime.csv")
                    : RegimeCsvPath.Trim();

                regime = new LgRegimeTable(resolvedCsvPath);
                regime.ReloadIfChanged();

                core = new LgCore();
                core.CheckMinute = checkMinute;
                core.ExitMinute = exitMinute;
                core.StopAtrMult = StopAtrMultiple;
                core.MinStopTicks = MinStopTicks;
                core.MaxStopTicks = MaxStopPoints > 0 ? (int)Math.Floor(MaxStopPoints / TickSize + 1e-9) : 0;
                core.SkipFomcDays = SkipFomcDays;
                core.SkipQqqCallFrenzy = SkipQqqCallFrenzy;
                core.QqqCallFrenzyMaxPct = QqqCallFrenzyMaxPct;
                core.SkipVix9dInverted = SkipVix9dInverted;
                core.MaxVix9dRatio = MaxVix9dRatio;
                core.SetExtraFomcDates(ExtraFomcDates);
                core.RegimeLookup = delegate(DateTime d)
                {
                    if (State == State.Realtime) regime.ReloadIfChanged();
                    return regime.Get(d);
                };

                sessionIterator = new SessionIterator(BarsArray[0]);
                if (WriteCsv && !optimizing) OpenCsv();

                Say(string.Format(CultureInfo.InvariantCulture,
                    "{0}, ES ref {1}, qty {2}. Check {3} ET, flat {4} ET. Stop max({5}, round({6} x ATR15)){7}. SkipFOMC={8}. CSV={9} ({10} rows). DLL {11}.",
                    Instrument.FullName, BarsArray[EsIndex].Instrument.FullName, Quantity, Hhmm(checkMinute), Hhmm(exitMinute),
                    MinStopTicks, StopAtrMultiple, MaxStopPoints > 0 ? string.Format(CultureInfo.InvariantCulture, ", cap {0} pts", MaxStopPoints) : "",
                    SkipFomcDays ? "on" : "off", resolvedCsvPath, regime.Count,
                    MaxDailyLossDollars > 0 ? "$" + MaxDailyLossDollars.ToString("0.##", CultureInfo.InvariantCulture) : "off"));
            }
            else if (State == State.Realtime)
            {
                if (refused) return;
                if (regime != null) regime.ReloadIfChanged();
                if (todaySignal != null && entryDay == todaySignal.Date && todaySignal.Date == railDay)
                    Say("Today's signal occurred on historical bars. No live entry will be sent today.");
                if (Position.MarketPosition != MarketPosition.Flat)
                    Say("A virtual historical position is open. StartBehavior WaitUntilFlat prevents live orders until flat.");
                StartClock();
            }
            else if (State == State.Terminated)
            {
                StopClock();
                if (core != null && core.StudyDays > 0)
                    Say(string.Format("{0} valid days, {1} signals, {2} entries placed, {3} trades written.",
                        core.StudyDays, core.SignalDays, signalsPlaced, tradesLogged));
                if (writer != null) { writer.Flush(); writer.Dispose(); writer = null; }
            }
        }

        private string StartProblem()
        {
            BarsPeriod bp = BarsArray[0].BarsPeriod;
            if (bp.BarsPeriodType != BarsPeriodType.Minute || bp.Value != 1)
                return "Primary series must be 1 Minute bars (found " + bp.ToString() + ").";
            string root = Instrument.MasterInstrument.Name;
            if (root != "MNQ" && root != "NQ" && !AllowOtherInstruments)
                return "Validated on MNQ (NQ allowed). Found " + Instrument.FullName + ". Enable 'Allow other instruments' to override.";
            if (BarsArray.Length < 3) return "ES reference series did not load.";
            checkMinute = ToMinute(CheckTimeEt);
            exitMinute = ToMinute(ExitTimeEt);
            if (checkMinute < 0 || exitMinute < 0) return "Times must be HHMM with minutes 00 to 59.";
            if (checkMinute < 12 * 60 || checkMinute > 15 * 60) return "Check time must be between 1200 and 1500 ET.";
            if (exitMinute <= checkMinute + 5 || exitMinute > 15 * 60 + 55) return "Exit time must be after check time and at or before 1555 ET.";
            return null;
        }

        protected override void OnBarUpdate()
        {
            if (refused || core == null) return;
            if (CurrentBars[BarsInProgress] < 0) return;
            if (BarsInProgress == TickIndex) { OnTrade(); return; }
            if (!runWritten && BarsInProgress == 0) { runWritten = true; WriteRunRow(); }

            int bip = BarsInProgress;
            DateTime end = ToEastern(Times[bip][0]);
            double ts = BarsArray[bip].Instrument.MasterInstrument.TickSize;
            if (bip == EsIndex)
            {
                core.OnEsBar(end, ToTicks(Opens[EsIndex][0], ts), ToTicks(Closes[EsIndex][0], ts));
                return;
            }
            if (bip != 0) return;

            NewDay(end.Date);
            core.OnBar(end, ToTicks(Opens[0][0], ts), ToTicks(Highs[0][0], ts), ToTicks(Lows[0][0], ts), ToTicks(Closes[0][0], ts), Volumes[0][0]);
            for (int i = 0; i < core.Events.Count; i++)
            {
                LgDecision ev = core.Events[i];
                if (ev.Kind == "decision") OnDecision(ev, end);
                else if (ev.Kind == "flat") Flatten(FlatName, end, "flat at " + Hhmm(exitMinute) + " ET");
            }
            core.Events.Clear();
        }

        private void OnTrade()
        {
            DateTime t = ToEastern(Times[TickIndex][0]);
            lastTradeEt = t;
            NewDay(t.Date);
            if (Position.MarketPosition == MarketPosition.Flat) return;
            string outside = OutsideHoldWindow(t);
            if (outside != null) { Flatten(SafetyName, t, outside); return; }
            if (MaxDailyLossDollars > 0 && !haltedToday)
            {
                double open = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Closes[TickIndex][0]) - openCommission;
                double total = realizedToday + open;
                if (total <= -MaxDailyLossDollars)
                {
                    haltedToday = true;
                    string note = string.Format(CultureInfo.InvariantCulture, "daily P&L ${0:0.00} hit ${1:0.00} limit at {2}", total, MaxDailyLossDollars, F(Closes[TickIndex][0]));
                    Rail(t, "daily loss limit", note);
                    Flatten(DailyLossName, t, note);
                }
            }
        }

        private void OnDecision(LgDecision ev, DateTime barEnd)
        {
            string head = string.Format("{0} {1} ET", D(ev.Date), barEnd.ToString("HH:mm", CultureInfo.InvariantCulture));
            if (ev.Action != "signal")
            {
                WriteDecisionRow(barEnd, ev, ev.Action, ev.Reason);
                return;
            }
            todaySignal = ev;
            DateTime now = State == State.Realtime && lastTradeEt > barEnd ? lastTradeEt : barEnd;
            string block = EntryBlock(ev, now);
            if (block != null)
            {
                WriteDecisionRow(barEnd, ev, "blocked", block);
                Rail(now, "entry refused", block);
                Say(head + ": SIGNAL LONG blocked by rail: " + block + ".");
                return;
            }
            WriteDecisionRow(barEnd, ev, "signal", "");
            Say(string.Format(CultureInfo.InvariantCulture,
                "{0}: SIGNAL LONG (Positive Gamma). Close {1} > VWAP {2}, ES Open {3} > Flip {4:0.00}, ATR15 {5:0.0}t, stop {6}t ({7} pts).",
                head, F(ev.CloseTicks * TickSize), F(ev.VwapTicks * TickSize), F(ev.EsOpenTicks * BarsArray[EsIndex].Instrument.MasterInstrument.TickSize),
                ev.MappedFlipTicks * BarsArray[EsIndex].Instrument.MasterInstrument.TickSize, ev.Atr15Ticks, ev.StopTicks, F(ev.StopTicks * TickSize)));

            SetStopLoss(LongName, CalculationMode.Ticks, ev.StopTicks, false);
            entryDay = ev.Date;
            openStopTicks = ev.StopTicks;
            signalsPlaced++;
            openNote = string.Format(CultureInfo.InvariantCulture, "PosGamma; ATR15 {0:0.0}t", ev.Atr15Ticks);
            Row("kind", "order", "date", D(ev.Date), "time_et", T(now), "action", "enter long", "dir", "1",
                "stop_ticks", ev.StopTicks.ToString(CultureInfo.InvariantCulture), "qty", Quantity.ToString(CultureInfo.InvariantCulture), "note", openNote);
            EnterLong(TickIndex, Quantity, LongName);
        }

        private string EntryBlock(LgDecision ev, DateTime nowEt)
        {
            if (haltedToday || (MaxDailyLossDollars > 0 && realizedToday <= -MaxDailyLossDollars)) return "daily loss limit reached today";
            if (entryDay == ev.Date) return "already entered once today";
            if (Position.MarketPosition != MarketPosition.Flat) return "position already open";
            if (State == State.Realtime && PositionAccount != null && PositionAccount.MarketPosition != MarketPosition.Flat)
            {
                if (!AllowSharedAccountPositions) return "account already holds a position in " + Instrument.FullName;
                if (BlockOpposingAccountPosition && ev.Dir > 0 && PositionAccount.MarketPosition == MarketPosition.Short)
                    return "account holds an opposing short position in " + Instrument.FullName;
            }
            if (nowEt.Date != ev.Date) return "decision date mismatch";
            TimeSpan tod = nowEt.TimeOfDay;
            if (tod < TimeSpan.FromMinutes(checkMinute - 5) || tod > TimeSpan.FromMinutes(checkMinute + 10)) return "outside check window";
            if (tod >= TimeSpan.FromMinutes(exitMinute)) return "at or after exit time";
            if (ev.StopTicks <= 0) return "invalid stop distance";
            return EarlyClose(ev.Date);
        }

        private string EarlyClose(DateTime day)
        {
            if (sessionIterator == null) return null;
            try
            {
                sessionIterator.GetNextSession(Times[0][0], true);
                DateTime endEt = ToEastern(sessionIterator.ActualSessionEnd);
                if (endEt.Date == day && endEt.TimeOfDay < new TimeSpan(16, 0, 0))
                    return "early close day (ends " + endEt.ToString("HH:mm", CultureInfo.InvariantCulture) + " ET)";
            }
            catch (Exception ex) { Say("session check warning: " + ex.Message); }
            return null;
        }

        private string OutsideHoldWindow(DateTime tEt)
        {
            if (tEt.TimeOfDay >= HardFlat) return "position open at or after 15:56 ET";
            if (tEt.TimeOfDay < TimeSpan.FromMinutes(checkMinute - 10) || tEt.Date != entryDay) return "position open outside hold window";
            return null;
        }

        private void Flatten(string name, DateTime tEt, string why)
        {
            if (Position.MarketPosition == MarketPosition.Flat || exitPending) return;
            exitPending = true; exitPendingName = name;
            bool isLong = Position.MarketPosition == MarketPosition.Long;
            Say(string.Format("{0} ET: FLATTEN {1} {2} ({3}).", T(tEt), isLong ? "long" : "short", Position.Quantity, why));
            Row("kind", "order", "date", D(tEt), "time_et", T(tEt), "action", "exit " + name, "note", why);
            if (isLong) ExitLong(TickIndex, Position.Quantity, name, LongName);
            else ExitShort(TickIndex, Position.Quantity, name, "");
        }

        private void NewDay(DateTime day) { if (day != railDay) { railDay = day; haltedToday = false; realizedToday = 0; } }

        protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice,
            OrderState orderState, DateTime time, ErrorCode error, string nativeError)
        {
            if (order == null) return;
            if (orderState != OrderState.Rejected && orderState != OrderState.Cancelled) return;
            if (order.Name == exitPendingName) exitPending = false;
            if (orderState != OrderState.Rejected) return;
            DateTime t = ToEastern(time);
            Rail(t, "order rejected", order.Name + ": " + nativeError);
            Say(string.Format("{0} ET: ORDER REJECTED {1}: {2}", T(t), order.Name, nativeError));
            if (order.Name == "Stop loss" && Position.MarketPosition != MarketPosition.Flat)
            {
                haltedToday = true;
                Flatten(SafetyName, t, "protective stop rejected");
            }
        }

        protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
        {
            if (execution.Order == null) return;
            string name = execution.Order.Name;
            DateTime t = ToEastern(execution.Time);
            int q = execution.Quantity;
            Row("kind", "fill", "date", D(t), "time_et", T(t), "action", name, "price", F(execution.Price),
                "dir", execution.MarketPosition == MarketPosition.Long ? "buy" : "sell", "commission", F(execution.Commission),
                "qty", q.ToString(CultureInfo.InvariantCulture));
            if (name == LongName)
            {
                if (openQty == 0) { openTime = t; openValue = openCommission = exitValue = exitCommission = 0; closedQty = 0; }
                openQty += q; openValue += execution.Price * q; openCommission += execution.Commission;
                double avg = openValue / openQty;
                Say(string.Format(CultureInfo.InvariantCulture, "{0} ET: filled long {1} at {2}. Stop {3} ({4} ticks).",
                    T(t), openQty, F(avg), F(avg - openStopTicks * TickSize), openStopTicks));
                return;
            }
            if (openQty <= 0) return;
            closedQty += q; exitValue += execution.Price * q; exitCommission += execution.Commission;
            lastExitTime = t; lastExitName = name;
            if (closedQty < openQty) return;
            WriteTradeRow();
            openQty = closedQty = 0; exitPending = false;
        }

        #region Clock, helpers, CSV

        private void StartClock()
        {
            if (clock != null || (Account != null && Account.Name == "Playback101")) return;
            clock = new System.Timers.Timer(5000); clock.AutoReset = true; clock.Elapsed += OnClock; clock.Start();
        }
        private void StopClock() { if (clock == null) return; clock.Stop(); clock.Elapsed -= OnClock; clock.Dispose(); clock = null; }
        private void OnClock(object s, System.Timers.ElapsedEventArgs e) { try { TriggerCustomEvent(ClockCheck, 0, null); } catch { } }
        private void ClockCheck(object st)
        {
            if (State != State.Realtime || Position.MarketPosition == MarketPosition.Flat) return;
            DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, eastern);
            string outside = OutsideHoldWindow(now);
            if (outside != null) Flatten(SafetyName, now, "clock check: " + outside);
        }

        private static bool IsAuto(string s) { return string.IsNullOrWhiteSpace(s) || s.Trim().Equals("auto", StringComparison.OrdinalIgnoreCase); }
        private string ContractSuffix()
        {
            if (Instrument == null) return "##-##";
            string full = Instrument.FullName ?? "";
            int sp = full.IndexOf(' ');
            return sp > 0 && sp + 1 < full.Length ? full.Substring(sp + 1) : "##-##";
        }
        private static bool InstrumentExists(string name) { if (name.Contains("##")) return true; try { return NinjaTrader.Cbi.Instrument.GetInstrument(name) != null; } catch { return false; } }
        private static long ToTicks(double price, double ts) { return (long)Math.Round(price / ts); }
        private DateTime ToEastern(DateTime t)
        {
            DateTime key = new DateTime(t.Year, t.Month, t.Day, t.Hour, 0, 0);
            if (key != offsetKey)
            {
                offsetKey = key;
                try { offsetToEastern = TimeZoneInfo.ConvertTime(DateTime.SpecifyKind(key, DateTimeKind.Unspecified), NinjaTrader.Core.Globals.GeneralOptions.TimeZoneInfo, eastern) - key; }
                catch { offsetToEastern = TimeSpan.Zero; }
            }
            return t + offsetToEastern;
        }
        private static int ToMinute(int hhmm) { int h = hhmm / 100, m = hhmm % 100; return (hhmm < 0 || m > 59 || h > 23) ? -1 : h * 60 + m; }
        private static string Hhmm(int minute) { return string.Format("{0:00}:{1:00}", minute / 60, minute % 60); }
        private void Say(string text) { if (!optimizing) Print("LongGammaDriftOpenSource: " + text); }
        private void Rail(DateTime tEt, string what, string note) { Row("kind", "rail", "date", D(tEt), "time_et", T(tEt), "action", what, "note", note); }
        private static string ExitReason(string n) { return n == FlatName ? "flat 15:55" : n == "Stop loss" ? "stop" : n == SafetyName ? "safety flat" : n == DailyLossName ? "daily loss" : n; }
        private static string Csv(string s) { return (s ?? "").Replace(',', ';').Replace('\n', ' ').Replace('\r', ' '); }

        private static readonly string[] Columns = { "kind", "date", "time_et", "action", "dir", "price", "vwap", "atr15_ticks",
            "stop_ticks", "qty", "entry_time_et", "entry_price", "exit_time_et", "exit_price", "exit_reason", "gross_ticks", "commission", "pnl", "note" };

        private void Row(params string[] pairs)
        {
            if (writer == null) return;
            string[] cells = new string[Columns.Length];
            for (int i = 0; i + 1 < pairs.Length; i += 2) { int k = Array.IndexOf(Columns, pairs[i]); if (k >= 0) cells[k] = Csv(pairs[i + 1]); }
            writer.WriteLine(string.Join(",", cells));
        }
        private static string F(double x) { return double.IsNaN(x) ? "" : x.ToString("0.##########", CultureInfo.InvariantCulture); }
        private static string D(DateTime t) { return t.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); }
        private static string T(DateTime t) { return t.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); }

        private void OpenCsv()
        {
            try
            {
                string dir = Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "OpenSourceEdge", "Logs");
                Directory.CreateDirectory(dir);
                string inst = Instrument.FullName;
                foreach (char ch in Path.GetInvalidFileNameChars()) inst = inst.Replace(ch, '_');
                string file = Path.Combine(dir, string.Format("longgamma_{0}_{1:yyyyMMdd_HHmmss}.csv", inst.Replace(' ', '_'), DateTime.Now));
                writer = new StreamWriter(file, false);
                writer.WriteLine(string.Join(",", Columns));
                Say("log file " + file);
            }
            catch (Exception ex) { writer = null; Say("could not open CSV: " + ex.Message); }
        }

        private void WriteRunRow()
        {
            if (BarsArray[TickIndex].Count == 0) Say("WARNING: 1-tick series has no data.");
            if (BarsArray[EsIndex].Count == 0) Say("WARNING: ES reference series " + esName + " has no data.");
            if (writer == null) return;
            Row("kind", "run", "note", string.Format(CultureInfo.InvariantCulture,
                "instrument={0};es={1};qty={2};check={3};exit={4};stop_mult={5};min_stop={6};max_stop_pts={7};skip_fomc={8};csv={9};dll={10}",
                Instrument.FullName, BarsArray[EsIndex].Instrument.FullName, Quantity, Hhmm(checkMinute), Hhmm(exitMinute),
                StopAtrMultiple, MinStopTicks, MaxStopPoints, SkipFomcDays ? 1 : 0, resolvedCsvPath, MaxDailyLossDollars));
        }

        private void WriteDecisionRow(DateTime barEnd, LgDecision ev, string action, string note)
        {
            if (writer == null) return;
            Row("kind", "decision", "date", D(ev.Date), "time_et", T(barEnd), "action", action, "dir", ev.Dir.ToString(CultureInfo.InvariantCulture),
                "price", ev.CloseTicks > 0 ? F(ev.CloseTicks * TickSize) : "",
                "vwap", !double.IsNaN(ev.VwapTicks) ? F(ev.VwapTicks * TickSize) : "",
                "atr15_ticks", ev.Atr15Ticks > 0 ? F(ev.Atr15Ticks) : "",
                "stop_ticks", ev.StopTicks > 0 ? ev.StopTicks.ToString(CultureInfo.InvariantCulture) : "", "note", note);
        }

        private void WriteTradeRow()
        {
            tradesLogged++;
            double entry = openValue / openQty, exit = exitValue / closedQty;
            double grossTicks = (exit - entry) * openQty / TickSize;
            double commission = openCommission + exitCommission;
            double pnl = (exitValue - openValue) * Instrument.MasterInstrument.PointValue - commission;
            realizedToday += pnl;
            Say(string.Format(CultureInfo.InvariantCulture, "{0} ET: closed long {1} ({2}): {3} contract-ticks, ${4:0.00} after ${5:0.00} commission.",
                T(lastExitTime), openQty, ExitReason(lastExitName), F(Math.Round(grossTicks, 2)), pnl, commission));
            if (writer == null) return;
            Row("kind", "trade", "date", D(openTime), "dir", "long", "qty", openQty.ToString(CultureInfo.InvariantCulture),
                "entry_time_et", T(openTime), "entry_price", F(entry), "stop_ticks", openStopTicks.ToString(CultureInfo.InvariantCulture),
                "exit_time_et", T(lastExitTime), "exit_price", F(exit), "exit_reason", ExitReason(lastExitName),
                "gross_ticks", F(Math.Round(grossTicks, 4)), "commission", F(commission),
                "pnl", pnl.ToString("0.00", CultureInfo.InvariantCulture), "note", openNote);
            writer.Flush();
        }

        #endregion

        #region Properties

        [NinjaScriptProperty, Range(1, 20)]
        [Display(Name = "Quantity", Order = 1, GroupName = "1. Signal")]
        public int Quantity { get; set; }

        [NinjaScriptProperty, Range(1200, 1500)]
        [Display(Name = "Check time ET (HHMM)", Description = "1300 is the research default (plateau 1230 to 1315).", Order = 2, GroupName = "1. Signal")]
        public int CheckTimeEt { get; set; }

        [NinjaScriptProperty, Range(1305, 1555)]
        [Display(Name = "Exit time ET (HHMM)", Order = 1, GroupName = "2. Stop and Exits")]
        public int ExitTimeEt { get; set; }

        [NinjaScriptProperty, Range(0.5, 10.0)]
        [Display(Name = "Stop (ATR15 multiple)", Order = 2, GroupName = "2. Stop and Exits")]
        public double StopAtrMultiple { get; set; }

        [NinjaScriptProperty, Range(4, 200)]
        [Display(Name = "Min stop (ticks)", Order = 3, GroupName = "2. Stop and Exits")]
        public int MinStopTicks { get; set; }

        [NinjaScriptProperty, Range(0, 100000)]
        [Display(Name = "Max stop (points)", Order = 4, GroupName = "2. Stop and Exits")]
        public double MaxStopPoints { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Skip FOMC days", Description = "Pause on scheduled Fed FOMC decision days.", Order = 1, GroupName = "3. Regime and Filters")]
        public bool SkipFomcDays { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Skip QQQ call frenzy (optional CSV)", Description = "Skips entry if qqq_pcvol_p60 <= threshold. Ignored if column absent.", Order = 2, GroupName = "3. Regime and Filters")]
        public bool SkipQqqCallFrenzy { get; set; }

        [NinjaScriptProperty, Range(0.05, 0.50)]
        [Display(Name = "QQQ call frenzy max pct", Order = 3, GroupName = "3. Regime and Filters")]
        public double QqqCallFrenzyMaxPct { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Skip VIX9D/VIX >= 1.05 (optional CSV)", Description = "Skips entry when short-term volatility is inverted. Ignored if column absent.", Order = 4, GroupName = "3. Regime and Filters")]
        public bool SkipVix9dInverted { get; set; }

        [NinjaScriptProperty, Range(0.80, 2.00)]
        [Display(Name = "VIX9D/VIX veto threshold", Order = 5, GroupName = "3. Regime and Filters")]
        public double MaxVix9dRatio { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Regime CSV path", Description = "Blank uses Documents\\NinjaTrader 8\\OpenSourceEdge\\daily_regime.csv.", Order = 6, GroupName = "3. Regime and Filters")]
        public string RegimeCsvPath { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "ES instrument", Description = "Blank or auto matches the chart contract month (e.g. ES 12-26).", Order = 7, GroupName = "3. Regime and Filters")]
        public string EsInstrument { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Extra FOMC dates (YYYY-MM-DD)", Order = 8, GroupName = "3. Regime and Filters")]
        public string ExtraFomcDates { get; set; }

        [NinjaScriptProperty, Range(0, 100000)]
        [Display(Name = "Max daily loss ($)", Order = 1, GroupName = "4. Safety")]
        public double MaxDailyLossDollars { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Allow other instruments", Order = 2, GroupName = "4. Safety")]
        public bool AllowOtherInstruments { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Allow shared account positions", Order = 3, GroupName = "4. Safety")]
        public bool AllowSharedAccountPositions { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Block opposing account position", Order = 4, GroupName = "4. Safety")]
        public bool BlockOpposingAccountPosition { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Write CSV log", Order = 1, GroupName = "5. Output")]
        public bool WriteCsv { get; set; }

        #endregion
    }
}

// CORE-BEGIN
namespace NinjaTrader.NinjaScript.Strategies.OpenSourceEdgeLgCore
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;

    public sealed class LgRegimeRow
    {
        public double SpotD1 = double.NaN;
        public double D14GammaFlip = double.NaN;
        public double AllGammaFlip = double.NaN;
        public double D14NetGexB = double.NaN;
        public double QqqPcVolP60 = double.NaN;
        public double Vix9dVix = double.NaN;
        public bool IsFomc;
    }

    public sealed class LgRegimeTable
    {
        private readonly string path;
        private DateTime lastWrite = DateTime.MinValue;
        private readonly Dictionary<DateTime, LgRegimeRow> rows = new Dictionary<DateTime, LgRegimeRow>();

        public LgRegimeTable(string csvPath) { path = csvPath ?? ""; }
        public int Count { get { return rows.Count; } }

        public void ReloadIfChanged()
        {
            if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) return;
            try
            {
                DateTime wt = File.GetLastWriteTimeUtc(path);
                if (wt == lastWrite && rows.Count > 0) return;
                lastWrite = wt;
                rows.Clear();
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (StreamReader sr = new StreamReader(fs))
                {
                    string header = sr.ReadLine();
                    if (header == null) return;
                    string[] cols = header.Split(',');
                    int iDate = Col(cols, "date"), iSpot = Col(cols, "spot_d1"), iGf14 = Col(cols, "d14_gamma_flip"),
                        iGfa = Col(cols, "all_gamma_flip"), iNg14 = Col(cols, "d14_net_gex_b"),
                        iQqq = Col(cols, "qqq_pcvol_p60"), iV9 = Col(cols, "vix9d_vix"), iFomc = Col(cols, "is_fomc");
                    if (iDate < 0) return;
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (string.IsNullOrWhiteSpace(line)) continue;
                        string[] f = line.Split(',');
                        DateTime d;
                        if (iDate >= f.Length || !DateTime.TryParseExact(f[iDate].Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d)) continue;
                        LgRegimeRow r = new LgRegimeRow();
                        r.SpotD1 = ParseD(f, iSpot);
                        r.D14GammaFlip = ParseD(f, iGf14);
                        r.AllGammaFlip = ParseD(f, iGfa);
                        r.D14NetGexB = ParseD(f, iNg14);
                        r.QqqPcVolP60 = ParseD(f, iQqq);
                        r.Vix9dVix = ParseD(f, iV9);
                        r.IsFomc = iFomc >= 0 && iFomc < f.Length && f[iFomc].Trim() == "1";
                        rows[d.Date] = r;
                    }
                }
            }
            catch { }
        }

        public LgRegimeRow Get(DateTime day) { LgRegimeRow r; return rows.TryGetValue(day.Date, out r) ? r : null; }

        private static int Col(string[] cols, string name)
        {
            for (int i = 0; i < cols.Length; i++) if (cols[i].Trim().Equals(name, StringComparison.OrdinalIgnoreCase)) return i;
            return -1;
        }
        private static double ParseD(string[] f, int idx)
        {
            double v;
            if (idx < 0 || idx >= f.Length) return double.NaN;
            return double.TryParse(f[idx].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out v) ? v : double.NaN;
        }
    }

    public sealed class LgAtr15
    {
        private readonly Queue<long> trs = new Queue<long>();
        private long trSum, prevC, curBucket, bh, bl, bc;
        private bool hasPrevC, hasBucket;

        public double Value { get { return trs.Count >= 5 ? (double)trSum / trs.Count : 0.0; } }
        private void PushTr(long tr) { trs.Enqueue(tr); trSum += tr; if (trs.Count > 14) trSum -= trs.Dequeue(); }

        public void FlushSession()
        {
            if (hasBucket)
            {
                PushTr(hasPrevC ? Math.Max(bh - bl, Math.Max(Math.Abs(bh - prevC), Math.Abs(bl - prevC))) : (bh - bl));
                prevC = bc; hasPrevC = true; hasBucket = false;
            }
        }

        public double Push(int etMin, long h, long l, long c)
        {
            int b = (int)Math.Floor((etMin - 1) / 15.0);
            if (!hasBucket) { hasBucket = true; curBucket = b; bh = h; bl = l; bc = c; }
            else if (b != curBucket)
            {
                PushTr(hasPrevC ? Math.Max(bh - bl, Math.Max(Math.Abs(bh - prevC), Math.Abs(bl - prevC))) : (bh - bl));
                prevC = bc; hasPrevC = true; curBucket = b; bh = h; bl = l; bc = c;
            }
            else { if (h > bh) bh = h; if (l < bl) bl = l; bc = c; }
            if (etMin % 15 == 0)
            {
                PushTr(hasPrevC ? Math.Max(bh - bl, Math.Max(Math.Abs(bh - prevC), Math.Abs(bl - prevC))) : (bh - bl));
                prevC = bc; hasPrevC = true; hasBucket = false;
            }
            return Value;
        }
    }

    public sealed class LgDecision
    {
        public string Kind = "decision";
        public DateTime Date;
        public string Action = "";
        public string Reason = "";
        public int Dir;
        public long CloseTicks, EsOpenTicks;
        public double MappedFlipTicks = double.NaN;
        public double VwapTicks = double.NaN;
        public double Atr15Ticks;
        public int StopTicks;
    }

    public sealed class LgCore
    {
        public const int RthOpen = 9 * 60 + 30;
        public const int RthClose = 16 * 60;
        public const int MinRthBars = 331;

        private static readonly HashSet<DateTime> BuiltinFomc = BuildFomcSet();

        private static HashSet<DateTime> BuildFomcSet()
        {
            string[] raw = {
                "2019-01-30","2019-03-20","2019-05-01","2019-06-19","2019-07-31","2019-09-18","2019-10-30","2019-12-11",
                "2020-01-29","2020-04-29","2020-06-10","2020-07-29","2020-09-16","2020-11-05","2020-12-16",
                "2021-01-27","2021-03-17","2021-04-28","2021-06-16","2021-07-28","2021-09-22","2021-11-03","2021-12-15",
                "2022-01-26","2022-03-16","2022-05-04","2022-06-15","2022-07-27","2022-09-21","2022-11-02","2022-12-14",
                "2023-02-01","2023-03-22","2023-05-03","2023-06-14","2023-07-26","2023-09-20","2023-11-01","2023-12-13",
                "2024-01-31","2024-03-20","2024-05-01","2024-06-12","2024-07-31","2024-09-18","2024-11-07","2024-12-18",
                "2025-01-29","2025-03-19","2025-05-07","2025-06-18","2025-07-30","2025-09-17","2025-10-29","2025-12-10",
                "2026-01-28","2026-03-18","2026-04-29","2026-06-17","2026-07-29","2026-09-16","2026-10-28","2026-12-09"
            };
            HashSet<DateTime> s = new HashSet<DateTime>();
            for (int i = 0; i < raw.Length; i++) s.Add(DateTime.ParseExact(raw[i], "yyyy-MM-dd", CultureInfo.InvariantCulture));
            return s;
        }

        public int CheckMinute = 13 * 60;
        public int MaxCheckLag = 3;
        public int ExitMinute = 15 * 60 + 55;
        public double StopAtrMult = 2.0;
        public int MinStopTicks = 16;
        public int MaxStopTicks;
        public bool SkipFomcDays = true;
        public bool SkipQqqCallFrenzy;
        public double QqqCallFrenzyMaxPct = 0.15;
        public bool SkipVix9dInverted;
        public double MaxVix9dRatio = 1.05;
        public Func<DateTime, LgRegimeRow> RegimeLookup;

        public readonly List<LgDecision> Events = new List<LgDecision>();
        public DateTime Day = DateTime.MinValue;
        public int StudyDays, SignalDays;

        private readonly LgAtr15 atr15 = new LgAtr15();
        private readonly HashSet<DateTime> extraFomc = new HashSet<DateTime>();

        private DateTime esDay = DateTime.MinValue;
        private int esRthBars;
        private bool esHasOpen, esHasClose, hasEsPdc;
        private long esOpen930, esLastRthClose, esPdc;

        private int rthBars, checkMin;
        private bool hasOpen, hasClose, decided, flatDone, hasCheck;
        private long checkClose;
        private double checkVwap = double.NaN, checkAtr;
        private double cumPv, cumV;

        public void SetExtraFomcDates(string csvDates)
        {
            extraFomc.Clear();
            if (string.IsNullOrWhiteSpace(csvDates)) return;
            string[] parts = csvDates.Split(new[] { ',', ';', ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < parts.Length; i++)
            {
                DateTime d;
                if (DateTime.TryParseExact(parts[i].Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out d))
                    extraFomc.Add(d.Date);
            }
        }

        public static DateTime SessionDate(DateTime endEt) { return endEt.AddMinutes(-1).AddHours(6).Date; }
        public static int MinuteOf(DateTime endEt, DateTime day) { return (int)Math.Round((endEt - day).TotalMinutes); }

        public void OnEsBar(DateTime endEt, long o, long c)
        {
            DateTime sd = SessionDate(endEt);
            if (sd.DayOfWeek == DayOfWeek.Saturday || sd.DayOfWeek == DayOfWeek.Sunday) return;
            if (sd < esDay) return;
            if (sd > esDay)
            {
                if (esDay != DateTime.MinValue && esRthBars >= MinRthBars && esHasOpen && esHasClose)
                {
                    esPdc = esLastRthClose;
                    hasEsPdc = true;
                }
                esDay = sd;
                esRthBars = 0;
                esHasOpen = esHasClose = false;
                esOpen930 = esLastRthClose = 0;
            }
            int e = MinuteOf(endEt, sd);
            if (e > RthOpen && e <= RthClose)
            {
                if (esRthBars == 0) esOpen930 = o;
                esRthBars++;
                if (e >= RthOpen + 1 && e <= RthOpen + 5) esHasOpen = true;
                if (e >= RthClose - 5 && e <= RthClose) esHasClose = true;
                esLastRthClose = c;
            }
        }

        public void OnBar(DateTime endEt, long o, long h, long l, long c, double volume)
        {
            Events.Clear();
            DateTime sd = SessionDate(endEt);
            if (sd.DayOfWeek == DayOfWeek.Saturday || sd.DayOfWeek == DayOfWeek.Sunday) return;
            if (sd < Day) return;
            if (sd > Day)
            {
                FinishDay();
                Day = sd;
                rthBars = 0;
                hasOpen = hasClose = decided = flatDone = hasCheck = false;
                checkMin = int.MinValue;
                checkClose = 0; checkVwap = double.NaN; checkAtr = 0;
                cumPv = cumV = 0;
            }

            int e = MinuteOf(endEt, sd);
            if (e > 16 * 60 + 15) return;

            double a15 = atr15.Push(e, h, l, c);
            if (e <= RthOpen) return;

            if (!decided && e > CheckMinute) Decide();
            if (!flatDone && e >= ExitMinute)
            {
                flatDone = true;
                LgDecision fl = new LgDecision();
                fl.Kind = "flat";
                fl.Date = Day;
                Events.Add(fl);
            }

            if (e <= RthClose)
            {
                rthBars++;
                if (e >= RthOpen + 1 && e <= RthOpen + 5) hasOpen = true;
                if (e >= RthClose - 5 && e <= RthClose) hasClose = true;

                double tp = ((double)h + (double)l + (double)c) / 3.0;
                double v = volume > 0 ? volume : 0.0;
                cumPv += tp * v;
                cumV += v;
                double vwap = cumV > 0 ? cumPv / cumV : tp;

                if (e <= CheckMinute && e >= CheckMinute - MaxCheckLag)
                {
                    checkMin = e;
                    checkClose = c;
                    checkVwap = vwap;
                    checkAtr = a15;
                    hasCheck = true;
                }
                if (!decided && e == CheckMinute) Decide();
            }
        }

        private void Decide()
        {
            decided = true;
            LgDecision ev = new LgDecision();
            ev.Kind = "decision";
            ev.Date = Day;
            LgRegimeRow row = RegimeLookup != null ? RegimeLookup(Day) : null;

            if (SkipFomcDays && (BuiltinFomc.Contains(Day) || extraFomc.Contains(Day) || (row != null && row.IsFomc)))
            {
                ev.Action = "skip"; ev.Reason = "FOMC decision day";
                Events.Add(ev); return;
            }
            if (row == null)
            {
                ev.Action = "skip"; ev.Reason = "no regime row in CSV for today";
                Events.Add(ev); return;
            }
            if (!hasEsPdc || esDay != Day || esOpen930 <= 0)
            {
                ev.Action = "skip"; ev.Reason = "missing ES prior close or 09:30 open";
                Events.Add(ev); return;
            }
            double mappedFlip = double.NaN;
            if (!double.IsNaN(row.SpotD1) && row.SpotD1 > 0)
            {
                if (!double.IsNaN(row.D14GammaFlip) && row.D14GammaFlip > 0)
                    mappedFlip = (double)esPdc * (row.D14GammaFlip / row.SpotD1);
                else if (!double.IsNaN(row.AllGammaFlip) && row.AllGammaFlip > 0)
                    mappedFlip = (double)esPdc * (row.AllGammaFlip / row.SpotD1);
            }
            ev.EsOpenTicks = esOpen930;
            ev.MappedFlipTicks = mappedFlip;
            int reg = !double.IsNaN(mappedFlip) ? ((double)esOpen930 > mappedFlip ? 1 : -1)
                : (!double.IsNaN(row.D14NetGexB) ? (row.D14NetGexB > 0 ? 1 : -1) : 0);
            if (reg != 1)
            {
                ev.Action = "no signal"; ev.Reason = "ES open not above mapped SPX Gamma Flip";
                Events.Add(ev); return;
            }
            if (SkipQqqCallFrenzy && !double.IsNaN(row.QqqPcVolP60) && row.QqqPcVolP60 <= QqqCallFrenzyMaxPct)
            {
                ev.Action = "skip"; ev.Reason = "QQQ call frenzy veto";
                Events.Add(ev); return;
            }
            if (SkipVix9dInverted && !double.IsNaN(row.Vix9dVix) && row.Vix9dVix >= MaxVix9dRatio)
            {
                ev.Action = "skip"; ev.Reason = "VIX9D/VIX inversion veto";
                Events.Add(ev); return;
            }
            if (!hasCheck || checkMin < CheckMinute - MaxCheckLag || checkAtr <= 0 || double.IsNaN(checkVwap))
            {
                ev.Action = "skip"; ev.Reason = "missing 13:00 bar or ATR15 not ready";
                Events.Add(ev); return;
            }
            ev.CloseTicks = checkClose;
            ev.VwapTicks = checkVwap;
            ev.Atr15Ticks = checkAtr;
            if ((double)checkClose <= checkVwap)
            {
                ev.Action = "no signal"; ev.Reason = "13:00 close at or below RTH VWAP";
                Events.Add(ev); return;
            }

            int rawStop = Math.Max(MinStopTicks, (int)Math.Round(StopAtrMult * checkAtr));
            ev.StopTicks = MaxStopTicks > 0 ? Math.Min(rawStop, MaxStopTicks) : rawStop;
            ev.Dir = 1;
            ev.Action = "signal";
            SignalDays++;
            Events.Add(ev);
        }

        private void FinishDay()
        {
            if (Day == DateTime.MinValue) return;
            atr15.FlushSession();
            if (rthBars >= MinRthBars && hasOpen && hasClose) StudyDays++;
        }
    }
}
// CORE-END
MNQ 1m ETH · sample_daily_regime.csv

Use with AI

Paste a prompt into your AI (recommended: Gemini 4 Neon). Strategy code is attached automatically.

  • Explain it

    Plain-English rules, dealer gamma, and worst-day risk.

    I'm new to trading. Below is the code for a futures trading strategy called Shield Battery. Explain it to me in plain English:
    
    1. What it trades and at what time of day.
    2. The exact rules for getting in and getting out.
    3. Why it might work, and what "dealer gamma" means here.
    4. What could go wrong, and roughly how much I could lose on a bad day trading 1 MNQ contract.
    
    Define every trading term the first time you use it. Keep it short.
  • Set it up

    Step-by-step install, backtest walkthrough, and error fixes.

    I'm new to TradingView. Below is a Pine Script strategy. Walk me through adding it to a chart one step at a time, and wait for me to say "done" before you give the next step:
    
    1. Open a 1-minute or 5-minute MNQ1! chart.
    2. Paste the script into the Pine Editor and add it to the chart.
    3. Open the Strategy Tester and explain what each number means.
    
    If I paste an error message, tell me what caused it and give me the fixed code.
    I'm new to NinjaTrader 8. Below is a NinjaScript strategy. Walk me through using it one step at a time, and wait for me to say "done" before you give the next step:
    
    1. Create a new strategy in the NinjaScript Editor, paste this code and compile it.
    2. Backtest it in the Strategy Analyzer on MNQ 1-minute data.
    3. Run it on the Sim101 practice account only.
    
    If I paste a compile error, tell me what caused it and give me the fixed code. Never tell me to turn it on with real money.
  • Test your own idea

    Paste it, then type your idea at the end.

    I'm going to describe a trading idea. Act as a skeptical research partner whose job is to find out whether it's real before I risk any money:
    
    1. Ask me who is forced to buy or sell, and why. If there's no clear reason, say so.
    2. Help me test it on years of data I did not use to build it.
    3. Include commissions and at least 1 tick of slippage on every entry and exit.
    4. Check that flipping the rule makes it worse, that nudging each setting up or down by a third still works, and that it survives removing the 3 best days. If it fails any of these, tell me it failed.
    5. Only if it survives, write it as a TradingView (Pine Script v6) and NinjaTrader 8 strategy with a hard stop and a 3:55 PM ET exit.
    
    Be blunt. Don't tweak settings to make the results look better.
    
    My idea:

Build orders

One strategy per episode. Seven queued.

  1. Live
  2. 02
    Stimpack
    First five minutes, one-way open
    Queued
  3. 03
    Siege Tank
    Noise band breakout
    Queued
  4. 04
    Blink Stalker
    Fast morning MNQ and YM scalp
    Queued
  5. 05
    Yamato Cannon
    Treasury post-auction, 1:10 PM
    Queued
  6. 06
    MULE Drop
    Turn-of-month flow
    Queued
  7. 07
    Psionic Storm
    Short-gamma last hour
    Queued
  8. 08
    Widow Mine
    QQQ call-euphoria short
    Queued

Graveyard

Popular setups that died under 8 years of real ticks and fees.

  • ICT / SMC

    Lost before fees

    iFVG, SMT divergence, session sweeps, and lunch fades lost money before commissions.

  • Order flow & CVD

    1B+ ticks

    CVD divergence, delta thrusts, and absorption failed to beat plain price out of sample.

  • Power hour & $TICK

    Broke in 2023

    Both worked from 2019 to 2022, then inverted once daily 0DTE options arrived.

  • Friction on ZN

    20.2% of stop

    One tick of slippage plus fees eats 20.2% of a normal 10-year note stop, vs. 1.26% on MNQ.

Full graveyard on GitHub →