fix: Make strategy backtest LONG and SHORT independently (not reversals)
CRITICAL BUG FIXED (Jan 2, 2026): - Old backtest treated SHORT signal as 'exit LONG only' (reversal system) - Live trading opens actual SHORT position = different outcome - Backtest showed profit but live trading lost money due to mismatch Changes: - Added close_entries_rule='ANY' to strategy declaration - Opposite signal now CLOSES current position first, then opens NEW - Added 'Direction Mode' dropdown: Both / Long Only / Short Only - Comments document the fix for future reference This allows honest evaluation: 1. Run 'Both' mode to see true combined performance 2. Run 'Long Only' to see LONG-only results 3. Run 'Short Only' to see SHORT-only results (likely the problem area)
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
//@version=6
|
||||
strategy("Money Line v11.2 STRATEGY", shorttitle="ML v11.2 Strat", overlay=true, pyramiding=0, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
|
||||
// V11.2 STRATEGY VERSION (Dec 25, 2025):
|
||||
// Same logic as indicator but with entries/exits for TradingView Strategy Report
|
||||
strategy("Money Line v11.2 STRATEGY", shorttitle="ML v11.2 Strat", overlay=true, pyramiding=0, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, close_entries_rule="ANY")
|
||||
// V11.2 STRATEGY VERSION (Jan 2, 2026):
|
||||
// FIXED: Now tests LONG and SHORT as independent trades (not reversals)
|
||||
// - Opposite signal CLOSES current position, then opens NEW position
|
||||
// - This matches how live webhook trading actually works
|
||||
// - Backtest results now accurately reflect real trading performance
|
||||
|
||||
// === DIRECTION MODE ===
|
||||
directionMode = input.string("Both", "Trade Direction", options=["Both", "Long Only", "Short Only"], group="Direction")
|
||||
|
||||
// === CORE PARAMETERS ===
|
||||
atrPeriod = input.int(12, "ATR Period", minval=1, group="Core")
|
||||
@@ -154,16 +160,31 @@ finalLongSignal = buyReady and adxOk and longBufferOk and rsiLongOk and longPosi
|
||||
finalShortSignal = sellReady and adxOk and shortBufferOk and rsiShortOk and shortPositionOk and volumeOk
|
||||
|
||||
// =============================================================================
|
||||
// STRATEGY ENTRIES & EXITS
|
||||
// STRATEGY ENTRIES & EXITS (FIXED: Independent Long/Short tracking)
|
||||
// =============================================================================
|
||||
|
||||
if finalLongSignal
|
||||
// Determine if direction is allowed
|
||||
allowLong = directionMode == "Both" or directionMode == "Long Only"
|
||||
allowShort = directionMode == "Both" or directionMode == "Short Only"
|
||||
|
||||
// Track positions
|
||||
isLong = strategy.position_size > 0
|
||||
isShort = strategy.position_size < 0
|
||||
isFlat = strategy.position_size == 0
|
||||
|
||||
// === LONG ENTRY ===
|
||||
if finalLongSignal and allowLong
|
||||
if isShort
|
||||
strategy.close("Short", comment="Short closed by Long signal") // Close short first
|
||||
strategy.entry("Long", strategy.long)
|
||||
|
||||
if finalShortSignal
|
||||
// === SHORT ENTRY ===
|
||||
if finalShortSignal and allowShort
|
||||
if isLong
|
||||
strategy.close("Long", comment="Long closed by Short signal") // Close long first
|
||||
strategy.entry("Short", strategy.short)
|
||||
|
||||
// Exits with TP/SL
|
||||
// === Exits with TP/SL ===
|
||||
if strategy.position_size > 0
|
||||
strategy.exit("L Exit", "Long", stop=strategy.position_avg_price * (1 - slPct/100), limit=strategy.position_avg_price * (1 + tpPct/100))
|
||||
if strategy.position_size < 0
|
||||
|
||||
Reference in New Issue
Block a user