//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Owned Strategy: Gyroscope CMO Median
// A robust rolling center and bounded momentum jointly define the regime.
strategy("PineForge — Gyroscope CMO Median", overlay=true,
initial_capital=100000,
default_qty_type=strategy.fixed, default_qty_value=2,
pyramiding=0, commission_type=strategy.commission.percent,
commission_value=0.05, slippage=1,
margin_long=100, margin_short=100,
process_orders_on_close=false, calc_on_order_fills=false)
medianLength = input.int(27, "Median Length", minval=3, group="Signal")
cmoLength = input.int(16, "CMO Length", minval=2, group="Momentum")
entryMomentum = input.float(12.0, "Entry CMO", minval=-80, maxval=80, step=1, group="Momentum")
exitMomentum = input.float(-9.0, "Exit CMO", minval=-80, maxval=80, step=1, group="Momentum")
atrLength = input.int(19, "ATR Length", minval=2, group="Risk")
maximumAtrLoss = input.float(2.1, "Maximum ATR Loss", minval=0.5, step=0.1, group="Risk")
medianLine = ta.median(close, medianLength)
cmoValue = ta.cmo(close, cmoLength)
atrValue = ta.atr(atrLength)
enterLong = ta.crossover(close, medianLine) and cmoValue > entryMomentum
momentumEnded = close < medianLine or cmoValue < exitMomentum
riskExceeded = strategy.position_size > 0 and close < strategy.position_avg_price - atrValue * maximumAtrLoss
if strategy.position_size == 0 and enterLong
strategy.entry("Gyroscope Long", strategy.long)
else if strategy.position_size > 0 and (momentumEnded or riskExceeded)
strategy.close("Gyroscope Long", comment=riskExceeded ? "ATR risk" : "Momentum ended")
plot(medianLine, "Rolling Median", color=color.yellow)
plot(cmoValue, "CMO", color=color.blue, display=display.data_window)