Skip to content

Conversation

@sackio
Copy link

@sackio sackio commented Dec 24, 2025

Summary

The fvg() function uses shift(-1) to look at the next bar's high/low, and swing_highs_lows() uses shift(-(swing_length//2)) to look forward. This creates lookahead bias that makes these indicators unsuitable for backtesting or live trading without adjustment.

This PR adds a causal=False parameter to both functions:

  • When causal=True, fvg() shifts all outputs forward by 1 bar
  • When causal=True, swing_highs_lows() shifts outputs forward by swing_length bars

This ensures signals only appear when they would realistically be known, making the indicators suitable for causal backtesting.

Usage

# Default behavior (with lookahead - for analysis/visualization)
fvg_result = smc.fvg(ohlc)

# Causal mode (no lookahead - for backtesting/trading)
fvg_result = smc.fvg(ohlc, causal=True)
swing_result = smc.swing_highs_lows(ohlc, swing_length=50, causal=True)

Note

This PR also includes the read-only buffer fix from #94.

Test plan

  • Verified causal shift delays signals appropriately
  • Existing unit tests should pass (default behavior unchanged)

…_highs_lows

The fvg() function uses shift(-1) to look at the next bar's high/low,
and swing_highs_lows() uses shift(-(swing_length//2)) to look forward.
This creates lookahead bias that makes these indicators unsuitable for
backtesting or live trading without adjustment.

This commit adds a 'causal=False' parameter to both functions:
- When causal=True, fvg() shifts all outputs forward by 1 bar
- When causal=True, swing_highs_lows() shifts outputs forward by swing_length bars

This ensures signals only appear when they would realistically be known,
making the indicators suitable for causal backtesting.

Also includes the read-only buffer fix (.copy() after np.where calls).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant