lowkey_backtest/data.py

25 lines
979 B
Python
Raw Normal View History

from __future__ import annotations
import pandas as pd
from pathlib import Path
def load_data(start: str, end: str, timeframe_minutes: int, csv_path: Path) -> tuple[pd.DataFrame, pd.DataFrame]:
df_1min = pd.read_csv(csv_path)
df_1min["Timestamp"] = pd.to_datetime(df_1min["Timestamp"], unit="s", utc=True)
df_1min = df_1min[(df_1min["Timestamp"] >= pd.Timestamp(start, tz="UTC")) &
(df_1min["Timestamp"] <= pd.Timestamp(end, tz="UTC"))] \
.sort_values("Timestamp").reset_index(drop=True)
if timeframe_minutes != 1:
g = df_1min.set_index("Timestamp").resample(f"{timeframe_minutes}min")
df = pd.DataFrame({
"Open": g["Open"].first(),
"High": g["High"].max(),
"Low": g["Low"].min(),
"Close": g["Close"].last(),
"Volume": g["Volume"].sum(),
}).dropna().reset_index()
else:
df = df_1min.copy()
return df_1min, df