public async Task <string> CreateSellOrder(Account account, int exchangeId, string baseCurrency, string quoteCurrnecy, decimal price, decimal quantity) { if (exchangeId == 0) { using (var client = new ExchangeBinanceAPI()) { client.PublicApiKey = account.ApiKey.ToSecureString(); client.PrivateApiKey = account.ApiSecret.ToSecureString(); var orderRequest = new ExchangeOrderRequest { Symbol = baseCurrency.ToUpper() + quoteCurrnecy.ToUpper(), Price = price, Amount = quantity, IsBuy = false, OrderType = ExchangeSharp.OrderType.Limit }; var result = await client.PlaceOrderAsync(orderRequest); return(result.OrderId); } } throw new ArgumentOutOfRangeException(nameof(exchangeId)); }
public override void Start(bool virtualTrading) { loggingService.Info("Start Binance Exchange service..."); binanceApi = new ExchangeBinanceAPI(); binanceApi.RateLimit = new RateGate(Config.RateLimitOccurences, TimeSpan.FromSeconds(Config.RateLimitTimeframe)); if (!virtualTrading && !String.IsNullOrWhiteSpace(Config.KeysPath)) { if (File.Exists(Config.KeysPath)) { loggingService.Info("Load keys from encrypted file..."); binanceApi.LoadAPIKeys(Config.KeysPath); } else { throw new FileNotFoundException("Keys file not found"); } } loggingService.Info("Get initial ticker values..."); tickers = new ConcurrentDictionary <string, Ticker>(binanceApi.GetTickers().Select(t => new KeyValuePair <string, Ticker>(t.Key, new Ticker { Pair = t.Key, AskPrice = t.Value.Ask, BidPrice = t.Value.Bid, LastPrice = t.Value.Last }))); lastTickersUpdate = DateTimeOffset.Now; healthCheckService.UpdateHealthCheck(Constants.HealthChecks.TickersUpdated, $"Updates: {tickers.Count}"); ConnectTickersWebsocket(); loggingService.Info("Binance Exchange service started"); }
private IDisposable StartBinanceWebsockets(bool display = true) { IExchangeAPI a = new ExchangeBinanceAPI(); var socket = a.GetTickersWebSocket((tickers) => { if (display) { Console.WriteLine("BINANCE {0,4} tickers, first: {1}", tickers.Count, tickers.First()); } //HandleTickerUpdate(a, tickers); //m_outputQ.Enqueue(new TickerOutput("BINANCE", tickers)); }); /*var osocket = a.GetCompletedOrderDetailsWebSocket((order) => * { * if (display) Console.WriteLine("BINANCE {0,4} order details, eor: {1}", 1, order); * //HandleTickerUpdate(a, tickers); * //m_outputQ.Enqueue(new TickerOutput("BINANCE", tickers)); * });*/ var tsocket = a.GetOrderBookWebSocket("BTCUSDT", (book) => { if (display) { Console.WriteLine("BINANCE {0,4} {1,4} order book, bid: {2} ask: {3}", book.Data.Bids.Count, book.Data.Asks.Count, book.Data.Bids[0], book.Data.Asks[0]); } }); return(socket); }
public CoinInfoService( ExchangeBittrexAPI bittrexApi, ExchangeBinanceAPI binanceApi, ExchangeHitbtcAPI hitbtcApi, ExchangeKucoinAPI kucoinApi, ExchangeCryptopiaAPI cryptopiaApi, MinExchangeOkexAPI okexApi, MinExchangeUpbitAPI upbitApi, MinExchangeHoubiAPI huobiApi, MinExchangeGateAPI gateApi, IMemoryCache memoryCache, IOptions <SiteSettings> options, IOptions <ExchangerEnableSettings> enableOptions) { this.binanceApi = binanceApi; this.bittrexApi = bittrexApi; this.hitbtcApi = hitbtcApi; this.kucoinApi = kucoinApi; this.cryptopiaApi = cryptopiaApi; this.memoryCache = memoryCache; this.okexApi = okexApi; this.options = options; this.gateApi = gateApi; this.huobiApi = huobiApi; this.upbitApi = upbitApi; this.enableOptions = enableOptions; }
public async Task CurrenciesParsedCorrectly() { var requestMaker = Substitute.For <IAPIRequestMaker>(); requestMaker.MakeRequestAsync(ExchangeBinanceAPI.GetCurrenciesUrl, new ExchangeBinanceAPI().BaseWebUrl).Returns(Resources.BinanceGetAllAssets); var binance = new ExchangeBinanceAPI { RequestMaker = requestMaker }; IReadOnlyDictionary <string, ExchangeCurrency> currencies = await binance.GetCurrenciesAsync(); currencies.Should().HaveCount(3); currencies.TryGetValue("bnb", out ExchangeCurrency bnb).Should().BeTrue(); bnb.DepositEnabled.Should().BeFalse(); bnb.WithdrawalEnabled.Should().BeTrue(); bnb.MinConfirmations.Should().Be(30); bnb.FullName.Should().Be("Binance Coin"); bnb.Name.Should().Be("BNB"); bnb.TxFee.Should().Be(0.23m); bnb.CoinType.Should().Be("ETH"); bnb.BaseAddress.Should().BeNullOrEmpty("api does not provide this info"); currencies.TryGetValue("NEO", out ExchangeCurrency neo).Should().BeTrue(); neo.Name.Should().Be("NEO"); neo.FullName.Should().Be("NEO"); neo.DepositEnabled.Should().BeTrue(); neo.WithdrawalEnabled.Should().BeFalse(); neo.TxFee.Should().Be(0); neo.MinConfirmations.Should().Be(5); neo.CoinType.Should().Be("NEO"); }
protected override ExchangeAPI InitializeApi() { var binanceApi = new ExchangeBinanceAPI { RateLimit = new RateGate(this.Config.RateLimitOccurences, TimeSpan.FromSeconds(this.Config.RateLimitTimeframe)) }; return(binanceApi); }
public CBinance() : base() { API_KEY = Properties.Settings.Default.BINANCE_API_KEY; API_SECRET = Properties.Settings.Default.BINANCE_API_SECRET; api = new ExchangeBinanceAPI(); api.LoadAPIKeysUnsecure(API_KEY, API_SECRET); Name = api.Name; getAccounts(); }
public Binance() { ExchangeAPI.UseDefaultMethodCachePolicy = false; _client = new ExchangeBinanceAPI(); Orderbooks = new Dictionary <string, Orderbook>(); Currencies = new Dictionary <string, CurrencyData>(); this.IsAuthenticated = false; //TODO: Pull from a DB for this }
public async Task <Order> GetOrder(Account account, int exchangeId, string orderId, string baseCurrency = null, string quoteCurrency = null) { if (exchangeId == 0) { if (baseCurrency == null || quoteCurrency == null) { throw new ArgumentNullException("Base currency and quote currency should be provided when getting orders from Binance exchange."); } using (var client = new BinanceClient()) { //Get fees and created date. decimal fees = 0; DateTime dateCreated; using (var sharpClient = new ExchangeBinanceAPI()) { sharpClient.PublicApiKey = account.ApiKey.ToSecureString(); sharpClient.PrivateApiKey = account.ApiSecret.ToSecureString(); var sharpResult = await sharpClient.GetOrderDetailsAsync(orderId, baseCurrency.ToUpper() + quoteCurrency.ToUpper()); fees = sharpResult.Fees; dateCreated = sharpResult.OrderDate; } client.SetApiCredentials(account.ApiKey, account.ApiSecret); var result = await client.QueryOrderAsync(baseCurrency.ToUpper() + quoteCurrency.ToUpper(), long.Parse(orderId)); var order = result.Data; Domain.Model.Markets.OrderStatus orderStatus = this.GetStatusFromBiannceNet(order.Status); //Calculate the average price. var averageExecutedPrice = order.CummulativeQuoteQuantity / order.ExecutedQuantity; return(new Order( exchangeId, order.OrderId.ToString(), order.Side == Binance.Net.Objects.OrderSide.Buy ? Domain.Model.Markets.OrderType.BUY_LIMIT : Domain.Model.Markets.OrderType.SELL_LIMIT, orderStatus, baseCurrency, quoteCurrency, averageExecutedPrice, order.OriginalQuantity, order.ExecutedQuantity, fees, dateCreated )); } } throw new ArgumentOutOfRangeException(nameof(exchangeId)); }
public BinanceConnector() { api = new ExchangeBinanceAPI(); var symbols = api.GetSymbols(); Debug.Print("{0}: ", api.Name); foreach (var s in symbols) { // Debug.Print("\t{0}", s); } base.Register(this); }
public IDisposable TestWebsocketsBinance(bool display = false) { IExchangeAPI a = new ExchangeBinanceAPI(); var socket = a.GetTickersWebSocket((tickers) => { if (display) { Console.WriteLine("BINANCE {0,4} tickers, first: {1}", tickers.Count, tickers.First()); } //HandleTickerUpdate(a, tickers); m_outputQ.Enqueue(new TickerOutput("BINANCE", tickers)); }); return(socket); }
public async Task <OrderBook> GetMarketData(int exchangeId, string baseCurrency, string quoteCurrency) { if (exchangeId == 0) { using (var client = new ExchangeBinanceAPI()) { var result = await client.GetOrderBookAsync(baseCurrency.ToUpper() + quoteCurrency.ToUpper(), 50); return(result.ToOrderBook(baseCurrency, quoteCurrency)); } } throw new ArgumentOutOfRangeException(nameof(exchangeId)); }
public void TestWebsocketsBinance() { // create a web socket connection to the exchange. Note you can Dispose the socket anytime to shut it down. // the web socket will handle disconnects and attempt to re-connect automatically. IExchangeAPI b = new ExchangeBinanceAPI(); using (var socket = b.GetTickersWebSocket((tickers) => { Console.WriteLine("{0} tickers, first: {1}", tickers.Count, tickers.First()); })) { Console.WriteLine("Press ENTER to shutdown."); Console.ReadLine(); } }
public async Task <IEnumerable <Order> > GetOpenOrders(Account account, int exchangeId) { if (exchangeId == 0) { using (var client = new ExchangeBinanceAPI()) { client.PublicApiKey = account.ApiKey.ToSecureString(); client.PrivateApiKey = account.ApiSecret.ToSecureString(); var result = await client.GetOpenOrderDetailsAsync(); return(result.ToOpenOrders(exchangeId)); } } throw new ArgumentOutOfRangeException(nameof(exchangeId)); }
public IDisposable StartWebsocketsBinance(bool display = false) { Console.WriteLine("\nLAUNCH: Binance\n"); IExchangeAPI a = new ExchangeBinanceAPI(); int count = 0; var socket = a.GetTickersWebSocket((tickers) => { //if (display) Console.WriteLine("BINANCE {0,4} tickers, first: {1}", tickers.Count, tickers.First()); if (count++ % 5 == 0) { Console.Write("."); } HandleTickerUpdate(a, tickers); //m_outputQ.Enqueue(new TickerOutput("BINANCE", tickers)); }); return(socket); }
public async Task <bool> CancelOrder(Account account, int exchangeId, string orderId, string baseCurrency = null, string quoteCurrency = null) { if (exchangeId == 0) { using (var client = new ExchangeBinanceAPI()) { client.PublicApiKey = account.ApiKey.ToSecureString(); client.PrivateApiKey = account.ApiSecret.ToSecureString(); await client.CancelOrderAsync(orderId, baseCurrency.ToUpper() + quoteCurrency.ToUpper()); var openOrders = await this.GetOpenOrders(account, exchangeId); bool isSuccess = !openOrders.Any(o => o.OrderId == orderId); return(isSuccess); } } throw new ArgumentOutOfRangeException(nameof(exchangeId)); }
public IDisposable StartWebsocketsBinance(bool display = false) { IExchangeAPI a = new ExchangeBinanceAPI(); var socket = a.GetTickersWebSocket((tickers) => { if (display) { Console.WriteLine("BINANCE {0,4} tickers, first: {1}", tickers.Count, tickers.First()); } HandleTickerUpdate("BINANCE", tickers); //m_outputQ.Enqueue(new TickerOutput("BINANCE", tickers)); if (tickers.Select(s => s.Key).Contains("BTCUSDT")) { //Console.WriteLine("BINANCE"); if (TickersExistForAllExchanges) { DisplaySpreads("BINANCE"); } } }); return(socket); }