Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            var api = new BinanceApi();

            if (await api.PingAsync())
            {
                Console.WriteLine("SUCCESSFUL!");
            }

            Console.WriteLine("...press any key to continue.");
            Console.ReadKey(true);
        }
        public static async Task ExampleMain(string[] args)
        {
            // Initialize with default services and no logger.
            var api = new BinanceApi();

            if (await api.PingAsync())
            {
                Console.WriteLine("SUCCESSFUL!");
            }

            Console.WriteLine("...press any key to continue.");
            Console.ReadKey(true);
        }
Exemplo n.º 3
0
        private static async Task RestAPI()
        {
            var api = new BinanceApi();

            if (await api.PingAsync())
            {
                Console.WriteLine("Successful!");
            }
            else
            {
                Console.WriteLine("Fail!");
            }


            var apiKey    = Environment.GetEnvironmentVariable("API_KEY");
            var apiSecret = Environment.GetEnvironmentVariable("API_SECRET");

            using var user = new BinanceApiUser(apiKey, apiSecret);


            //await Symbol.UpdateCacheAsync(api);


            //Console.WriteLine(Symbol.Cache.GetAll().Count());
            //Console.WriteLine(Symbol.Cache.Get("BTCUSD"));
            //Console.WriteLine(Symbol.Cache.Get("BTCEUR"));
            //Console.WriteLine($"{Symbol.Cache.Get("BTCUSDT")}  {Symbol.Cache.Get("BTCUSDT")?.IsMarginTradingAllowed}");


            try
            {
                var clientOrderId = "my_2";

                //var clientOrder = new MarketOrder(user)
                //{
                //    Symbol = Symbol.XLM_USDT,
                //    Side = OrderSide.Sell,
                //    Quantity = 90m,
                //    Id = clientOrderId
                //};
                //var conf = await api.PlaceMarginMarketAsync(clientOrder, true);
                //Console.WriteLine(JsonConvert.SerializeObject(conf, Formatting.Indented));


                var order = await api.GetMarginOrderByClientIdAsync(user, Symbol.XLM_USDT, clientOrderId);

                Console.WriteLine(JsonConvert.SerializeObject(order, Formatting.Indented));

                //var xlmMaxBorrow = api.GetMaxBorrowAsync(user, "XLM");
                //var usdMaxBorrow = api.GetMaxBorrowAsync(user, "USDT");
                //var btcMaxBorrow = api.GetMaxBorrowAsync(user, "BTC");
                //await Task.WhenAll(new[] {xlmMaxBorrow, usdMaxBorrow, btcMaxBorrow});

                //Console.WriteLine($"Xlm: {xlmMaxBorrow.Result}");
                //Console.WriteLine($"usd: {usdMaxBorrow.Result}");
                //Console.WriteLine($"usd: {btcMaxBorrow.Result}");


                //var pairs = await api.GetMarginPairsAsync(user);
                //Console.WriteLine(JsonConvert.SerializeObject(pairs, Formatting.Indented));
            }
            catch (BinanceRequestRateLimitExceededException ex)
            {
                Console.WriteLine(
                    $"TEST Order HTTP Failed: Status: {ex.StatusCode}, Code: {ex.ErrorCode}, Message: {ex.ErrorMessage}");
            }
            catch (BinanceHttpException ex)
            {
                Console.WriteLine(
                    $"TEST Order HTTP Failed: Status: {ex.StatusCode}, Code: {ex.ErrorCode}, Message: {ex.ErrorMessage}");
            }
            catch (BinanceApiException e)
            {
                Console.WriteLine($"TEST Order Failed: \"{e.Message}\"");
            }

            catch (Exception e)
            {
                Console.WriteLine($"TEST Order Exception: \"{e.Message}\"");
            }
        }
Exemplo n.º 4
0
        public async Task <bool> Ping()
        {
            var online = await _binanceClient.PingAsync();

            return(online);
        }
