{"product_id":"python-short-selling-algorithm-nasdaq-100-futures-nq-cme","title":"Python Short Selling Algorithm — Nasdaq-100 Futures (NQ CME)","description":"\u003cp\u003eOVERVIEW\u003cbr\u003e────────\u003cbr\u003eMost algorithmic trading products focus on the long side. But professional\u003cbr\u003equant systems trade both directions — and the short side of Nasdaq-100 futures\u003cbr\u003erequires a completely different signal architecture: overextension detection,\u003cbr\u003emean-reversion confirmation, momentum exhaustion, and a precisely calibrated\u003cbr\u003etrailing stop that tightens as the trade moves in your favour.\u003c\/p\u003e\n\u003cp\u003eThis listing gives you the complete, heavily commented Python source code for\u003cbr\u003ea SHORT mean-reversion strategy on CME Nasdaq-100 E-Mini Futures (NQ).\u003cbr\u003eThe strategy uses a multi-layer signal stack: z-score overextension, RSI\u003cbr\u003emomentum turn detection, declining volume confirmation, and a dual-timeframe\u003cbr\u003e(30m signal \/ 5m execution) architecture that only fires when all conditions\u003cbr\u003ealign. Both the original Gen1 bot and the Gen2 evolutionary improvement are\u003cbr\u003eincluded — giving you a side-by-side view of how the strategy was refined.\u003c\/p\u003e\n\u003cp\u003eThis is an educational resource for Python developers, quantitative trading\u003cbr\u003estudents, and systematic traders who want to study or adapt a real, production-\u003cbr\u003egrade SHORT strategy that was built and live-tested on Rithmic infrastructure.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eBOT IDENTITY\u003cbr\u003e────────────\u003cbr\u003e  Bot Name   : bar_nq_long_20260911_145036\u003cbr\u003e  Created    : September 11, 2026 at 14:50:36 UTC\u003cbr\u003e  Instrument : NQ — Nasdaq-100 E-Mini Futures @ CME (front-month contract)\u003cbr\u003e  Direction  : SHORT  (overextension mean-reversion)\u003cbr\u003e  Generation : Gen1 (original) + Gen2 evolutionary variant (both included)\u003cbr\u003e  Symbol     : NQM6 (Gen1)  \/  NQU6 (Gen2)\u003cbr\u003e  Repository : qln-live-trading-rithmic9\u003cbr\u003e  Folder     : bots\/bar_historical\/2026-09-11\/\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eBACKTESTED PERFORMANCE  (honest disclosure)\u003cbr\u003e───────────────────────────────────────────\u003cbr\u003e  ⚠️  IMPORTANT: This bot has a limited backtest sample due to its strict\u003cbr\u003e      multi-condition entry filter. Only 3 qualifying trades were recorded\u003cbr\u003e      in the backtest window. The strategy requires all of: z-score\u003cbr\u003e      overextension, RSI momentum turn, weakening volume, and price above\u003cbr\u003e      EMA — a high-conviction filter that fires rarely but precisely.\u003c\/p\u003e\n\u003cp\u003e  Metric                          Gen1 Value\u003cbr\u003e  ──────────────────────────────────────────────\u003cbr\u003e  Instrument                      NQ Nasdaq-100 E-Mini @ CME\u003cbr\u003e  Direction                       SHORT (mean-reversion)\u003cbr\u003e  Backtest Trades                 3   (strict minimum: 20 for ranking)\u003cbr\u003e  Win Rate                        0%  (3-trade sample — statistically limited)\u003cbr\u003e  Profit Factor                   0.0  (sample too small for reliability)\u003cbr\u003e  Net PnL (backtest)              -$201\u003cbr\u003e  Total Return (backtest)         -100%\u003cbr\u003e  Max Drawdown — raw              100%\u003cbr\u003e  Max Drawdown — live hard cap    15%  (session circuit-breaker enforced)\u003cbr\u003e  Signal Timeframe                30-minute bars\u003cbr\u003e  Execution Timeframe             5-minute bars\u003cbr\u003e  ATR Multiplier (entry)          Dynamic 1.2x–2.6x  (vol + momentum adjusted)\u003cbr\u003e  ATR Trailing Stop               entry + ATR×1.4  (tightening trailing)\u003cbr\u003e  Min Entry Equity Guard          15% max drawdown → session halted\u003cbr\u003e  Starting Capital (eval run)     $17,092\u003cbr\u003e  ──────────────────────────────────────────────\u003c\/p\u003e\n\u003cp\u003e  Gen2 AI Probability Improvement:\u003cbr\u003e    Gen1 AI profit probability : 55%   (volume score: 30, order-flow: 40)\u003cbr\u003e    Gen2 AI profit probability : 65%   (volume score: 60, order-flow: 70)\u003c\/p\u003e\n\u003cp\u003e  Ranking status: NOT in the Sep 21 profitable bots ranking.\u003cbr\u003e  Reason: Only 3 backtest trades recorded — below the strict 20-trade minimum\u003cbr\u003e  required for ranking inclusion. The strategy's high-conviction filter means\u003cbr\u003e  it fires rarely. This is disclosed fully so buyers can make an informed\u003cbr\u003e  decision. The value here is the strategy architecture and methodology.\u003c\/p\u003e\n\u003cp\u003e  ⚠️  A 3-trade backtest is NOT statistically significant. Past backtested\u003cbr\u003e      performance (or non-performance) does NOT predict future results.\u003cbr\u003e      This product is sold as an educational code resource, not a signal service.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eMARKET CONDITIONS AT CREATION  (Sep 11, 2026 14:50 UTC)\u003cbr\u003e─────────────────────────────────────────────────────────\u003cbr\u003e  ATR (% of price)      0.181%  — moderate volatility for NQ\u003cbr\u003e  Trailing return       +0.163% — slight positive drift\u003cbr\u003e  Up-bar fraction       65.2%   — NQ leaning bullish at creation\u003cbr\u003e  Avg volume\/bar        11,657 contracts  (trend: -28.8%, sharply declining)\u003cbr\u003e  ATR analysis source   ai (forward-looking estimate, not a guarantee)\u003c\/p\u003e\n\u003cp\u003e  Analysis rationale: Strong positive up-bar fraction (65%) with sharply\u003cbr\u003e  declining participation (-28.8% volume trend) signals a classic weakening\u003cbr\u003e  uptrend. When buying pressure dries up in an overbought market, the risk of\u003cbr\u003e  a mean-reversion snap-back to the downside increases — exactly the condition\u003cbr\u003e  this SHORT strategy is designed to exploit.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eHOW THE STRATEGY WORKS\u003cbr\u003e───────────────────────\u003cbr\u003eARCHITECTURE: Dual-Timeframe SHORT with Z-Score Overextension Filter\u003c\/p\u003e\n\u003cp\u003e  Signal timeframe (30m): Detects when NQ is overextended to the upside\u003cbr\u003e    and momentum is turning — arms a SHORT signal.\u003c\/p\u003e\n\u003cp\u003e  Execution timeframe (5m): Waits for the first bearish confirmation bar\u003cbr\u003e    below the execution EMA before entering — avoids top-picking.\u003c\/p\u003e\n\u003cp\u003eSIGNAL ARMING (30m bar logic — _update_signal_state)\u003cbr\u003e  All conditions below must be TRUE to arm the SHORT signal:\u003c\/p\u003e\n\u003cp\u003e  1. Z-Score overextension:  z-score of close prices (20-bar lookback) \u0026gt;= 0.8\u003cbr\u003e     OR close \u0026gt; EMA_fast  →  price is stretched above normal range\u003cbr\u003e  2. RSI momentum turn:  RSI(14) is TURNING DOWN (rsi_now \u0026lt; rsi_prev)\u003cbr\u003e     AND RSI is still elevated (rsi_now \u0026gt;= 45)  →  exhaustion signal\u003cbr\u003e  3. Trend bias up:  close \u0026gt;= EMA_slow(12)  →  confirming we are in an\u003cbr\u003e     uptrend before fading it (mean-reversion, not counter-trend bottom)\u003cbr\u003e  4. Gate check passes (relaxed mode if vol_slope \u0026lt; 0 AND z \u0026gt;= 1.0):\u003cbr\u003e       momentum_score \u0026gt;= 35 (relaxed) or \u0026gt;= 45 (strict)\u003cbr\u003e  Signal expires after 6 signal bars if not triggered.\u003c\/p\u003e\n\u003cp\u003eCOMPOSITE MOMENTUM SCORE (0–50 capped):\u003cbr\u003e  Component 1: RSI-based (max 20pts):  (60 - RSI) × 1.2\u003cbr\u003e  Component 2: Z-score component (max 20pts):  z × 8.0\u003cbr\u003e  Component 3: Momentum turn bonus (+10 pts if RSI turning down)\u003cbr\u003e  Component 4: Weakening volume bonus (+8 pts if vol_slope \u0026lt; 0)\u003cbr\u003e  Component 5: Trend bias bonus (+8 pts if close \u0026gt;= EMA_slow)\u003c\/p\u003e\n\u003cp\u003eDYNAMIC ATR MULTIPLIER (entry stop placement):\u003cbr\u003e  Base: 1.6x  |  +0.3x if ATR% \u0026gt; 0.8  |  -0.2x if ATR% \u0026lt; 0.4\u003cbr\u003e  +0.2x if momentum_score \u0026gt;= 45  |  -0.1x if vol_slope \u0026lt; 0\u003cbr\u003e  Clamped: 1.2x – 2.6x\u003c\/p\u003e\n\u003cp\u003eDYNAMIC R:R RATIO:\u003cbr\u003e  Base: 1.6  |  +0.4 if score \u0026gt;= 45  |  +0.3 if z \u0026gt;= 1.2\u003cbr\u003e  +0.2 if vol_slope \u0026lt; 0  |  Clamped: 1.2 – 3.0\u003c\/p\u003e\n\u003cp\u003eEXECUTION TRIGGER (5m bar — _execution_entry_ok):\u003cbr\u003e  Signal must be armed. Then on each 5m bar:\u003cbr\u003e  1. Bearish bar: close \u0026lt; open  (sellers in control)\u003cbr\u003e  2. Below EMA: close \u0026lt;= EMA(5) of execution closes  (pullback confirmed)\u003cbr\u003e  3. ATR \u0026gt; 0  (valid volatility environment)\u003cbr\u003e  If all three true → enter SHORT at mid-price.\u003c\/p\u003e\n\u003cp\u003ePOSITION MANAGEMENT (_manage_open_position):\u003cbr\u003e  Stop price  : entry + stop_distance (ATR × multiplier from signal)\u003cbr\u003e  Target price: entry - (stop_distance × R:R ratio)\u003cbr\u003e  ATR TRAILING STOP (unique to this strategy):\u003cbr\u003e    On each execution bar, recalculates: proposed_stop = close + ATR×1.4\u003cbr\u003e    If proposed_stop \u0026lt; current stop → TIGHTEN stop  (locks in profit on shorts)\u003cbr\u003e  Exit triggers:\u003cbr\u003e    • High bar \u0026gt;= stop_price         → close at stop (protective ATR stop)\u003cbr\u003e    • Low bar  \u0026lt;= target_price       → close at target (soft target hit)\u003cbr\u003e    • Max drawdown \u0026gt;= 15% of session → force-flatten (circuit breaker)\u003c\/p\u003e\n\u003cp\u003eSESSION MANAGEMENT:\u003cbr\u003e  Each calendar day is a new session.\u003cbr\u003e  If 15% drawdown is hit mid-session → entries halted for rest of that day.\u003cbr\u003e  New day = drawdown counter resets → strategy re-arms.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eGEN2 EVOLUTIONARY IMPROVEMENT\u003cbr\u003e──────────────────────────────\u003cbr\u003e  Gen2 was auto-generated by the system's evolutionary optimisation engine\u003cbr\u003e  after analysing Gen1 live logs.\u003c\/p\u003e\n\u003cp\u003e  AI profit probability : 55% (Gen1) → 65% (Gen2)  (+10 ppts)\u003cbr\u003e  Volume score          : 30  (Gen1) → 60  (Gen2)   (2× improvement)\u003cbr\u003e  Order-flow score      : 40  (Gen1) → 70  (Gen2)   (+75%)\u003cbr\u003e  Architecture adds: statistics module, Path-based imports, extended\u003cbr\u003e  warmup handling, additional internal safeguards in bar-processing logic.\u003cbr\u003e  Both Gen1 and Gen2 source files are included in this product.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eWHAT YOU RECEIVE\u003cbr\u003e─────────────────\u003cbr\u003e  ✅  bar_nq_long_20260911_145036_portable.py\u003cbr\u003e        Gen1 broker-agnostic Python strategy — Rithmic + Redis removed,\u003cbr\u003e        clearly labelled placeholder comments throughout.\u003cbr\u003e  ✅  Complete Gen1 strategy logic intact:\u003cbr\u003e        Z-score (20-bar), RSI(14), EMA(6\/12) signal + EMA(5) execution,\u003cbr\u003e        ATR(14) signal + execution, composite momentum score (50-pt),\u003cbr\u003e        dynamic ATR multiplier (1.2x–2.6x), dynamic R:R (1.2–3.0),\u003cbr\u003e        ATR tightening trailing stop, gate check (strict + relaxed),\u003cbr\u003e        signal arming + 6-bar expiry, drawdown guard, session reset,\u003cbr\u003e        _finalize_position with P\u0026amp;L tracking\u003cbr\u003e  ✅  Gen2 variant file noted (bar_nq_long_20260911_145036_gen2.py)\u003cbr\u003e  ✅  Heavily commented — every method explained\u003cbr\u003e  ✅  Broker-agnostic: ib_insync, Alpaca, TradeStation, NinjaTrader, etc.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eINTEGRATION QUICK-START\u003cbr\u003e────────────────────────\u003cbr\u003e  bot = BarNqLong20260911145036()\u003cbr\u003e  # tf_key = \"30m\" for signal bars, \"5m\" for execution bars\u003cbr\u003e  await bot.on_bar_closed(\"30m\", {\"open\":x,\"high\":x,\"low\":x,\"close\":x,\"volume\":x,\"timestamp\":t})\u003cbr\u003e  await bot.on_bar_closed(\"5m\",  {\"open\":x,\"high\":x,\"low\":x,\"close\":x,\"volume\":x,\"timestamp\":t})\u003cbr\u003e  # Implement in subclass:\u003cbr\u003e  async def _submit_short_entry(self, qty, reason): ...   # place SELL order\u003cbr\u003e  async def _submit_flatten(self, reason): ...            # cover\/close order\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eTECHNICAL REQUIREMENTS\u003cbr\u003e───────────────────────\u003cbr\u003e  Python 3.10+  |  stdlib: asyncio, math, statistics, os, sys, datetime,\u003cbr\u003e                           pathlib, typing\u003cbr\u003e  Any OHLCV bar source with timestamp  |  Any broker API or paper engine\u003cbr\u003e  Tested: Windows 10\/11, Linux Ubuntu 22.04+, macOS 13+\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eFREQUENTLY ASKED QUESTIONS\u003cbr\u003e───────────────────────────\u003cbr\u003e  Q: Only 3 backtest trades — is this strategy usable?\u003cbr\u003e  A: The architecture is complete and production-grade. The 3-trade count\u003cbr\u003e     reflects a very strict, high-conviction filter. Relax the z-score or\u003cbr\u003e     RSI thresholds, or use shorter timeframes, to generate more signals.\u003cbr\u003e     The methodology is sound; the backtest window was short relative to\u003cbr\u003e     the filter's selectivity. All parameters are documented and adjustable.\u003c\/p\u003e\n\u003cp\u003e  Q: Why \"long\" in the filename but trades SHORT?\u003cbr\u003e  A: Naming encodes the creation sequence, not direction. The header, class\u003cbr\u003e     definition, and all trading logic explicitly operate SHORT. Fully documented.\u003c\/p\u003e\n\u003cp\u003e  Q: Does this require Rithmic or Redis?\u003cbr\u003e  A: No. The portable edition removes all Rithmic and Redis dependencies.\u003c\/p\u003e\n\u003cp\u003e  Q: Can I paper-trade this?\u003cbr\u003e  A: Yes — replace the _submit_short_entry and _submit_flatten placeholders\u003cbr\u003e     with print statements or a simulated fill engine.\u003c\/p\u003e\n\u003cp\u003e  Q: Can I use this on other instruments?\u003cbr\u003e  A: Yes — the z-score + RSI + volume-slope architecture is instrument-agnostic.\u003cbr\u003e     Update SYMBOL, EXCHANGE, and tune z-score threshold and R:R parameters.\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003eRISK DISCLAIMER\u003cbr\u003e───────────────\u003cbr\u003eThis software is for EDUCATIONAL PURPOSES ONLY. It does NOT constitute\u003cbr\u003einvestment or trading advice. Futures trading involves substantial risk of loss.\u003cbr\u003eShort selling futures carries theoretically unlimited upside risk. The 3-trade\u003cbr\u003ebacktest is NOT statistically significant and must NOT be used as a basis for\u003cbr\u003eany trading decision. Past performance does not guarantee future results.\u003cbr\u003eThe 15% drawdown circuit breaker is a software safeguard only — it does not\u003cbr\u003eeliminate risk of loss. Consult a qualified financial professional before\u003cbr\u003etrading real capital.\u003cbr\u003e\u003c\/p\u003e","brand":"HFTCODE.COM","offers":[{"title":"Default Title","offer_id":53624627921205,"sku":null,"price":0.0,"currency_code":"USD","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0967\/8549\/8421\/files\/Screenshot2026-09-21190828.png?v=1790039279","url":"https:\/\/hftcode.com\/products\/python-short-selling-algorithm-nasdaq-100-futures-nq-cme","provider":"HFTCODE.COM","version":"1.0","type":"link"}