def do_receipt_list(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() if args.org is not None: org_obj = try_get_then_lookup( state, 'organization', ['organization:ticker', 'organization:pricing-source'], args.org) if org_obj is not None: ListWriter(state, 'receipt', args.full, args.yaml).write('PayeeId', org_obj.get('object-id')) else: raise ClientException("{} does not match an organization".format( args.org)) elif args.bond is not None: bond_obj = try_get_then_lookup(state, 'bond', ['bond:isin', 'bond:cusip'], args.bond) if bond_obj is not None: ListWriter(state, 'receipt', args.full, args.yaml).write('BondId', bond_obj.get('object-id')) else: raise ClientException("{} does not match a bond".format(args.bond)) else: ListWriter(state, 'receipt', args.full, args.yaml).write()
def do_order_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') username = config.get("DEFAULT", 'username') client = BondClient(base_url=url, keyfile=key_file) args_dict = vars(args) store = client.get_all_store_objects() limit_price = None if args_dict.get('order-type') == 'Limit': if args.limit_price is not None: limit_price = args.limit_price if args.firm_id is None: firm_id = args.firm_id try: firm_id = try_get_then_lookup(store, 'participant', ['participant:username'], username)["firm-id"] print firm_id except KeyError: pass else: firm_id = args.firm_id client.create_order(args.action, args_dict.get('order-type'), firm_id, args.quantity, args.isin, args.cusip, limit_price, object_id=args.object_id) if args.wait: client.wait_for_commit()
def do_quote_list(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() if args.org is not None: org_obj = try_get_then_lookup( state, 'organization', ['organization:ticker', 'organization:pricing-source'], args.org) if org_obj is not None: ListWriter(state, 'quote', args.full, args.yaml).write('Firm', org_obj.get('pricing-source')) else: raise ClientException("{} does not match an organization".format( args.org)) elif args.user is not None: user_obj = try_get_then_lookup( state, 'participant', ['participant:username', 'participant:key-id'], args.user) if user_obj is not None: ListWriter(state, 'quote', args.full, args.yaml).write('CreatorId', user_obj.get('object-id')) else: raise ClientException("{} does not match a user".format(args.user)) else: ListWriter(state, 'quote', args.full, args.yaml).write()
def do_org_list(args, config): url = config.get("DEFAULT", 'url') key_file = config.get("DEFAULT", 'key_file') client = BondClient(base_url=url, keyfile=key_file) state = client.get_all_store_objects() ListWriter(state, 'organization', args.full, args.yaml).write()
def do_holding_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() org_obj = try_get_then_lookup(state, 'holding', [], args.identifier) ShowWriter(org_obj, 'holding', args.identifier).write()
def do_bond_list(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() ListWriter(state, 'bond', args.full, args.yaml).write()
def do_bond_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() bond_obj = try_get_then_lookup(state, 'bond', ['bond:isin', 'bond:cusip'], args.identifier) ShowWriter(bond_obj, 'bond', args.identifier).write()
def do_settlement_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() object_id = vars(args).get('object-id') settlement_obj = try_get_then_lookup(state, 'settlement', ['settlement:order-id'], object_id) ShowWriter(settlement_obj, 'settlement', object_id).write()
def do_user_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() user_obj = try_get_then_lookup( state, 'participant', ['participant:username', 'participant:key-id'], args.identifier) ShowWriter(user_obj, 'User', args.identifier).write()
def do_org_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() org_obj = try_get_then_lookup( state, 'organization', ['organization:ticker', 'organization:pricing-source'], args.identifier) ShowWriter(org_obj, 'organization', args.identifier).write()
def do_holding_list(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() if args.org is not None: org_obj = try_get_then_lookup( state, 'organization', ['organization:ticker', 'organization:pricing-source'], args.org) if org_obj is None: raise ClientException("--org did not specify an organization") ListWriter(state, 'holding', args.full, args.yaml).write('OwnerId', org_obj.get('object-id')) else: ListWriter(state, 'holding', args.full, args.yaml).write()
def do_settlement_list(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() if args.creator is not None: user_obj = try_get_then_lookup( state, 'participant', ['participant:username', 'participant:key-id'], args.creator) if user_obj is not None: ListWriter(state, 'settlement', args.full, args.yaml).write('CreatorId', user_obj.get('object-id')) else: raise ClientException("{} does not match a user".format( args.creator)) else: ListWriter(state, 'settlement', args.full, args.yaml).write()
def do_order_show(args, config): url = config.get("DEFAULT", 'url') client = BondClient(base_url=url, keyfile=None) state = client.get_all_store_objects() order_obj = try_get_then_lookup(state, 'order', [], args.identifier) ShowWriter(order_obj, 'order', args.identifier).write() if order_obj is not None and order_obj.get('status') == 'Settled': settlement_obj = None try: settlement_obj = state.lookup('settlement:order-id', order_obj.get('object-id')) print "Settlement:" print "----------------------------------------------------" except KeyError: pass ShowWriter(settlement_obj, 'settlement', order_obj.get('object-id')).write()
def do_holding_create(args, config): url = config.get('DEFAULT', 'url') key_file = config.get('DEFAULT', 'key_file') client = BondClient(base_url=url, keyfile=key_file) state = client.get_all_store_objects() args_dict = vars(args) org = try_get_then_lookup( state, "organization", ["organization:ticker", "organization:pricing_source"], args.owner) if args_dict.get("asset-type") == "Currency": asset_id = args_dict.get("asset-id") else: asset_id = try_get_then_lookup(state, "bond", ["bond:cusip", "bond:isin"], args_dict.get("asset-id"))["object-id"] client.create_holding(org["object-id"], args_dict.get("asset-type"), asset_id, args.amount, args.object_id) if args.wait: client.wait_for_commit()
def on_validator_discovered(self, url): # We need a key file for the client then create a new client and add # it to our cadre of clients to use. For each client, we are going to # get the list of organizations it knows about. For any organization # that has a pricing source (i.e., can be a market maker) that is not # one of our made up ones (i.e., starts with Z), we are going to add # an authorization to it for the newly-created participant and # role of market maker. If the client has no such organizations, we # will create one of our own. Later on we will be able to issue orders # and quotes against this(these) organization(s). It is important # that orders (buy/sell) and quotes be issued from the same client # that creates the participant. with self._create_temporary_key_file() as key_file: try: client = BondClient(base_url=url, keyfile=key_file.name) # Participant and organization names are limited to 16 # characters and a full ISO formatted date/time is too long # so generate something that can be used as a fairly random # string of 14 characters random_name = \ hashlib.md5(datetime.now().isoformat()).hexdigest() participant_name = 'P_{0:.14s}'.format(random_name) participant_id = 'intel_simparticipant_{0}'.format(random_name) LOGGER.info( 'Create participant %s with ID %s for %s', participant_name, participant_id, client.base_url) client.create_participant( username=participant_name, object_id=participant_id) # Get the list of organizations that have a pricing source # that doesn't start with Z (i.e., is one we made up). state = client.get_all_store_objects() organizations = [] org_list = [x[1] for x in state.iteritems() if x[1]["object-type"] == 'organization'] for org in org_list: pricing_source = org.get('pricing-source') if pricing_source is not None \ and not pricing_source.startswith('Z'): organizations.append(org) # If there were none, then we need to create one so that we # at least have one market maker for this client to issue # quotes/orders against. if len(organizations) == 0: marketmaker_name = 'M_{0:.14s}'.format(random_name) marketmaker_id = \ 'intel_simmarketmaker_{0}'.format(random_name) # Just in case there are market makers left lying around # from a previous run we will keep trying until we hit a # unique pricing source. Once successful, add this # organization to the list while True: try: pricing_source = \ next(self._pricing_source_generator) client.create_org( name=marketmaker_name, object_id=marketmaker_id, pricing_src=pricing_source) break except InvalidTransactionError: pass organizations.append({ 'object-id': marketmaker_id, 'name': marketmaker_name, 'pricing-source': pricing_source }) LOGGER.info( 'Create marketmaker %s with pricing source %s and ' 'ID %s for %s', marketmaker_name, pricing_source, marketmaker_id, client.base_url) # Now, we need to add a participant/market maker role # authorization to each organization for organization in organizations: LOGGER.info( 'Add marketmaker role authorization to %s (%s) by ' 'participant %s', organization.get('name'), organization.get('pricing-source'), participant_id) client.add_authorization_to_org( object_id=organization.get('object-id'), role='marketmaker', participant_id=participant_id) # Add the client to our list so that we can choose from it # when randomly issuing new transactions. with self._lock: self._clients.append( Client( organizations=organizations, bond_client=client, id=participant_id)) except (InvalidTransactionError, MessageException) as err: LOGGER.error( 'Failed to create participant and authorize ' 'organization(s): %s', err)