Exemplo n.º 5
0
        public override Boolean Initialize()
        {
            m_MessagePump.Signal(Resources.ExchangeInitialization);

            try
            {
                BinanceStatus status = m_Api.GetSystemStatusAsync().Result;

                if (status == BinanceStatus.Maintenance)
                {
                    throw new Exception(Resources.ServerMaintenance);
                }

                TimeSpan delta = TimeSpan.Zero;

                for (Int32 i = 0; i < 5; ++i)
                {
                    DateTime now    = DateTime.Now;
                    Boolean  result = m_Api.PingAsync().Result;

                    if (!result)
                    {
                        throw new Exception(Resources.ServerPing);
                    }

                    delta += DateTime.Now - now;
                }

                m_MessagePump.Signal(Utilities.FormatMessage(Resources.MessageConnectivityOK, delta.Milliseconds / 5));
            }
            catch (Exception e)
            {
                m_MessagePump.Signal(Utilities.FormatMessage(Resources.ConnectivityKO, Utilities.GetExceptionMessage(e)));
                return(false);
            }

            try
            {
                m_User = new BinanceApiUser(Config.KeyApi, Config.KeySecret);
                m_MessagePump.Signal(Resources.LoginOK);
            }
            catch (Exception e)
            {
                m_MessagePump.Signal(Utilities.FormatMessage(Resources.LoginKO, Utilities.GetExceptionMessage(e)));
                return(false);
            }

            try
            {
                UpdateBalances(m_Api.GetAccountInfoAsync(m_User).Result);

                m_UserDataWebSocket = new UserDataWebSocketManager();
                m_UserDataWebSocket.SubscribeAsync <AccountUpdateEventArgs>(m_User, x => UpdateBalances(x.AccountInfo)).Wait();
                m_UserDataWebSocket.WaitUntilWebSocketOpenAsync().Wait();

                if (m_Balances.Count == 0)
                {
                    m_MessagePump.Signal(Resources.BalancesOK0);
                }
                else
                {
                    m_MessagePump.Signal(Utilities.FormatMessage(Resources.BalancesKO, m_Balances.Count, (m_Balances.Count == 1 ? String.Empty : "s")));

                    foreach (KeyValuePair <String, Decimal> balance in m_Balances.OrderBy(x => x.Key))
                    {
                        m_MessagePump.Signal($" - {balance.Value:F8} {balance.Key}");
                    }
                }
            }
            catch (Exception e)
            {
                m_MessagePump.Signal(Utilities.FormatMessage(Resources.BalancesKO, Utilities.GetExceptionMessage(e)));
                return(false);
            }

            try
            {
                Dictionary <String, Int32> symbolsData = new Dictionary <String, Int32>();
                HashSet <String>           assets      = new HashSet <String>();

                foreach (Symbol symbol in m_Api.GetSymbolsAsync().Result)
                {
                    if (symbol.Status != SymbolStatus.Trading)
                    {
                        continue;
                    }

                    String baseAsset  = symbol.BaseAsset;
                    String quoteAsset = symbol.BaseAsset;
                    String name       = symbol.ToString();

                    if ((Config.TradingWhitelist.Count > 0) && (!Config.TradingWhitelist.Contains(baseAsset) || !Config.TradingWhitelist.Contains(quoteAsset)))
                    {
                        continue;
                    }

                    String mininumQuantity = symbol.Quantity.Minimum.ToString(CultureInfo.InvariantCulture);
                    Int32  dustDecimals    = Math.Max(0, mininumQuantity.IndexOf("1", StringComparison.OrdinalIgnoreCase) - 1);

                    assets.Add(baseAsset);
                    assets.Add(quoteAsset);
                    symbolsData[name] = dustDecimals;
                }

                ConcurrentBag <Trade> trades = new ConcurrentBag <Trade>();

                Parallel.ForEach(assets.SelectMany(x => assets, (a1, a2) => new { Asset1 = a1, Asset2 = a2 }), pair =>
                {
                    if (pair.Asset1 == pair.Asset2)
                    {
                        return;
                    }

                    TradeLeg leg1 = BuildTradeLeg(symbolsData, Config.InvestmentBaseAsset, pair.Asset1);

                    if ((leg1 == null) || (leg1.Position != Config.TradingPositions[0]))
                    {
                        return;
                    }

                    TradeLeg leg2 = BuildTradeLeg(symbolsData, pair.Asset1, pair.Asset2);

                    if ((leg2 == null) || (leg2.Position != Config.TradingPositions[1]))
                    {
                        return;
                    }

                    TradeLeg leg3 = BuildTradeLeg(symbolsData, pair.Asset2, Config.InvestmentBaseAsset);

                    if ((leg3 == null) || (leg3.Position != Config.TradingPositions[2]))
                    {
                        return;
                    }

                    trades.Add(new Trade(leg1, leg2, leg3));
                });

                if (trades.Count == 0)
                {
                    throw new Exception(Resources.NoTriangularRelationships);
                }

                m_Trades.AddRange(trades);

                m_MessagePump.Signal(Utilities.FormatMessage(Resources.TradesOK, m_Trades.Count, (m_Trades.Count == 1 ? String.Empty : "s")));
            }
            catch (Exception e)
            {
                m_MessagePump.Signal(Utilities.FormatMessage(Resources.TradesKO, Utilities.GetExceptionMessage(e)));
                return(false);
            }

            try
            {
                List <String> symbols = m_Trades.Select(x => x.Leg1.Symbol)
                                        .Union(m_Trades.Select(x => x.Leg2.Symbol))
                                        .Union(m_Trades.Select(x => x.Leg3.Symbol))
                                        .Distinct().OrderBy(x => x).ToList();

                foreach (String symbol in symbols)
                {
                    m_OrderBooks[symbol] = new SortedOrderBook(m_Api.GetOrderBookAsync(symbol, Config.DataSize).Result);

                    m_OrderBooksCache[symbol]            = new DepthWebSocketCache();
                    m_OrderBooksCache[symbol].Error     += (sender, e) => m_OrderBooks[symbol].Invalid = true;
                    m_OrderBooksCache[symbol].OutOfSync += (sender, e) => m_OrderBooks[symbol].Invalid = true;
                    m_OrderBooksCache[symbol].Subscribe(symbol, e => m_OrderBooks[symbol] = new SortedOrderBook(e.OrderBook));

                    Thread.Sleep(Config.DataStagger);
                }

                m_MessagePump.Signal(Utilities.FormatMessage(Resources.OrderBooksOK, symbols.Count, (symbols.Count == 1 ? String.Empty : "s")));
                m_MessagePump.Signal(String.Empty);

                m_Initialized = true;

                return(true);
            }
            catch (Exception e)
            {
                m_MessagePump.Signal(Utilities.FormatMessage(Resources.OrderBooksKO, Utilities.GetExceptionMessage(e)));
                return(false);
            }
        }
