Prediction and model training work

This commit is contained in:
2026-08-06 17:22:57 +03:00
parent fddcc9190a
commit 465e09fc82
13 changed files with 1689 additions and 403 deletions
+21 -229
View File
@@ -1,236 +1,31 @@
"""Alpaca daily candle fetcher."""
"""Alpaca daily candle fetcher CLI wrapper.
The reusable implementation lives in the shared library module so the same
fetching and parquet persistence logic can be reused by the prediction flow.
"""
from __future__ import annotations
import argparse
import os
import re
from dataclasses import dataclass
from datetime import date, datetime, time, timedelta
from pathlib import Path
from zoneinfo import ZoneInfo
import pandas as pd
from dotenv import load_dotenv
# Load environment variables from .env file if available
load_dotenv()
DEFAULT_OUTPUT_DIR = Path("data/alpaca/daily")
DEFAULT_DURATION = "1 W"
EASTERN_TZ = ZoneInfo("America/New_York")
DURATION_PATTERN = re.compile(r"^(\d+)\s*([DWMY])$")
@dataclass(frozen=True)
class DailyCandle:
"""Daily OHLCV market data for one trading session."""
trading_day: date
open: float
high: float
low: float
close: float
volume: int
def default_end_date() -> date:
"""Return yesterday's date in the US/Eastern market timezone."""
return datetime.now(EASTERN_TZ).date() - timedelta(days=1)
def current_market_date() -> date:
"""Return today's date in the US/Eastern market timezone."""
return datetime.now(EASTERN_TZ).date()
def parse_end_date(value: str) -> date:
"""Parse an end date in YYYYMMDD format."""
try:
return datetime.strptime(value, "%Y%m%d").date()
except ValueError as exc:
raise argparse.ArgumentTypeError(
"end date must use YYYYMMDD format, such as 20250605"
) from exc
def parse_duration(value: str) -> str:
"""Normalize and validate a duration string like '1 W' or '1 M'."""
normalized = " ".join(value.upper().split())
if not DURATION_PATTERN.fullmatch(normalized):
raise argparse.ArgumentTypeError(
"duration must look like '1 D', '1 W', '1 M', or '1 Y'"
)
return normalized
def duration_to_start_date(end_date: date, duration: str) -> date:
"""Calculate the start date based on end date and a duration string."""
match = DURATION_PATTERN.match(duration)
if not match:
raise ValueError(f"Invalid duration format: {duration}")
amount = int(match.group(1))
unit = match.group(2)
if unit == "D":
return end_date - timedelta(days=amount)
if unit == "W":
return end_date - timedelta(weeks=amount)
if unit == "M":
return end_date - timedelta(days=amount * 30)
if unit == "Y":
return end_date - timedelta(days=amount * 365)
raise ValueError(f"Unsupported duration unit: {unit}")
def fetch_daily_candles(
symbol: str,
end_date: date,
duration: str,
api_key: str | None = None,
secret_key: str | None = None,
) -> list[DailyCandle]:
"""Fetch daily candles for a symbol using Alpaca Market Data API."""
try:
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame
except ImportError as exc:
raise SystemExit(
"Missing dependency: alpaca-py. Install it via 'pip install alpaca-py' "
"before running the script."
) from exc
start_date = duration_to_start_date(end_date, duration)
start_dt = datetime.combine(start_date, time.min, tzinfo=EASTERN_TZ)
end_dt = datetime.combine(end_date, time.max, tzinfo=EASTERN_TZ)
print(start_dt, end_dt)
client = StockHistoricalDataClient(api_key=api_key, secret_key=secret_key)
request_params = StockBarsRequest(
symbol_or_symbols=symbol,
timeframe=TimeFrame.Day,
start=start_dt,
end=end_dt,
)
bars = client.get_stock_bars(request_params)
if not bars or symbol not in bars.data:
print(f"Alpaca returned no historical bars for {symbol}.")
return []
candles: list[DailyCandle] = []
for bar in bars[symbol]:
trading_day = bar.timestamp.astimezone(EASTERN_TZ).date()
candles.append(
DailyCandle(
trading_day=trading_day,
open=float(bar.open),
high=float(bar.high),
low=float(bar.low),
close=float(bar.close),
volume=int(bar.volume),
)
)
return candles
def candles_to_frame(symbol: str, candles: list[DailyCandle]) -> pd.DataFrame:
"""Convert candles to a date-indexed dataframe ready for Parquet storage."""
rows = [
{
"date": candle.trading_day,
"symbol": symbol,
"open": candle.open,
"high": candle.high,
"low": candle.low,
"close": candle.close,
"volume": candle.volume,
}
for candle in candles
]
frame = pd.DataFrame.from_records(rows)
if frame.empty:
return pd.DataFrame(
columns=["symbol", "open", "high", "low", "close", "volume"],
index=pd.Index([], name="date"),
)
frame["date"] = pd.to_datetime(frame["date"]).dt.date
return frame.set_index("date")
def read_existing_candles(path: Path) -> pd.DataFrame:
"""Read an existing candle Parquet file as a date-indexed dataframe."""
if not path.exists():
return pd.DataFrame(
columns=["symbol", "open", "high", "low", "close", "volume"],
index=pd.Index([], name="date"),
)
frame = pd.read_parquet(path)
if "date" in frame.columns:
frame["date"] = pd.to_datetime(frame["date"]).dt.date
frame = frame.set_index("date")
frame.index = pd.to_datetime(frame.index).date
frame.index.name = "date"
return frame
def oldest_stored_date_or_today(path: Path) -> date:
"""Return the oldest stored candle date, or today if no data exists yet."""
existing = read_existing_candles(path)
if existing.empty:
return current_market_date()
return min(existing.index)
def write_candles(path: Path, symbol: str, candles: list[DailyCandle]) -> pd.DataFrame:
"""Append candles to a ticker Parquet file, keeping one row per date."""
existing = read_existing_candles(path)
fetched = candles_to_frame(symbol, candles)
combined = pd.concat([existing, fetched])
if not combined.empty:
combined = combined[~combined.index.duplicated(keep="last")]
combined = combined.sort_index()
path.parent.mkdir(parents=True, exist_ok=True)
combined.to_parquet(path, index=True)
return combined
def print_candles(symbol: str, candles: list[DailyCandle]) -> None:
"""Print candles in a compact table."""
print("date,symbol,open,high,low,close,volume")
for candle in candles:
print(
f"{candle.trading_day.isoformat()},"
f"{symbol},"
f"{candle.open:.2f},"
f"{candle.high:.2f},"
f"{candle.low:.2f},"
f"{candle.close:.2f},"
f"{candle.volume}"
)
from trading_bot.data.alpaca_daily_lib import (
DEFAULT_DURATION,
DEFAULT_OUTPUT_DIR,
DailyCandle,
candles_to_frame,
current_market_date,
default_end_date,
duration_to_start_date,
fetch_daily_candles,
oldest_stored_date_or_today,
parse_duration,
parse_end_date,
print_candles,
read_existing_candles,
write_candles,
)
def parse_args() -> argparse.Namespace:
@@ -292,8 +87,6 @@ def main() -> None:
else args.end_date or default_end_date()
)
print(oldest_stored_date_or_today(output_path))
print(
f"Fetching {symbol} daily candles ending {end_date:%Y-%m-%d} "
f"for duration {args.duration} via Alpaca API"
@@ -305,7 +98,6 @@ def main() -> None:
api_key=args.api_key,
secret_key=args.secret_key,
)
#print_candles(symbol, candles)
stored = write_candles(output_path, symbol, candles)
print(f"Wrote {len(stored)} total daily rows to {output_path}")