public void Update(Ticker ticker, IncrementalUpdateInfo info)
 {
     for (int i = 0; i < info.BidsUpdates.Count; i++)
     {
         string[] item = info.BidsUpdates[i];
         ticker.OrderBook.ApplyIncrementalUpdate(OrderBookEntryType.Bid, item[1], item[2]);
     }
     for (int i = 0; i < info.AsksUpdates.Count; i++)
     {
         string[] item = info.AsksUpdates[i];
         ticker.OrderBook.ApplyIncrementalUpdate(OrderBookEntryType.Ask, item[1], item[2]);
     }
     for (int i = 0; i < info.TradeUpdates.Count; i++)
     {
         string[]      item  = info.TradeUpdates[i];
         TradeInfoItem trade = new TradeInfoItem(null, ticker)
         {
             Type         = item[1][0] == 'S' ? TradeType.Sell : TradeType.Buy,
             RateString   = item[2],
             AmountString = item[3],
             Time         = new DateTime(Convert.ToInt64(item[4])).ToLocalTime()
         };
         ticker.AddTradeHistoryItem(trade);//.InsertTradeHistoryItem(trade);
         CandleStickChartHelper.UpdateVolumes(ticker.CandleStickData, trade, ticker.CandleStickPeriodMin);
     }
     ticker.OnApplyIncrementalUpdate();
 }
Пример #2
0
 public void Update(Ticker ticker, IncrementalUpdateInfo info)
 {
     for (int i = 0; i < info.Updates.Count; i++)
     {
         string[] item = info.Updates[i];
         if (item[0][0] == 'o')
         {
             ticker.OrderBook.ApplyIncrementalUpdate(item[1][0] == '1' ? OrderBookEntryType.Bid : OrderBookEntryType.Ask, item[2], item[3]);
         }
         else if (item[0][0] == 't')
         {
             TradeInfoItem trade = new TradeInfoItem(null, ticker)
             {
                 Type = item[2][0] == '0' ? TradeType.Sell : TradeType.Buy, RateString = item[3], AmountString = item[4], Time = ticker.Exchange.FromUnixTimestamp(Convert.ToInt64(item[5]))
             };
             if (trade.Time.Year == 1)
             {
                 throw new InvalidOperationException();
             }
             ticker.AddTradeHistoryItem(trade);//.InsertTradeHistoryItem(trade);
             CandleStickChartHelper.UpdateVolumes(ticker.CandleStickData, trade, ticker.CandleStickPeriodMin);
         }
     }
     ticker.OnApplyIncrementalUpdate();
 }
        protected internal override void OnTradeHistorySocketMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            LastWebSocketRecvTime = DateTime.Now;
            SocketConnectionInfo info = TradeHistorySockets.FirstOrDefault(c => c.Key == sender);

            if (info == null)
            {
                return;
            }
            Ticker t = info.Ticker;

            if (t.IsUpdatingTrades)
            {
                return;
            }

            if (t.CaptureData)
            {
                t.CaptureDataCore(CaptureStreamType.TradeHistory, CaptureMessageType.Incremental, e.Message);
            }

            JObject obj   = JsonConvert.DeserializeObject <JObject>(e.Message);
            JArray  items = obj.Value <JArray>("data");

            if (items == null)
            {
                return;
            }
            foreach (JObject item in items)
            {
                TradeInfoItem ti = new TradeInfoItem(null, t);
                ti.TimeString   = item.Value <string>("timestamp");
                ti.Type         = String2TradeType(item.Value <string>("side"));
                ti.AmountString = item.Value <string>("size");
                ti.RateString   = item.Value <string>("price");
                t.AddTradeHistoryItem(ti); //.InsertTradeHistoryItem(ti);
            }

            if (t.HasTradeHistorySubscribers)
            {
                TradeHistoryChangedEventArgs ee = new TradeHistoryChangedEventArgs()
                {
                    NewItem = t.TradeHistory.First()
                };
                t.RaiseTradeHistoryChanged(ee);
            }
        }
        public TickerDownloadData DownloadItem(TickerInputInfo info, bool downloadCandle)
        {
            Ticker ticker = info.Ticker;
            SimulationStrategyDataProvider provider = new SimulationStrategyDataProvider();

            provider.DownloadProgressChanged += DownloadProgressChanged;
            if (downloadCandle)
            {
                ResizeableArray <CandleStickData> kline = provider.DownloadCandleStickData(info);
                if (kline == null)
                {
                    LogManager.Default.Error("Cannot download candlesticks for " + ticker.Name);
                    return(null);
                }

                LogManager.Default.Success("Downloaded candlesticks for " + ticker.Name);
                ticker.CandleStickData.AddRange(kline);
            }

            ResizeableArray <TradeInfoItem> trades = provider.DownloadTradeHistory(info, info.StartDate);

            if (trades == null)
            {
                LogManager.Default.Error("Cannot download trade history for " + ticker.Name);
                return(null);
            }
            LogManager.Default.Success("Downloaded trade history for " + ticker.Name);
            ticker.AddTradeHistoryItem(trades);

            TickerDownloadData tradeInfo = new TickerDownloadData()
            {
                Ticker = ticker
            };

            tradeInfo.HistogrammIntervalSec = HistogrammIntervalSec;
            return(tradeInfo);
        }
        public void ApplySnapshot(JObject jObject, Ticker ticker)
        {
            ticker.OrderBook.BeginUpdate();
            try {
                ticker.OrderBook.Clear();
                OrderBook orderBook = ticker.OrderBook;

                JArray jbids = jObject.Value <JArray>("Z");
                JArray jasks = jObject.Value <JArray>("S");

                List <OrderBookEntry> entries         = orderBook.Asks;
                List <OrderBookEntry> entriesInverted = orderBook.AsksInverted;
                for (int i = 0; i < jasks.Count; i++)
                {
                    JObject item = (JObject)jasks[i];
                    entries.Add(new OrderBookEntry()
                    {
                        ValueString = item.Value <string>("R"), AmountString = item.Value <string>("Q")
                    });
                    if (entriesInverted != null)
                    {
                        entriesInverted.Insert(0, new OrderBookEntry()
                        {
                            ValueString = item.Value <string>("R"), AmountString = item.Value <string>("Q")
                        });
                    }
                }
                entries = orderBook.Bids;
                for (int i = 0; i < jbids.Count; i++)
                {
                    JObject item = (JObject)jbids[i];
                    entries.Add(new OrderBookEntry()
                    {
                        ValueString = item.Value <string>("R"), AmountString = item.Value <string>("Q")
                    });
                }
            }
            finally {
                ticker.OrderBook.IsDirty = false;
                ticker.OrderBook.EndUpdate();
            }

            ticker.ClearTradeHistory();
            JArray jtrades = jObject.Value <JArray>("f");

            foreach (JObject item in jtrades)
            {
                TradeInfoItem t = new TradeInfoItem(null, ticker);
                t.AmountString = item.Value <string>("Q");
                t.RateString   = item.Value <string>("P");
                t.Time         = new DateTime(Convert.ToInt64(item.Value <string>("T"))).ToLocalTime();
                t.TimeString   = t.Time.ToLongTimeString();
                t.Type         = (item.Value <string>("OT")) == "BUY" ? TradeType.Buy : TradeType.Sell;
                ticker.AddTradeHistoryItem(t);
            }

            if (ticker.HasTradeHistorySubscribers)
            {
                ticker.RaiseTradeHistoryChanged(new TradeHistoryChangedEventArgs()
                {
                    NewItems = ticker.TradeHistory
                });
            }
        }