Files
strat-playground/historical_data/historical_data.py
David Chen 05c17f6b5d -
2026-03-14 20:14:33 +08:00

29 lines
847 B
Python

from typing import List
import pandas as pd
from internal_types.types import OHLC, Instrument
# todo: download parquet from clickhouse and store locally
def historical_data_tradingview_csv(csv: str, instr: Instrument, t0: str, t1: str) -> List[OHLC]:
# [t0, t1)
# t0 & t1: YYYY-mm-dd
df = pd.read_csv(csv)
df['date_dt'] = pd.to_datetime(df['time'], unit='s')
df = df.rename(columns={'time': 'timestamp', 'Volume': 'volume'})
df = df[(pd.Timestamp(t0) <= df['date_dt']) & (df['date_dt'] < pd.Timestamp(t1))]
return [
OHLC(
instr=instr,
timestamp=row.timestamp, # type: ignore
open=row.open, # type: ignore
high=row.high, # type: ignore
low=row.low, # type: ignore
close=row.close, # type: ignore
volume=row.volume) # type: ignore
for row in df.itertuples(index=False)
]