Пример #1
0
 void OnRemove(OrderBookUpdateInfo info)
 {
     if (info.Type == OrderBookEntryType.Bid)
     {
         OrderBookEntry entry = Bids.FirstOrDefault((e) => e.Value == info.Entry.Value);
         if (entry == null)
         {
             Debug.WriteLine("Error removing: '" + info.Type + "' with rate " + info.Entry.Value + " not found");
             return;
         }
         Bids.Remove(entry);
     }
     else
     {
         OrderBookEntry entry = Asks.FirstOrDefault((e) => e.Value == info.Entry.Value);
         if (entry == null)
         {
             Debug.WriteLine("Error removing: '" + info.Type + "' with rate " + info.Entry.Value + " not found");
             return;
         }
         Asks.Remove(entry);
     }
     RaiseOnChanged(info);
     OnChangedCore(info);
 }
Пример #2
0
 void AddUpdateToQueue(OrderBookUpdateInfo info)
 {
     if (Updates.Count == 0)
     {
         GetSnapshot();
     }
     Updates.Add(info);
 }
Пример #3
0
        protected internal void OnAddAsk(OrderBookUpdateInfo info)
        {
            OrderBookEntry before = Asks.FirstOrDefault((e) => e.Value < info.Entry.Value);

            if (before == null)
            {
                Asks.Insert(0, info.Entry);
                return;
            }
            Asks.Insert(Asks.IndexOf(before), info.Entry);
        }
Пример #4
0
        void OnAddBid(OrderBookUpdateInfo info)
        {
            OrderBookEntry before = Bids.FirstOrDefault((e) => e.Value < info.Entry.Value);

            if (before == null)
            {
                Bids.Add(info.Entry);
                return;
            }
            Bids.Insert(Bids.IndexOf(before), info.Entry);
        }
Пример #5
0
 protected internal void ApplyInfo(OrderBookUpdateInfo info)
 {
     if (info.Action == OrderBookUpdateType.Remove)
     {
         OnRemove(info);
     }
     else if (info.Action == OrderBookUpdateType.Modify)
     {
         OnModify(info);
     }
 }
Пример #6
0
        protected internal void RaiseOnChanged(OrderBookUpdateInfo info)
        {
            OrderBookEventArgs e = new OrderBookEventArgs()
            {
                Update = info
            };

            if (OnChanged != null)
            {
                OnChanged(this, e);
            }
        }
Пример #7
0
        public void GetOrderBook(PoloniexTicker ticker, int depth)
        {
            string address = string.Format("https://poloniex.com/public?command=returnOrderBook&currencyPair={0}&depth={1}",
                                           Uri.EscapeDataString(ticker.CurrencyPair), depth);
            string text;

            using (WebClient client = new WebClient()) {
                text = client.DownloadString(address);
            }
            ticker.OrderBook.Bids.Clear();
            ticker.OrderBook.Asks.Clear();

            JObject res = (JObject)JsonConvert.DeserializeObject(text);

            foreach (JProperty prop in res.Children())
            {
                if (prop.Name == "asks" || prop.Name == "bids")
                {
                    OrderBookEntryType type  = prop.Name == "asks" ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                    JArray             items = prop.Value.ToObject <JArray>();
                    foreach (JArray item in items.Children())
                    {
                        OrderBookUpdateInfo info = new OrderBookUpdateInfo();
                        info.Type   = prop.Name == "asks" ? OrderBookEntryType.Ask : OrderBookEntryType.Bid;
                        info.Entry  = new OrderBookEntry();
                        info.Action = OrderBookUpdateType.Modify;
                        JEnumerable <JToken> values = (JEnumerable <JToken>)item.Children();
                        JValue rateValue            = (JValue)values.First();
                        info.Entry.Value  = rateValue.ToObject <double>();
                        info.Entry.Amount = rateValue.Next.ToObject <double>();
                        if (type == OrderBookEntryType.Ask)
                        {
                            ticker.OrderBook.ForceAddAsk(info);
                        }
                        else
                        {
                            ticker.OrderBook.ForceAddBid(info);
                        }
                    }
                }
                else if (prop.Name == "seq")
                {
                    ticker.OrderBook.SeqNumber = prop.Value.ToObject <int>();
                    Console.WriteLine("Snapshot seq no = " + ticker.OrderBook.SeqNumber);
                }
                else if (prop.Name == "isFrozen")
                {
                    ticker.IsFrozen = prop.Value.ToObject <int>() == 0;
                }
            }
            ticker.OrderBook.ApplyQueueUpdates();
        }
Пример #8
0
 protected internal void OnAdd(OrderBookUpdateInfo info)
 {
     info.Action = OrderBookUpdateType.Add;
     if (info.Type == OrderBookEntryType.Bid)
     {
         OnAddBid(info);
     }
     else
     {
         OnAddAsk(info);
     }
     RaiseOnChanged(info);
     OnChangedCore(info);
 }
Пример #9
0
        protected internal void OnModify(OrderBookUpdateInfo info)
        {
            OrderBookEntry entry = info.Type == OrderBookEntryType.Ask ?
                                   Asks.FirstOrDefault((e) => e.Value == info.Entry.Value) :
                                   Bids.FirstOrDefault((e) => e.Value == info.Entry.Value);

            if (entry == null)
            {
                OnAdd(info);
                return;
            }
            entry.Amount = info.Entry.Amount;
            info.Entry   = entry;
            RaiseOnChanged(info);
            OnChangedCore(info);
        }
Пример #10
0
 protected internal void ApplyQueueUpdates()
 {
     if (Updates.Count == 0)
     {
         return;
     }
     while (Updates.Count > 0)
     {
         OrderBookUpdateInfo info = Updates.First();
         if (info.SeqNo <= SeqNumber)
         {
             Updates.Remove(info);
             continue;
         }
         if (info.SeqNo == SeqNumber + 1)
         {
             ApplyInfo(info);
             Updates.RemoveAt(0);
         }
     }
 }
Пример #11
0
        public void OnRecvUpdate(OrderBookUpdateInfo info)
        {
            bool prevActualState = IsActualState;

            if (IsExpected(info.SeqNo))
            {
                ApplyInfo(info);
                ApplyQueueUpdates();
                return;
            }
            if (IsOutdated(info.SeqNo))
            {
                return;
            }
            if (Updates.Count == 0 || Updates.Last().SeqNo == info.SeqNo - 1)
            {
                AddUpdateToQueue(info);
            }
            else
            {
                Debug.WriteLine("Error: update with seq no = " + info.SeqNo + " is skipped. Last seq no in que = " + Updates.Last().SeqNo);
                return;
            }
        }
Пример #12
0
 void ITicker.OnChanged(OrderBookUpdateInfo info)
 {
     RaiseChanged();
 }
Пример #13
0
 void OnChangedCore(OrderBookUpdateInfo info)
 {
     Owner.OnChanged(info);
 }
Пример #14
0
 protected internal void ForceAddBid(OrderBookUpdateInfo info)
 {
     Bids.Add(info.Entry);
 }
Пример #15
0
 protected internal void ForceAddAsk(OrderBookUpdateInfo info)
 {
     Asks.Insert(0, info.Entry);
 }