def _process_ticker(self, code): try: now = self.__context.get_now() response = self.__context.requests_get(self.__endpoint + '/api/1/ticker/' + code) if response is None: return ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = now ticker.tk_ask = response.get('ask') ticker.tk_bid = response.get('bid') ticker.tk_ltp = response.get('last') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', code, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', code, type(e), e.args)
def _process_ticker(self, symbol): try: now = self.__context.get_now() value = self.__context.requests_get( self.__endpoint + '/market/detail/merged?symbol=' + symbol) if value is None or "tick" not in value: return ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = symbol ticker.tk_time = self.__context.parse_iso_timestamp( value.get('ts') / 1000.0) ticker.tk_ask = value.get('tick').get('ask', [None])[0] ticker.tk_bid = value.get('tick').get('bid', [None])[0] ticker.tk_ltp = value.get('tick').get('close') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', symbol, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', symbol, type(e), e.args)
def _process_ticker(self, code): try: response = self.__context.requests_get(self.__endpoint + '/v1/pubticker/' + code) if response is None: return ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = self.__context.parse_iso_timestamp( response.get('timestamp')) ticker.tk_ask = response.get('ask') ticker.tk_bid = response.get('bid') ticker.tk_ltp = response.get('last_price') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', code, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', code, type(e), e.args)
def _process_ticker(self, now, code, products): try: for product in products if products is not None else []: if code != product.get('currency_pair_code'): continue ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = now ticker.tk_ask = self._decimalize(product, 'market_ask') ticker.tk_bid = self._decimalize(product, 'market_bid') ticker.tk_ltp = self._decimalize(product, 'last_traded_price') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', code, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', code, type(e), e.args)
def _process_ticker(self, pair): try: now = self.__context.get_now() response = self.__context.requests_get( 'https://public.bitbank.cc/%s/ticker' % pair) if response is None: return if response.get('success', 1) != 1: raise Exception(str(response)) data = response.get('data', {}) ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = pair ticker.tk_time = now ticker.tk_ask = data.get('sell') ticker.tk_bid = data.get('buy') ticker.tk_ltp = data.get('last') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', pair, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', pair, type(e), e.args)
def _process_ticker(self, coin): try: now = self.__context.get_now() value = self.__context.requests_get(self.__endpoint + '/api/v1/ticker?coin=' + coin) if value is None: return ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = coin ticker.tk_time = now ticker.tk_ask = value.get('sell') ticker.tk_bid = value.get('buy') ticker.tk_ltp = value.get('last') self.__context.save_tickers([ticker]) self.__logger.debug('Ticker : %s - %s', coin, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', coin, type(e), e.args)
def _process_ticker(self): try: now = self.__context.get_now() response = self.__context.requests_get( self.__endpoint + '/api/v1/instrument/activeAndIndices') if response is None: return values = [] codes = self.__context.get_property(self._ID, 'codes', '.BXBT').split(',') references = self.__context.get_property( self._ID, 'references', '.BXBT,.BXBT30M,.ETHXBT30M,.BCHXBT30M').split(',') for instrument in response: code = instrument.get('symbol') reference = instrument.get('referenceSymbol') state = instrument.get('state') multiplier = instrument.get('multiplier') if (code is None or code not in codes) and ( reference is None or reference not in references or state == 'Unlisted'): continue # Cache ticker symbols for transaction query. if state != 'Unlisted': self.__code_cache[code] = (now, multiplier) ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = self.__context.parse_iso_timestamp( instrument.get('timestamp')) ticker.tk_ask = instrument.get('askPrice') ticker.tk_bid = instrument.get('bidPrice') ticker.tk_ltp = instrument.get('lastPrice') values.append(ticker) self.__context.save_tickers(values) for v in values: self.__logger.debug('Ticker : %s', v) except Exception as e: self.__logger.warn('Ticker Failure : %s - %s', type(e), e.args)
def test_save_tickers(self): self.target._create_all() dt = datetime.now() t1 = Ticker() t1.tk_site = 'ts' t1.tk_code = 'tp' t1.tk_time = dt + timedelta(minutes=1) t1.tk_ltp = Decimal('1.2') t2 = Ticker() t2.tk_site = 'ts' t2.tk_code = 'tp' t2.tk_time = dt + timedelta(minutes=2) t2.tk_ltp = Decimal('2.3') t3 = copy(t1) t3.tk_ltp = Decimal('3.4') t4 = copy(t2) t4.tk_ltp = Decimal('4.5') t5 = copy(t1) t5.tk_code = None # All new records results = self.target.save_tickers([t1, t2]) self.assertEqual(len(results), 2) self.assertTrue(t1 in results) self.assertTrue(t2 in results) # Existing records results = self.target.save_tickers([t3, None, t4]) self.assertEqual(len(results), 2) self.assertTrue(t3 in results) self.assertTrue(t4 in results) # PK Failure with self.assertRaises(BaseException): self.target.save_tickers([t5]) # Read-only self.target._is_read_only = lambda: True results = self.target.save_tickers([t1]) self.assertEqual(len(results), 0)
def test__calculate_prices(self): t1 = Ticker() t1.tk_site = 's1' t1.tk_code = 'c1' t1.tk_ask = Decimal('1.5') t1.tk_bid = Decimal('1.1') t1.tk_ltp = Decimal('1.2') t2 = Ticker() t2.tk_site = 's1' t2.tk_code = 'c2' t2.tk_ask = None t2.tk_bid = Decimal('1.1') t2.tk_ltp = Decimal('1.2') t3 = Ticker() t3.tk_site = 's2' t3.tk_code = 'c1' t3.tk_ask = Decimal('1.5') t3.tk_bid = Decimal(0) t3.tk_ltp = Decimal('1.2') t4 = Ticker() t4.tk_site = 's3' t4.tk_code = 'c3' t4.tk_ask = Decimal('0.0') t4.tk_bid = None t4.tk_ltp = Decimal('1.2') t5 = Ticker() t5.tk_site = 's4' t5.tk_code = 'c4' t5.tk_ask = None t5.tk_bid = None t5.tk_ltp = Decimal('0.0') dto = namedtuple('TickerDto', ('ticker', )) prices = self.target._calculate_prices( (dto(t1), dto(t2), dto(t3), dto(t4), dto(t5))) self.assertEqual(Decimal('1.3'), prices['s1']['c1']) self.assertEqual(Decimal('1.15'), prices['s1']['c2']) self.assertEqual(Decimal('1.35'), prices['s2']['c1']) self.assertEqual(Decimal('1.2'), prices['s3']['c3']) self.assertIsNone(prices['s4']['c4'])
def test_Ticker(self): value = Ticker() self.assertEqual("{'table': 't_ticker', 'site': 'None', 'code': 'None', 'time': 'None', " "'ask': 'None', 'bid': 'None', 'ltp': 'None'}", str(value)) value.tk_site = 'foo' value.tk_code = 'bar' value.tk_time = datetime.fromtimestamp(1234567890.123456, tz=utc) value.tk_ask = Decimal('1.2') value.tk_bid = Decimal('2.3') value.tk_ltp = Decimal('3.4') self.assertEqual("{'table': 't_ticker', 'site': 'foo', 'code': 'bar', " "'time': '2009-02-13 23:31:30.123456 UTC', " "'ask': '1.2', 'bid': '2.3', 'ltp': '3.4'}", str(value))
def _process_ticker(self): try: now = self.__context.get_now() trades = self.__context.requests_get(self.__endpoint + '/api/v3/ticker/price') quotes = self.__context.requests_get(self.__endpoint + '/api/v3/ticker/bookTicker') if trades is None and quotes is None: return values = [] codes = self.__context.get_property( self._ID, 'codes', 'BTCUSDT,ETHBTC,BCCBTC').split(',') for code in codes: ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = now for trade in trades if trades is not None else []: if code == trade.get('symbol'): ticker.tk_ltp = trade.get('price') break for quote in quotes if quotes is not None else []: if code == quote.get('symbol'): ticker.tk_ask = quote.get('askPrice') ticker.tk_bid = quote.get('bidPrice') break values.append(ticker) self.__context.save_tickers(values) for v in values: self.__logger.debug('Ticker : %s', v) except Exception as e: self.__logger.warn('Ticker Failure : %s - %s', type(e), e.args)
def _process_ticker(self): try: now = self.__context.get_now() response = self.__context.requests_get( self.__endpoint + '/public?command=returnTicker') if response is None: return values = [] codes = self.__context.get_property( self._ID, 'codes', 'USDT_BTC,BTC_BCH,BTC_ETH').split(',') for code in codes: value = response.get(code) if value is None: continue ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = now ticker.tk_ask = value.get('lowestAsk') ticker.tk_bid = value.get('highestBid') ticker.tk_ltp = value.get('last') values.append(ticker) self.__context.save_tickers(values) for v in values: self.__logger.debug('Ticker : %s', v) except Exception as e: self.__logger.warn('Ticker Failure : %s - %s', type(e), e.args)
def _process_ticker(self): try: ticks = self.__context.requests_get(self.__endpoint + '/api/v1/market/allTickers') if ticks is None or ticks.get('code') != '200000': raise Exception(str(ticks)) codes = self.__context.get_property(self._ID, 'codes', 'BTC-USDT,ETH-BTC').split(',') data = ticks.get('data', {}) time = self.__context.parse_iso_timestamp(data.get('time') / 1000.0) values = [] for tick in data.get('ticker', []): if tick.get('symbol') not in codes: continue ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = tick.get('symbol') ticker.tk_time = time ticker.tk_ask = tick.get('sell') ticker.tk_bid = tick.get('buy') ticker.tk_ltp = tick.get('last') values.append(ticker) self.__context.save_tickers(values) for v in values: self.__logger.debug('Ticker : %s', v) except Exception as e: self.__logger.warn('Ticker Failure : %s - %s', type(e), e.args)
def _process_ticker(self, code): try: with self.__lock: now = self.__context.get_nonce(self._ID, delta=timedelta(seconds=2)) response = self.__context.requests_get( self.__endpoint + '/public/v1/ticker?symbol=' + code) if response is None or response.get( 'status', None) != 0 or not isinstance( response.get('data'), list): raise Exception(str(response)) values = [] for data in response.get('data'): if data.get('symbol') != code: continue ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code ticker.tk_time = now ticker.tk_ask = data.get('ask') ticker.tk_bid = data.get('bid') ticker.tk_ltp = data.get('last') values.append(ticker) self.__logger.debug('Ticker : %s - %s', code, ticker) self.__context.save_tickers(values) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', code, type(e), e.args)
def _process_ticker(self): try: token = self.__context.get_property(self._ID, 'token', None) if token is None: return pairs = self.__context.get_property(self._ID, 'pairs', 'USD_JPY') response = self.__context.requests_get( self.__endpoint + '/v1/prices?instruments=' + pairs, headers={"Authorization": "Bearer " + token} ) values = [] for price in response.get('prices', {}) if response is not None else []: ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = price.get('instrument') ticker.tk_time = self.__context.parse_iso_timestamp(price.get('time')) ticker.tk_ask = price.get('ask') ticker.tk_bid = price.get('bid') ticker.tk_ltp = None values.append(ticker) self.__context.save_tickers(values) for v in values: self.__logger.debug('Ticker : %s', v) except Exception as e: self.__logger.warn('Ticker Failure : %s - %s', type(e), e.args)
def _process_ticker(self, code): try: special_quotation = self._fetch_special_quotation(code) ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = code if special_quotation is not None: ticker.tk_time = self._parse_expiry(code) ticker.tk_ask = None ticker.tk_bid = None ticker.tk_ltp = special_quotation else: json = self.__context.requests_get(self.__endpoint + '/v1/ticker?product_code=' + code) if json is None: return ticker.tk_time = self.__context.parse_iso_timestamp(json.get('timestamp')) ticker.tk_ask = json.get('best_ask') ticker.tk_bid = json.get('best_bid') ticker.tk_ltp = json.get('ltp') self.__context.save_tickers([ticker] if ticker.tk_time is not None else []) self.__logger.debug('Ticker : %s - %s', code, ticker) except Exception as e: self.__logger.warn('Ticker Failure - %s : %s - %s', code, type(e), e.args)
def _process_coin(self, token, *, retry=True): if token is None: return try: params = { self._TOKEN: token } headers = { 'Content-Type': 'application/json' } path = self.__endpoint + '/bpj-api/vc_balance_list' json = { "calcCurrencyCd": self.__context.get_property(self._ID, 'calc', 'JPY'), "currencyCdList": self.__context.get_property(self._ID, 'coins', 'BTC').split(',') } now = self.__context.get_now() response = self.__context.requests_post(path, params=params, headers=headers, json=json) if response is None: return if response.get('resultCode', self._FAILURE) != self._SUCCESS: raise Exception(response) balances = [] tickers = [] for entry in response.get('vcBalanceList', []): currency1 = entry.get('currencyCd1') currency2 = entry.get('currencyCd2') try: unit1 = UnitType[currency1.upper()] except KeyError: continue try: unit2 = UnitType[currency2.upper()] except KeyError: continue balance = Balance() balance.bc_site = self._ID balance.bc_acct = AccountType.CASH balance.bc_unit = unit1 balance.bc_time = now balance.bc_amnt = entry.get('nominal') balances.append(balance) ticker = Ticker() ticker.tk_site = self._ID ticker.tk_code = unit1.name + '_' + unit2.name ticker.tk_time = now ticker.tk_ltp = entry.get('valuationPrice') tickers.append(ticker) self.__context.save_balances(balances) for balance in balances: self.__logger.debug('Coin : %s', balance) self.__context.save_tickers(tickers) for ticker in tickers: self.__logger.debug('Tick : %s', ticker) except Exception as e: if retry: token = self._fetch_token(force=True) self._process_coin(token, retry=False) else: self.__logger.warn('Coin Failure : %s - %s', type(e), e.args)