@backtrader
Here's the output of remove fromdate script and output:
# !/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
from datetime import datetime, timedelta
import backtrader as bt
import ccxt
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))
self.log("data#0: " + str(self.datas[0].datetime[0]))
self.log("data#1: " + str(self.datas[1].datetime[0]))
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)
if __name__ == '__main__':
cerebro = bt.Cerebro()
#exchange = sys.argv[1] if len(sys.argv) > 1 else 'gdax'
exchange = sys.argv[1] if len(sys.argv) > 1 else 'gateio'
symbol = sys.argv[2] if len(sys.argv) > 2 else 'ETH/USDT'
hist_start_date = datetime.utcnow() - timedelta(minutes=10)
print('UTC NOW: ', datetime.utcnow())
print('hist_start_data: ', hist_start_date)
print('Using symbol: ', symbol)
# Create data feeds
data_1m = bt.feeds.CCXT(exchange=exchange, symbol=symbol, name="1m",
timeframe=bt.TimeFrame.Minutes, #fromdate=hist_start_date,
compression=1)
cerebro.adddata(data_1m)
cerebro.replaydata(data_1m, timeframe=bt.TimeFrame.Minutes, name="1m", compression = 5)
cerebro.addstrategy(TestStrategy)
cerebro.run()
Output:
UTC NOW: 2019-06-24 02:30:45.980828
hist_start_data: 2019-06-24 02:20:45.980597
Using symbol: ETH/USDT
START
...
prenext len 163 - counter 163
2019-06-23 12:33:00, data#0: 737233.5229166667
2019-06-23 12:33:00, data#1: 737233.5229166667
dts[]: [737233.5236111111, 737233.5236111111]
prenext len 164 - counter 164
2019-06-23 12:34:00, data#0: 737233.5236111111
2019-06-23 12:34:00, data#1: 737233.5236111111
dts[]: [737233.5243055555, 737233.5243055555]
prenext len 165 - counter 165
2019-06-23 12:35:00, data#0: 737233.5243055555
2019-06-23 12:35:00, data#1: 737233.5243055555
dts[]: [737233.525, 737233.525]
2019-06-23 12:36:00, Counter: 166 Open/Close/High/Low/Volume: 312.57 - 312.45 - 312.71 - 312.45 - 99.16966564 Len: 166
2019-06-23 12:36:00, MACD#1: MACD: 0.07, Histo: -0.12
2019-06-23 12:36:00, MACD#2: MACD: 1.19, Histo: -0.09
dts[]: [737233.5256944444, 737233.5256944444]
2019-06-23 12:37:00, Counter: 167 Open/Close/High/Low/Volume: 312.45 - 312.54 - 312.54 - 312.36 - 21.3511 Len: 167
2019-06-23 12:37:00, MACD#1: MACD: 0.04, Histo: -0.12
2019-06-23 12:37:00, MACD#2: MACD: 1.19, Histo: -0.08
dts[]: [737233.526388889, 737233.526388889]
2019-06-23 12:38:00, Counter: 168 Open/Close/High/Low/Volume: 312.45 - 312.2 - 312.45 - 312.2 - 18.69802458 Len: 168
2019-06-23 12:38:00, MACD#1: MACD: -0.01, Histo: -0.13
2019-06-23 12:38:00, MACD#2: MACD: 1.17, Histo: -0.10
dts[]: [737233.5270833333, 737233.5270833333]
2019-06-23 12:39:00, Counter: 169 Open/Close/High/Low/Volume: 312.34 - 312.14 - 312.34 - 311.43 - 122.3391867563 Len: 169
2019-06-23 12:39:00, MACD#1: MACD: -0.05, Histo: -0.14
2019-06-23 12:39:00, MACD#2: MACD: 1.16, Histo: -0.11
dts[]: [737233.5277777778, 737233.5277777778]
2019-06-23 12:40:00, Counter: 170 Open/Close/High/Low/Volume: 311.44 - 311.79 - 311.79 - 311.43 - 22.0295 Len: 170
2019-06-23 12:40:00, MACD#1: MACD: -0.11, Histo: -0.16
2019-06-23 12:40:00, MACD#2: MACD: 1.13, Histo: -0.13
dts[]: [737233.5284722223, 737233.5284722223]
...
You see the UTC NOW? I don't know why the MACDs' date is at around 2019-06-23 12:40:00.
And I'm confused that:
using fromdate will case error
without fromdate, the script is still getting old data, but without error
And I don't know why it's getting old data from that time
How can these two happen in the same script?
I'm just lost.