public static void SellAllHoldings(List <Currency> allCurrencies, BinanceClient client) { try { foreach (Currency currency in allCurrencies) { foreach (Tuple <decimal, decimal> tuplePair in currency.CoinsBought) { try { // NewOrder order = client.PostNewOrder(currency.Name, tuplePair.Item1, 0.1m, OrderSide.SELL, OrderType.MARKET, TimeInForce.IOC).Result; // if (order.OrderId != -1) // { BinanceAPIMain.WriteToLogTrades(currency.Name + "|sold " + tuplePair.Item1 + "|coins on" + DateTime.Now.ToString()); BinanceAPIMain.WriteToLog(currency.Name + ": sold " + tuplePair.Item1 + " coins on " + DateTime.Now.ToString(), ConsoleColor.White); // } } catch { } } currency.CoinsBought.Clear(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
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 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); } } }