//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Corporate Strategy: Ledger History NA
strategy("PineForge — Ledger History NA", overlay=true,
initial_capital=100000,
default_qty_type=strategy.percent_of_equity, default_qty_value=6,
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)
lookback = input.int(31, "History Lookback", minval=2, group="Signal")
smoothLength = input.int(12, "Anchor Smoothing", minval=2, group="Signal")
deviationPercent = input.float(2.3, "Entry Deviation %", minval=0.2, step=0.1, group="Signal") * 0.01
atrLength = input.int(14, "ATR Length", minval=2, group="Risk")
stopAtr = input.float(2.3, "Stop ATR", minval=0.5, step=0.1, group="Risk")
float rawAnchor = bar_index >= lookback ? close[lookback] : na
float carriedAnchor = fixnan(rawAnchor)
float safeAnchor = nz(carriedAnchor, close)
float smoothAnchor = ta.sma(safeAnchor, smoothLength)
float atrValue = ta.atr(atrLength)
var int completedSignals = 0
entryThreshold = smoothAnchor * (1.0 - deviationPercent)
exitThreshold = smoothAnchor
longSignal = not na(rawAnchor) and ta.crossunder(close, entryThreshold)
if longSignal
strategy.entry("Ledger Long", strategy.long)
completedSignals += 1
if strategy.position_size > 0 and ta.crossover(close, exitThreshold)
strategy.close("Ledger Long", comment="Mean return")
if strategy.position_size > 0
strategy.exit("Ledger Guard", from_entry="Ledger Long",
stop=strategy.position_avg_price - atrValue * stopAtr)
plot(smoothAnchor, "Carried History Anchor", color=color.purple)
plot(completedSignals, "Signal Count", display=display.data_window)