Example #1
0
        public BitmexWebsocketClient(IBitmexCommunicator communicator)
        {
            BmxValidations.ValidateInput(communicator, nameof(communicator));

            _communicator = communicator;
            _messageReceivedSubsciption = _communicator.MessageReceived.Subscribe(HandleMessage);
        }
 private void OnStop()
 {
     _pingSubscription.Dispose();
     _client.Dispose();
     _communicator.Dispose();
     _client       = null;
     _communicator = null;
     Clear();
 }
Example #3
0
        /// <inheritdoc />
        public BitmexWebsocketClient(IBitmexCommunicator communicator)
        {
            BmxValidations.ValidateInput(communicator, nameof(communicator));

            _communicator = communicator;
            _messageReceivedSubscription = _communicator.MessageReceived.Subscribe(HandleMessage);

            JsonSerializer.SetDefaultResolver(StandardResolver.CamelCase);
        }
        public BitmexMultiplexClient(IBitmexCommunicator communicator)
        {
            BmxValidations.ValidateInput(communicator, nameof(communicator));

            _communicator = communicator;
            _messageReceivedSubscription = _communicator.MessageReceived.Subscribe(HandleMessage);

            _channels = new ConcurrentDictionary <Guid, BitmexWebsocketChannel>();
            Task.Run(this.PingMonitor);
        }
 /// <summary>
 /// A BitmexWebsocketClient which records the raw data comming off the websocket
 /// </summary>
 /// <param name="communicator"></param>
 /// <param name="recordingPath">Recording file pathname. Overwritten if it exists. If null recording is disabled.</param>
 /// <param name="delimiter">Separator inserted between records.</param>
 public BitmexWebsocketRecorderClient(IBitmexCommunicator communicator, string recordingPath = null, string delimiter = ";;")
     : base(communicator)
 {
     if (recordingPath == null)
     {
         _stopped = true;
     }
     else
     {
         _recordingStream = File.Create(recordingPath);
         _recorder        = new StreamWriter(_recordingStream);
         _delimiter       = delimiter;
     }
 }
        private async Task OnStart()
        {
            var pair = _view.Pair;

            if (string.IsNullOrWhiteSpace(pair))
            {
                pair = _defaultPair;
            }
            pair = pair.ToUpper();

            _tradeStatsComputer     = new TradeStatsComputer();
            _orderBookStatsComputer = new OrderBookStatsComputer();

            var url = _view.IsTestNet ?
                      BitmexValues.ApiWebsocketTestnetUrl :
                      BitmexValues.ApiWebsocketUrl;

            _communicator = new BitmexWebsocketCommunicator(url);
            _client       = new BitmexWebsocketClient(_communicator);

            Subscribe(_client);

            _communicator.ReconnectionHappened.Subscribe(async type =>
            {
                _view.Status($"Reconnected (type: {type})", StatusType.Info);
                await SendSubscriptions(_client, pair);
            });

            _communicator.DisconnectionHappened.Subscribe(type =>
            {
                if (type == DisconnectionType.Error)
                {
                    _view.Status($"Disconnected by error, next try in {_communicator.ErrorReconnectTimeoutMs/1000} sec",
                                 StatusType.Error);
                    return;
                }
                _view.Status($"Disconnected (type: {type})",
                             StatusType.Warning);
            });

            await _communicator.Start();

            StartPingCheck(_client);
        }
Example #7
0
 private void OnStopB()
 {
     //OnStopZoneRecovery();
     if (_pingSubscriptionB != null)
     {
         _pingSubscriptionB.Dispose();
     }
     if (_clientB != null)
     {
         _clientB.Dispose();
     }
     if (_communicatorB != null)
     {
         _communicatorB.Dispose();
     }
     _clientB       = null;
     _communicatorB = null;
     Clear(ZoneRecoveryAccount.B);
 }
Example #8
0
        private void OnStopA()
        {
            OnStopZoneRecovery();
            if (_pingSubscriptionA != null)
            {
                _pingSubscriptionA.Dispose();
            }
            if (_clientA != null)
            {
                _clientA.Dispose();
            }
            if (_communicatorA != null)
            {
                _communicatorA.Dispose();
            }
            _clientA       = null;
            _communicatorA = null;

            Clear(ZoneRecoveryAccount.A);
        }
