Hi everybody,
@Ed-Bartosh great work.
There seems to be a problem when fetching live data. The backfilling works correctly, but once the live data is used the feeds receives the data of the current minute (assuming we are trading minute data) that is not yet finished. Essentially the opening value is correct, but the rest (close high low vol) is not.
With the following 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
import ccxt
#pylint: disable=E1101,E1123
class TestStrategy(bt.Strategy):
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):
pass
def next(self):
print('------ next len %d - counter %d' % (len(self), self.counter))
self.counter += 1
print('*' * 5, 'NEXT:', bt.num2date(self.data0.datetime[0]),
self.data0._name, self.data0.open[0], self.data0.high[0],
self.data0.low[0], self.data0.close[0], self.data0.volume[0],
bt.TimeFrame.getname(self.data0._timeframe), len(self.data0))
if __name__ == '__main__':
cerebro = bt.Cerebro()
exchange = sys.argv[1] if len(sys.argv) > 1 else 'binance'
symbol = sys.argv[2] if len(sys.argv) > 2 else 'BTC/USDT'
hist_start_date = datetime.utcnow() - timedelta(minutes=3)
data = bt.feeds.CCXT(exchange=exchange,
symbol=symbol,
timeframe=bt.TimeFrame.Minutes,
fromdate=hist_start_date,
ohlcv_limit=999)
cerebro.adddata(data)
cerebro.addstrategy(TestStrategy)
cerebro.run()
I get the following output:
START
------ next len 1 - counter 0
***** NEXT: 2018-03-09 14:40:00 9090.0 9115.35 9080.0 9114.99 30.29132 Minute 1
------ next len 2 - counter 1
***** NEXT: 2018-03-09 14:41:00 9112.25 9148.0 9090.11 9102.02 73.48228 Minute 2
------ next len 3 - counter 2
***** NEXT: 2018-03-09 14:42:00 9109.99 9110.0 9097.0 9097.0 35.020203 Minute 3
------ next len 4 - counter 3
***** NEXT: 2018-03-09 14:43:00 9099.0 9100.0 9090.43 9090.43 2.0025 Minute 4
------ next len 5 - counter 4
***** NEXT: 2018-03-09 14:44:00 9099.0 9099.0 9095.0 9095.0 0.058638 Minute 5
------ next len 6 - counter 5
***** NEXT: 2018-03-09 14:45:00 9088.78 9088.78 9088.78 9088.78 0.135 Minute 6
------ next len 7 - counter 6
***** NEXT: 2018-03-09 14:46:00 9099.0 9100.9 9099.0 9100.9 0.0327 Minute 7
The first 2 minutes are correct, because they have completely finished. Afterwards the values are off because the minute data is obtained when the minute is still running. This is essentially some 1off error. If you want to check for yourself binance - BTC/USDT
I also checked for bitfinex
(python script.py bitfinex2 BTC/USD
). Same issue.
I don't know if this is related to the ccxt/backtrader branch or related to ccxt master. @Ed-Bartosh let me know if you want me to look into it.