entries = df.index[(df["pos"].diff() == 1)]
exits = df.index[(df["pos"].diff() == -1)]
fig, (ax1, ax2, ax3) = plt.subplots(
3, 1, figsize=(13, 10), sharex=True,
gridspec_kw={"height_ratios": [3, 1, 2]},
)
ax1.plot(df.index, df["close"], lw=1.1, color="black", label="Close")
ax1.plot(df.index, df["SMA_20"], lw=0.9, label="SMA 20")
ax1.plot(df.index, df["SMA_50"], lw=0.9, label="SMA 50")
bbu, bbl = "BBU_20_2.0", "BBL_20_2.0"
if bbu in df and bbl in df:
ax1.fill_between(df.index, df[bbl], df[bbu], alpha=0.12, label="Bollinger 20,2")
ax1.scatter(entries, df.loc[entries, "close"], marker="^", s=70,
color="green", zorder=5, label="Entry")
ax1.scatter(exits, df.loc[exits, "close"], marker="v", s=70,
color="red", zorder=5, label="Exit")
ax1.set_title(f"{TICKER} — price, MAs, Bollinger, signals")
ax1.legend(loc="upper left"); ax1.grid(alpha=0.3)
ax2.plot(df.index, df["RSI_14"], lw=0.9, label="RSI 14")
ax2.axhline(70, color="red", ls="--", lw=0.6)
ax2.axhline(30, color="green", ls="--", lw=0.6)
ax2.set_title("RSI 14"); ax2.legend(loc="upper left"); ax2.grid(alpha=0.3)
ax3.plot(df.index, (1 + df["ret"]).cumprod(), lw=1.1, label="Buy & Hold")
ax3.plot(df.index, (1 + df["strat_ret"]).cumprod(), lw=1.1, label="Strategy")
ax3.set_title("Equity curves ($1 start)")
ax3.legend(loc="upper left"); ax3.grid(alpha=0.3)
plt.tight_layout(); plt.show()
print("\nTweak TICKER, the Strategy list, or the sweep grid to keep exploring.")
Credit: Source link
























