//@version=6
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 PineForge
// PineForge Corporate Strategy: Waypoint Weekday MACD
// Calendar gating is explicit so weekday behavior remains reproducible.
strategy("PineForge — Waypoint Weekday MACD", overlay=false,
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)
fastLength = input.int(12, "MACD Fast", minval=2, group="Signal")
slowLength = input.int(26, "MACD Slow", minval=3, group="Signal")
signalLength = input.int(9, "MACD Signal", minval=2, group="Signal")
tradeWeekends = input.bool(false, "Trade Weekends", group="Calendar")
allowShort = input.bool(true, "Allow Short", group="Execution")
atrLength = input.int(14, "ATR Length", minval=2, group="Risk")
stopAtr = input.float(2.1, "Stop ATR", minval=0.5, step=0.1, group="Risk")
[macdLine, signalLine, macdHistogram] = ta.macd(close, fastLength, slowLength, signalLength)
atrValue = ta.atr(atrLength)
weekday = dayofweek >= dayofweek.monday and dayofweek <= dayofweek.friday
calendarAllowed = tradeWeekends or weekday
calendarClosed = not calendarAllowed and calendarAllowed[1]
if calendarAllowed and ta.crossover(macdLine, signalLine)
strategy.entry("Waypoint Long", strategy.long)
if calendarAllowed and allowShort and ta.crossunder(macdLine, signalLine)
strategy.entry("Waypoint Short", strategy.short)
if calendarClosed
strategy.close("Waypoint Long", comment="Weekday window closed")
strategy.close("Waypoint Short", comment="Weekday window closed")
if strategy.position_size > 0
strategy.exit("Waypoint Long Guard", from_entry="Waypoint Long",
stop=strategy.position_avg_price - atrValue * stopAtr)
if strategy.position_size < 0
strategy.exit("Waypoint Short Guard", from_entry="Waypoint Short",
stop=strategy.position_avg_price + atrValue * stopAtr)
plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)
plot(macdHistogram, "Histogram", color=macdHistogram >= 0 ? color.green : color.red,
style=plot.style_columns)