public static void CheckThereIsNotOpenOrders() { bool hasOpenOrders = true; while (hasOpenOrders) { var hasException = false; MyThread.StartWithThread(() => { hasException = MyBinance._checkThereIsNotOpenOrders(out hasOpenOrders); }, _waitingForApiAnswer, "Get Binance Open Orders"); if (!hasException && hasOpenOrders) { MyConsole.WriteLine_Error($"\n\nThere is open orders. plaese close them to start.\n I will rechech on 120 seconds !!!!\n"); MyThread.GenerateConsoleWaiting(120); } if (hasException) { hasOpenOrders = true; MyConsole.WriteLine_Error($"\nThere is some error on Get Binance Open Orders.\n I will recheck on {_waitingAfterApiError} seconds !!!\n"); MyThread.GenerateConsoleWaiting(_waitingAfterApiError); } } }
private static bool _getLastBuyOrderPrice(out decimal?lastPrice) { lastPrice = null; try { BinanceClient binanceClient = MyBinance.CreateBinanceClient(); var lastTrad = binanceClient.Spot.Order.GetMyTrades(MyConfig.Config.currency_name, null, null, 1); if (!lastTrad.Success) { MyConsole.WriteLine_Error("\n\nGet Last Buy Order Price Error: error on GetMyTrades api service - " + lastTrad.Error.Message + "\n\n"); return(true); } foreach (Binance.Net.Objects.Spot.SpotData.BinanceTrade item in lastTrad.Data) { if (!item.IsBuyer) { MyConsole.WriteLine_Error("Last Trade was sell not buy. We use current price as 'Last buy price'"); return(false); } lastPrice = item.Price; } return(false); } catch (Exception ex) { MyConsole.WriteLine_Exception("GetLastBuyOrderPrice", ex); return(true); } }
private static bool _getPrice(out decimal?price) { price = null; try { BinanceClient binanceClient = MyBinance.CreateBinanceClient(); var answer = binanceClient.Spot.Market.GetBookPrice(MyConfig.Config.currency_name); if (!answer.Success) { MyConsole.WriteLine_Error("\n\nGet Price Error: " + answer.Error.Message + "\n\n"); return(true); } var p = (Binance.Net.Objects.Spot.MarketData.BinanceBookPrice)answer.Data; price = p.BestBidPrice; return(false); } catch (Exception ex) { MyConsole.WriteLine_Exception("Get price", ex); return(true); } }
static bool _placeOrder(string type, decimal value, out decimal?orderPrice) { string currencyName = MyConfig.Config.currency_name; orderPrice = null; try { BinanceClient binanceClient = MyBinance.CreateBinanceClient(); if (type == "buy") { value = _calValue(value, MyConfig.Config.currency_second__USDT_number_of_zero_after_point); var answer = binanceClient.Spot.Order.PlaceOrder(currencyName, Binance.Net.Enums.OrderSide.Buy, Binance.Net.Enums.OrderType.Market, null, value); if (!answer.Success) { MyConsole.WriteLine_Error("\n\nPlace Order Buy Error: " + answer.Error.Message + "\n\n"); return(true); } orderPrice = answer.Data.Price; } else { value = _calValue(value, MyConfig.Config.currency_base__BTC_number_of_zero_after_point); var answer = binanceClient.Spot.Order.PlaceOrder(currencyName, Binance.Net.Enums.OrderSide.Sell, Binance.Net.Enums.OrderType.Market, value); if (!answer.Success) { MyConsole.WriteLine_Error("\n\nPlace Order Sell Error: " + answer.Error.Message + "\n\n"); return(true); } orderPrice = answer.Data.Price; } return(false); } catch (Exception ex) { MyConsole.WriteLine_Exception("Place Binance Order", ex); return(true); } }
private static bool _getBalance(out decimal usdt, out decimal btc) { string baseCurrency = MyConfig.Config.currency_base__BTC; string secondCurrency = MyConfig.Config.currency_second__USDT; usdt = 0; btc = 0; try { BinanceClient binanceClient = MyBinance.CreateBinanceClient(); var acc = binanceClient.General.GetAccountInfo(); if (!acc.Success) { MyConsole.WriteLine_Error("\n\nGetBalance Error: " + acc.Error.Message + "\n\n"); return(true); } var assets = acc.Data.Balances; foreach (var item in assets) { if (item.Asset == secondCurrency) { usdt = item.Total; } if (item.Asset == baseCurrency) { btc = item.Total; } } return(false); } catch (Exception ex) { MyConsole.WriteLine_Exception("Get Binance Balance", ex); return(true); } }
private static bool _checkThereIsNotOpenOrders(out bool hasOpenOrders) { hasOpenOrders = false; try { BinanceClient binanceClient = MyBinance.CreateBinanceClient(); var orders = binanceClient.Spot.Order.GetOpenOrders(); if (!orders.Success) { MyConsole.WriteLine_Error("\n\nGetOpenOrders Error: " + orders.Error.Message + "\n\n"); return(true); } var ans = orders.Data; foreach (Binance.Net.Objects.Spot.SpotData.BinanceOrder order in ans) { if (order.Symbol == MyConfig.Config.currency_name || order.Symbol == MyConfig.Config.currency_base__BTC || order.Symbol == MyConfig.Config.currency_second__USDT) { hasOpenOrders = true; return(false); } } return(false); } catch (Exception ex) { MyConsole.WriteLine_Exception(nameof(CheckThereIsNotOpenOrders), ex); return(true); } }
static void start() { MyConfig.LoadJson(); var validEmailTimeInMinute = MyConfig.Config.validDuration_in_minute; var CheckEmailDurationInSecond = MyConfig.Config.gmailCheckPeriod_in_second; DateTime startTime = DateTime.Now; decimal minBTC = MyConfig.Config.currency_base__BTC_minimum_order; decimal minUSDT = MyConfig.Config.currency_second__USDT_minimum_order; MyConsole.WriteLine_Info("\nStart on " + DateTime.Now + "--------------\n"); MyBinance.CheckThereIsNotOpenOrders(); decimal USDT = 0; decimal BTC = 0; MyBinance.GetBalance(out USDT, out BTC); MyConsole.WriteLine_Info($"Wallet Balance=> {MyConfig.Config.currency_second__USDT}: {USDT} {MyConfig.Config.currency_base__BTC}: {BTC}"); if (USDT >= minUSDT) { MyConsole.WriteLine_Info("I going to buy ....."); var ans = MyGmail.CheckBuyEmail("buy", validEmailTimeInMinute, CheckEmailDurationInSecond, startTime); if (ans.HasValue) { decimal?buyPrice = MyBinance.PlaceOrder("buy", USDT); if (buyPrice.HasValue) { lastBuyPrice = buyPrice; } } } if (BTC >= minBTC) { MyConsole.WriteLine_Info("I going to sell ......"); var ans = MyGmail.CheckBuyEmail("sell", validEmailTimeInMinute, CheckEmailDurationInSecond, startTime); if (ans.HasValue) { decimal?sellPrice = MyBinance.PlaceOrder("sell", BTC); } } // // if (BTC >= minBTC) // // { // // MyConsole.WriteLine_Info("I going to sell by checking price .........."); // // if (!lastBuyPrice.HasValue) lastBuyPrice = MyBinance.GetLastBuyOrderPrice(); // // if (!lastBuyPrice.HasValue) lastBuyPrice = MyBinance.GetPrice(); // // MyConsole.WriteLine_Info($"\n\n'Last Buy Price' is: {lastBuyPrice}\n\n"); // // var selled = false; // // while (!selled) // // { // // var price = MyBinance.GetPrice(); // // MyConsole.WriteLine_Info($"\n\nLastBuyPrice: {lastBuyPrice} Current price: {price}\n\n"); // // var growPercent = MyConfig.Config.priceGrowthPercent_comparedWithBasePrice_forChangeBasePrice; // // if (price >= lastBuyPrice + (lastBuyPrice.Value * growPercent)) // // { // // MyConsole.WriteLine_Info($"\n\n{growPercent}% grow. I change base price to {price}. Have a good Trade ...\n\n"); // // lastBuyPrice = price; // // } // // else if (price <= lastBuyPrice - (lastBuyPrice.Value * MyConfig.Config.priceFallPercent_comparedWithBasePrice_forSell)) // // { // // MyConsole.WriteLine_Info($"\n\nLast Buy Price is {lastBuyPrice} current price is {price}. Must sell now\n\n"); // // decimal? sellPrice = MyBinance.PlaceOrder("sell", BTC); // // selled = true; // // } // // else // // { // // MyConsole.WriteLine_Info($"We are Monitoring price. Check price after 2 seconds"); // // MyThread.GenerateConsoleWaiting(2); // // } // // } // // // var ans = MyGmail.CheckBuyEmail("sell", validEmailTimeInMinute, CheckEmailDurationInSecond, startTime); // // // if (ans.HasValue) // // // { // // // decimal? sellPrice = MyBinance.PlaceOrder("sell", BTC); // // // } // // } if (BTC < minBTC && USDT < minUSDT) { MyConsole.WriteLine_Error("\n\nErrore - Please Check your balance. Cannot buy or sell."); MyConsole.WriteLine_Error($"Must have min {minUSDT} USDT for Buy or min {minBTC} BTC for Sell"); MyConsole.WriteLine_Error("\n\nRecheck on 1 Minute."); MyThread.GenerateConsoleWaiting(60); } }