public async Task SellAsync(CandleModel candle) { if (candle != null) { if (_userBalanceService.HasOpenOrder) { await _exchangeProvider.CancelOrderAsync(_tradingPair, _userBalanceService.OpenOrderNumber); return; } var sellPrice = !_userBalanceService.EnableRealtimeTrading ? candle.ClosePrice : _exchangeProvider.GetTicker(_tradingPair).Result.HighestBid; _userBalanceService.TradingCount++; long orderNumber = 0; if (_userBalanceService.EnableRealtimeTrading) { orderNumber = await _exchangeProvider.CreateOrderAsync(TradeType.Sell, _tradingPair, sellPrice, _userBalanceService.Rate); _userBalanceService.HasOpenOrder = true; await CheckOrderInvoked(orderNumber, TradeType.Sell); } var profit = _userBalanceService.GetProfit(sellPrice, candle.StartDateTime); var msg = $"Sell crypto currency. Date: {candle.StartDateTime}; Price: ${sellPrice}; Rate: {_userBalanceService.Rate}; OrderNumber: {orderNumber}\n" + $"Profit: ${profit.Profit}\n" + $"Trading time in hours: {Math.Round(profit.TradingTimeInMinutes / 60.0, 2)}\n" + "\n" + _userBalanceService.TradingSummary(); Console.WriteLine(msg); _emailService.SendEmail(string.Format(_cryptoTradingOptions.EmailSubject, _tradingPair), msg); } }
// ReSharper disable once UnusedParameter.Local public static void Main(string[] args) { Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; CancellationTokenSource.Cancel(); Console.WriteLine("Waiting...."); }; var options = new ArgumentOptions(); var isValid = CommandLine.Parser.Default.ParseArgumentsStrict(args, options); if (!isValid) { Console.WriteLine("Parameter is wrong!"); return; } InitializeAutoMappers(); var serviceProvider = InitDependencyInjection.Init(options.Exchange, options.Strategy); Console.WriteLine("Starting parameters:"); Console.WriteLine($"\tExchange name: {options.Exchange}"); Console.WriteLine($"\tTrading pair: {options.TradingPair}"); Console.WriteLine($"\tCandle period: {options.CandlePeriod}"); Console.WriteLine($"\tStrategy name: {options.Strategy}"); Console.WriteLine(); try { _userBalanceService = serviceProvider.GetService <IUserBalanceService>(); _realTimeService = serviceProvider.GetService <ITraderService>(); var utcNow = DateTime.UtcNow; var delayStartInSeconds = (int)options.CandlePeriod * 60 - utcNow.Minute % (int)options.CandlePeriod * 60 - utcNow.Second; Console.WriteLine($"Delaying realtime trading start. Delay time (seconds): {delayStartInSeconds}"); Thread.Sleep(delayStartInSeconds * 1000); Console.WriteLine(); Console.WriteLine("Started trading"); Console.WriteLine(); _realTimeService.StartTradingAsync(options.TradingPair, options.CandlePeriod, CancellationTokenSource.Token).Wait(); } catch (Exception ex) { var emailService = serviceProvider.GetService <IEmailService>(); emailService.SendEmail($"Exception {options.TradingPair}", ex.ToString()); Console.WriteLine($"Exception: {ex}"); } Console.WriteLine("############ SUMMARY ############"); Console.WriteLine(_userBalanceService.TradingSummary()); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }