//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Owned Strategy: Archive Map Presence
// Regime thresholds are retrieved only after a fail-closed key-presence check.
strategy("PineForge — Archive Map Presence", 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)
regimeChoice = input.string("balanced", "Regime Key", options=["patient", "balanced", "responsive", "unknown"], group="Configuration")
fallbackRsi = input.float(63.0, "Fallback RSI", minval=40, maxval=80, step=0.5, group="Configuration")
trendLength = input.int(46, "Trend EMA Length", minval=5, group="Trend")
atrLength = input.int(18, "ATR Length", minval=2, group="Risk")
maximumAtrLoss = input.float(2.3, "Maximum ATR Loss", minval=0.5, step=0.1, group="Risk")
var map<string, float> thresholdArchive = map.new<string, float>()
if barstate.isfirst
thresholdArchive.put("patient", 61.5)
thresholdArchive.put("balanced", 56.5)
thresholdArchive.put("responsive", 52.0)
hasRequestedRegime = thresholdArchive.contains(regimeChoice)
entryRsi = hasRequestedRegime ? thresholdArchive.get(regimeChoice) : fallbackRsi
rsiValue = ta.rsi(close, 15)
trendLine = ta.ema(close, trendLength)
atrValue = ta.atr(atrLength)
enterLong = hasRequestedRegime and ta.crossover(rsiValue, entryRsi) and close > trendLine
regimeEnded = rsiValue < entryRsi - 9 or close < trendLine
riskExceeded = strategy.position_size > 0 and close < strategy.position_avg_price - atrValue * maximumAtrLoss
if strategy.position_size == 0 and enterLong
strategy.entry("Archive Long", strategy.long)
else if strategy.position_size > 0 and (regimeEnded or riskExceeded)
strategy.close("Archive Long", comment=riskExceeded ? "ATR risk" : "Archive regime")
plot(trendLine, "Trend EMA", color=color.orange)
plot(entryRsi, "Selected RSI", color=color.purple, display=display.data_window)