static void GetAllCoins(BinanceClient client) { try { decimal walletPercentage = Convert.ToDecimal(ConfigurationManager.AppSettings["ETHWalletPercentage"]); string[] coinArray = ConfigurationManager.AppSettings["Coins"].Split(',').ToArray(); var allPrices = client.GetAllPrices().Result; //now get the ETH/USD conversion rate var ethValue = allPrices.Where(p => p.Symbol == "ETHUSDT"); numETH = client.GetAccountInfo().Result.Balances.Where(p => p.Asset == "ETH").FirstOrDefault().Free; Console.WriteLine("Free ETH: " + numETH); Console.WriteLine("ETH Percentage: " + walletPercentage.ToString()); Console.WriteLine("ETH For Trading: " + (walletPercentage / 100) * numETH); foreach (var value in coinArray) { var coin = allPrices.Where(p => p.Symbol == value).FirstOrDefault(); //Get the binance coin/price pair for each coin specified in the appSettings file. string trimmedString = coin.Symbol.Substring(coin.Symbol.Length - 3); decimal currentValueUSD = trimmedString == "ETH" ? coin.Price * ethValue.FirstOrDefault().Price : coin.Price; if (currentValueUSD >= 10) { continue; } Currency c = new Currency { Name = coin.Symbol, Price = coin.Price, CurrentValueUSD = currentValueUSD, }; //Look to see if we have anything with the specific coin. If we do, then we are in a trade, and add it to the coinsBought and set the InTrade to true. if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\BinanceAPI\" + coin.Symbol + ".txt")) { Dictionary <decimal, decimal> priorHoldings = ReadFromCurrencyFiles(coin.Symbol); if (priorHoldings.Count > 0) { c.CoinsBought.Add(new Tuple <decimal, decimal>(priorHoldings.ElementAt(0).Key, priorHoldings.ElementAt(0).Value)); c.InTrade = true; TotalTrades++; Console.WriteLine(c.Name + " found in previous trade, " + priorHoldings.ElementAt(0).Key.ToString() + " coins at " + priorHoldings.ElementAt(0).Value.ToString()); } } AllCurrencies.Add(c); Console.WriteLine(c.Name + " Price: " + c.Price + " Current Value USD: " + c.CurrentValueUSD + " allotted coins: " + c.AllottedCoins); } } catch (Exception ex) { WriteToLog(ex.Message + ex.InnerException, ConsoleColor.DarkRed); Console.WriteLine(ex.Message + ex.InnerException); } }
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); } } }
public static string GetCurrentPurchases(BinanceClient client) { var currentCurrencies = BinanceAPIMain.AllCurrencies.Where(p => p.InTrade); var currentPrices = client.GetAllPrices().Result; StringBuilder b = new StringBuilder(); b.AppendLine("Current gain/loss:"); foreach (Currency c in currentCurrencies) { var purchasePrice = c.CoinsBought.FirstOrDefault().Item2; var currentPrice = currentPrices.Where(p => p.Symbol.ToLower() == c.Name.ToLower()).Select(p => p.Price).FirstOrDefault(); decimal percentage = Math.Round(((currentPrice - purchasePrice) / purchasePrice) * 100, 2); b.AppendLine(c.Name + ":"); b.AppendLine("\tBought at: " + purchasePrice); b.AppendLine("\tCurrent Price: " + currentPrice); b.AppendLine("\tPercent Change:" + percentage.ToString()); b.AppendLine(); } return(b.ToString()); }
public static void ExecuteTrade(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(); DateTime currentTime = DateTime.Now; if (side == OrderSide.BUY && BinanceAPIMain.TotalTrades < 4) { //Before we execute, update the allotted coins BinanceAPIMain.CalculateCoinsToTrade(client, currency, allPrices); //Orders will break if not successful, so check for try/catch block and add into currency ledger or move on. try { var order = client.PostNewOrder(currency.Name.ToLower(), currency.AllottedCoins, 0.1m, side, OrderType.MARKET, TimeInForce.IOC); Task.WaitAll(order); if (order.Result.OrderId != -1) { BinanceAPIMain.TotalTrades++; currency.InTrade = true; var finalCoinsBought = GetHeldCoins(client, currency.Name); currency.CoinsBought.Add(new Tuple <decimal, decimal>(finalCoinsBought, price)); BinanceAPIMain.WriteToLog(finalCoinsBought + ":" + currency.Name + ": go in on trade at: " + price + " Time: " + currentTime, ConsoleColor.Yellow); BinanceAPIMain.WriteToLogTrades(currency.Name + ": Buy " + finalCoinsBought + " coins at " + price + " Time: " + currentTime); BinanceAPIMain.WriteToCurrencyFile(finalCoinsBought + "|" + price, currency.Name); } } catch (OperationCanceledException oce) { BinanceAPIMain.WriteToLog(oce.Message + " while buying " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.InnerException + " while buying " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } } else if (side == OrderSide.SELL) { try { foreach (Tuple <decimal, decimal> tuplePair in currency.CoinsBought) { var order = client.PostNewOrder(currency.Name.ToLower(), tuplePair.Item1, 0.1m, side, OrderType.MARKET, TimeInForce.IOC); Task.WaitAll(order); if (order.Result.OrderId != -1) { decimal percentage = Math.Round(((price - tuplePair.Item2) / tuplePair.Item2) * 100, 2); ConsoleColor color = percentage > 0 ? ConsoleColor.White : ConsoleColor.Red; BinanceAPIMain.WriteToLogTrades(currency.Name + ": Sell " + tuplePair.Item1 + " coins at " + price + " Time: " + currentTime + " for a " + percentage + " percent change"); BinanceAPIMain.WriteToLog(currency.Name + ": sold " + tuplePair.Item1 + " coins at " + price + " on " + currentTime + Environment.NewLine + currency.Name + ": Made: " + (price - tuplePair.Item2) + " Percentage: " + percentage, color); } } BinanceAPIMain.TotalTrades--; 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 (OperationCanceledException oce) { BinanceAPIMain.WriteToLog(oce.Message + " while selling " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } catch (Exception ex) { BinanceAPIMain.WriteToLog(ex.InnerException + " while selling " + currency.Name + " " + DateTime.Now, ConsoleColor.DarkRed); } } }