public BinanceAdapter(SubscriptionManager subMgr) { _subMgr = subMgr; string BINANCE_API_KEY = Environment.GetEnvironmentVariable("BINANCE_API_KEY"); string BINANCE_SECRET = Environment.GetEnvironmentVariable("BINANCE_SECRET"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; BinanceSocketClient.SetDefaultOptions(new BinanceSocketClientOptions() { ApiCredentials = new CryptoExchange.Net.Authentication.ApiCredentials(BINANCE_API_KEY, BINANCE_SECRET), // LogVerbosity = CryptoExchange.Net.Logging.LogVerbosity.Debug, // LogWriters = { Console.Out } }); BinanceClient.SetDefaultOptions(new BinanceClientOptions() { ApiCredentials = new CryptoExchange.Net.Authentication.ApiCredentials(BINANCE_API_KEY, BINANCE_SECRET), LogVerbosity = CryptoExchange.Net.Logging.LogVerbosity.Debug, LogWriters = { Console.Out } }); socketClient = new BinanceSocketClient(); _exchangeInfo = QueryExchangeInfo(); _drift = QueryDrift(); }
public decimal GetFilteredSellQuantity(BinanceClient client, string symbol, decimal quantity) { decimal retQuantity = 0; try { BinanceExchangeInfo exchangeInfo = GetExchangeInfo(client); if (exchangeInfo != null) { var symbolData = exchangeInfo.Symbols.Where(s => s.SymbolName == symbol).FirstOrDefault(); if (symbolData != null) { BinanceSymbolLotSizeFilter lotSizeFilter = (BinanceSymbolLotSizeFilter)symbolData.Filters.Where(f => f.FilterType == SymbolFilterType.LotSize).FirstOrDefault(); retQuantity = quantity - (quantity % lotSizeFilter.StepSize); retQuantity = Math.Round(retQuantity, 20); var isValidQuantity = (retQuantity >= lotSizeFilter.MinQuantity && retQuantity <= lotSizeFilter.MaxQuantity); if (!isValidQuantity) { return(0); } } } } catch (Exception ex) { _logger.LogException(ex); } return(retQuantity); }
private BinanceExchangeInfo QueryExchangeInfo() { using (var client = new BinanceClient()) { CallResult <BinanceExchangeInfo> info = client.GetExchangeInfo(); if (info.Success) { _exchangeInfo = info.Data; var symbols = new List <string>(_exchangeInfo.Symbols.Length); foreach (BinanceSymbol symbol in _exchangeInfo.Symbols) { symbols.Add(symbol.Name); PreCacheResult(BINANCE, symbol.Name, RtdFields.BASE_ASSET, symbol.BaseAsset); PreCacheResult(BINANCE, symbol.Name, RtdFields.BASE_ASSET_PRECISION, symbol.BaseAssetPrecision); //PreCacheResult(BINANCE, symbol.Name, RtdFields.FILTERS, MakeStringArray(symbol.Filters)); PreCacheResult(BINANCE, symbol.Name, RtdFields.ICEBERG_ALLOWED, symbol.IceBergAllowed); PreCacheResult(BINANCE, symbol.Name, RtdFields.NAME, symbol.Name); PreCacheResult(BINANCE, symbol.Name, RtdFields.ORDER_TYPES, MakeStringArray(symbol.OrderTypes)); PreCacheResult(BINANCE, symbol.Name, RtdFields.QUOTE_ASSET, symbol.QuoteAsset); PreCacheResult(BINANCE, symbol.Name, RtdFields.QUOTE_ASSET_PRECISION, symbol.QuoteAssetPrecision); PreCacheResult(BINANCE, symbol.Name, RtdFields.STATUS, symbol.Status); } PreCacheResult(BINANCE, String.Empty, RtdFields.EXCHANGE_SYMBOLS, MakeStringArray(symbols)); return(_exchangeInfo); } else { Console.Out.WriteLine(info.Error); return(null); } } }
public List <BinanceSymbol> GetAllSymbols() { SaveableBindingList <BinanceSymbol> result = new SaveableBindingList <BinanceSymbol>(); BinanceExchangeInfo exchangeInfo = client.Spot.System.GetExchangeInfo().Data; foreach (BinanceSymbol symbol in exchangeInfo.Symbols) { result.Add(symbol); } return(result.ToList()); }
public List <BinanceSymbol> GetAllSymbols() { if (!File.Exists(Symbols.LogFile)) { BinanceExchangeInfo exchangeInfo = client.Spot.System.GetExchangeInfo().Data; foreach (BinanceSymbol symbol in exchangeInfo.Symbols) { Symbols.Add(symbol); } } return(Symbols.ReadAll()); }
public async Task <BinanceSymbol> GetSymbolInfo(string symbol) { if (_exchangeInfo == null) { var result = await _client.GetExchangeInfoAsync(); if (!result.Success) { throw new Exception(result.Error.Message); } _exchangeInfo = result.Data; } return(_exchangeInfo.Symbols.FirstOrDefault(x => x.SymbolName == symbol)); }
public BinanceExchangeInfo GetExchangeInfo(BinanceClient client) { BinanceExchangeInfo exchangeInfo = new BinanceExchangeInfo(); try { var exchangeData = client.GetExchangeInfo(); if (exchangeData.Error != null) { _logger.LogError(exchangeData.Error.Message); } else { exchangeInfo = exchangeData.Data; } } catch (Exception ex) { _logger.LogException(ex); } return(exchangeInfo); }
//private Trades GetPairBySmartStrategy(BinanceClient client) { // Trades pairSmart = new Trades(); // decimal maxPercent = 25, minPercent = 1, minVolume = 125; //filter criteria // try { // List<Binance24HPrice> lstBinanceHistoricDetails = new List<Binance24HPrice>(); // var pairsList = GetSymbolsByBaseCurrency(client); // foreach (BinancePrice pair in pairsList) { // Binance24HPrice pairs24hr = Get24hrHistory(client, pair.Symbol); // if (pairs24hr != null) { // lstBinanceHistoricDetails.Add(pairs24hr); // } // } // var bestPair = lstBinanceHistoricDetails.OrderByDescending(d => d.PriceChangePercent); // if (bestPair != null) { // //check for smart stretegy criterias.. // if (pairs24hr.PriceChangePercent < maxPercent && pairs24hr.PriceChangePercent >= minPercent && pairs24hr.QuoteVolume > minVolume) { // //check if pair isn't already added or max limit exceeded for openTrades.. // if (!IsMaxPairExistsInOpenTrades(pairs24hr.Symbol)) { // //check for valid quantity filters for binance.. // decimal validQuantity = GetFilteredBuyQuantity(client, pair.Symbol, pair.Price); // var expectedBuyPrice = GetBuyPrice(pairs24hr.WeightedAveragePrice); // if (validQuantity > 0 && pairs24hr.AskPrice <= expectedBuyPrice) { //Bid/Buy price must be <= expected buy price // } // } // } // pairSmart.Symbol = bestPair.Symbol; // pairSmart.Quantity = validQuantity; // pairSmart.BuyPrice = expectedBuyPrice; // _logger.LogInfoMessage("Symbol: " + bestPair.Symbol + " ::> 24hr Change: " + pairs24hr.PriceChangePercent + " ::> Buy Price: " + expectedBuyPrice + " ::> Qty: " + validQuantity); // return pairSmart; // } // } catch (Exception ex) { // _logger.LogException(ex); // } // return pairSmart; //} public decimal GetFilteredBuyQuantity(BinanceClient client, string symbol, decimal price) { decimal retQuantity = 0; try { BinanceExchangeInfo exchangeInfo = GetExchangeInfo(client); if (exchangeInfo != null) { var symbolData = exchangeInfo.Symbols.Where(s => s.SymbolName == symbol).FirstOrDefault(); if (symbolData != null) { decimal tradeLimit = Convert.ToDecimal(dbHelper.GetSettingByKey("TradingLimitPerPair")); decimal originalQuantity = tradeLimit / price; BinanceSymbolPriceFilter priceFilter = (BinanceSymbolPriceFilter)symbolData.Filters.Where(f => f.FilterType == SymbolFilterType.PriceFilter).FirstOrDefault(); BinanceSymbolLotSizeFilter lotSizeFilter = (BinanceSymbolLotSizeFilter)symbolData.Filters.Where(f => f.FilterType == SymbolFilterType.LotSize).FirstOrDefault(); BinanceSymbolMinNotionalFilter minNotionalFilter = (BinanceSymbolMinNotionalFilter)symbolData.Filters.Where(f => f.FilterType == SymbolFilterType.MinNotional).FirstOrDefault(); retQuantity = originalQuantity - (originalQuantity % lotSizeFilter.StepSize); retQuantity = Math.Round(retQuantity, 20); var isValidQuantity = (retQuantity >= lotSizeFilter.MinQuantity && retQuantity <= lotSizeFilter.MaxQuantity); if (!isValidQuantity) { return(0); //quantity doesn't match for the min and max criteria for buy/sell } else { decimal totalPrice = retQuantity * price; totalPrice = totalPrice - (totalPrice % priceFilter.TickSize); var isValidMinNotional = (totalPrice >= minNotionalFilter.MinNotional); if (!isValidMinNotional) { return(0); //quantity total price doesn't match for the min notional } } } } } catch (Exception ex) { _logger.LogException(ex); } return(retQuantity); }