mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-02 12:30:45 +08:00
BUG: accounting for daily historical bars with minute freq algo
This commit is contained in:
@@ -16,26 +16,28 @@ def handle_data(context, data):
|
||||
price = data.current(context.asset, 'close')
|
||||
print('got price {price}'.format(price=price))
|
||||
|
||||
# prices = data.history(
|
||||
# context.asset,
|
||||
# fields='price',
|
||||
# bar_count=20,
|
||||
# frequency='1T'
|
||||
# )
|
||||
# rsi = talib.RSI(prices.values, timeperiod=14)[-1]
|
||||
# print('got rsi: {}'.format(rsi))
|
||||
pass
|
||||
try:
|
||||
prices = data.history(
|
||||
context.asset,
|
||||
fields='price',
|
||||
bar_count=16,
|
||||
frequency='1D'
|
||||
)
|
||||
rsi = talib.RSI(prices.values, timeperiod=14)[-1]
|
||||
print('got rsi: {}'.format(rsi))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
run_algorithm(
|
||||
capital_base=250,
|
||||
start=pd.to_datetime('2017-1-1', utc=True),
|
||||
end=pd.to_datetime('2017-10-22', utc=True),
|
||||
data_frequency='daily',
|
||||
data_frequency='minute',
|
||||
initialize=initialize,
|
||||
handle_data=handle_data,
|
||||
analyze=None,
|
||||
exchange_name='poloniex',
|
||||
exchange_name='bitfinex',
|
||||
algo_namespace='simple_loop',
|
||||
base_currency='btc'
|
||||
)
|
||||
|
||||
@@ -284,7 +284,7 @@ class ExchangeBundle:
|
||||
|
||||
self._write(data, writer, data_frequency)
|
||||
|
||||
def ingest_ctable(self, asset, data_frequency, period, start_dt, end_dt,
|
||||
def ingest_ctable(self, asset, data_frequency, period,
|
||||
writer, empty_rows_behavior='strip', cleanup=False):
|
||||
"""
|
||||
Merge a ctable bundle chunk into the main bundle for the exchange.
|
||||
@@ -315,6 +315,12 @@ class ExchangeBundle:
|
||||
if reader is None:
|
||||
raise TempBundleNotFoundError(path=path)
|
||||
|
||||
start_dt = reader.first_trading_day
|
||||
end_dt = reader.last_available_dt
|
||||
|
||||
if data_frequency == 'daily':
|
||||
end_dt = end_dt - pd.Timedelta(hours=23, minutes=59)
|
||||
|
||||
arrays = None
|
||||
try:
|
||||
arrays = reader.load_raw_arrays(
|
||||
@@ -420,6 +426,12 @@ class ExchangeBundle:
|
||||
dict[TradingPair, list[dict(str, Object]]]
|
||||
|
||||
"""
|
||||
get_start_end = get_month_start_end \
|
||||
if data_frequency == 'minute' else get_year_start_end
|
||||
|
||||
start_dt, _ = get_start_end(start_dt)
|
||||
_, end_dt = get_start_end(end_dt)
|
||||
|
||||
reader = self.get_reader(data_frequency)
|
||||
|
||||
chunks = dict()
|
||||
@@ -450,8 +462,6 @@ class ExchangeBundle:
|
||||
|
||||
chunks[asset] = []
|
||||
for index, dt in enumerate(dates):
|
||||
get_start_end = get_month_start_end \
|
||||
if data_frequency == 'minute' else get_year_start_end
|
||||
|
||||
period_start, period_end = get_start_end(
|
||||
dt=dt,
|
||||
@@ -543,8 +553,6 @@ class ExchangeBundle:
|
||||
asset=chunk['asset'],
|
||||
data_frequency=data_frequency,
|
||||
period=chunk['period'],
|
||||
start_dt=chunk['period_start'],
|
||||
end_dt=chunk['period_end'],
|
||||
writer=writer,
|
||||
empty_rows_behavior='strip',
|
||||
cleanup=True
|
||||
@@ -563,8 +571,6 @@ class ExchangeBundle:
|
||||
asset=chunk['asset'],
|
||||
data_frequency=data_frequency,
|
||||
period=chunk['period'],
|
||||
start_dt=chunk['period_start'],
|
||||
end_dt=chunk['period_end'],
|
||||
writer=writer,
|
||||
empty_rows_behavior='strip',
|
||||
cleanup=True
|
||||
|
||||
@@ -328,17 +328,20 @@ class DataPortalExchangeBacktest(DataPortalExchangeBase):
|
||||
"""
|
||||
bundle = self.exchange_bundles[exchange.name] # type: ExchangeBundle
|
||||
|
||||
freq, candle_size, unit, data_frequency = get_frequency(
|
||||
freq, candle_size, unit, adj_data_frequency = get_frequency(
|
||||
frequency, data_frequency
|
||||
)
|
||||
adj_bar_count = candle_size * bar_count
|
||||
|
||||
if data_frequency == 'minute' and adj_data_frequency == 'daily':
|
||||
end_dt = end_dt.floor('1D')
|
||||
|
||||
series = bundle.get_history_window_series_and_load(
|
||||
assets=assets,
|
||||
end_dt=end_dt,
|
||||
bar_count=adj_bar_count,
|
||||
field=field,
|
||||
data_frequency=data_frequency,
|
||||
data_frequency=adj_data_frequency,
|
||||
algo_end_dt=self._last_available_session,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user