Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public static void CalculateCoinsToTrade(BinanceClient client, Currency c, IEnumerable <Binance.API.Csharp.Client.Models.Market.SymbolPrice> allPrices)
        {
            decimal walletPercentage = Convert.ToDecimal(ConfigurationManager.AppSettings["ETHWalletPercentage"]);

            numETH = client.GetAccountInfo().Result.Balances.Where(p => p.Asset == "ETH").FirstOrDefault().Free;
            var ethForTrading = (walletPercentage / 100) * numETH;
            var price         = allPrices.Where(p => p.Symbol == c.Name).FirstOrDefault();

            c.AllottedCoins = Decimal.Truncate((walletPercentage / 100) * numETH * (1 / (4 - TotalTrades)) / price.Price);
        }
Ejemplo n.º 3
0
        private static decimal GetHeldCoins(BinanceClient client, string currency)
        {
            string trimmedName = currency.ToUpper().Replace("ETH", "");

            try
            {
                Binance.API.Csharp.Client.Models.Market.Balance balance = client.GetAccountInfo().Result.Balances.Where(p => p.Asset == trimmedName.ToUpper()).FirstOrDefault();
                return(decimal.Truncate(balance.Free));
            }
            catch
            {
                return(-1);
            }
        }
Ejemplo n.º 4
0
        public static void GetExistingHoldings(BinanceClient client, string baseCurrency)
        {
            //Get the currencies that we have ordered in the past
            foreach (Binance.API.Csharp.Client.Models.Market.Balance b in client.GetAccountInfo().Result.Balances.Where(p => p.Free > 0))
            {
                try
                {
                    var allOrders = client.GetTradeList(b.Asset + baseCurrency);
                    if (allOrders.Result != null)
                    {
                        foreach (var trade in allOrders.Result)
                        {
                            BinanceAPIMain.AllCurrencies.Where(p => p.Name == b.Asset + baseCurrency).FirstOrDefault().HeldPositions.Add(trade.Quantity, trade.Price);
                        }
                        BinanceAPIMain.AllCurrencies.Where(p => p.Name == b.Asset + baseCurrency).FirstOrDefault().CalculateReturn(BinanceAPIMain.AllCurrencies);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            var heldCurrencies = BinanceAPIMain.AllCurrencies.Where(x => x.HeldPositions.Count > 0);

            //Now output the data to a text file.
            using (StreamWriter wr = new StreamWriter(@"C:\Users\user\Desktop\HeldPositions.csv", true))
            {
                wr.WriteLine("Symbol, Purchase Value, Current Value, Purchase Value USD, Current Value USD, RoR");
                foreach (Currency c in heldCurrencies)
                {
                    wr.WriteLine(
                        c.Name + "," +
                        c.PurchaseValue.ToString() + "," +
                        c.CurrentValue.ToString() + "," +
                        c.PurchaseValueUSD.ToString() + "," +
                        c.CurrentValueUSD.ToString() + "," +
                        c.Return.ToString());
                }
            }
        }