Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created May 3, 2026 20:50
Show Gist options
  • Select an option

  • Save quantra-go-algo/29b991f1c002c1cd3c3828325441f00e to your computer and use it in GitHub Desktop.

Select an option

Save quantra-go-algo/29b991f1c002c1cd3c3828325441f00e to your computer and use it in GitHub Desktop.
def main():
if DEEPSEEK_API_KEY.strip() == "PASTE_YOUR_DEEPSEEK_KEY_HERE":
print("WARNING: You have not set your DeepSeek API key. LLM curve will fail.")
print("Edit DEEPSEEK_API_KEY at the top of this script.\n")
print(f"Loading {SYMBOL} from {START} to today ...")
df = load_data()
df_feat = add_features(df)
oos_start = pd.to_datetime(OOS_START)
if not (df_feat.index > oos_start).any():
raise RuntimeError("OOS_START is outside your data range.")
# --- Regime labels ---
print("\nComputing Non‑LLM regimes (KMeans fit pre‑2023) ...")
reg_non = label_regimes_kmeans(df_feat)
reg_llm = None
try:
print("\nComputing LLM regimes (DeepSeek, cached) ...")
reg_llm = label_regimes_llm(df_feat)
except Exception as e:
print("\nLLM regimes failed (check API key / connectivity).")
print("Error:", str(e))
reg_llm = None
# --- Walk-forward OOS equity curves (monthly optimization) ---
print("\nRunning walk-forward optimization (Non‑LLM regimes) ...")
eq_non, params_non = walk_forward_oos(df_feat, reg_non)
params_non.to_csv("wfo_params_non_llm.csv", index=False)
eq_llm = None
params_llm = None
if reg_llm is not None:
print("\nRunning walk-forward optimization (LLM regimes) ...")
eq_llm, params_llm = walk_forward_oos(df_feat, reg_llm)
params_llm.to_csv("wfo_params_llm.csv", index=False)
# --- Metrics (OOS 2023+) ---
non_m = qs_metrics(eq_non)
llm_m = qs_metrics(eq_llm) if eq_llm is not None else None
print_metrics_table(non_m, llm_m)
# --- Plot (single plot, two curves) ---
plt.figure()
plt.plot(eq_non.index, eq_non.values, label="Non‑LLM (KMeans) + monthly WFO")
if eq_llm is not None:
plt.plot(eq_llm.index, eq_llm.values, label="LLM (DeepSeek) + monthly WFO")
plt.title(f"{SYMBOL}: OOS Equity Curves (from {OOS_START})")
plt.xlabel("Date")
plt.ylabel("Equity (rebased)")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
# --- Save equity curves ---
eq_non.to_csv("equity_non_llm_oos_wfo.csv")
print("\nSaved: equity_non_llm_oos_wfo.csv, wfo_params_non_llm.csv")
if eq_llm is not None:
eq_llm.to_csv("equity_llm_oos_wfo.csv")
print("Saved: equity_llm_oos_wfo.csv, wfo_params_llm.csv")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment