public async Task DoAutoTrading(int transNumber, TradeInfo tradeInfo) { _logger.LogInformation("AutoTrader: ON"); try { var trans = $"{this.Coin}-{DateTime.Now.ToString("ddMMM")}-{transNumber}"; var autoTrader = new AutoTrader( sellAccount: SellAccount, buyAccount: BuyAccount, tradeInfo: tradeInfo, testMode: this.TestMode, trans: trans, logger: this._loggerFactory.CreateLogger <AutoTrader>() ); var tradeResult = await autoTrader.Trade(); if (tradeResult.Success) { BackgroundJob.Enqueue(() => WaitUntilOrdersAreMatched(tradeInfo, trans)); } else { await this._emailHelper.SendEmail( $"{transNumber} - Trade error, please check!!!", $"{tradeResult.ErrorMessage}"); _logger.LogCritical("Trading Error occurred! Exit for now..."); Environment.Exit(1); } } catch (Exception ex) { var message = ex.Message; if (ex.InnerException != null) { message = message + "\n" + ex.InnerException.Message; } await this._emailHelper.SendEmail($"[{this.Coin}] - [{this._options.TradeFlow.ToString()}] Trade Error! Please check!!!", message); } }
public async Task Execute() { int errorCount = 0; while (true) { try { await UpdateCoinPrices(); var tradeInfo = AnalyzeDelta(); var content = $"{Coin} - Bit: {this.BuyAccount.TradeCoin.CoinPrice.BidPrice} * " + $"Bin: {this.SellAccount.TradeCoin.CoinPrice.BidPrice} * " + $"B-B: {tradeInfo.DeltaBidBid} * " + $"B-A: {tradeInfo.DeltaBidAsk} * " + $"Profit: {Math.Round(tradeInfo.ProfitQuantity)} * " + $"Sell Qt.: {Math.Round(tradeInfo.CoinQuantityAtSell)} * " + $"Buy Qt.: {Math.Round(tradeInfo.BitcoinQuantityAtBuy)}"; Console.WriteLine(content); // Check to send notification if (tradeInfo.DeltaBidBid >= this.ExpectedDelta) { Console.Write("Time to buy ..."); if (IsAutoTrading) { Console.WriteLine("AutoTrader is initializing..."); var autoTrader = new AutoTrader( this.SellAccount, this.BuyAccount, tradeInfo ).Trade(); } Console.Write($"Send email in {_timeLeftToSendEmail}s..."); await SendMailIfTimePassed(tradeInfo, content); } errorCount = 0; this._timeLeftToSendEmail -= 2; Thread.Sleep(2000); } catch (Exception ex) { Console.WriteLine("we saw an error. Please try again!"); if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } errorCount++; if (errorCount > 100) { await EmailHelper.SendEmail( $"[TradeBot] Program Error, Please double check", this.EmailTo, ex.Message, this.MailApiKey); Thread.Sleep(TimeSpan.FromMinutes(this.ResumeAfterExpectedDelta)); } Thread.Sleep(2000); } } }