Example #1
0
    def reqMktData(self, contract):

        #logging.info('SubscriptionManager: reqMktData')

        def add_subscription(contract):
            self.handle.append(contract)
            newId = len(self.handle) - 1
            self.tickerId[ContractHelper.makeRedisKeyEx(contract)] = newId

            return newId

        id = self.is_subscribed(contract)
        if id == -1:  # not found
            id = add_subscription(contract)

            #
            # the conId must be set to zero when calling TWS reqMktData
            # otherwise TWS will fail to subscribe the contract
            self.parent.connection.reqMktData(id, contract, '', False)

            if self.persist_f:
                logging.debug(
                    'SubscriptionManager reqMktData: trigger callback')
                self.persist_f(self.handle)

            logging.info(
                'SubscriptionManager: reqMktData. Requesting market data, id = %d, contract = %s'
                % (id, ContractHelper.makeRedisKeyEx(contract)))

        else:
            self.parent.connection.reqMktData(1000 + id, contract, '', True)
            logging.info(
                'SubscriptionManager: reqMktData: contract already subscribed. Request snapshot = %d, contract = %s'
                % (id, ContractHelper.makeRedisKeyEx(contract)))
Example #2
0
    def tickSize(self, items):

        try:
            contract = self.tickerMap[items.__dict__['tickerId']]['contract']
            tick2oc_slot = self.tickerMap[
                items.__dict__['tickerId']]['tick2oc_slot']
            field = items.__dict__['field']

            logging.debug('tickSize>> %s' % ('[%d:%s:%s:%d] %s=%d %0.2f [%s]' % \
                                        (items.__dict__['tickerId'], ContractHelper.makeRedisKeyEx(contract),\
                                         tick2oc_slot[0], tick2oc_slot[1],\
                                        'bid_q' if field == 0 else ('ask_q' if field == 3 else ('last_q' if field == 5 else field)), \
                                        items.__dict__['size'], self.option_chains[tick2oc_slot[0]].multiplier,\
                                        datetime.datetime.fromtimestamp(items.__dict__['ts']).strftime('%Y-%m-%d %H:%M:%S.%f')))
                          )
            # is an option
            if tick2oc_slot[1] <> -999:
                o = self.get_option_in_chain(tick2oc_slot[0], tick2oc_slot[1])
                o.set_tick_value(field, items.__dict__['size'])

            # is an underylying
            else:

                self.get_underlying_in_chain(tick2oc_slot[0]).set_tick_value(
                    field, items.__dict__['size'])
                #print 'set fut price %s %d:%0.2f' % (ContractHelper.makeRedisKeyEx(self.get_underlying_in_chain(tick2oc_slot[0]).get_contract()), field, items.__dict__['price'])

        except KeyError:
            logging.error(
                'tickSize: keyerror: (this could happen on the 1st run as the subscription manager sub list is still empty.'
            )
            logging.error(''.join('%s=%s, ' % (k, v)
                                  for k, v in items.__dict__.iteritems()))
Example #3
0
    def broadcast_analytics(self, chain_id, option):

        o_key = ContractHelper.makeRedisKeyEx(option.get_contract())
        self.rs.sadd('%s%s' % (self.rs_oc_prefix, chain_id), o_key)

        msg_str = option.object2kvstring()
        self.rs.set(o_key, msg_str)

        self.get_producer().send_messages('optionAnalytics', msg_str)
