Python backtrader.date2num() Examples
The following are 17
code examples of backtrader.date2num().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
backtrader
, or try the search function
.
Example #1
Source File: bokeh.py From backtrader_plotting with GNU General Public License v3.0 | 6 votes |
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
Example #2
Source File: alpacadata.py From alpaca-backtrader-api with Apache License 2.0 | 6 votes |
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
Example #3
Source File: alpacadata.py From alpaca-backtrader-api with Apache License 2.0 | 6 votes |
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
Example #4
Source File: jitter.py From sptrader with BSD 2-Clause "Simplified" License | 6 votes |
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
Example #5
Source File: mt5data.py From Backtrader-MQL5-API with GNU General Public License v3.0 | 6 votes |
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
Example #6
Source File: mt5data.py From Backtrader-MQL5-API with GNU General Public License v3.0 | 6 votes |
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
Example #7
Source File: oanda.py From backtrader with GNU General Public License v3.0 | 6 votes |
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
Example #8
Source File: ibdata.py From backtrader with GNU General Public License v3.0 | 6 votes |
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
Example #9
Source File: ibdata.py From backtrader with GNU General Public License v3.0 | 6 votes |
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
Example #10
Source File: ccxtfeed.py From bt-ccxt-store with MIT License | 6 votes |
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
Example #11
Source File: blaze.py From backtrader with GNU General Public License v3.0 | 5 votes |
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
Example #12
Source File: pandafeed.py From backtrader with GNU General Public License v3.0 | 5 votes |
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
Example #13
Source File: ccxtfeed.py From bt-ccxt-store with MIT License | 5 votes |
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
Example #14
Source File: oanda.py From backtrader with GNU General Public License v3.0 | 5 votes |
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
Example #15
Source File: oandav20feed.py From backtrader-oandav20 with Apache License 2.0 | 5 votes |
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
Example #16
Source File: oandav20feed.py From backtrader-oandav20 with Apache License 2.0 | 5 votes |
def _load_candle(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] = 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['bid']['o']) self.lines.high[0] = float(msg['bid']['h']) self.lines.low[0] = float(msg['bid']['l']) self.lines.close[0] = float(msg['bid']['c']) else: self.lines.open[0] = float(msg['ask']['o']) self.lines.high[0] = float(msg['ask']['h']) self.lines.low[0] = float(msg['ask']['l']) self.lines.close[0] = float(msg['ask']['c']) else: self.lines.open[0] = float(msg['mid']['o']) self.lines.high[0] = float(msg['mid']['h']) self.lines.low[0] = float(msg['mid']['l']) self.lines.close[0] = float(msg['mid']['c']) return True
Example #17
Source File: mt5store.py From Backtrader-MQL5-API with GNU General Public License v3.0 | 4 votes |
def write_csv(self, symbol: str, timeframe: str, compression: int = 1, fromdate: datetime = None, todate: datetime = None) -> None: """Request MT5 to write history data to CSV a file""" if fromdate is None: fromdate = float('-inf') else: fromdate = date2num(fromdate) if todate is None: todate = float('inf') else: todate = date2num(todate) date_begin = num2date( fromdate) if fromdate > float('-inf') else None date_end = num2date( todate) if todate < float('inf') else None begin = end = None if date_begin: begin = int((date_begin - self._DTEPOCH).total_seconds()) if date_end: end = int((date_end - self._DTEPOCH).total_seconds()) tf = self.get_granularity(timeframe, compression) if self.debug: print('Fetching: {}, Timeframe: {}, Fromdate: {}'.format( symbol, tf, date_begin)) ret_val = self.oapi.construct_and_send(action="HISTORY", actionType="WRITE", symbol=symbol, chartTF=tf, fromDate=begin, toDate=end) # TODO Error # Error handling if ret_val["error"]: if ret_val["description"] == "Wrong symbol dosn't exist": raise ServerConfigError("Symbol dosn't exist") self.put_notification(ret_val["description"]) else: print( f'Request to write CVS data for symbol {tf} and timeframe {tf} succeeded. Check MT5 EA logging for the exact output location ...') # TODO live updates # self.streaming_events() # while True: # try: # msg = self.q_livedata.get() # except queue.Empty: # return None # if msg['type'] == "FLUSH": # print(msg['data'], end="\r", flush=True) # else: # print(msg['data']) # if msg['status']=='DISCONNECTED': # return