Exemplo n.º 6
0
        public async Task BinanceBuySell(string BuyOrSell)
        {
            var api = new BinanceApi();

            if (await api.PingAsync())
            {
                Console.WriteLine("Successfull");
                switch (BuyOrSell)
                {
                case "buy":
                    using (var user = new BinanceApiUser("yIruCEm5k2TkExflzSf183xBp4HUt66G2BDx6WJqlL7HATRyJmcgp5UAiqYl0XsF", "BEqWhtDUPDIpNmaHFxA5ZhXLElcS74oal6yHLEx5sbE5gu46EsYLAfWAx1veyUr0"))
                    {
                        // Create a client (MARKET) order.
                        var clientOrder = new MarketOrder(user)
                        {
                            Symbol   = Symbol.ETH_BTC,
                            Side     = Binance.OrderSide.Buy,
                            Quantity = 1m
                        };

                        try
                        {
                            // Send the TEST order.
                            await api.PlaceAsync(clientOrder);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"TEST Order Failed: \"{e.Message}\"");
                        }
                        WithdrawRequest withdrawRequest = new WithdrawRequest(user)
                        {
                            Address = "",
                            Amount  = 2,
                            Asset   = Asset.ETH,
                        };
                        await api.WithdrawAsync(withdrawRequest);
                    }
                    break;

                case "sell":
                    using (var user = new BinanceApiUser("yIruCEm5k2TkExflzSf183xBp4HUt66G2BDx6WJqlL7HATRyJmcgp5UAiqYl0XsF", "BEqWhtDUPDIpNmaHFxA5ZhXLElcS74oal6yHLEx5sbE5gu46EsYLAfWAx1veyUr0"))
                    {
                        WithdrawRequest withdrawRequest = new WithdrawRequest(user)
                        {
                            Address = "",
                            Amount  = 2,
                            Asset   = Asset.BTC,
                        };
                        // Create a client (MARKET) order.
                        var clientOrder = new MarketOrder(user)
                        {
                            Symbol   = Symbol.ETH_BTC,
                            Side     = Binance.OrderSide.Sell,
                            Quantity = 1m
                        };

                        try
                        {
                            // Send the TEST order.
                            await api.PlaceAsync(clientOrder);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"TEST Order Failed: \"{e.Message}\"");
                        }
                        await api.WithdrawAsync(withdrawRequest);
                    }
                    break;
                }
            }
        }
