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);
        }
Beispiel #2
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);
        }