import json from binance.client import Client from binance.exceptions import BinanceAPIException import config import requests from binance.enums import * class Exchange: def __init__(self): # Initialise a connection to the Binance client API self.client = Client(config.API_KEY, config.API_SECRET) # List of time intervals available for trading. self.intervals = None # all symbols available for trading. self.symbols = None # Any non-zero balance-info for all assets. self.balances = None # Request data from the exchange and set the above values. self.get_exchange_data() # Info on all futures symbols self.futures_info = self.client.futures_exchange_info() # Dictionary of places the exchange requires after the decimal for all symbols. self.symbols_n_precision = {} for item in self.futures_info['symbols']: self.symbols_n_precision[item['symbol']] = item['quantityPrecision'] def get_exchange_data(self): # Pull all balances from the exchange. account = self.client.futures_coin_account_balance() # Discard assets with zero balance. self.balances = [asset for asset in account if float(asset['balance']) > 0] # Pull all available symbols from client exchange_info = self.client.futures_exchange_info() self.symbols = exchange_info['symbols'] # Available intervals self.intervals = ( KLINE_INTERVAL_1MINUTE, KLINE_INTERVAL_3MINUTE, KLINE_INTERVAL_5MINUTE, KLINE_INTERVAL_15MINUTE, KLINE_INTERVAL_30MINUTE, KLINE_INTERVAL_1HOUR, KLINE_INTERVAL_2HOUR, KLINE_INTERVAL_4HOUR, KLINE_INTERVAL_6HOUR, KLINE_INTERVAL_8HOUR, KLINE_INTERVAL_12HOUR, KLINE_INTERVAL_1DAY, KLINE_INTERVAL_3DAY, KLINE_INTERVAL_1WEEK, KLINE_INTERVAL_1MONTH ) def get_precision(self, symbol): return self.symbols_n_precision[symbol] def get_min_qty(self, symbol): """ *TODO: what does this represent? :param symbol: The symbol of the trading pair. :return: The minimum quantity sold per trade. """ info = self.client.get_symbol_info(symbol=symbol) for f_index, item in enumerate(info['filters']): if item['filterType'] == 'LOT_SIZE': break return float(info['filters'][f_index]['minQty']) # 'minQty' def get_min_notional_qty(self, symbol): """ * Not Working: Can't buy this amount??? TODO: what does this represent? :param symbol: The symbol of the trading pair. :return: The minimum quantity sold per trade. """ info = self.client.get_symbol_info(symbol=symbol) for f_index, item in enumerate(info['filters']): if item['filterType'] == 'MIN_NOTIONAL': break return float(info['filters'][f_index]['minNotional']) # 'minNotional' @staticmethod def get_price(symbol): """ :param symbol: The symbol of the trading pair. :return: The current ticker price. """ # Todo: Make sure im getting the correct market price for futures. request = requests.get(f'https://api.binance.com/api/v3/ticker/price?symbol={symbol}') json_obj = json.loads(request.text) return float(json_obj['price'])