def execute_report(start_date, end_date, security, market, account, market_database, time_zone_database, service_locator_client, order_execution_client): orders = [] activity_log = [] if account is not None: order_queue = beam.Queue() nexus.order_execution_service.query_daily_order_submissions( account, start_date, end_date, market_database, time_zone_database, order_execution_client, order_queue) account_orders = [] beam.flush(order_queue, account_orders) for order in account_orders: if security is not None and security != order.info.fields.security: continue if market is not None and market != order.info.fields.security.market: continue orders.append(order) execution_reports = order.get_publisher().get_snapshot() for execution_report in execution_reports: activity_log.append((order.info.fields, execution_report)) else: accounts = service_locator_client.load_all_accounts() for account in accounts: (account_orders, account_log) = execute_report( start_date, end_date, security, market, account, market_database, time_zone_database, service_locator_client, order_execution_client) orders += account_orders activity_log += account_log orders.sort(key=lambda value: value.info.timestamp) activity_log.sort(key=lambda value: value[1].timestamp) return (orders, activity_log)
def main(): parser = argparse.ArgumentParser( description='v1.0 Copyright (C) 2020 Spire Trading Inc.') parser.add_argument('-c', '--config', type=str, help='Configuration file', default='config.yml') parser.add_argument('-d', '--date', type=parse_date, help='Date', required=True) parser.add_argument('-m', '--destination', type=str, help='Destination', required=True) args = parser.parse_args() try: stream = open(args.config, 'r').read() config = yaml.load(stream, yaml.SafeLoader) except IOError: sys.stderr.write('%s not found\n' % args.config) exit(1) except yaml.YAMLError as e: report_yaml_error(e) exit(1) date = beam.time_service.to_utc_time(args.date) section = config['service_locator'] address = parse_ip_address(section['address']) username = section['username'] password = section['password'] service_clients = nexus.ApplicationServiceClients(address, username, password) service_clients.open() market_database = \ service_clients.get_definitions_client().load_market_database() time_zone_database = \ service_clients.get_definitions_client().load_time_zone_database() for account in \ service_clients.get_service_locator_client().load_all_accounts(): queue = beam.Queue() nexus.order_execution_service.query_daily_order_submissions( account, date, date, market_database, time_zone_database, service_clients.get_order_execution_client(), queue) orders = [] beam.flush(queue, orders) for order in orders: if order.info.fields.destination != args.destination: continue execution_reports = order.get_publisher().get_snapshot() if not nexus.is_terminal(execution_reports[-1].status): cancel_report = \ nexus.order_execution_service.ExecutionReport.build_updated_report( execution_reports[-1], nexus.OrderStatus.CANCELED, service_clients.get_time_client().get_time()) service_clients.get_order_execution_client().update( order.info.order_id, cancel_report)
def cancel_account(service_clients, account, region, begin, end, message): market_database = \ service_clients.get_definitions_client().load_market_database() time_zone_database = \ service_clients.get_definitions_client().load_time_zone_database() queue = beam.Queue() nexus.order_execution_service.query_daily_order_submissions( account, begin, end, market_database, time_zone_database, service_clients.get_order_execution_client(), queue) orders = [] beam.flush(queue, orders) for order in orders: if nexus.Region(order.info.fields.security) <= region: cancel_order(service_clients, order, message)
def execute_report(start_date, end_date, currency_database, market_database, time_zone_database, service_locator_client, administration_client, order_execution_client): trading_group_entries = administration_client.load_managed_trading_groups( service_locator_client.account) for trading_group_entry in trading_group_entries: print(trading_group_entry.name) group_profit_and_loss = defaultdict(lambda: nexus.Money.ZERO) trading_group = administration_client.load_trading_group( trading_group_entry) for trader in trading_group.traders: account_portfolio = nexus.accounting.TrueAveragePortfolio( market_database) account_volumes = Counter() order_queue = beam.Queue() nexus.order_execution_service.query_daily_order_submissions( trader, start_date, end_date, market_database, time_zone_database, order_execution_client, order_queue) orders = [] beam.flush(order_queue, orders) for order in orders: execution_reports = order.get_publisher().get_snapshot() for execution_report in execution_reports: account_portfolio.update(order.info.fields, execution_report) account_volumes[order.info.fields.currency.value] += \ execution_report.last_quantity if sum(account_volumes.itervalues()) > 0: print('\t%s' % trader.name) for currency_value in account_volumes.iterkeys(): currency = nexus.CurrencyId(currency_value) account_totals = account_portfolio.bookkeeper.get_total( currency) net_profit_and_loss = nexus.accounting.get_realized_profit_and_loss( account_totals) group_profit_and_loss[ currency.value] += net_profit_and_loss print('\t\tCurrency: %s' % currency_database.from_id(currency).code) print('\t\t\tVolume: %s' % account_volumes[currency.value]) print('\t\t\tP/L: %s\n' % net_profit_and_loss) if len(group_profit_and_loss) > 0: print('\tTotals') for currency_value in group_profit_and_loss.iterkeys(): currency = nexus.CurrencyId(currency_value) print('\t\t%s: %s' % (currency_database.from_id(currency).code, group_profit_and_loss[currency.value])) print('')
def execute_report(account, start_date, end_date, fee_table, service_clients): definitions_client = service_clients.get_definitions_client() market_database = definitions_client.load_market_database() time_zone_database = definitions_client.load_time_zone_database() order_execution_client = service_clients.get_order_execution_client() order_queue = beam.Queue() nexus.order_execution_service.query_daily_order_submissions( account, start_date, end_date, market_database, time_zone_database, order_execution_client, order_queue) orders = [] beam.flush(order_queue, orders) fee_state = nexus.ConsolidatedTmxFeeTable.State() for order in orders: if order.info.fields.security.country != nexus.default_countries.CA: continue if order.info.fields.destination == nexus.default_destinations.MOE: continue execution_reports = order.get_publisher().get_snapshot() for execution_report in execution_reports: calculated_fee = nexus.order_execution_service.ExecutionReport( execution_report) calculated_fee.execution_fee = nexus.Money.ZERO calculated_fee.processing_fee = nexus.Money.ZERO calculated_fee.commission = nexus.Money.ZERO calculated_fee = nexus.calculate_fee(fee_table, fee_state, order, calculated_fee) if (calculated_fee.execution_fee, calculated_fee.processing_fee, calculated_fee.commission) != \ (execution_report.execution_fee, execution_report.processing_fee, execution_report.commission): print( '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % (order.info.order_id, execution_report.sequence, order.info.fields.security.symbol, order.info.fields.security.market, order.info.fields.side, execution_report.last_price, execution_report.last_quantity, execution_report.last_market, execution_report.liquidity_flag, execution_report.execution_fee, execution_report.processing_fee, execution_report.commission, calculated_fee.execution_fee, calculated_fee.processing_fee, calculated_fee.commission)) sys.stdout.flush()