Example #4
0
    def map_gw_ticker_to_option_chains(self):

        for k, option_chain in self.option_chains.iteritems():
            for option in option_chain.get_option_chain():
                self.get_command_handler().reqMktData(option.get_contract())
            self.get_command_handler().reqMktData(
                option_chain.get_underlying().get_contract())

        self.get_command_handler().gw_req_subscriptions()
        while not self.download_gw_map_done:
            sleep(1)
            logging.info(
                'map_gw_ticker_to_option_chains: awaiting subscription table down to get done...'
            )
            pass

        logging.info(
            'map_gw_ticker_to_option_chains: complete download subscription table from gw'
        )

        # for each tick id in the tickerMap, compare...
        for tickerId, cv in self.tickerMap.iteritems():

            # for each chain in the option chain, iterate...
            for chain_id, chain in self.option_chains.iteritems():

                # for each of the item (Option object) within the option chain...
                for i in range(len(chain.get_option_chain())):

                    # if a match is found...
                    if cv['contract'] == chain.get_option_chain(
                    )[i].get_contract():
                        print 'id:%s col:%d -> tickerId: %d >> %s %s' % (chain_id, i, tickerId, ContractHelper.makeRedisKeyEx(cv['contract']),\
                                                                          ContractHelper.makeRedisKeyEx(chain.get_option_chain()[i].get_contract()))

                        # update the ticker map
                        # key= tws ticker id
                        # value-> key: tick2oc_slot (ticker map to option chain slot) => [chain_id, the ith element]
                        cv['tick2oc_slot'] = [chain_id, i]

                if chain.get_underlying().get_contract() == cv['contract']:
                    print 'id:%s col:%d -> tickerId: %d >> %s %s' % (chain_id, i, tickerId, ContractHelper.makeRedisKeyEx(cv['contract']),\
                                                                      ContractHelper.makeRedisKeyEx(chain.get_underlying().get_contract()))
                    cv['tick2oc_slot'] = [chain_id, -999]
Example #5
0
 def is_subscribed(self, contract):
     #print self.conId.keys()
     ckey = ContractHelper.makeRedisKeyEx(contract)
     if ckey not in self.tickerId.keys():
         return -1
     else:
         # note that 0 can be a key
         # be careful when checking the return values
         # check for true false instead of using implicit comparsion
         return self.tickerId[ckey]
Example #6
0
 def tickPrice(self, items):
     try:
         contract = self.tickerMap[items.__dict__['tickerId']]
         field = items.__dict__['field']
         ct = ContractHelper.kv2object(contract, Contract)
         print 'tickPrice>> %s' % ('[%d:%s] %s=%0.4f [%s]' % \
                                     (items.__dict__['tickerId'], ContractHelper.makeRedisKeyEx(ct),\
                                     'bid_q' if field == 1 else ('ask_q' if field == 2 else ('last_q' if field == 4 else field)), \
                                     items.__dict__['price'], datetime.datetime.fromtimestamp(items.__dict__['ts']).strftime('%Y-%m-%d %H:%M:%S.%f')))
     except KeyError:
         print 'tickPrice: keyerror:'
         print items
Example #7
0
 def tickSize(self, items):
     try:
         contract = self.tickerMap[items.__dict__['tickerId']]
         field = items.__dict__['field']
         ct = ContractHelper.kv2object(contract, Contract)
         print 'tickSize>> %s' % ('[%d:%s] %s=%0.4f [%s]' % \
                                     (items.__dict__['tickerId'], ContractHelper.makeRedisKeyEx(ct),\
                                     'bid' if field == 0 else ('ask' if field == 3 else ('last' if field == 5 else field)), \
                                     items.__dict__['size'], datetime.datetime.fromtimestamp(items.__dict__['ts']).strftime('%Y-%m-%d %H:%M:%S.%f')))
     except KeyError:
         print 'tickSize: keyerror: (this could happen on the 1st run as the subscription manager sub list is still empty.'
         print items
Example #8
0
    def gw_subscriptions(self, items):
        # <class 'comms.tws_protocol_helper.Message'>
        # sample
        #{0: {'contract': <ib.ext.Contract.Contract object at 0x7ff8f8c9e210>}, 1: {'contract': <ib.ext.Contract.Contract object at 0x7ff8f8c9e250>},... }
        #print items.__dict__['subscriptions']

        l = map(
            lambda x: {x[0]: {
                           'contract': x[1]
                       }},
            map(
                lambda x:
                (x[0], ContractHelper.kvstring2object(x[1], Contract)),
                items.__dict__['subscriptions']))
        #l = map(lambda x: {x[0]: x[1]}, map(lambda x: (x[0], json.loads(x[1])), items.__dict__['subscriptions']))
        for i in l:
            self.tickerMap.update(i)
        logging.info('gw_subscriptions -> dump tickerMap ')
        logging.info(''.join('%s=%s,' %
                             (k, ContractHelper.makeRedisKeyEx(v['contract']))
                             for k, v in self.tickerMap.iteritems()))

        self.download_gw_map_done = True
