diff --git a/engine.py b/engine.py index 7fb242e..e44659d 100644 --- a/engine.py +++ b/engine.py @@ -544,11 +544,17 @@ class TechnicalEngine: tenkan_val = tenkan.iloc[-1] kijun_val = kijun.iloc[-1] - # Current (unshifted) cloud - sa = (tenkan.iloc[-1] + kijun.iloc[-1]) / 2 + # Senkou Spans are plotted 26 bars ahead of their calculation date, so + # the cloud boundaries at the *current* bar are the values computed 26 + # bars ago. Using iloc[-1] would compare price to the future cloud + # (look-ahead bias). iloc[-27] gives the correctly lagged cloud. + lag = c.ichimoku_kijun # 26 by default sb_raw = (high.rolling(c.ichimoku_senkou_b).max() + low.rolling(c.ichimoku_senkou_b).min()) / 2 - sb = sb_raw.iloc[-1] + if len(sb_raw) < lag + 1: + return + sa = (tenkan.iloc[-(lag + 1)] + kijun.iloc[-(lag + 1)]) / 2 + sb = sb_raw.iloc[-(lag + 1)] if pd.isna(sa) or pd.isna(sb) or pd.isna(tenkan_val) or pd.isna(kijun_val): return