Esempio n. 1
0
        public void Disconnect(string key, string secret)
        {
            _connector = new RestApiConnector(BITMEX_URL, key, secret);
            _connector.Connect();
            var response = _connector.Disconnect();

            Assert.IsTrue(response.IsSuccess, "Disconnect failed");
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("BitMEX Test Net Connector.\nPress CTRL+Break to exit.");
            try
            {
                _restEndpoint = ConfigurationManager.AppSettings.Get("RestEndpoint");
                _wsEndpoint   = ConfigurationManager.AppSettings.Get("WebSocketEndpoint");
                _key          = ConfigurationManager.AppSettings.Get("BitmexApiKey");
                _secret       = ConfigurationManager.AppSettings.Get("BitmexApiSecret");

                _client = new RestApiConnector(_restEndpoint, _key, _secret);

                // intinite loop wraparound
                System.Console.CancelKeyPress += (sender, e) =>
                {
                    var isCtrlC = e.SpecialKey == ConsoleSpecialKey.ControlC;
                    if (isCtrlC)
                    {
                        var disconnectionResult = _client.Disconnect();
                        _wsf.Disconnect();
                        _wsf.Dispose();
                        if (disconnectionResult.IsSuccess)
                        {
                            System.Console.WriteLine("[REST API]Client disconnected.");
                        }
                        else
                        {
                            System.Console.WriteLine("[REST API]Could not terminate session: {0}", disconnectionResult.Error);
                        }
                        System.Console.WriteLine("Press Enter key to close the app.");
                        System.Console.ReadLine();
                        e.Cancel = true;
                    }
                };

                Task.Factory.StartNew(ListenWebSocket);

                // Wait for websocket to connect
                Thread.Sleep(300);
                var user = _client.Connect();
                if (user.IsSuccess)
                {
                    System.Console.WriteLine("Client connected: {0}, {1}", user.FirstName, user.LastName);
                    System.Console.WriteLine("Trying to set order");

                    // to prevent anti-spam filtering of BitMex
                    var random          = new Random(int.MaxValue);
                    var priceDivergence = random.Next(1, 30);
                    var valueSign       = priceDivergence % 2 == 0 ? 1 : -1;
                    var order           = new OrderItem
                    {
                        side     = "Buy",
                        symbol   = _instrument,
                        orderQty = random.Next(1, 5),
                        price    = (decimal)(6330 + priceDivergence * valueSign),
                        ordType  = "Limit"
                    };

                    var orderSubmitResult = _client.RegisterOrder(order);
                    if (orderSubmitResult.IsSuccess)
                    {
                        System.Console.WriteLine("[REST API][{0}] {1} {2} by {3:#.00} - order sent ({4})", order.side, order.orderQty, order.symbol, order.price, orderSubmitResult.orderID);
                    }
                    else
                    {
                        System.Console.WriteLine("[REST API]Could not allocate order: {0}", orderSubmitResult.Error);
                    }

                    var cancelOrderResult = _client.CancelOrder(orderSubmitResult.orderID);
                    if (cancelOrderResult.IsSuccess)
                    {
                        System.Console.WriteLine("[REST API]Order {0} cancelled.", orderSubmitResult.orderID);
                    }
                }
                else
                {
                    System.Console.WriteLine("[API]Connection error: {0}", user.Error);
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Application error: {0}.", e.Message);
                System.Console.ReadLine();
            }

            while (true)
            {
                // infinite loop to keep websocket open
            }
        }