# ============================================================
# Cell 2 — Fetch OHLCV Data (OKX + IST to match TradingView)
# ============================================================
importccxtimportpandasaspdexchange=ccxt.okx()ohlcv=exchange.fetch_ohlcv("ENJ/USDT",timeframe="15m",limit=200)df=pd.DataFrame(ohlcv,columns=["timestamp","open","high","low","close","volume"])df["timestamp"]=pd.to_datetime(df["timestamp"],unit="ms")# ✅ Convert UTC → IST to match TradingView (India)
df["timestamp"]=df["timestamp"].dt.tz_localize("UTC").dt.tz_convert("Asia/Kolkata")df.set_index("timestamp",inplace=True)print(df.tail(5))
# ============================================================
# Cell 3 — CISD Detection
# ============================================================
importnumpyasnpdefdetect_swing_points(df,left=5,right=5):df["swing_high"]=Falsedf["swing_low"]=Falseforiinrange(left,len(df)-right):window_high=df["high"].iloc[i-left:i+right+1]window_low=df["low"].iloc[i-left:i+right+1]ifdf["high"].iloc[i]==window_high.max():df.at[df.index[i],"swing_high"]=Trueifdf["low"].iloc[i]==window_low.min():df.at[df.index[i],"swing_low"]=Truereturndfdefdetect_cisd(df):"""
Bullish CISD → bias was bearish + close breaks ABOVE last swing high
Bearish CISD → bias was bullish + close breaks BELOW last swing low
"""df=detect_swing_points(df)df["cisd_bullish"]=Falsedf["cisd_bearish"]=Falsedf["cisd_level"]=np.nanbias=Noneswing_highs=[]swing_lows=[]last_swing_high_price=Nonelast_swing_low_price=Noneforiinrange(len(df)):row=df.iloc[i]ifrow["swing_high"]:ifswing_highs:bias="bullish"ifrow["high"]>swing_highs[-1]else"bearish"swing_highs.append(row["high"])last_swing_high_price=row["high"]ifrow["swing_low"]:ifswing_lows:bias="bearish"ifrow["low"]<swing_lows[-1]else"bullish"swing_lows.append(row["low"])last_swing_low_price=row["low"]# Bullish CISD
if (bias=="bearish"andlast_swing_high_priceisnotNoneandrow["close"]>last_swing_high_price):df.at[df.index[i],"cisd_bullish"]=Truedf.at[df.index[i],"cisd_level"]=last_swing_high_pricebias="bullish"# Bearish CISD
elif (bias=="bullish"andlast_swing_low_priceisnotNoneandrow["close"]<last_swing_low_price):df.at[df.index[i],"cisd_bearish"]=Truedf.at[df.index[i],"cisd_level"]=last_swing_low_pricebias="bearish"returndfdf=detect_cisd(df)bullish_cisd=df[df["cisd_bullish"]]bearish_cisd=df[df["cisd_bearish"]]print(f"✅ Bullish CISDs : {len(bullish_cisd)}")print(f"🔻 Bearish CISDs : {len(bearish_cisd)}")signals=df[df["cisd_bullish"]|df["cisd_bearish"]][["open","high","low","close","cisd_bullish","cisd_bearish","cisd_level"]]print("\nRecent CISD Signals:")print(signals.tail(5))
Top comments (0)