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); } }