public void InitiateBuy(BuyOptions options) { IPairConfig pairConfig = tradingService.GetPairConfig(options.Pair); if (!options.ManualOrder && pairConfig.BuyTrailing != 0) { if (!trailingBuys.ContainsKey(options.Pair)) { StopTrailingSell(options.Pair); decimal currentPrice = tradingService.GetPrice(options.Pair); decimal currentMargin = 0; var trailingInfo = new BuyTrailingInfo { BuyOptions = options, Trailing = pairConfig.BuyTrailing, TrailingStopMargin = pairConfig.BuyTrailingStopMargin, TrailingStopAction = pairConfig.BuyTrailingStopAction, InitialPrice = currentPrice, LastTrailingMargin = currentMargin, BestTrailingMargin = currentMargin }; if (trailingBuys.TryAdd(options.Pair, trailingInfo)) { if (LoggingEnabled) { ITradingPair tradingPair = tradingService.Account.GetTradingPair(options.Pair); loggingService.Info($"Start trailing buy {tradingPair?.FormattedName ?? options.Pair}. " + $"Price: {currentPrice:0.00000000}, Margin: {currentMargin:0.00}"); } } } } else { orderingService.PlaceBuyOrder(options); } }
public void Swap(SwapOptions options) { lock (syncRoot) { PauseTasks(); try { if (CanSwap(options, out string message)) { ITradingPair oldTradingPair = Account.GetTradingPair(options.OldPair); var sellOptions = new SellOptions(options.OldPair) { Swap = true, ManualOrder = options.ManualOrder, Metadata = new OrderMetadata { SwapPair = options.NewPair } }; if (CanSell(sellOptions, out message)) { decimal currentMargin = oldTradingPair.CurrentMargin; decimal additionalCosts = oldTradingPair.Cost - oldTradingPair.CurrentCost + (oldTradingPair.Metadata.AdditionalCosts ?? 0); int additionalDCALevels = oldTradingPair.DCALevel; IOrderDetails sellOrderDetails = orderingService.PlaceSellOrder(sellOptions); if (!Account.HasTradingPair(options.OldPair)) { var buyOptions = new BuyOptions(options.NewPair) { Swap = true, ManualOrder = options.ManualOrder, MaxCost = sellOrderDetails.Cost, Metadata = options.Metadata }; buyOptions.Metadata.LastBuyMargin = currentMargin; buyOptions.Metadata.SwapPair = options.OldPair; buyOptions.Metadata.AdditionalDCALevels = additionalDCALevels; buyOptions.Metadata.AdditionalCosts = additionalCosts; IOrderDetails buyOrderDetails = orderingService.PlaceBuyOrder(buyOptions); var newTradingPair = Account.GetTradingPair(options.NewPair) as TradingPair; if (newTradingPair != null) { newTradingPair.Metadata.AdditionalCosts += CalculateOrderFees(sellOrderDetails); loggingService.Info($"Swap {oldTradingPair.FormattedName} for {newTradingPair.FormattedName}. " + $"Old margin: {oldTradingPair.CurrentMargin:0.00}, new margin: {newTradingPair.CurrentMargin:0.00}"); } else { loggingService.Info($"Unable to swap {options.OldPair} for {options.NewPair}. Reason: failed to buy {options.NewPair}"); notificationService.Notify($"Unable to swap {options.OldPair} for {options.NewPair}: Failed to buy {options.NewPair}"); } } else { loggingService.Info($"Unable to swap {options.OldPair} for {options.NewPair}. Reason: failed to sell {options.OldPair}"); } } else { loggingService.Info($"Unable to swap {options.OldPair} for {options.NewPair}: {message}"); } } else { loggingService.Info(message); } } finally { ContinueTasks(); } } }