// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe wunder-scalper-probe-01-reverse-on-signal — explicit close-then-entry reverse logic
// Clean-room: written from validation goal, not from source.
//
// Purpose: isolates the explicit reverse-position pattern used by
// WunderTrading-style scalp bots. On a long signal, if the position is
// currently short, the script first calls `strategy.close_all()` and then
// `strategy.entry("L", strategy.long)` — same bar, two distinct orders
// rather than the single auto-reversing `strategy.entry`. The engine
// surface under test is the same-bar `close_all → entry` ordering: TV
// generates a closing trade for the short followed by an opening trade
// for the long, both at the next-bar open by default. Any disagreement
// in trade list length or fill price points to engine reverse-handling
// drift.
//
// Validation goal: bit-exact trade list against TradingView's broker emulator
// on the reference 15-minute ETH-USDT-USDT feed. Each reversal must show as
// two trades (close + open), not a single auto-reverse trade.
//
// TV setup: 15m chart, ETH-USDT-USDT (Binance USDT-M Perpetual). Full window
// matching corpus/data/ohlcv_ETH-USDT-USDT_15m.csv. Strategy properties:
// leave defaults from script header. Export "List of trades" → tv_trades.csv.
//@version=6
strategy("PF probe wunder-scalper-probe-01-reverse-on-signal", shorttitle="PF_WS01_REV",
overlay=true, initial_capital=1000000,
commission_type=strategy.commission.percent, commission_value=0,
slippage=0, default_qty_type=strategy.fixed, default_qty_value=1,
pyramiding=1, process_orders_on_close=false)
// ---- inputs --------------------------------------------------------
int i_fast = input.int(5, "Fast EMA", minval=2, maxval=50)
int i_slow = input.int(13, "Slow EMA", minval=3, maxval=100)
// ---- direction signal ---------------------------------------------
float ema_fast = ta.ema(close, i_fast)
float ema_slow = ta.ema(close, i_slow)
bool long_signal = ta.crossover(ema_fast, ema_slow)
bool short_signal = ta.crossunder(ema_fast, ema_slow)
// ---- explicit reverse routing -------------------------------------
if long_signal
if strategy.position_size < 0
strategy.close_all(comment="reverse close short")
strategy.entry("L", strategy.long, qty=1, comment="open long")
if short_signal
if strategy.position_size > 0
strategy.close_all(comment="reverse close long")
strategy.entry("S", strategy.short, qty=1, comment="open short")