As it took me some time to figure it out and I love the open-source idea (no need to do the same work multiple times).
Here two ways how I solved it. Be aware that I shifted the Date in both versions, as in my case the CSV were predictions for the next day. The CSV was in my case daily.
Numpy:
from datetime import datetime
from pathlib import Path
import numpy as np
here = Path(__file__).parent
self.vars['external_data'] = np.genfromtxt(here / './ml-LTC-USD-daily-index.csv', dtype=None, delimiter=",",
names=True,
encoding='UTF-8',
converters={0: lambda s: datetime.strptime(s, "%Y-%m-%d")})
Use it like:
@property
def external_signal(self):
now = datetime.fromtimestamp(self.candles[-1, 0] / 1000).replace(hour=0, minute=0, second=0, microsecond=0)
# search and find the index for the current date.
signal_idx = np.where(self.vars['external_data']['Date'] == now)[0] + 1
return self.vars['external_data']['Action'][signal_idx]
Pandas:
from datetime import datetime
from pathlib import Path
import pandas as pd
def before(self):
# on the first candle get csv data
if self.index == 0:
here = Path(__file__).parent
# dynamically determine the right csv from the self.symbol and shift the index 1 day.
df_external = pd.read_csv(here / './ml-{}-USD-daily-index.csv'.format(self.symbol.split("-")[0]),
parse_dates=['Date'], index_col=0).tshift(periods=1, freq='D')
# slice not needed data
now = datetime.fromtimestamp(self.candles[-1, 0] / 1000).replace(hour=0, minute=0, second=0, microsecond=0)
df_external = df_external.loc[now:]
# dynamically determine the timeframe we need to resample those data to.
self.vars['raw_external_data'] = df_external.resample(self.timeframe.replace("m", "T").replace("h", "H")).fillna(
"pad")
# merge the candles with the external data
candle_timestamps = pd.DataFrame(index=pd.to_datetime(self.candles[-50:, 0], unit='ms'))
self.vars['external_data'] = candle_timestamps.merge(self.vars['raw_external_data'], how='left', left_index=True,
right_index=True)
Use it like:
self.vars['external_data']['Action'].iloc[-1]