def fetch_market(self, market_id): """ Fetch and cache it. @param market_id Is the IG epic name """ market_info = self._ig_service.fetch_market_by_epic(market_id) if not market_info: return instrument = market_info['instrument'] snapshot = market_info['snapshot'] dealing_rules = market_info['dealingRules'] market = Market(market_id, instrument['marketId']) # cannot interpret this value because IG want it as it is market.expiry = instrument['expiry'] # not perfect but IG does not provides this information if instrument["marketId"].endswith( instrument["currencies"][0]["name"]): base_symbol = instrument[ "marketId"][:-len(instrument["currencies"][0]["name"])] else: base_symbol = instrument["marketId"] market.base_exchange_rate = instrument['currencies'][0][ 'baseExchangeRate'] # "exchangeRate": 0.77 market.one_pip_means = float(instrument['onePipMeans'].split(' ')[0]) market.value_per_pip = float(instrument['valueOfOnePip']) market.contract_size = float(instrument['contractSize']) market.lot_size = float(instrument['lotSize']) # @todo how to determine base precision ? market.set_base(base_symbol, base_symbol) market.set_quote( instrument["currencies"][0]["name"], instrument["currencies"][0]['symbol'], -int(math.log10(market.one_pip_means))) # "USD", "$", precision if snapshot: market.is_open = snapshot["marketStatus"] == "TRADEABLE" market.bid = snapshot['bid'] market.ofr = snapshot['offer'] # "marginFactorUnit": "PERCENTAGE" not avalaible if market is down if instrument.get('marginFactor') and market.is_open: market.margin_factor = float(instrument['marginFactor']) margin_factor = instrument['marginFactor'] elif instrument.get('margin') and market.is_open: market.margin_factor = 0.1 / float(instrument['margin']) margin_factor = str(market.margin_factor) else: # we don't want this when market is down because it could overwrite the previous stored value margin_factor = None if instrument['unit'] == 'AMOUNT': market.unit_type = Market.UNIT_AMOUNT elif instrument['unit'] == 'CONTRACTS': market.unit_type = Market.UNIT_CONTRACTS elif instrument['unit'] == 'SHARES': market.unit_type = Market.UNIT_SHARES # BINARY OPT_* BUNGEE_* if instrument['type'] == 'CURRENCIES': market._market_type = Market.TYPE_CURRENCY elif instrument['type'] == 'INDICES': market._market_type = Market.TYPE_INDICE elif instrument['type'] == 'COMMODITIES': market._market_type = Market.TYPE_COMMODITY elif instrument['type'] == 'SHARES': market._market_type = Market.TYPE_STOCK elif instrument['type'] == 'RATES': market._market_type = Market.TYPE_RATE elif instrument['type'] == 'SECTORS': market._market_type = Market.TYPE_SECTOR market.trade = Market.TRADE_MARGIN market.contract_type = Market.CONTRACT_CFD # take minDealSize as tick size market.set_size_limits(dealing_rules["minDealSize"]["value"], 0.0, dealing_rules["minDealSize"]["value"]) # @todo there is some limits in contract size market.set_notional_limits(0.0, 0.0, 0.0) # @todo maybe decimal_place of onePipMeans for tick_size market.set_price_limits(0.0, 0.0, 0.0) # commission for stocks commission = "0.0" # @todo # store it self.lock() self._markets[market_id] = market self.unlock() # store market info Database.inst().store_market_info(( self.name, 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 instrument['lotSize'], instrument['contractSize'], str(market.base_exchange_rate), instrument['valueOfOnePip'], instrument['onePipMeans'].split(' ')[0], margin_factor, dealing_rules["minDealSize"]["value"], "0.0", dealing_rules["minDealSize"]["value"], # size limits "0.0", "0.0", "0.0", # notional limits "0.0", "0.0", "0.0", # price limits "0.0", "0.0", commission, commission) # fees )