Example #1
0
        public static async void ShowOrderDetails(Order order)
        {
            Log.Write($"SHOW ORDER DETAILS FOR {order.Id}");
            var ordersApi      = new OrdersApi();
            var orderCommands  = ordersApi.GetOwnedCommands(order.Id);
            var currentCommand = orderCommands.LastOrDefault(command => command.CommandType == Command.CommandTypeEnum.New || command.CommandType == Command.CommandTypeEnum.Modify && (command.CommandStatus == Command.CommandStatusEnum.AtExecution || command.CommandStatus == Command.CommandStatusEnum.OnHold || command.CommandStatus == Command.CommandStatusEnum.ExecutionSuspended));

            if (currentCommand != null)
            {
                var currentOrderVersion = ordersApi.GetOrderVersion(currentCommand.Id);
                var fills            = ordersApi.GetOwnedFills(order.Id);
                var commandIds       = orderCommands.Select(command => command.Id).ToList();
                var executionReports = ordersApi.GetOwnedExecutionReportsBatch(commandIds);
                var commandReports   = ordersApi.GetOwnedCommandReportsBatch(commandIds);

                var contractLibraryApi = new ContractLibraryApi();
                var contract           = await contractLibraryApi.GetContractAsync(order.ContractId);

                var contractMaturity = await contractLibraryApi.GetContractMaturityAsync(contract.ContractMaturityId);

                var product = await contractLibraryApi.GetProductAsync(contractMaturity.ProductId);

                var lines = orderCommands.Select(command => new
                {
                    Id        = command.Id,
                    Timestamp = command.Timestamp,
                    Text      = GetCommandDescription(order, contract, command)
                })
                            .Concat(
                    fills.Select(fill => new
                {
                    Id        = fill.Id,
                    Timestamp = fill.Timestamp,
                    Text      = GetFillDescription(fill)
                }))
                            .Concat(
                    commandReports.Select(commandReport => new
                {
                    Id        = commandReport.Id,
                    Timestamp = commandReport.Timestamp,
                    Text      = GetCommandReportDescription(commandReport)
                }))
                            .Concat(
                    executionReports.Select(executionReport => new
                {
                    Id        = executionReport.Id,
                    Timestamp = executionReport.Timestamp,
                    Text      = GetExecutionReportDescription(executionReport)
                }));

                string orderStatus;

                switch (order.OrdStatus)
                {
                case Order.OrdStatusEnum.Working:
                    var totalFilled = fills.Where(fill => fill.Active == true).Sum(fill => fill.Qty);
                    if (totalFilled > 0)
                    {
                        orderStatus = "Partially Filled";
                    }
                    else
                    {
                        orderStatus = order.OrdStatus.ToString();
                    }
                    break;

                default:
                    orderStatus = order.OrdStatus.ToString();
                    break;
                }

                string accountHolderEmail;
                var    accountingApi = new AccountingApi();
                var    account       = await accountingApi.GetAccountAsync(order.AccountId);

                try
                {
                    // CTA should look through trading permissions
                    var allTradingPermissions = await accountingApi.GetAllTradingPermissionsAsync();

                    var tradingPermission = allTradingPermissions.First(x => x.AccountId == account.Id && x.Status == TradingPermission.StatusEnum.Approved);
                    accountHolderEmail = tradingPermission.AccountHolderEmail;
                }
                catch
                {
                    // Organization Admin or actual owner of the account has access to User entity
                    var userApi = new UsersApi();
                    var user    = await userApi.GetUserAsync(account.UserId);

                    accountHolderEmail = user.Email;
                }

                Log.Write($"Order #{order.Id} {GetOrderVersionDescription(order, contract, currentOrderVersion)} - {orderStatus}, Acc: #{account.Name}, Email: #{accountHolderEmail}");
                foreach (var line in lines.OrderBy(x => x.Id).ThenBy(x => x.Timestamp))
                {
                    Console.WriteLine($"{line.Timestamp} #{line.Id}: {line.Text}");
                }
                Console.WriteLine();
            }
        }