예제 #1
0
        private void RaiseNewSecurity(Security security)
        {
            var arr = new[] { security };

            _added?.Invoke(arr);

            NewSecurity?.Invoke(security);
            NewSecurities?.Invoke(arr);
        }
예제 #2
0
        private void SearchCallback(List <Instrument> instruments, bool hasMoreResults)
        {
            var lastAddedInstrumentId = 0;

            lock (Securities)
            {
                foreach (var instrument in instruments)
                {
                    if (_securities.ContainsKey((int)instrument.Id) || instrument.Name.Contains("weekend"))
                    {
                        continue;
                    }

                    var security = new Security
                    {
                        DataFeed       = Name,
                        Symbol         = instrument.Underlying.Symbol,
                        Name           = instrument.Name,
                        SecurityId     = (int)instrument.Id,
                        AssetClass     = instrument.Underlying.AssetClass,
                        BaseCurrency   = instrument.Contract.Currency,
                        UnitOfMeasure  = instrument.Contract.UnitOfMeasure,
                        ContractSize   = instrument.Contract.ContractSize,
                        Digit          = Math.Max(instrument.OrderBook.PriceIncrement.ToString().Length - 2, 1),
                        PriceIncrement = instrument.OrderBook.PriceIncrement,
                        MarginRate     = instrument.Risk.MarginRate,
                        MaxPosition    = instrument.Risk.MaximumPosition,
                        UnitPrice      = instrument.Contract.UnitPrice,
                        QtyIncrement   = instrument.OrderBook.QuantityIncrement,
                        MarketOpen     = instrument.Calendar.Open,
                        MarketClose    = instrument.Calendar.Close
                    };

                    Securities.Add(security);
                    NewSecurity?.Invoke(security);
                    _securities[security.SecurityId] = security;
                    Subscribe(security);
                    lastAddedInstrumentId = security.SecurityId;

                    _session.Subscribe(new OrderBookSubscriptionRequest(instrument.Id), () => { }, FailureCallback);
                    _session.Subscribe(new OrderBookStatusSubscriptionRequest(instrument.Id), () => { }, FailureCallback);
                }
            }

            if (hasMoreResults && lastAddedInstrumentId > 0)
            {
                _session.SearchInstruments(new SearchRequest("", lastAddedInstrumentId), SearchCallback, FailureCallback);
            }
        }
        public void Start()
        {
            //start live stream
            _api = new PoloniexClient(Url, PublicKey, PrivateKey);

            //get latest quotes to populate securities list
            List <Quote> quotes = null;

            Task.Run(async() => quotes = await _api.Markets.GetSummary()).Wait();
            if (quotes != null && quotes.Count > 0)
            {
                foreach (var q in quotes)
                {
                    if (Securities.All(i => i.Symbol != q.Symbol.ToString()))
                    {
                        var security = GetSecurityFromCurrencyPair(q.Symbol);
                        Securities.Add(security);
                        NewSecurity?.Invoke(security);
                        _securities[q.Symbol]        = security;
                        _securitiesById[q.Symbol.Id] = security;
                        Subscribe(security);
                    }
                }
            }
            else
            {
                throw new Exception($"No securities defined for {Name} data feed");
            }

            _api.Live.OnOrderBookUpdate += Api_OnOrderBookUpdate;
            _api.Live.OnTickerUpdate    += Api_OnTickerUpdate;
            _api.Live.OnSessionError    += Api_OnSessionError;
            _api.Live.Start();
            IsStarted = true;

            Task.Run(async() =>
            {
                var books = await _api.Markets.GetOrderBooks();
                lock (_orderBooks)
                {
                    _orderBooks.Clear();
                    foreach (var book in books)
                    {
                        _orderBooks.Add(book.Key, book.Value);
                    }
                }
            });
        }