def convert_ticker_from_db2tt(db_ticker): if '-' in db_ticker: spreadQ = True ticker_list = db_ticker.split('-') else: spreadQ = False ticker_list = [db_ticker] contract_specs_list = [cmi.get_contract_specs(x) for x in ticker_list] ticker_head_list = [x['ticker_head'] for x in contract_specs_list] exchange_traded = cmi.get_exchange_traded(ticker_head_list[0]) if exchange_traded == 'CME': exchange_string = 'CME' elif exchange_traded == 'ICE': exchange_string = 'ICE_IPE' tt_ticker_head = su.get_key_in_dictionary(dictionary_input=conversion_from_tt_ticker_head, value=ticker_head_list[0]) maturity_string_list = [dt.date(x['ticker_year'],x['ticker_month_num'],1).strftime('%b%y') for x in contract_specs_list] if spreadQ: if exchange_traded == 'ICE': tt_ticker = exchange_string + ' ' + tt_ticker_head + ' Spread ' + maturity_string_list[0] + '-' + maturity_string_list[1] elif exchange_traded == 'CME': tt_ticker = exchange_string + ' Calendar- 1x' + tt_ticker_head + ' ' + maturity_string_list[0] + '--1x' + maturity_string_list[1] else: tt_ticker = exchange_string + ' ' + tt_ticker_head + ' ' + maturity_string_list[0] return tt_ticker
def request_spread_market_data(self): spread_ticker_list = self.price_request_dictionary['spread'] self.nonfinished_bid_price_list.extend(spread_ticker_list) self.nonfinished_ask_price_list.extend(spread_ticker_list) self.nonfinished_bid_quantity_list.extend(spread_ticker_list) self.nonfinished_ask_quantity_list.extend(spread_ticker_list) for i in range(len(spread_ticker_list)): self.bid_price_dictionary[spread_ticker_list[i]] = np.nan self.ask_price_dictionary[spread_ticker_list[i]] = np.nan self.bid_quantity_dictionary[spread_ticker_list[i]] = np.nan self.ask_quantity_dictionary[spread_ticker_list[i]] = np.nan self.fair_price_dictionary[spread_ticker_list[i]] = np.nan split_out = spread_ticker_list[i].split("-") ticker1 = split_out[0] ticker2 = split_out[1] contract_specs_output = cmi.get_contract_specs(ticker1) ticker_head = contract_specs_output['ticker_head'] exchange = cmi.get_ib_exchange_name(ticker_head) ib_ticker_head = su.get_key_in_dictionary( dictionary_input=tfl.conversion_from_tt_ticker_head, value=contract_specs_output['ticker_head']) spread_contract = Contract() spread_contract.symbol = ib_ticker_head spread_contract.secType = "BAG" spread_contract.currency = "USD" spread_contract.exchange = exchange leg1 = ComboLeg() leg1.conId = self.contractIDDictionary[ticker1] leg1.ratio = 1 leg1.action = "BUY" leg1.exchange = exchange leg2 = ComboLeg() leg2.conId = self.contractIDDictionary[ticker2] leg2.ratio = 1 leg2.action = "SELL" leg2.exchange = exchange spread_contract.comboLegs = [] spread_contract.comboLegs.append(leg1) spread_contract.comboLegs.append(leg2) self.market_data_ReqId_dictionary[ self.next_val_id] = spread_ticker_list[i] self.log.info('req id: ' + str(self.next_val_id) + ', spread_ticker:' + str(spread_ticker_list[i])) self.reqMktData(self.next_valid_id(), spread_contract, "", False, False, []) self.spread_contract_dictionary[ spread_ticker_list[i]] = spread_contract
def get_db_ticker_from_ib_contract(**kwargs): ib_contract = kwargs['ib_contract'] if 'contract_id_dictionary' in kwargs.keys(): contract_id_dictionary = kwargs['contract_id_dictionary'] return su.get_key_in_dictionary(dictionary_input=contract_id_dictionary, value=ib_contract.conId) contract_output = {} contract_output['option_type'] = None contract_output['strike'] = None sec_type = ib_contract.secType if sec_type=='STK': contract_output['ticker'] = ib_contract.symbol contract_output['instrument'] = 'S' return contract_output ticker_head = conversion_from_ib_ticker_head[ib_contract.symbol] local_symbol_out = ib_contract.localSymbol.split(' ') date_now = cu.get_doubledate() if len(local_symbol_out) in [1,2]: contract_month_str = local_symbol_out[0][-2] if local_symbol_out[0][-1]=='0' and 0: contract_year_str = str(m.ceil(date_now / 10000)) else: contract_year_str = str(m.floor(date_now / 100000)) + local_symbol_out[0][-1] else: contract_month_str = cmi.full_letter_month_list[cu.three_letter_month_dictionary[local_symbol_out[3]]-1] contract_year_str = str(m.floor(date_now / 1000000)) + local_symbol_out[4] #contract_year_str = '2020' contract_output['ticker'] = ticker_head + contract_month_str + contract_year_str if sec_type=='FOP': contract_output['instrument'] = 'O' contract_output['option_type'] = ib_contract.right contract_output['strike'] = round(ib_contract.strike/ib_strike_multiplier_dictionary.get(ticker_head, 1),4) else: contract_output['instrument'] = 'F' return contract_output
def get_ib_contract_from_db_ticker(**kwargs): sec_type = kwargs['sec_type'] ticker = kwargs['ticker'] contract_out = Contract() contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] if sec_type in ['F', 'OF']: secType = "FUT" currency = 'USD' ib_ticker_head = su.get_key_in_dictionary(dictionary_input=conversion_from_ib_ticker_head, value=contract_specs_output['ticker_head']) ib_contract_month = str(contract_specs_output['ticker_year']*100 + contract_specs_output['ticker_month_num']) exchange = cmi.get_ib_exchange_name(contract_specs_output['ticker_head']) contract_out.secType = secType contract_out.symbol = ib_ticker_head contract_out.exchange = exchange contract_out.currency = currency contract_out.lastTradeDateOrContractMonth = ib_contract_month if sec_type=='OF': contract_out.secType = "FOP" if 'option_type' in kwargs.keys(): contract_out.right = kwargs['option_type'] if 'strike' in kwargs.keys(): contract_out.strike = str(round(kwargs['strike'],2)) contract_out.tradingClass = ib_option_trading_class_dictionary[ticker_head] contract_out.multiplier = ib_multiplier_dictionary.get(ticker_head, 1) if sec_type=='S': contract_out.secType = 'STK' contract_out.symbol = ticker contract_out.currency = 'USD' contract_out.exchange = smi.get_ib_exchange_name(ticker) return contract_out
def get_ttapi_filename(**kwargs): ticker = kwargs['ticker'] contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] exchange_traded = cmi.get_exchange_traded(ticker_head) ttapi_ticker_head = su.get_key_in_dictionary(dictionary_input=tfl.conversion_from_tt_ticker_head, value=ticker_head) if exchange_traded == 'CME': exchange_string = 'CME' elif exchange_traded == 'ICE': exchange_string = 'ICE_IPE' maturity_string = dt.date(contract_specs_output['ticker_year'],contract_specs_output['ticker_month_num'],1).strftime('%b%y') return exchange_string + ' ' + ttapi_ticker_head + ' ' + maturity_string + '.csv'
def prepare_orders(self): vcs_pairs = self.vcs_pairs for i in range(len(vcs_pairs.index)): if vcs_pairs.loc[i, 'validQ']: if (vcs_pairs.loc[i, 'QC'] <= 30) and (vcs_pairs.loc[i, 'Q'] <= 30): trade_decision = 'SELL' quantity = round(vcs_pairs.loc[i, 'long_quantity'] / 2) elif (vcs_pairs.loc[i, 'QC'] >= 70) and (vcs_pairs.loc[i, 'Q'] >= 70): trade_decision = 'BUY' quantity = round(vcs_pairs.loc[i, 'short_quantity'] / 2) else: continue exchange = cmi.get_ib_exchange_name( vcs_pairs.loc[i, 'tickerHead']) option_tick_size = cmi.option_tick_size[vcs_pairs.loc[ i, 'tickerHead']] ib_ticker_head = su.get_key_in_dictionary( dictionary_input=tfl.conversion_from_tt_ticker_head, value=vcs_pairs.loc[i, 'tickerHead']) ib_underlying_multiplier = ib_contract.ib_underlying_multiplier_dictionary.get( vcs_pairs.loc[i, 'tickerHead'], 1) spread_contract = Contract() spread_contract.symbol = ib_ticker_head spread_contract.secType = "BAG" spread_contract.currency = "USD" spread_contract.exchange = exchange spread_contract.comboLegs = [] action_dict = {1: 'SELL', 2: 'BUY'} for j in [1, 2]: call_option_ticker_string = vcs_pairs.loc[ i, 'ticker' + str(j)] + '_C_' + str( vcs_pairs.loc[i, 'current_strike' + str(j)]) put_option_ticker_string = vcs_pairs.loc[ i, 'ticker' + str(j)] + '_P_' + str( vcs_pairs.loc[i, 'current_strike' + str(j)]) leg1 = ComboLeg() leg1.conId = self.contractIDDictionary[ call_option_ticker_string] leg1.ratio = 1 leg1.action = action_dict[j] leg1.exchange = exchange leg2 = ComboLeg() leg2.conId = self.contractIDDictionary[ put_option_ticker_string] leg2.ratio = 1 leg2.action = action_dict[j] leg2.exchange = exchange spread_contract.comboLegs.append(leg1) spread_contract.comboLegs.append(leg2) mid_price_db_raw = vcs_pairs.loc[ i, 'call_mid_price2'] + vcs_pairs.loc[ i, 'put_mid_price2'] - vcs_pairs.loc[ i, 'call_mid_price1'] - vcs_pairs.loc[ i, 'put_mid_price1'] print('mid_price_db_raw: ' + str(mid_price_db_raw)) mid_price_db_raw_ticks = mid_price_db_raw / option_tick_size print('mid_price_db_raw_ticks: ' + str(mid_price_db_raw_ticks)) if trade_decision == 'BUY': order_price = ib_underlying_multiplier * m.floor( mid_price_db_raw_ticks) * option_tick_size elif trade_decision == 'SELL': order_price = ib_underlying_multiplier * m.ceil( mid_price_db_raw_ticks) * option_tick_size print(vcs_pairs.loc[i, 'ticker1'] + '_' + vcs_pairs.loc[i, 'ticker2'] + '_' + str(vcs_pairs.loc[i, 'current_strike1']) + '_' + str(vcs_pairs.loc[i, 'current_strike2'])) print(trade_decision + ', quantity: ' + str(quantity) + ', price: ' + str(order_price)) continue_q = 'n' continue_q = input('Continue? (y/n): ') if continue_q == 'y': self.order_alias_dictionary[ self.next_val_id] = vcs_pairs.loc[i, 'alias'] self.placeOrder( self.next_valid_id(), spread_contract, ib_api_trade.ComboLimitOrder(trade_decision, quantity, order_price, False))