private async Task <IReadOnlyCollection <HedgeLimitOrder> > CreateLimitOrdersAsync( IEnumerable <AssetInvestment> assetInvestments) { HedgeSettings hedgeSettings = await _hedgeSettingsService.GetAsync(); var hedgeLimitOrders = new List <HedgeLimitOrder>(); foreach (AssetInvestment assetInvestment in assetInvestments) { AssetHedgeSettings assetHedgeSettings = await _assetHedgeSettingsService.EnsureAsync(assetInvestment.AssetId); LimitOrderType limitOrderType = assetInvestment.RemainingAmount > 0 ? LimitOrderType.Sell : LimitOrderType.Buy; if (!CanCreateHedgeLimitOrder(assetInvestment, assetHedgeSettings, hedgeSettings, limitOrderType)) { continue; } decimal commonThresholdUp = limitOrderType == LimitOrderType.Buy ? hedgeSettings.ThresholdUpBuy : hedgeSettings.ThresholdUpSell; LimitOrderPrice limitOrderPrice = LimitOrderPriceCalculator.Calculate(assetInvestment.Quote, Math.Abs(assetInvestment.RemainingAmount), limitOrderType, assetHedgeSettings.ThresholdUp ?? commonThresholdUp, hedgeSettings.MarketOrderMarkup); decimal price = limitOrderPrice.Price; decimal volume = Math.Abs(assetInvestment.RemainingAmount / assetInvestment.Quote.Mid); HedgeLimitOrder hedgeLimitOrder = HedgeLimitOrder.Create(assetHedgeSettings.Exchange, assetHedgeSettings.AssetId, assetHedgeSettings.AssetPairId, limitOrderType, limitOrderPrice.Type, price, volume); hedgeLimitOrder.Context = assetInvestment.ToJson(); hedgeLimitOrders.Add(hedgeLimitOrder); } return(hedgeLimitOrders); }
public void Calculate_Buy_Limit_Price() { // arrange var quote = new Quote("BTCUSD", DateTime.UtcNow, 6000, 5000, "source"); decimal volume = 2000; LimitOrderType limitOrderType = LimitOrderType.Buy; decimal thresholdUp = 5000; decimal marketOrderMarkup = .1m; // act LimitOrderPrice limitOrderPrice = LimitOrderPriceCalculator.Calculate(quote, volume, limitOrderType, thresholdUp, marketOrderMarkup); // assert Assert.IsTrue(limitOrderPrice.Price == quote.Bid && limitOrderPrice.Type == PriceType.Limit); }