def fetch_ticker(self, asset): history = self.feed.history(t_minus=1) open_ = history.get('open', asset.symbol, self.ex_id, idx=-1) close = history.get('close', asset.symbol, self.ex_id, idx=-1) low = history.get('low', asset.symbol, self.ex_id, idx=-1) high = history.get('high', asset.symbol, self.ex_id, idx=-1) volume = history.get('volume', asset.symbol, self.ex_id, idx=-1) utc = history.get('utc') return { 'symbol': asset.symbol, 'info': {}, 'timestamp': utc_to_epoch(utc), 'datetime': utc, 'high': open_, 'low': low, 'bid': close, # faking with close 'ask': close, # faking with close 'vwap': None, #volumn weighted price 'open': open_, 'first': open_, 'last': close, 'change': None, #(percentage change), 'average': np.mean([open_, close]), #TODO: No idea what this means 'baseVolume': volume, #(volume of base currency), #TODO: should be based on weighted average price 'quoteVolume': volume * np.mean([open_, close]), }
def history(self, t_minus=0): """Return t_minus rows from feed Which should represent the latest dates seen by Strategy """ data = self.ohlcv_df[self.ohlcv_df.index <= utc_to_epoch( self.prior_time)] return OHLCVData(data[-t_minus:])
def add_ohlcv(self, data): data = data.ohlcv_df.copy() data['epoch'] = [utc_to_epoch(t) for t in data['utc']] data.set_index('epoch', inplace=True) if len(self.ohlcv) == 0: self.ohlcv = data else: self.ohlcv = self.ohlcv.append(data)
def fetch_ohlcv(self, asset, timeframe, start_utc): """ Returns OHLCV for the symbol based on the time_period ex. fetch_ohlcv(btcusd, 1d) To see all timeframes for an exchange use timeframes property when hasFetchTickers is True as well """ assert self.client.hasFetchOHLCV # CCXT expects milliseconds since epoch for 'since' epoch_ms = utc_to_epoch(start_utc) * 1000 return self.client.fetch_ohlcv(asset.symbol, timeframe.id, since=epoch_ms)
def next(self, refresh=False): if refresh: self.end = datetime.datetime.utcnow() self.update() row = self.ohlcv_df[self.ohlcv_df.index > utc_to_epoch( self.prior_time)] if len(row) > 0: row = row.iloc[0] self.prior_time = row['utc'] return OHLCVData(row.to_frame().T) print("No data after prior poll:", self.prior_time) return None
def peek(self): row = self.ohlcv_df[self.ohlcv_df.index > utc_to_epoch( self.prior_time)] if len(row) > 0: return OHLCVData(row.iloc[0].to_frame().T) return None