public static void ExecuteTradeDebug(BinanceClient client, KlineData data, Currency currency, OrderSide side) { var allPrices = client.GetAllPrices().Result; var price = allPrices.Where(p => p.Symbol.Contains(currency.Name)).Select(a => a.Price).FirstOrDefault(); var finalPrice = BinanceAPIMain.Debug == "true" ? data.Close : price; DateTime currentTime = BinanceAPIMain.Debug == "true" ? CurrencyCalc.UnixTimeStampToDateTime(data.EndTime) : DateTime.Now; if (side == OrderSide.BUY) { //Orders will break if not successful, so check for try/catch block and add into currency ledger or move on. try { currency.CoinsBought.Add(new Tuple <decimal, decimal>(currency.AllottedCoins, data.Close)); currency.InTrade = true; BinanceAPIMain.WriteToLog(currency.AllottedCoins + ":" + currency.Name + ": go in on trade at: " + data.Close + " Time: " + currentTime, ConsoleColor.Yellow); BinanceAPIMain.WriteToLogTrades(currency.Name + ": Buy " + currency.AllottedCoins + " coins at " + data.Close + " for the kline ending at " + currentTime); BinanceAPIMain.WriteToCurrencyFile(currency.AllottedCoins + "|" + data.Close, currency.Name); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.Message + " while buying " + currency.Name, ConsoleColor.DarkRed); } } else if (side == OrderSide.SELL) { try { foreach (Tuple <decimal, decimal> tuplePair in currency.CoinsBought) { decimal percentage = Math.Round(((data.Close - tuplePair.Item2) / tuplePair.Item2) * 100, 2); ConsoleColor color = percentage > 0 ? ConsoleColor.White : ConsoleColor.Red; BinanceAPIMain.WriteToLogTrades(currency.Name + ": Sell " + currency.AllottedCoins + " coins at " + data.Close + " for the kline ending at " + currentTime + " for a " + percentage + " percent change"); BinanceAPIMain.WriteToLog(currency.Name + ": sold " + tuplePair.Item1 + " coins at " + data.Close + " on " + currentTime + Environment.NewLine + currency.Name + ": Made: " + (data.Close - tuplePair.Item2) + " Percentage: " + percentage, color); } currency.CoinsBought.Clear(); currency.LastSalePrice = data.Close; currency.LastTransactionTime = DateTime.Now; //Remove the file which indicated we had an open position on the trade. if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt")) { File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + currency.Name + ".txt"); } } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.Message + " while selling " + currency.Name, ConsoleColor.DarkRed); } } }
static void KlineHandler(KlineMessage data, string symbol, BinanceClient client) { if (data.EventType == "Cancellation Requested") { Console.WriteLine(data.Symbol + ", " + data.EventType); } else { CurrencyCalc currency = BinanceAPIMain.AllCurrencies.Where(p => p.Name == symbol).FirstOrDefault(); currency.CalculateTradeAlgorithm(data.KlineInfo, client); } }