candles.py 1014 B

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import annotations
  2. from pathlib import Path
  3. import pandas as pd
  4. from okx_codex_trader.models import Candle
  5. def load_candles_csv(data_dir: Path, symbol: str, bar: str) -> list[Candle]:
  6. frame = pd.read_csv(data_dir / symbol / f"{bar}.csv")
  7. return [
  8. Candle(
  9. symbol=symbol,
  10. ts=int(row.ts),
  11. open=float(row.open),
  12. high=float(row.high),
  13. low=float(row.low),
  14. close=float(row.close),
  15. volume=float(row.volume),
  16. )
  17. for row in frame.itertuples(index=False)
  18. ]
  19. def align_candles_by_ts(left: list[Candle], right: list[Candle]) -> tuple[list[Candle], list[Candle]]:
  20. right_by_ts = {candle.ts: candle for candle in right}
  21. left_out: list[Candle] = []
  22. right_out: list[Candle] = []
  23. for candle in left:
  24. other = right_by_ts.get(candle.ts)
  25. if other is not None:
  26. left_out.append(candle)
  27. right_out.append(other)
  28. return left_out, right_out