Пример #1
0
        public Client(Config cfg)
        {
            Condition.Requires(cfg.Endpoint, "cfg.Endpoint").IsNotNull();
            Condition.Requires(cfg.Products, "cfg.Products").IsNotNull();
            Condition.Requires(cfg.Scheduler, "cfg.Scheduler").IsNotNull();
            if (cfg.Keys != null)
            {
                Condition.Requires(cfg.Keys.ApiKey, "cfg.Keys.ApiKey").IsNotNullOrWhiteSpace();
                Condition.Requires(cfg.Keys.SecretKey, "cfg.Keys.SecretKey").IsNotNullOrWhiteSpace();
            }
            if (cfg.EnableTrading)
            {
                Condition.Requires(cfg.Keys, "cfg.Keys").IsNotNull();
            }
            _cfg      = cfg.Clone();
            _cfg.Keys = _cfg.Keys ?? new Keys()
            {
                ApiKey = "NONE", SecretKey = "NONE"
            };
            var connector = new CodingConnector <IMessageIn, IMessageOut>(
                new ExchangeApi.WebSocket.Connector(_cfg.Endpoint.WebSocket), new WebSocket.Codec(_cfg.Keys));

            _connection = new DurableConnection <IMessageIn, IMessageOut>(connector, _cfg.Scheduler);
            _gateway    = new WebSocket.Gateway(_connection);
            _connection.OnConnection += OnConnection;
            _connection.OnMessage    += OnMessage;
            _restClient           = new REST.RestClient(_cfg.Endpoint.REST, _cfg.Keys);
            _pinger               = new PeriodicAction(_cfg.Scheduler, PingPeriod, PingPeriod, Ping);
            _futurePositionPoller = new FuturePositionPoller(
                _restClient, _cfg.Scheduler,
                _cfg.EnableTrading ? _cfg.Products : new List <Product>());
            _futurePositionPoller.OnFuturePositions += msg => OnFuturePositionsUpdate?.Invoke(msg);
            _spotPositionPoller = new PeriodicAction(_cfg.Scheduler, SpotPositionPollPeriod, SpotPositionPollPeriod, PollSpotPositions);
        }
Пример #2
0
        public FuturePositionPoller(REST.RestClient restClient, Scheduler scheduler, IEnumerable <Product> products)
        {
            Condition.Requires(restClient, "restClient").IsNotNull();
            _restClient = restClient;
            _products   = products
                          .Where(p => p.ProductType == ProductType.Future)
                          .Select(p => Tuple.Create(p.Currency, p.CoinType))
                          .Dedup()
                          .ToArray();

            _pollers = new PeriodicAction[_products.Length];
            try
            {
                for (int i = 0; i != _pollers.Length; ++i)
                {
                    var p = _products[i];
                    // Spread polling of different products over the polling period.
                    // While we are polling positions, we aren't processing market data and order updates.
                    // To reduce downtime we query positions for one product at a time.
                    var delay = PollPeriod.Mul(i + 1).Div(_pollers.Length);
                    // PollOne can throw. That's OK -- PeriodicAction will log it and continue.
                    _pollers[i] = new PeriodicAction(scheduler, delay, PollPeriod, () =>
                    {
                        if (Connected)
                        {
                            PollOne(p.Item1, p.Item2);
                        }
                    });
                }
            }
            catch
            {
                foreach (PeriodicAction a in _pollers)
                {
                    if (a != null)
                    {
                        a.Dispose();
                    }
                }
                throw;
            }
        }