This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def max_drawdown(equity: pd.Series) -> float: | |
| peak = equity.cummax() | |
| dd = (equity / peak) - 1.0 | |
| return float(dd.min()) | |
| def qs_metrics(equity: pd.Series) -> dict: | |
| """ | |
| Quantstats-style metrics from an equity curve. | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def make_positions(df_feat: pd.DataFrame, regime: pd.Series, params: dict) -> pd.Series: | |
| """ | |
| Same logic as before, with parameters chosen by WFO. | |
| """ | |
| z_thr = params["z_thr"] | |
| range_size = params["range_size"] | |
| lowvol_size = params["lowvol_size"] | |
| highvol_size = params["highvol_size"] | |
| pos = pd.Series(0.0, index=df_feat.index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def load_cache() -> dict: | |
| p = Path(LLM_CACHE_FILE) | |
| if p.exists(): | |
| return json.loads(p.read_text(encoding="utf-8")) | |
| return {} | |
| def save_cache(cache: dict) -> None: | |
| Path(LLM_CACHE_FILE).write_text(json.dumps(cache, indent=2, sort_keys=True), encoding="utf-8") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fit_kmeans_pre_oos(df_feat: pd.DataFrame) -> tuple[KMeans, np.ndarray, np.ndarray, dict[int, str]]: | |
| """ | |
| Fit KMeans using ONLY data BEFORE OOS_START (prevents training leakage). | |
| """ | |
| split = pd.to_datetime(OOS_START) | |
| rows = [] | |
| for t in label_dates(df_feat.index): | |
| if t >= split: | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def load_data() -> pd.DataFrame: | |
| df = yf.download(SYMBOL, start=START, end=END, interval="1d", progress=False, group_by='tickers')[SYMBOL] | |
| if df.empty: | |
| raise RuntimeError(f"No data returned for {SYMBOL}. Check symbol/date range.") | |
| df = df.rename(columns=str.title) | |
| if "Close" not in df.columns and "Adj Close" in df.columns: | |
| df["Close"] = df["Adj Close"] | |
| df = df.dropna(subset=["Close"]) | |
| df.index = pd.to_datetime(df.index) | |
| return df |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DEEPSEEK_API_KEY = "PASTE_YOUR_DEEPSEEK_KEY_HERE" | |
| DEEPSEEK_BASE_URL = "https://api.deepseek.com" | |
| DEEPSEEK_MODEL = "deepseek-chat" | |
| # --- Market / data --- | |
| SYMBOL = "EURUSD=X" | |
| START = "2006-01-01" | |
| END = "2026-04-11" | |
| OOS_START = "2023-01-01" # we show equity curves from this date onward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import math | |
| import time | |
| import re | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import yfinance as yf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Execution Log --- | |
| --- Starting analysis for AAPL --- | |
| Preliminary decision for AAPL: Signal=0 | |
| --- Starting analysis for MSFT --- | |
| Preliminary decision for MSFT: Signal=0 | |
| --- Starting analysis for GOOG --- | |
| Preliminary decision for GOOG: Signal=0 | |
| Portfolio Agent: No BUY signals. Allocating 0% to all tickers. | |
| Execution Agent: Submitting real trades based on portfolio plan. | |
| Execution: Fetched cash balance: $100000.00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- Market is OPEN. Starting trading loop for 2026-01-12 until 16:00:00 --- | |
| --- Running Engine Iteration at 15:32:35 --- | |
| INFO: Fetching 90 observations of 5min data for AAPL... | |
| INFO: Searching web for 10 news links about AAPL since 2026-01-12 15:22 | |
| Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY. | |
| INFO: Fetching 90 observations of 5min data for MSFT... | |
| INFO: Searching web for 10 news links about MSFT since 2026-01-12 15:22 | |
| Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (base) josgt@josgt-desktop:~/Downloads/testing11$ cd "/home/josgt/Downloads/testing11/AI-in-Trading-Workflow/LLMs/example_02_Agentic_AI_based_Portfolio_manager_using_Alpaca_API" | |
| (base) josgt@josgt-desktop:~/Downloads/testing11/AI-in-Trading-Workflow/LLMs/example_02_Agentic_AI_based_Portfolio_manager_using_Alpaca_API$ conda activate alpaca_bot | |
| (alpaca_bot) josgt@josgt-desktop:~/Downloads/testing11/AI-in-Trading-Workflow/LLMs/example_02_Agentic_AI_based_Portfolio_manager_using_Alpaca_API$ python3 main.py | |
| /home/josgt/anaconda3/envs/alpaca_bot/lib/python3.12/site-packages/pyfolio/pos.py:25: UserWarning: Module "zipline.assets" not found; multipliers will not be applied to position notionals. | |
| warnings.warn( | |
| /home/josgt/anaconda3/envs/alpaca_bot/lib/python3.12/site-packages/langchain_tavily/tavily_research.py:97: UserWarning: Field name "output_schema" in "TavilyResearch" shadows an attribute in parent "BaseTool" | |
| class TavilyResearch(BaseTool): # type: ignore[override, override] | |
| /home/josgt/anaconda3/envs/alpa |