Example #9
0
    def tickPrice(self, items):
        try:
            contract = self.tickerMap[items.__dict__['tickerId']]['contract']
            field = items.__dict__['field']
            tick2oc_slot = self.tickerMap[
                items.__dict__['tickerId']]['tick2oc_slot']
            today = time.strftime('%Y%m%d')
            price = items.__dict__['price']

            #
            # perform some sanity check
            #
            # if field is not bid, ask, last, or close, pass
            if field not in [1, 2, 4, 9]:
                logging.debug('tickPrice: discard unwanted msg field:%d' %
                              field)
                return

            # if we received a negative price, pass
            if price == -1:
                logging.debug('tickPrice: discard unwanted msg price==-1')
                return

            logging.debug( 'tickPrice>> %s' % ('[%d:%s:%s:%d] %s=%0.4f [%s]' % \
                                        (items.__dict__['tickerId'], ContractHelper.makeRedisKeyEx(contract),\
                                         tick2oc_slot[0], tick2oc_slot[1],\
                                        'bid' if field == 1 else ('ask' if field == 2 else ('last' if field == 4 else field)), \
                                        items.__dict__['price'], datetime.datetime.fromtimestamp(items.__dict__['ts']).strftime('%Y-%m-%d %H:%M:%S.%f')))
                          )

            # is an option
            if tick2oc_slot[1] <> -999:
                o = self.get_option_in_chain(tick2oc_slot[0], tick2oc_slot[1])
                o.set_tick_value(field, items.__dict__['price'])

                try:
                    spot = self.get_underlying_in_chain(
                        tick2oc_slot[0]).get_tick_value(4)

                    # the underlying price may not be available when we receive tick price for options
                    if spot <> None:

                        rate = self.option_chains[tick2oc_slot[0]].rate
                        div = self.option_chains[tick2oc_slot[0]].div
                        tvol = self.option_chains[tick2oc_slot[0]].trade_vol
                        logging.debug('sp=%0.4f, x=%0.4f, %s, evald=%s, expiryd=%s, r=%0.4f, d=%0.4f, v=%0.4f, px[%d]=%0.4f' % (\
                                                spot, contract.m_strike, contract.m_right, today, contract.m_expiry, rate,\
                                                div, tvol, field, items.__dict__['price']))

                        results = None
                        iv = optcal.cal_implvol(spot, contract.m_strike, contract.m_right, today, contract.m_expiry, rate,\
                                                div, tvol, items.__dict__['price'])
                        results = optcal.cal_option(spot, contract.m_strike,
                                                    contract.m_right, today,
                                                    contract.m_expiry, rate,
                                                    div, iv['imvol'])
                        results[Option.IMPL_VOL] = iv['imvol']
                        #print results
                        o.set_analytics(**results)
                        logging.debug(o.get_analytics())
                        o.set_extra_attributes('spot', spot)
                        o.set_extra_attributes('rate', rate)
                        o.set_extra_attributes('div', div)
                        o.set_extra_attributes('chain_id', tick2oc_slot[0])

                except Exception, err:
                    logging.error(traceback.format_exc())

                o.set_extra_attributes(
                    'last_updated',
                    datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
                self.broadcast_analytics(tick2oc_slot[0], o)
                logging.debug(o.object2kvstring())
            # is an underylying
            else:
Example #10
0
    def pretty_print(self):
        sorted_opt = sorted(
            map(
                lambda i:
                (self.options[i].get_contract().m_strike, self.options[i]),
                range(len(self.options))))

        def format_tick_val(val, fmt):
            if val == None:
                length = len(fmt % (0))
                return ' ' * length

            return fmt % (val)

        sorted_call = filter(lambda x: x[1].get_contract().m_right == 'C',
                             sorted_opt)
        sorted_put = filter(lambda x: x[1].get_contract().m_right == 'P',
                            sorted_opt)
        # last, bidq, bid, ask, askq, imvol, delta, theta
        fmt_spec = '%8.2f'
        fmt_spec2 = '%8.4f'
        fmt_specq = '%8d'
        fmt_call = map(
            lambda x: (x[0], '%s,%s,%s,%s,%s,%s,%s,%s' % (
                format_tick_val(x[1].get_tick_value(4), fmt_spec),
                format_tick_val(x[1].get_tick_value(0), fmt_specq),
                format_tick_val(x[1].get_tick_value(1), fmt_spec),
                format_tick_val(x[1].get_tick_value(2), fmt_spec),
                format_tick_val(x[1].get_tick_value(3), fmt_specq),
                format_tick_val(x[1].get_analytics()[Option.IMPL_VOL],
                                fmt_spec2),
                format_tick_val(x[1].get_analytics()[Option.DELTA], fmt_spec2),
                format_tick_val(x[1].get_analytics()[Option.THETA], fmt_spec2),
            )), sorted_call)

        fmt_put = map(
            lambda x: (x[0], '%s,%s,%s,%s,%s,%s,%s,%s' % (
                format_tick_val(x[1].get_tick_value(4), fmt_spec),
                format_tick_val(x[1].get_tick_value(0), fmt_specq),
                format_tick_val(x[1].get_tick_value(1), fmt_spec),
                format_tick_val(x[1].get_tick_value(2), fmt_spec),
                format_tick_val(x[1].get_tick_value(3), fmt_specq),
                format_tick_val(x[1].get_analytics()[Option.IMPL_VOL],
                                fmt_spec2),
                format_tick_val(x[1].get_analytics()[Option.DELTA], fmt_spec2),
                format_tick_val(x[1].get_analytics()[Option.THETA], fmt_spec2),
            )), sorted_put)
        title = '%s%30s%s' % ('-' * 40,
                              ContractHelper.makeRedisKeyEx(
                                  self.get_underlying().get_contract()).center(
                                      50, ' '), '-' * 40)
        header = '%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s |%8s| %8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s' % (
            'last', 'bidq', 'bid', 'ask', 'askq', 'ivol', 'delta', 'theta',
            'strike', 'last', 'bidq', 'bid', 'ask', 'askq', 'ivol', 'delta',
            'theta')
        combined = map(
            lambda i: '%s |%8.2f| %s' %
            (fmt_call[i][1], fmt_put[i][0], fmt_put[i][1]),
            range(len(fmt_call)))
        footer = '%s' % ('-' * 130)
        print title
        print header
        for e in combined:
            print e
        print footer
Example #11
0
        raise ValueError, "Failed to open config file"

    logconfig = eval(
        config.get(
            "options_chain",
            "options_calculation_engine.logconfig").strip('"').strip("'"))
    logconfig['format'] = '%(asctime)s %(levelname)-8s %(message)s'
    logging.basicConfig(**logconfig)

    contractTuple = ('QQQ', 'STK', 'SMART', 'USD', '', 0, '')
    contract = ContractHelper.makeContract(contractTuple)
    oc = OptionsChain('QQQ-MAR24')
    oc.set_option_structure(contract, 2.5, 100, 0.005, 0.003, '20160324')
    oc.build_chain(98.0, 0.025, 0.25)
    for c in oc.get_option_chain():
        print '%s' % ContractHelper.makeRedisKeyEx(c.get_contract())

    near_expiry = '20160226'
    contractTuple = ('HSI', 'FUT', 'HKFE', 'HKD', near_expiry, 0, '')
    contract = ContractHelper.makeContract(contractTuple)
    oc1 = OptionsChain('HSI-%s' % near_expiry)
    oc1.set_option_structure(contract, 200, 50, 0.0012, 0.0328, near_expiry)
    oc1.build_chain(19200, 0.08, 0.219)
    for c in oc1.get_option_chain():
        print '%s' % ContractHelper.makeRedisKeyEx(c.get_contract())

    far_expiry = '20160330'
    contractTuple = ('HSI', 'FUT', 'HKFE', 'HKD', far_expiry, 0, '')
    contract = ContractHelper.makeContract(contractTuple)
    oc2 = OptionsChain('HSI-%s' % far_expiry)
    oc2.set_option_structure(contract, 200, 50, 0.0012, 0.0328, far_expiry)
Example #12
0
        def add_subscription(contract):
            self.handle.append(contract)
            newId = len(self.handle) - 1
            self.tickerId[ContractHelper.makeRedisKeyEx(contract)] = newId

            return newId