Example #9
0
        private async Task OnStartB()
        {
            var pair = _defaultPair.ToUpper();

            var url = BitmexValues.ApiWebsocketTestnetUrl;

            _communicatorB = new BitmexWebsocketCommunicator(url);
            _clientB       = new BitmexWebsocketClient(_communicatorB);

            Subscribe(_clientB, MTAccount.B);

            _communicatorB.ReconnectionHappened.Subscribe(async type =>
            {
                try
                {
                    ErrorStatsMutex.WaitOne();

                    if (type != ReconnectionType.Initial)
                    {
                        Log.Warning($"Reconnected B (type: {type})");
                        _errorStatsHandler.Add2Reconnections(1, ZoneRecoveryAccount.B);
                    }
                    else if (type == ReconnectionType.Error)
                    {
                        _errorStatsHandler.Add2ErrorCnt(1, ZoneRecoveryAccount.B);
                    }
                }
                catch (Exception exc)
                {
                    Log.Error($"_communicatorB.ReconnectionHappened: {exc.Message}");
                }
                finally
                {
                    ErrorStatsMutex.ReleaseMutex();
                }

                _view.StatusB($"Reconnected (type: {type})", StatusType.Info);
                _view.ConnStartB = System.DateTime.Now.ToString("dd-MM-yy HH:mm:ss");

                await SendSubscriptions(_clientB, pair, MTAccount.B);
            });

            _communicatorB.DisconnectionHappened.Subscribe(type =>
            {
                try
                {
                    ErrorStatsMutex.WaitOne();

                    _errorStatsHandler.Add2Disconnections(1, ZoneRecoveryAccount.B);

                    if (type == DisconnectionType.Error)
                    {
                        _errorStatsHandler.Add2ErrorCnt(1, ZoneRecoveryAccount.B);
                        _view.StatusB($"Disconnected by error, next try in {_communicatorB.ErrorReconnectTimeoutMs / 1000} sec", StatusType.Error);
                        return;
                    }
                }
                catch (Exception exc)
                {
                    Log.Error($"_communicatorB.DisconnectionHappened: {exc.Message}");
                }
                finally
                {
                    ErrorStatsMutex.ReleaseMutex();
                }

                _view.StatusB($"Disconnected (type: {type})", StatusType.Warning);
            });

            await _communicatorB.Start();

            StartPingCheckB(_clientB);
        }
Example #10
0
        private async Task OnStartA()
        {
            //if (ZRComputer == null)
            //    ZRComputer = new ZoneRecoveryComputer();

            DateTime dt = DateTime.Now;

            _generalStatsHandler = new GeneralStatsHandler(dt);

            var pair = _defaultPair.ToUpper();

            _posStatsHandler = new PositionStatsHandler();

            _tradeStatsComputer     = new TradeStatsComputer();
            _orderBookStatsComputer = new OrderBookStatsComputer();
            _orderStatsHandler      = new OrdersStatsHandler();
            _errorStatsHandler      = new ErrorStatsHandler();
            _marginStatsHandler     = new MarginStatsHandler();

            var url = BitmexValues.ApiWebsocketTestnetUrl;

            _communicatorA = new BitmexWebsocketCommunicator(url);
            _clientA       = new BitmexWebsocketClient(_communicatorA);

            // Enable stream listeners
            Subscribe(_clientA, MTAccount.A);

            _communicatorA.ReconnectionHappened.Subscribe(async type =>
            {
                try
                {
                    ErrorStatsMutex.WaitOne();

                    if (type != ReconnectionType.Initial)
                    {
                        Log.Warning($"Reconnected A (type: {type})");
                        _errorStatsHandler.Add2Reconnections(1, ZoneRecoveryAccount.A);
                    }
                    else if (type == ReconnectionType.Error)
                    {
                        _errorStatsHandler.Add2ErrorCnt(1, ZoneRecoveryAccount.A);
                    }
                }
                catch (Exception exc)
                {
                    Log.Error($"_communicatorA.ReconnectionHappened: {exc.Message}");
                }
                finally
                {
                    ErrorStatsMutex.ReleaseMutex();
                }

                _view.StatusA($"Reconnected (type: {type})", StatusType.Info);
                _view.ConnStartA = dt.ToString("dd-MM-yy HH:mm:ss");

                await SendSubscriptions(_clientA, pair, MTAccount.A);
            });

            _communicatorA.DisconnectionHappened.Subscribe(type =>
            {
                try
                {
                    ErrorStatsMutex.WaitOne();

                    _errorStatsHandler.Add2Disconnections(1, ZoneRecoveryAccount.A);

                    if (type == DisconnectionType.Error)
                    {
                        _errorStatsHandler.Add2ErrorCnt(1, ZoneRecoveryAccount.A);
                        _view.StatusA($"Disconnected by error, next try in {_communicatorA.ErrorReconnectTimeoutMs / 1000} sec", StatusType.Error);
                        return;
                    }
                }
                catch (Exception exc)
                {
                    Log.Error($"_communicatorA.DisconnectionHappened: {exc.Message}");
                }
                finally
                {
                    ErrorStatsMutex.ReleaseMutex();
                }

                _view.StatusA($"Disconnected (type: {type})", StatusType.Warning);
            });

            await _communicatorA.Start();

            StartPingCheckA(_clientA);
        }