예제 #1
0
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)
예제 #2
0
 def test_price_retreat(self):
     security = nexus.parse_security('ABX.TSX')
     order_fields = nexus.order_execution_service.OrderFields.build_limit_order(
         security, nexus.Side.ASK, 1000, nexus.Money.ZERO)
     order = pegged_order.PeggedOrder(self.service_clients, order_fields,
                                      nexus.Money.CENT)
     order.start()
     self.environment.update_bbo_price(security,
                                       nexus.Money.from_value('1.00'),
                                       nexus.Money.from_value('1.01'))
     submission_queue = beam.Queue()
     self.environment.monitor_order_submissions(submission_queue)
     expected_order = submission_queue.top()
     submission_queue.pop()
     self.assertEqual(expected_order.info.fields.price,
                      nexus.Money.from_value('1.02'))
     self.assertEqual(expected_order.info.fields.quantity, 1000)
     self.environment.accept_order(expected_order)
     self.environment.update_bbo_price(security,
                                       nexus.Money.from_value('0.90'),
                                       nexus.Money.from_value('0.91'))
     self.assertTrue(
         nexus.order_execution_service.tests.is_pending_cancel(
             expected_order))
     self.environment.cancel_order(expected_order)
     expected_order = submission_queue.top()
     submission_queue.pop()
     self.assertEqual(expected_order.info.fields.price,
                      nexus.Money.from_value('0.92'))
     self.assertEqual(expected_order.info.fields.quantity, 1000)
     self.environment.accept_order(expected_order)
     self.environment.fill_order(expected_order, 1000)
     order.wait()
     self.assertTrue(submission_queue.is_empty())
예제 #3
0
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)
예제 #4
0
 def __init__(self, service_clients, param1, param2, param3):
     self.service_clients = service_clients
     self.param1 = param1
     self.param2 = param2
     self.param3 = param3
     self.var1 = None
     self.var2 = None
     self.var3 = None
     self.state = None
     self.tasks = beam.RoutineTaskQueue()
     self.completion_queue = beam.Queue()
예제 #5
0
 def __init__(self, service_clients, param1, param2, param3, param4, param5, param6, param7, param8):
     self.service_clients = service_clients
     self.param1 = param1
     self.param2 = param2
     self.param3 = param3
     self.param4 = param4
     self.param5 = param5
     self.param6 = param6
     self.param7 = param7
     self.param8 = param8
     self.state = None
     self.tasks = beam.RoutineTaskQueue()
     self.completion_queue = beam.Queue()
예제 #6
0
 def __init__(self, service_clients):
     self.service_clients = service_clients
     self.var_1 = None
     self.var_2 = None
     self.var1 = None
     self.var2 = None
     self.var3 = None
     self.var4 = None
     self.var5 = None
     self.var_6 = None
     self.state = None
     self.tasks = beam.RoutineTaskQueue()
     self.completion_queue = beam.Queue()
예제 #7
0
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)
예제 #8
0
파일: settlement.py 프로젝트: tschof/nexus
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('')
예제 #9
0
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()
예제 #10
0
 def test_rejection(self):
     security = nexus.parse_security('ABX.TSX')
     order_fields = nexus.order_execution_service.OrderFields.build_limit_order(
         security, nexus.Side.BID, 1000, nexus.Money.ZERO)
     order = pegged_order.PeggedOrder(self.service_clients, order_fields,
                                      nexus.Money.CENT)
     order.start()
     self.environment.update_bbo_price(security,
                                       nexus.Money.from_value('1.00'),
                                       nexus.Money.from_value('1.01'))
     submission_queue = beam.Queue()
     self.environment.monitor_order_submissions(submission_queue)
     expected_order = submission_queue.pop()
     self.assertEqual(expected_order.info.fields.price,
                      nexus.Money.from_value('0.99'))
     self.assertEqual(expected_order.info.fields.quantity, 1000)
     self.environment.reject(expected_order)
     order.wait()
     self.assertIsNone(submission_queue.try_pop())
예제 #11
0
 def __init__(self, service_clients):
     self.service_clients = service_clients
     self.state = None
     self.tasks = beam.RoutineTaskQueue()
     self.completion_queue = beam.Queue()