示例#1
0
 /// <summary>Return candle data for the given time range</summary>
 public List <Candle> GetChartData(CurrencyPair pair, EMarketTimeFrames tf, DateTimeOffset time_beg, DateTimeOffset time_end, CancellationToken?cancel = null)
 {
     return(GetChartData(pair, tf, time_beg.Ticks, time_end.Ticks, cancel));
 }
示例#2
0
 public SubscriptionCandleData(CurrencyPair pair, EMarketTimeFrames tf)
     : base(API.ChannelName.Candles)
 {
     Pair      = pair;
     TimeFrame = tf;
 }
示例#3
0
 /// <summary>True if there is candle data for 'pair' at period 'tf'</summary>
 public bool Contains(CurrencyPair pair, EMarketTimeFrames tf)
 {
     return
         (m_data.TryGetValue(pair, out var time_frames) &&
          time_frames.TryGetValue(tf, out var candles));
 }
示例#4
0
        /// <summary>Parse a candle message</summary>
        public void ParseUpdate(CurrencyPair pair, EMarketTimeFrames tf, JArray data)
        {
            // Get the candle collection
            var candles = this[pair, tf];

            // If the update contains an array of arrays, then this is a history update
            if (data[0].Type == JTokenType.Array)
            {
                // Expect the data to be order from oldest to newest
                var new_candles = data.Select(x => new Candle((JArray)x)).ToList();

                // Insert 'new_candles' into 'candles'
                if (new_candles.Count != 0)
                {
                    // Erase the range spanned by 'new_candles'
                    var beg  = new_candles[0];
                    var end  = new_candles[new_candles.Count - 1];
                    var ibeg = candles.BinarySearch(beg);
                    var iend = candles.BinarySearch(end);
                    if (ibeg < 0)
                    {
                        ibeg = ~ibeg;
                    }
                    if (iend < 0)
                    {
                        iend = ~iend;
                    }
                    candles.RemoveRange(ibeg, iend - ibeg);

                    // Insert the new candles into 'candles'
                    candles.InsertRange(ibeg, new_candles);
                }
            }
            // Otherwise, this is an update to the latest candle
            else
            {
                var new_candle = new Candle(data);

                // Insert/Update 'new_candle' in 'candles'
                var last = candles.Count != 0 ? candles[candles.Count - 1] : null;
                if (last == null || last.Timestamp < new_candle.Timestamp)
                {
                    candles.Add(new_candle);
                }
                else if (last.Timestamp == new_candle.Timestamp)
                {
                    candles[candles.Count - 1] = new_candle;
                }
                else
                {
                    var idx = candles.BinarySearch(new_candle);
                    if (idx >= 0)
                    {
                        candles[idx] = new_candle;
                    }
                    else
                    {
                        candles.Insert(~idx, new_candle);
                    }
                }
            }

            // Save the candle collection
            this[pair, tf] = candles;
        }
示例#5
0
 /// <summary>Remove the chart data for the given pair</summary>
 public void Clear(CurrencyPair pair)
 {
     m_data.Remove(pair);
 }
示例#6
0
        /// <summary>Parse a market data update message</summary>
        public void ParseUpdate(CurrencyPair pair, JArray data)
        {
            // Get the order book to update
            var order_book = this[pair];

            // If the update contains an array of arrays, then this is a snapshot
            if (data[0].Type == JTokenType.Array)
            {
                order_book.BuyOrders.Clear();
                order_book.SellOrders.Clear();
                foreach (var upd in data)
                {
                    var price  = upd[0].Value <decimal>();
                    var count  = upd[1].Value <int>();
                    var amount = upd[2].Value <decimal>();
                    if (count == 0)
                    {
                        throw new Exception("Market data snapshot containing a 'count' of zero doesn't make sense");
                    }

                    // Get the associated book
                    var book = amount > 0 ? order_book.BuyOrders : order_book.SellOrders;

                    // Add the order
                    var order = new OrderBook.Order(price, Math.Abs(amount), count);
                    book.Add(order);
                }
            }
            // Otherwise it's a single update
            else
            {
                var price  = data[0].Value <decimal>();
                var count  = data[1].Value <int>();
                var amount = data[2].Value <decimal>();
                var sign   = Math.Sign(amount);

                // Get the associated book
                var book  = amount > 0 ? order_book.BuyOrders : order_book.SellOrders;
                var order = new OrderBook.Order(price, Math.Abs(amount), count);

                // Find the index of 'price' in 'book'
                var cmp = Comparer <OrderBook.Order> .Create((l, r) => - sign *l.Price.CompareTo(r.Price));

                var idx = book.BinarySearch(order, cmp);

                // If 'count' is zero, remove the given price level
                if (count == 0)
                {
                    if (idx >= 0)
                    {
                        book.RemoveAt(idx);
                    }
                }
                // Otherwise, add/update the price level
                else
                {
                    if (idx >= 0)
                    {
                        book[idx] = order;
                    }
                    else
                    {
                        book.Insert(~idx, order);
                    }
                }
            }

            // Set the updated order book
            this[pair] = order_book;
        }
示例#7
0
 /// <summary>Return the order book for the given pair</summary>
 public OrderBook this[CurrencyPair pair]
 {
     get { return(m_data.TryGetValue(pair, out var ob) ? ob : new OrderBook(pair)); }
     set { m_data[pair] = value; }
 }
示例#8
0
 public bool Equals(CurrencyPair b)
 {
     return(b.Base == Base && b.Quote == Quote);
 }
示例#9
0
 public OrderBook(CurrencyPair pair)
 {
     Pair       = pair;
     BuyOrders  = new List <Order>();
     SellOrders = new List <Order>();
 }