public static async void PrepopulateCache() { // Cache pre-populating can save some latency at trading time, but not recommended for short-term connections Log.Write("PRE-POPULATING CONTRACT LIBRARY"); var contractLibraryApi = new ContractLibraryApi(); await contractLibraryApi.GetAllContractGroupsAsync(); await contractLibraryApi.GetAllExchangesAsync(); await contractLibraryApi.GetAllCurrenciesAsync(); await contractLibraryApi.GetAllCurrencyRatesAsync(); var riskApi = new RisksApi(); await riskApi.GetAllProductMarginsAsync(); var products = await contractLibraryApi.GetAllProductsAsync(); // Highly not recommended for short-term connections foreach (var product in products) { await contractLibraryApi.GetOwnedContractMaturitiesAsync(product.Id) .ContinueWith(contractMaturities => { return(contractLibraryApi.GetOwnedContractsBatchAsync(contractMaturities.Result.Select(contractMaturity => contractMaturity.Id).ToList())); }); } }
public static void ContractSpec(string symbol) { var contractLibraryApi = new ContractLibraryApi(); Contract contract = contractLibraryApi.FindContract(symbol); Log.Write(contract); ContractMaturity contractMaturity = contractLibraryApi.GetContractMaturity(contract.ContractMaturityId); Log.Write(contractMaturity); Product product = contractLibraryApi.GetProduct(contractMaturity.ProductId); Log.Write(product); Exchange exchange = contractLibraryApi.GetExchange(product.ExchangeId); Log.Write(exchange); ContractGroup contractGroup = contractLibraryApi.GetContractGroup(product.ContractGroupId); Log.Write(contractGroup); Currency currency = contractLibraryApi.GetCurrency(product.CurrencyId); Log.Write(currency); CurrencyRate currencyRate = contractLibraryApi.GetCurrencyRate(product.CurrencyId); Log.Write(currencyRate); ProductSession productSession = contractLibraryApi.GetProductSession(product.Id); Log.Write(productSession); ProductFeeParamsResponse productFees = contractLibraryApi.GetProductFeeParams(new GetProductFeeParams(new List <int?> { product.Id })); foreach (var fee in productFees._Params) { Log.Write(fee); } var riskApi = new RisksApi(); ProductMargin productMargin = riskApi.GetProductMargin(product.Id); Log.Write(productMargin); try { ContractMargin contractMargin = riskApi.GetContractMargin(contract.Id); Log.Write(contractMargin); } catch { Log.Write("Per-contract margin is not specified"); } }
public void Init() { instance = new ContractLibraryApi(); }
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(); } }