Exemplo n.º 7
0
        public static async Task ExampleMain(string[] args)
        {
            // Initialize REST API client.
            var api = new BinanceApi();

            // Check connectivity.
            if (await api.PingAsync())
            {
                Console.WriteLine("Successful!");
            }


            // Create user with API-Key and API-Secret.
            using (var user = new BinanceApiUser("<API-Key>", "<API-Secret>"))
            {
                // Create a client (MARKET) order.
                var order = new MarketOrder(user)
                {
                    Symbol   = Symbol.BTC_USDT,
                    Side     = OrderSide.Buy,
                    Quantity = 0.01m
                };

                try
                {
                    // Validate client order.
                    order.Validate();

                    // Send the TEST order.
                    await api.TestPlaceAsync(order);

                    Console.WriteLine("Test Order Successful!");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Test Order Failed: \"{e.Message}\"");
                }
            }


            // Initialize web socket client (with automatic streaming enabled).
            var webSocketClient = new AggregateTradeWebSocketClient();

            // Handle error events.
            webSocketClient.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };

            // Subscribe callback to BTC/USDT (automatically begin streaming).
            webSocketClient.Subscribe(Symbol.BTC_USDT, evt =>
            {
                var side = evt.Trade.IsBuyerMaker ? "SELL" : "BUY ";

                // Handle aggregate trade events.
                Console.WriteLine($"{evt.Trade.Symbol} {side} {evt.Trade.Quantity} @ {evt.Trade.Price}");
            });

            Console.ReadKey(true); // wait for user input.

            // Unsubscribe from symbol (automatically end streaming).
            webSocketClient.Unsubscribe();


            // Initiatlize web socket cache (with automatic streaming enabled).
            var webSocketCache = new DepthWebSocketCache();

            // Handle error events.
            webSocketCache.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };

            // Subscribe callback to symbol (automatically begin streaming).
            webSocketCache.Subscribe(Symbol.BTC_USDT, evt =>
            {
                // Get symbol from cache (update cache if a symbol is missing).
                var symbol = Symbol.Cache.Get(evt.OrderBook.Symbol);

                var minBidPrice = evt.OrderBook.Bids.Last().Price;
                var maxAskPrice = evt.OrderBook.Asks.Last().Price;

                // Handle order book update events.
                Console.WriteLine($"Bid Quantity: {evt.OrderBook.Depth(minBidPrice)} {symbol.BaseAsset} - " +
                                  $"Ask Quantity: {evt.OrderBook.Depth(maxAskPrice)} {symbol.BaseAsset}");
            });

            Console.ReadKey(true); // wait for user input.

            // Unsubscribe from symbol (automatically end streaming).
            webSocketCache.Unsubscribe();
        }
Exemplo n.º 8
0
        public static async Task ExampleMain(string[] args)
        {
            var api = new BinanceApi();

            if (await api.PingAsync())
            {
                Console.WriteLine("Successful!");
            }

            /*
             * using (var user = new BinanceApiUser("<API-Key>", "<API-Secret>"))
             * {
             *  var order = new MarketOrder(user)
             *  {
             *      Symbol = Symbol.BTC_USDT,
             *      Side = OrderSide.Buy,
             *      Quantity = 0.01m
             *  };
             *
             *  try
             *  {
             *      order.Validate();
             *
             *      await api.TestPlaceAsync(order);
             *
             *      Console.WriteLine("Test Order Successful!");
             *  }
             *  catch (Exception e)
             *  {
             *      Console.WriteLine($"Test Order Failed: \"{e.Message}\"");
             *  }
             * }
             */

            using (var webSocketManager = new AggregateTradeWebSocketClientManager())
            {
                webSocketManager.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };

                webSocketManager.Subscribe(Symbol.BTC_USDT, evt =>
                {
                    var side = evt.Trade.IsBuyerMaker ? "SELL" : "BUY ";

                    Console.WriteLine($"{evt.Trade.Symbol} {side} {evt.Trade.Quantity} @ {evt.Trade.Price}");
                });

                Console.ReadKey(true);
            }

            using (var webSocketManager = new DepthWebSocketCacheManager())
            {
                webSocketManager.Error += (s, e) => { Console.WriteLine(e.Exception.Message); };

                webSocketManager.Subscribe(Symbol.BTC_USDT, evt =>
                {
                    Symbol symbol = evt.OrderBook.Symbol; // use implicit conversion.

                    var minBidPrice = evt.OrderBook.Bids.Last().Price;
                    var maxAskPrice = evt.OrderBook.Asks.Last().Price;

                    Console.WriteLine($"Bid Quantity: {evt.OrderBook.Depth(minBidPrice)} {symbol.BaseAsset} - " +
                                      $"Ask Quantity: {evt.OrderBook.Depth(maxAskPrice)} {symbol.BaseAsset}");
                });

                Console.ReadKey(true);
            }
        }