本文整理汇总了Python中backtrader.date2num方法的典型用法代码示例。如果您正苦于以下问题:Python backtrader.date2num方法的具体用法?Python backtrader.date2num怎么用?Python backtrader.date2num使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类backtrader
的用法示例。
在下文中一共展示了backtrader.date2num方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _load_ohlcv
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_ohlcv(self):
try:
ohlcv = self._data.popleft()
except IndexError:
return None # no data in the queue
tstamp, open_, high, low, close, volume = ohlcv
dtime = datetime.utcfromtimestamp(tstamp // 1000)
self.lines.datetime[0] = bt.date2num(dtime)
self.lines.open[0] = open_
self.lines.high[0] = high
self.lines.low[0] = low
self.lines.close[0] = close
self.lines.volume[0] = volume
return True
示例2: _load_rtbar
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_rtbar(self, rtbar, hist=False):
# A complete 5 second bar made of real-time ticks is delivered and
# contains open/high/low/close/volume prices
# The historical data has the same data but with 'date' instead of
# 'time' for datetime
dt = date2num(rtbar.time if not hist else rtbar.date)
if dt < self.lines.datetime[-1] and not self.p.latethrough:
return False # cannot deliver earlier than already delivered
self.lines.datetime[0] = dt
# Put the tick into the bar
self.lines.open[0] = rtbar.open
self.lines.high[0] = rtbar.high
self.lines.low[0] = rtbar.low
self.lines.close[0] = rtbar.close
self.lines.volume[0] = rtbar.volume
self.lines.openinterest[0] = 0
return True
示例3: _load_rtvolume
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_rtvolume(self, rtvol):
# A single tick is delivered and is therefore used for the entire set
# of prices. Ideally the
# contains open/high/low/close/volume prices
# Datetime transformation
dt = date2num(rtvol.datetime)
if dt < self.lines.datetime[-1] and not self.p.latethrough:
return False # cannot deliver earlier than already delivered
self.lines.datetime[0] = dt
# Put the tick into the bar
tick = rtvol.price
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
self.lines.close[0] = tick
self.lines.volume[0] = rtvol.size
self.lines.openinterest[0] = 0
return True
示例4: _load_tick
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_tick(self, msg):
dtobj = datetime.utcfromtimestamp(int(msg['time']) / 10 ** 6)
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
tick = float(msg['ask']) if self.p.useask else float(msg['bid'])
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
self.lines.close[0] = tick
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
return True
示例5: _get_start_end
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _get_start_end(strategy, start, end):
st_dtime = strategy.lines.datetime.array
if start is None:
start = 0
if end is None:
end = len(st_dtime)
if isinstance(start, datetime.date):
start = bisect.bisect_left(st_dtime, bt.date2num(start))
if isinstance(end, datetime.date):
end = bisect.bisect_right(st_dtime, bt.date2num(end))
if end < 0:
end = len(st_dtime) + 1 + end
return start, end
示例6: _load_tick
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_tick(self, msg):
time_stamp, _bid, _ask = msg
# convert unix timestamp to float for millisecond resolution
d_time = datetime.utcfromtimestamp(
float(time_stamp) / 1000.0)
dt = date2num(d_time)
# time already seen
if dt <= self.lines.datetime[-1]:
return False
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
tick = float(_ask) if self.p.useask else float(_bid)
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
self.lines.close[0] = tick
return True
示例7: _load_candle
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_candle(self, ohlcv):
# TODO support bid/ask using spread
time_stamp, _open, _high, _low, _close, _volume = ohlcv
d_time = datetime.utcfromtimestamp(
time_stamp)
dt = date2num(d_time)
# time already seen
if dt <= self.lines.datetime[-1]:
return False
self.lines.datetime[0] = dt
self.lines.open[0] = _open
self.lines.high[0] = _high
self.lines.low[0] = _low
self.lines.close[0] = _close
self.lines.volume[0] = _volume
self.lines.openinterest[0] = 0.0
return True
示例8: __call__
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def __call__(self, data):
'''
Return Values:
- False: data stream was not touched
- True: data stream was manipulated (bar outside of session times and
- removed)
'''
datadt = data.datetime.datetime()
newdt = datetime(datadt.year,
datadt.month,
datadt.day,
datadt.hour,
datadt.minute,
0)
dseconds = (datadt - newdt).seconds
if dseconds <= self.p.jitter:
data.datetime[0] = backtrader.date2num(newdt)
return True
return False
示例9: _load_tick
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_tick(self, msg):
dtobj = datetime.utcfromtimestamp(int(msg['time']))
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
tick = float(
msg['askprice']) if self.p.useask else float(
msg['bidprice'])
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
self.lines.close[0] = tick
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
return True
示例10: _load_history
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_history(self, msg):
dtobj = msg['time'].to_pydatetime()
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = msg['volume']
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
self.lines.open[0] = msg['open']
self.lines.high[0] = msg['high']
self.lines.low[0] = msg['low']
self.lines.close[0] = msg['close']
return True
示例11: _load_ticks
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_ticks(self):
if self._last_id is None:
# first time get the latest trade only
trades = [self.store.fetch_trades(self.p.dataname)[-1]]
else:
trades = self.store.fetch_trades(self.p.dataname)
for trade in trades:
trade_id = trade['id']
if trade_id > self._last_id:
trade_time = datetime.strptime(trade['datetime'], '%Y-%m-%dT%H:%M:%S.%fZ')
self._data.append((trade_time, float(trade['price']), float(trade['amount'])))
self._last_id = trade_id
try:
trade = self._data.popleft()
except IndexError:
return None # no data in the queue
trade_time, price, size = trade
self.lines.datetime[0] = bt.date2num(trade_time)
self.lines.open[0] = price
self.lines.high[0] = price
self.lines.low[0] = price
self.lines.close[0] = price
self.lines.volume[0] = size
return True
示例12: _load_history
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_history(self, msg):
dtobj = datetime.utcfromtimestamp(int(msg['time']) / 10 ** 6)
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = float(msg['volume'])
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
if self.p.bidask:
if not self.p.useask:
self.lines.open[0] = float(msg['openBid'])
self.lines.high[0] = float(msg['highBid'])
self.lines.low[0] = float(msg['lowBid'])
self.lines.close[0] = float(msg['closeBid'])
else:
self.lines.open[0] = float(msg['openAsk'])
self.lines.high[0] = float(msg['highAsk'])
self.lines.low[0] = float(msg['lowAsk'])
self.lines.close[0] = float(msg['closeAsk'])
else:
self.lines.open[0] = float(msg['openMid'])
self.lines.high[0] = float(msg['highMid'])
self.lines.low[0] = float(msg['lowMid'])
self.lines.close[0] = float(msg['closeMid'])
return True
示例13: _load
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load(self):
try:
row = next(self._rows)
except StopIteration:
return False
# Set the standard datafields - except for datetime
for datafield in self.datafields[1:]:
# get the column index
colidx = getattr(self.params, datafield)
if colidx < 0:
# column not present -- skip
continue
# get the line to be set
line = getattr(self.lines, datafield)
line[0] = row[colidx]
# datetime - assumed blaze always serves a native datetime.datetime
colidx = getattr(self.params, self.datafields[0])
dt = row[colidx]
dtnum = date2num(dt)
# get the line to be set
line = getattr(self.lines, self.datafields[0])
line[0] = dtnum
# Done ... return
return True
示例14: _load
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load(self):
self._idx += 1
if self._idx >= len(self.p.dataname):
# exhausted all rows
return False
# Set the standard datafields
for datafield in self.getlinealiases():
if datafield == 'datetime':
continue
colindex = self._colmapping[datafield]
if colindex is None:
# datafield signaled as missing in the stream: skip it
continue
# get the line to be set
line = getattr(self.lines, datafield)
# indexing for pandas: 1st is colum, then row
line[0] = self.p.dataname.iloc[self._idx, colindex]
# datetime conversion
coldtime = self._colmapping['datetime']
if coldtime is None:
# standard index in the datetime
tstamp = self.p.dataname.index[self._idx]
else:
# it's in a different column ... use standard column index
tstamp = self.p.dataname.iloc[self._idx, coldtime]
# convert to float via datetime and store it
dt = tstamp.to_pydatetime()
dtnum = date2num(dt)
self.lines.datetime[0] = dtnum
# Done ... return
return True
示例15: _load_tick
# 需要导入模块: import backtrader [as 别名]
# 或者: from backtrader import date2num [as 别名]
def _load_tick(self, msg):
dtobj = datetime.utcfromtimestamp(float(msg['time']))
dt = date2num(dtobj)
if dt <= self.lines.datetime[-1]:
return False # time already seen
# Common fields
self.lines.datetime[0] = dt
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
# Put the prices into the bar
if self.p.bidask:
if self.p.useask:
tick = float(msg['asks'][0]['price'])
else:
tick = float(msg['bids'][0]['price'])
else:
# create mid price
tick = (
float(msg['bids'][0]['price'])
+ float(msg['asks'][0]['price'])) / 2
self.lines.open[0] = tick
self.lines.high[0] = tick
self.lines.low[0] = tick
self.lines.close[0] = tick
self.lines.volume[0] = 0.0
self.lines.openinterest[0] = 0.0
return True