// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe 27 — 3-MA stack with var-bool transition latch
// Purpose: parity test for triple-SMA stack with `var bool prevBull / prevBear`
// latch suppressing duplicate same-direction entries.
//
//@version=6
strategy("MA Stack Array", overlay=true, initial_capital=1000000, process_orders_on_close=false, pyramiding=1, commission_type=strategy.commission.percent, commission_value=0, slippage=0, default_qty_type=strategy.fixed, default_qty_value=1)
// === Inputs ===
ma1Len = input.int(10, "MA 1 Length", minval=2)
ma2Len = input.int(20, "MA 2 Length", minval=2)
ma3Len = input.int(50, "MA 3 Length", minval=2)
// === Calculate MAs ===
ma1 = ta.sma(close, ma1Len)
ma2 = ta.sma(close, ma2Len)
ma3 = ta.sma(close, ma3Len)
// Check stack order directly (no array crash risk)
bullStack = ma1 > ma2 and ma2 > ma3
bearStack = ma1 < ma2 and ma2 < ma3
// === Track transitions ===
var bool prevBull = false
var bool prevBear = false
if bullStack and not prevBull
strategy.entry("Long", strategy.long)
if bearStack and not prevBear
strategy.entry("Short", strategy.short)
prevBull := bullStack
prevBear := bearStack
// === Exit when stack breaks ===
if strategy.position_size > 0 and not bullStack
strategy.close("Long")
if strategy.position_size < 0 and not bearStack
strategy.close("Short")
plot(ma1, "MA 10", color=color.blue)
plot(ma2, "MA 20", color=color.green)
plot(ma3, "MA 50", color=color.red)