def fetch_market(self, market_id): """ Fetch and cache it. It rarely changes. """ instrument = self._instruments.get(market_id) market = None if instrument: market = Market(market_id, instrument['altname']) market.is_open = True market.expiry = '-' # "wsname":"XBT\/USD" # wsname = WebSocket pair name (if available) # pair_decimals = scaling decimal places for pair # lot_decimals = scaling decimal places for volume # lot_multiplier = amount to multiply lot volume by to get currency volume # "aclass_base":"currency" base_asset = instrument['base'] # XXBT market.set_base(base_asset, base_asset, instrument['pair_decimals']) # "aclass_quote":"currency" quote_asset = instrument['quote'] # ZUSD market.set_quote(quote_asset, quote_asset, instrument['lot_decimals']) # 8 # tick size at the base asset precision market.one_pip_means = math.pow(10.0, -instrument['pair_decimals']) # 1 market.value_per_pip = 1.0 market.contract_size = 1.0 market.lot_size = 1.0 # "lot":"unit", "lot_multiplier":1 # "margin_call":80, "margin_stop":40 # margin_call = margin call level # margin_stop = stop-out/liquidation margin level leverages = set(instrument.get('leverage_buy', [])) leverages.intersection(set(instrument.get('leverage_sell', []))) market.margin_factor = 1.0 / max(leverages) if len( leverages) > 0 else 1.0 market.set_leverages(leverages) size_limit = self._size_limits.get(instrument['altname'], {}) min_size = size_limit.get('min-size', 1.0) size_limits = [str(min_size), "0.0", str(min_size)] notional_limits = ["0.0", "0.0", "0.0"] price_limits = ["0.0", "0.0", "0.0"] market.set_size_limits(float(size_limits[0]), float(size_limits[1]), float(size_limits[2])) market.set_price_limits(float(price_limits[0]), float(price_limits[1]), float(price_limits[2])) market.set_notional_limits(float(notional_limits[0]), 0.0, 0.0) # "lot":"unit" market.unit_type = Market.UNIT_AMOUNT market.market_type = Market.TYPE_CRYPTO market.contract_type = Market.CONTRACT_SPOT market.trade = Market.TRADE_ASSET if leverages: market.trade |= Market.TRADE_MARGIN market.trade |= Market.TRADE_FIFO # orders capacities market.orders = Order.ORDER_LIMIT | Order.ORDER_MARKET | Order.ORDER_STOP | Order.ORDER_TAKE_PROFIT # @todo take the first but it might depends of the traded volume per 30 days, then request volume window to got it # "fees":[[0,0.26],[50000,0.24],[100000,0.22],[250000,0.2],[500000,0.18],[1000000,0.16],[2500000,0.14],[5000000,0.12],[10000000,0.1]], # "fees_maker":[[0,0.16],[50000,0.14],[100000,0.12],[250000,0.1],[500000,0.08],[1000000,0.06],[2500000,0.04],[5000000,0.02],[10000000,0]], fees = instrument.get('fees', []) fees_maker = instrument.get('fees_maker', []) if fees: market.taker_fee = round(fees[0][1] * 0.01, 6) if fees_maker: market.maker_fee = round(fees_maker[0][1] * 0.01, 6) if instrument.get('fee_volume_currency'): market.fee_currency = instrument['fee_volume_currency'] if quote_asset != self.BASE_QUOTE: # from XXBTZUSD / XXBTZEUR ... # @todo pass # if self._tickers_data.get(quote_asset+self.BASE_QUOTE): # market.base_exchange_rate = float(self._tickers_data.get(quote_asset+self.BASE_QUOTE, {'price', '1.0'})['price']) # elif self._tickers_data.get(self.BASE_QUOTE+quote_asset): # market.base_exchange_rate = 1.0 / float(self._tickers_data.get(self.BASE_QUOTE+quote_asset, {'price', '1.0'})['price']) # else: # market.base_exchange_rate = 1.0 else: market.base_exchange_rate = 1.0 # @todo contract_size # market.contract_size = 1.0 / mid_price # market.value_per_pip = market.contract_size / mid_price # volume 24h : not have here # notify for strategy self.service.notify(Signal.SIGNAL_MARKET_INFO_DATA, self.name, (market_id, market)) # store the last market info to be used for backtesting if not self._read_only: Database.inst().store_market_info(( self.name, market.market_id, market.symbol, market.market_type, market.unit_type, market.contract_type, # type market.trade, market.orders, # type market.base, market.base_display, market.base_precision, # base market.quote, market.quote_display, market.quote_precision, # quote market.expiry, int(market.last_update_time * 1000.0), # expiry, timestamp str(market.lot_size), str(market.contract_size), str(market.base_exchange_rate), str(market.value_per_pip), str(market.one_pip_means), '-', *size_limits, *notional_limits, *price_limits, str(market.maker_fee), str(market.taker_fee), str(market.maker_commission), str(market.taker_commission))) return market
def process_market(self): # # insert market info # with self._mutex: mki = self._pending_market_info_insert self._pending_market_info_insert = [] if mki: try: cursor = self._db.cursor() for mi in mki: if mi[16] is None: # margin factor is unavailable when market is down, so use previous value if available cursor.execute("""SELECT margin_factor FROM market WHERE broker_id = '%s' AND market_id = '%s'""" % (mi[0], mi[1])) row = cursor.fetchone() if row: # replace by previous margin factor from the DB margin_factor = row[0] mi = list(mi) mi[16] = margin_factor else: mi[16] = "1.0" cursor.execute("""INSERT INTO market(broker_id, market_id, symbol, market_type, unit_type, contract_type, trade_type, orders, base, base_display, base_precision, quote, quote_display, quote_precision, expiry, timestamp, lot_size, contract_size, base_exchange_rate, value_per_pip, one_pip_means, margin_factor, min_size, max_size, step_size, min_notional, max_notional, step_notional, min_price, max_price, step_price, maker_fee, taker_fee, maker_commission, taker_commission) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT (broker_id, market_id) DO UPDATE SET symbol = EXCLUDED.symbol, market_type = EXCLUDED.market_type, unit_type = EXCLUDED.unit_type, contract_type = EXCLUDED.contract_type, trade_type = EXCLUDED.trade_type, orders = EXCLUDED.orders, base = EXCLUDED.base, base_display = EXCLUDED.base_display, base_precision = EXCLUDED.base_precision, quote = EXCLUDED.quote, quote_display = EXCLUDED.quote_display, quote_precision = EXCLUDED.quote_precision, expiry = EXCLUDED.expiry, timestamp = EXCLUDED.timestamp, lot_size = EXCLUDED.lot_size, contract_size = EXCLUDED.contract_size, base_exchange_rate = EXCLUDED.base_exchange_rate, value_per_pip = EXCLUDED.value_per_pip, one_pip_means = EXCLUDED.one_pip_means, margin_factor = EXCLUDED.margin_factor, min_size = EXCLUDED.min_size, max_size = EXCLUDED.max_size, step_size = EXCLUDED.step_size, min_notional = EXCLUDED.min_notional, max_notional = EXCLUDED.max_notional, step_notional = EXCLUDED.step_notional, min_price = EXCLUDED.min_price, max_price = EXCLUDED.max_price, step_price = EXCLUDED.step_price, maker_fee = EXCLUDED.maker_fee, taker_fee = EXCLUDED.taker_fee, maker_commission = EXCLUDED.maker_commission, taker_commission = EXCLUDED.taker_commission""", (*mi,)) self._db.commit() except self.psycopg2.OperationalError as e: self.try_reconnect(e) # retry the next time with self._mutex: self._pending_market_info_insert = mki + self._pending_market_info_insert except Exception as e: self.on_error(e) # retry the next time with self._mutex: self._pending_market_info_insert = mki + self._pending_market_info_insert # # select market info # with self._mutex: mis = self._pending_market_info_select self._pending_market_info_select = [] if mis: try: cursor = self._db.cursor() for mi in mis: cursor.execute("""SELECT symbol, market_type, unit_type, contract_type, trade_type, orders, base, base_display, base_precision, quote, quote_display, quote_precision, expiry, timestamp, lot_size, contract_size, base_exchange_rate, value_per_pip, one_pip_means, margin_factor, min_size, max_size, step_size, min_notional, max_notional, step_notional, min_price, max_price, step_price, maker_fee, taker_fee, maker_commission, taker_commission FROM market WHERE broker_id = '%s' AND market_id = '%s'""" % ( mi[1], mi[2])) row = cursor.fetchone() if row: market_info = Market(mi[2], row[0]) market_info.is_open = True market_info.market_type = row[1] market_info.unit_type = row[2] market_info.contract_type = row[3] market_info.trade = row[4] market_info.orders = row[5] market_info.set_base(row[6], row[7], int(row[8])) market_info.set_quote(row[9], row[10], int(row[11])) market_info.expiry = row[12] market_info.last_update_time = row[13] * 0.001 market_info.lot_size = float(row[14]) market_info.contract_size = float(row[15]) market_info.base_exchange_rate = float(row[16]) market_info.value_per_pip = float(row[17]) market_info.one_pip_means = float(row[18]) if row[19] is not None or row[19] is not 'None': if row[19] == '-': # not defined mean 1.0 or no margin market_info.margin_factor = 1.0 else: market_info.margin_factor = float(row[19] or "1.0") market_info.set_size_limits(float(row[20]), float(row[21]), float(row[22])) market_info.set_notional_limits(float(row[23]), float(row[24]), float(row[25])) market_info.set_price_limits(float(row[26]), float(row[27]), float(row[28])) market_info.maker_fee = float(row[29]) market_info.taker_fee = float(row[30]) market_info.maker_commission = float(row[31]) market_info.taker_commission = float(row[32]) else: market_info = None # notify mi[0].notify(Signal.SIGNAL_MARKET_INFO_DATA, mi[1], (mi[2], market_info)) except self.psycopg2.OperationalError as e: self.try_reconnect(e) # retry the next time with self._mutex: self._pending_market_info_select = mis + self._pending_market_info_select except Exception as e: self.on_error(e) # retry the next time with self._mutex: self._pending_market_info_select = mis + self._pending_market_info_select # # select market list # with self._mutex: mls = self._pending_market_list_select self._pending_market_list_select = [] if mls: try: cursor = self._db.cursor() for m in mls: cursor.execute("""SELECT market_id, symbol, base, quote FROM market WHERE broker_id = '%s'""" % (m[1],)) rows = cursor.fetchall() market_list = [] for row in rows: market_list.append(row) # notify m[0].notify(Signal.SIGNAL_MARKET_LIST_DATA, m[1], market_list) except self.psycopg2.OperationalError as e: self.try_reconnect(e) # retry the next time with self._mutex: self._pending_market_list_select = mls + self._pending_market_list_select except Exception as e: self.on_error(e) # retry the next time with self._mutex: self._pending_market_list_select = mls + self._pending_market_list_select
def fetch_market(self, market_id): """ Fetch and cache it. It rarely changes. """ instrument = self._instruments.get(market_id) market = None if instrument: market = Market(market_id, instrument['altname']) market.is_open = True market.expiry = '-' # "wsname":"XBT\/USD" # wsname = WebSocket pair name (if available) # pair_decimals = scaling decimal places for pair # lot_decimals = scaling decimal places for volume # lot_multiplier = amount to multiply lot volume by to get currency volume # "aclass_base":"currency" base_asset = instrument['base'] # XXBT market.set_base(base_asset, base_asset, instrument['pair_decimals']) # "aclass_quote":"currency" quote_asset = instrument['quote'] # ZUSD market.set_quote(quote_asset, quote_asset, instrument['lot_decimals']) # 8 # tick size at the base asset precision market.one_pip_means = math.pow(10.0, -instrument['pair_decimals']) # 1 market.value_per_pip = 1.0 market.contract_size = 1.0 market.lot_size = 1.0 # "lot":"unit", "lot_multiplier":1 # "margin_call":80, "margin_stop":40 # margin_call = margin call level # margin_stop = stop-out/liquidation margin level leverages = set(instrument.get('leverage_buy', [])) leverages.intersection(set(instrument.get('leverage_sell', []))) market.margin_factor = 1.0 / max(leverages) market.set_leverages(leverages) size_limits = ["1.0", "0.0", "1.0"] notional_limits = ["1.0", "0.0", "0.0"] price_limits = ["0.0", "0.0", "0.0"] # size min/max/step @todo using lot_decimals / pair_decimals # for afilter in instrument["filters"]: # if afilter['filterType'] == "LOT_SIZE": # size_limits = [afilter['minQty'], afilter['maxQty'], afilter['stepSize']] # elif afilter['filterType'] == "MIN_NOTIONAL": # notional_limits[0] = afilter['minNotional'] # elif afilter['filterType'] == "PRICE_FILTER": # price_limits = [afilter['minPrice'], afilter['maxPrice'], afilter['tickSize']] # if float(size_limits[2]) < 1: # size_limits[2] = str(float(size_limits[2])) # * 10) market.set_size_limits(float(size_limits[0]), float(size_limits[1]), float(size_limits[2])) market.set_price_limits(float(price_limits[0]), float(price_limits[1]), float(price_limits[2])) market.set_notional_limits(float(notional_limits[0]), 0.0, 0.0) # "lot":"unit" market.unit_type = Market.UNIT_AMOUNT market.market_type = Market.TYPE_CRYPTO market.trade = Market.TRADE_ASSET market.contract_type = Market.CONTRACT_SPOT # @todo add a copy with market.trade = Market.TRADE_MARGIN market.contract_type = Market.CONTRACT_FUTUR # orders capacities market.orders = Order.ORDER_LIMIT | Order.ORDER_MARKET | Order.ORDER_STOP | Order.ORDER_TAKE_PROFIT # "fees":[[0,0.26],[50000,0.24],[100000,0.22],[250000,0.2],[500000,0.18],[1000000,0.16],[2500000,0.14],[5000000,0.12],[10000000,0.1]], # "fees_maker":[[0,0.16],[50000,0.14],[100000,0.12],[250000,0.1],[500000,0.08],[1000000,0.06],[2500000,0.04],[5000000,0.02],[10000000,0]], # market.maker_fee = account['makerCommission'] * 0.0001 # market.taker_fee = account['takerCommission'] * 0.0001 # 'fee_volume_currency': 'ZUSD' # fee_volume_currency = volume discount currency # if quote_asset != self.BASE_QUOTE: # if self._tickers_data.get(quote_asset+self.BASE_QUOTE): # market.base_exchange_rate = float(self._tickers_data.get(quote_asset+self.BASE_QUOTE, {'price', '1.0'})['price']) # elif self._tickers_data.get(self.BASE_QUOTE+quote_asset): # market.base_exchange_rate = 1.0 / float(self._tickers_data.get(self.BASE_QUOTE+quote_asset, {'price', '1.0'})['price']) # else: # market.base_exchange_rate = 1.0 # else: # market.base_exchange_rate = 1.0 # market.contract_size = 1.0 / mid_price # market.value_per_pip = market.contract_size / mid_price # volume 24h : not have here # store the last market info to be used for backtesting if not self._read_only: Database.inst().store_market_info(( self.name, market.market_id, market.symbol, market.market_type, market.unit_type, market.contract_type, # type market.trade, market.orders, # type market.base, market.base_display, market.base_precision, # base market.quote, market.quote_display, market.quote_precision, # quote market.expiry, int(market.last_update_time * 1000.0), # expiry, timestamp str(market.lot_size), str(market.contract_size), str(market.base_exchange_rate), str(market.value_per_pip), str(market.one_pip_means), '-', *size_limits, *notional_limits, *price_limits, str(market.maker_fee), str(market.taker_fee), str(market.maker_commission), str(market.taker_commission))) # notify for strategy self.service.notify(Signal.SIGNAL_MARKET_INFO_DATA, self.name, (market_id, market)) return market