can't resample data
See original GitHub issueHi there,
I’m trying to add 1 minute data to CCXTFeed, and then resample it to 5 minutes, so I can get MACD of 1m and 5m. But failed.
Here’s the code:
# !/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import sys
import time
from datetime import datetime, timedelta
import backtrader as bt
from ccxtbt import CCXTFeed
class TestStrategy(bt.Strategy):
params = (
('printlog', True),
)
def log(self, txt, dt=None, doprint=False):
''' Logging function fot this strategy'''
if self.params.printlog or doprint:
dt = dt or bt.num2date(self.data.datetime[0])
print('%s, %s' % (dt, txt))
def start(self):
self.counter = 0
print('START')
def prenext(self):
self.counter += 1
print('prenext len %d - counter %d' % (len(self), self.counter))
def __init__(self):
self.macd = bt.indicators.MACDHisto(self.datas[0])
self.macd2 = bt.indicators.MACDHisto(self.datas[1])
def next(self):
self.counter += 1
price_txt = "Counter: " + str(self.counter) + " Open/Close/High/Low/Volume: " + str(self.data0.open[0]) + " - "+ str(self.data0.close[0]) + " - " + str(self.data0.high[0]) + " - " + str(self.data0.low[0]) + " - " + str(self.data0.volume[0]) + " Len: "+ str(len(self.data0))# + " Time Frame:" + bt.TimeFrame.getname(self.data0._timeframe) + " Len: "+ str(len(self.data0))
self.log(price_txt)
macd_txt = "MACD: {:.2f}, Histo: {:.2f}".format(self.macd.macd[0],self.macd.histo[0])
self.log("MACD#1: " + macd_txt)
macd2_txt = "MACD: {:.2f}, Histo: {:.2f}".format(self.macd2.macd[0],self.macd2.histo[0])
self.log("MACD#2: " + macd2_txt)
def notify_data(self, data, status, *args, **kwargs):
dn = data._name
dt = datetime.now()
msg= 'Data Status: {}'.format(data._getstatusname(status))
print(dt,dn,msg)
if data._getstatusname(status) == 'LIVE':
self.live_data = True
else:
self.live_data = False
if __name__ == '__main__':
#cerebro = bt.Cerebro(quicknotify=True)
cerebro = bt.Cerebro()
#exchange = sys.argv[1] if len(sys.argv) > 1 else 'gdax'
exchange = sys.argv[1] if len(sys.argv) > 1 else 'binance'
symbol = sys.argv[2] if len(sys.argv) > 2 else 'ETH/USDT'
#store = CCXTStore(exchange=exchange, currency='LTC', config=config, retries=5, debug=False)
hist_start_date = datetime.utcnow() - timedelta(minutes=10)
print('UTC NOW: ', datetime.utcnow())
print('hist_start_data: ', hist_start_date)
print('Using symbol: ', symbol)
data = CCXTFeed(exchange=exchange,
dataname=symbol,
timeframe=bt.TimeFrame.Minutes,
fromdate=hist_start_date,
#todate=datetime(2019, 1, 1, 0, 2),
compression=1,
ohlcv_limit=2,
currency='USDT',
retries=5,
# 'apiKey' and 'secret' are skipped
config={'enableRateLimit': True, 'nonce': lambda: str(int(time.time() * 1000))})
cerebro.adddata(data)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
cerebro.addstrategy(TestStrategy)
# Run the strategy
cerebro.run()
And here’s the output:
python ccxtbttest.py
UTC NOW: 2019-06-21 07:19:54.687534
hist_start_data: 2019-06-21 07:09:54.687358
Using symbol: ETH/USDT
START
2019-06-21 15:20:01.656669 ETH/USDT Data Status: DELAYED
prenext len 1 - counter 1
prenext len 2 - counter 2
2019-06-21 15:20:03.363956 ETH/USDT Data Status: LIVE
prenext len 3 - counter 3
prenext len 4 - counter 4
prenext len 5 - counter 5
prenext len 6 - counter 6
prenext len 7 - counter 7
prenext len 8 - counter 8
prenext len 9 - counter 9
prenext len 10 - counter 10
prenext len 11 - counter 11
prenext len 12 - counter 12
Traceback (most recent call last):
File "ccxtbttest.py", line 90, in <module>
cerebro.run()
File "/Users/michael/.venv/bt-ccxt-store/lib/python3.6/site-packages/backtrader-1.9.74.123-py3.6.egg/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/Users/michael/.venv/bt-ccxt-store/lib/python3.6/site-packages/backtrader-1.9.74.123-py3.6.egg/backtrader/cerebro.py", line 1298, in runstrategies
self._runnext(runstrats)
File "/Users/michael/.venv/bt-ccxt-store/lib/python3.6/site-packages/backtrader-1.9.74.123-py3.6.egg/backtrader/cerebro.py", line 1557, in _runnext
dt0 = min((d for i, d in enumerate(dts)
ValueError: min() arg is an empty sequence
Any idea?
Thank you.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (1 by maintainers)
Top Results From Across the Web
python - Resample Pandas dataframe not valid - Stack Overflow
When I open the Dataframe and double-click on a date+time it mentions that I can't edit a Timestamp. When I try te resample...
Read more >Can't resample two images with different coordinates and ...
I am very new to image analysis and GIS. I tried to resample a tiff file based on a reference raster, the following...
Read more >Resampling problem - Bugs - Brainstorm
Hello, I have a problem when I try to resample my sEEG data. When I visualize my resampled data (4096 Hz), all the...
Read more >Snap do not perform resample - STEP Forum
I try to resample a sentinel 2a image in 10m resolution and I get the following error message java.lang.RuntimeException: Cannot construct ...
Read more >Time Series Data Analysis — Resample | by James Ho
In some cases, you got the data with a resolution higher than you need. Upsampling can help you sort out this issue. However,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No specific reason. Excessive caution.
After testing it seems that this bug only triggers when a bar which needs to be resampled is only a few seconds old. I haven’t been able to trigger it while walking through the code.
I came up with this ugly fix: Inside of ccxtfeed.py -> _load
Log snippets from some testing I did with 1 and 2 minute bars: