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); } } }
public static decimal?GetLastBuyOrderPrice() { decimal?lastPrice = null; bool mustContinue = true; while (mustContinue) { mustContinue = false; var hasException = false; MyThread.StartWithThread(() => { hasException = _getLastBuyOrderPrice(out lastPrice); }, _waitingForApiAnswer, $"Get Last Buy OrderPrice"); if (hasException) { mustContinue = true; MyConsole.WriteLine_Error($"\nThere is some error on GetLastBuyOrderPrice.\n I will recheck on {_waitingAfterApiError} seconds !!!\n"); MyThread.GenerateConsoleWaiting(_waitingAfterApiError); } } return(lastPrice); }
public static void GetBalance(out decimal usdt, out decimal btc) { usdt = 0; btc = 0; decimal myUsdt = 0; decimal myBtc = 0; bool mustContinue = true; while (mustContinue) { mustContinue = false; var hasException = false; MyThread.StartWithThread(() => { hasException = _getBalance(out myUsdt, out myBtc); }, _waitingForApiAnswer, "Get My Binance Balance"); if (hasException) { mustContinue = true; MyConsole.WriteLine_Error($"\nThere is some error on Get Binance Balance.\n I will recheck on {_waitingAfterApiError} second !!!\n"); MyThread.GenerateConsoleWaiting(_waitingAfterApiError); } usdt = myUsdt; btc = myBtc; } }
public static decimal?PlaceOrder(string type, decimal value) { decimal?orderPrice = null; bool mustContinue = true; while (mustContinue) { mustContinue = false; var hasException = false; MyThread.StartWithThread(() => { hasException = _placeOrder(type, value, out orderPrice); }, _waitingForApiAnswer, $"Place Bainance Order type:{type} value:{value}"); if (hasException) { mustContinue = true; MyConsole.WriteLine_Error("\nThere is some error on Place Order.\n I will recheck on {_waitingAfterApiError} seconds !!!\n"); MyThread.GenerateConsoleWaiting(_waitingAfterApiError); } } return(orderPrice); }
public static void StartWithThread(System.Threading.ThreadStart s, int timeout_second, string message) { var performed = false; while (!performed) { var tr = new System.Threading.Thread(s); try { tr.Start(); var co = 1; while (co < timeout_second + 1) { System.Threading.Thread.Sleep(1000); if (co > 1) { Console.SetCursorPosition(0, Console.CursorTop - 1); } Console.WriteLine($"{message} {co} second"); if (tr.ThreadState == ThreadState.Stopped) { performed = true; break; } co++; } if (!performed) { tr.Interrupt(); MyConsole.WriteLine_Error($"{message} not ended on {timeout_second} seconds - restarting on 10 seconds -------\n"); MyThread.GenerateConsoleWaiting(10); } if (performed) { MyConsole.WriteLine_Succ(message + $" Ended in {co} seconds !!!"); } } catch (Exception ex) { MyConsole.WriteLine_Exception("error in executing " + message, ex); throw new Exception(ex.Message); } } }
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); } }
internal static DateTime?CheckBuyEmail(string type, int validMinuteDuration, double recheckEmail_second, DateTime startTime) { var validSecondDuration = validMinuteDuration * 60; var searchSubject = "Alert: buy signal!!!"; var tokenAddress = "token_buy.json"; var credentialFile = "gmail_Buy_cridential.json"; if (type == "sell") { searchSubject = "Alert: sell signal!!!"; tokenAddress = "token_sell.json"; credentialFile = "gmail_Sell_cridential.json"; } DateTime?lastMessageDate = null; var co = 1; var maxChack = MyConfig.Config.numberOfEmailCheck ?? 5; bool mustContinue = true; while (mustContinue && co <= maxChack) { MyConsole.WriteLine_Info($"\n\nChecking Email {co} of {maxChack} \n\n"); mustContinue = false; var hasException = false; MyThread.StartWithThread(() => { hasException = checkEmail(credentialFile, tokenAddress, searchSubject, out lastMessageDate); }, 60, "Check Gmail"); if (hasException) { mustContinue = true; MyConsole.WriteLine_Error("\nThere is some error on Check Gmail.\n I will recheck on 60 second !!!\n"); MyThread.GenerateConsoleWaiting(60); } if (!hasException && !lastMessageDate.HasValue) { //کلا ایمیلی یافت نشد mustContinue = true; MyConsole.WriteLine_Info($"Dont Found any Email. Recheck on {recheckEmail_second} seconds "); MyThread.GenerateConsoleWaiting(recheckEmail_second); } if (!hasException && lastMessageDate.HasValue) { var a = DateTime.Now.Subtract(lastMessageDate.Value).TotalSeconds; if (a > validSecondDuration) { mustContinue = true; MyConsole.WriteLine_Error($"\nEmail time is {lastMessageDate} and Expired. More than {validSecondDuration} seconds have elapsed since the email arrived {a}\n"); MyConsole.WriteLine_Info($"Recheck on {recheckEmail_second} seconds"); MyThread.GenerateConsoleWaiting(recheckEmail_second); } else { if (lastMessageDate.Value > startTime) { return(lastMessageDate.Value); } MyConsole.WriteLine_Error($"\n\nEmail after start. start:{startTime} emailTime:{lastMessageDate.Value}. I wait {recheckEmail_second} for another one.\n\n"); MyThread.GenerateConsoleWaiting(recheckEmail_second); mustContinue = true; } } co++; } return(null); }