public static void SetDailyLossLimit(Account account, double maxDailyLoss) { var risksApi = new RisksApi(); var autoLiqRules = risksApi.GetOwnedUserAccountAutoLiqs(account.Id); var dailyLossRule = autoLiqRules.FirstOrDefault(); if (dailyLossRule == null) { Console.WriteLine("Daily Loss Auto-Liq rule is not found. Create a new one"); // id should be the same as account's id dailyLossRule = new UserAccountAutoLiq(Id: account.Id, DailyLossAutoLiq: maxDailyLoss); risksApi.CreateUserAccountAutoLiq(dailyLossRule); } else if (dailyLossRule.DailyLossAutoLiq != maxDailyLoss) { Console.WriteLine("Daily Loss Auto-Liq rule exists, but Daily Loss Auto-Liq doesn't match new requirements. Updating old one"); // id should be the same as account's id dailyLossRule.DailyLossAutoLiq = maxDailyLoss; risksApi.UpdateUserAccountAutoLiq(dailyLossRule); } else { Console.WriteLine("Daily Loss Auto-Liq rule exists and Daily Loss Auto-Liq matches the new value"); } }
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 SetMaxOverallPositionLimit(Account account, int maxExposedPos) { var name = "Max Exposed Position"; var risksApi = new RisksApi(); // Find or create a position risk limit entity - an entity that describes a threshold level var positionLimits = risksApi.GetOwnedUserAccountPositionLimits(account.Id); var exposureRiskLimit = positionLimits.FirstOrDefault(x => x.Description == name && x.TotalBy == UserAccountPositionLimit.TotalByEnum.Overall); if (exposureRiskLimit == null) { Console.WriteLine($"'{name}' risk limit is not found. Creating a new one."); var request = new UserAccountPositionLimit( AccountId: account.Id, Active: true, TotalBy: UserAccountPositionLimit.TotalByEnum.Overall, Description: name, ExposedLimit: maxExposedPos); exposureRiskLimit = risksApi.CreateUserAccountPositionLimit(request); } else if (exposureRiskLimit.ExposedLimit != maxExposedPos || exposureRiskLimit.Active != true) { Console.WriteLine($"'{name}' risk limit is found, but doesn't match new requirements. Updating old one."); exposureRiskLimit.ExposedLimit = maxExposedPos; exposureRiskLimit.Active = true; exposureRiskLimit = risksApi.UpdateUserAccountPositionLimit(exposureRiskLimit); } else { Console.WriteLine($"'{name}' risk limit is found and it matches new requirements."); } // Now find or create "hard limit" risk limit parameter - an entity that specifies // what to do when the thresold reached. // "hard limit" tells to reject orders over the threshold. var riskLimitParams = risksApi.GetOwnedUserAccountRiskParameters(exposureRiskLimit.Id); var hardLimitParam = riskLimitParams.FirstOrDefault(x => x.HardLimit == true); if (hardLimitParam == null) { Console.WriteLine($"Hard-Limit parameter is not found. Creating a new one"); hardLimitParam = new UserAccountRiskParameter( UserAccountPositionLimitId: exposureRiskLimit.Id, HardLimit: true); risksApi.CreateUserAccountRiskParameter(hardLimitParam); } else { Console.WriteLine($"Hard-Limit parameter exists already"); } }
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 static void ShowRiskLimits(Account account) { var risksApi = new RisksApi(); var positionLimits = risksApi.GetOwnedUserAccountPositionLimits(account.Id); foreach (var positionLimit in positionLimits) { Console.WriteLine(positionLimit); var riskParameters = risksApi.GetOwnedUserAccountRiskParameters(positionLimit.Id); foreach (var riskParameter in riskParameters) { Console.WriteLine(riskParameter); } } if (positionLimits.Count == 0) { Console.WriteLine("This account doesn't have user-defined risk limits"); } }
public void Init() { instance = new RisksApi(); }