예제 #1
0
        private void InitHistory(string data)
        {
            List <Candle> list;
            Candles       candles;

            using (var reader = new StringReader(data))
            {
                list = new List <Candle>();
                if (_candleSerializer == null)
                {
                    _candleSerializer = new XmlSerializer(typeof(Candles));
                }
                candles =
                    (Candles)
                    _candleSerializer.Deserialize(reader);
                reader.Close();
            }
            //check if seccode matches
            if (candles.Seccode != Seccode)
            {
                return;
            }
            //check if timeframe matches
            if (candles.Candle.Count > 1 && candles.Period != _periodId.ToString())
            {
                return;
            }

            list = candles.Candle;
            list =
                list.Where(
                    item =>
                    item.TradeTime.Date >= DateTime.Today.AddDays(-_daysBack)).ToList();

            if (list.Count == 0)
            {
                return;
            }
            _dispatcher.Invoke(() =>
            {
                try
                {
                    StockChartAnnotations.RemoveWhere(
                        a => a is HorizontalLineAnnotation && ((HorizontalLineAnnotation)a).Name == "history");
                }
                catch
                {
                }
            }
                               )
            ;
            OhlcDataSeries = new OhlcDataSeries <DateTime, double> {
                AcceptsUnsortedData = true
            };
            OhlcDataSeries.Append(GetDateTime(list), GetValues(list));

            XyDataSeries = new XyDataSeries <DateTime, int>();
            XyDataSeries.Append(GetDateTime(list), GetVolumes(list));
            //Add trendlines

            if (OhlcDataSeries.Count == 0)
            {
                return;
            }
            var dates =
                OhlcDataSeries.XValues.GroupBy(date => date.Date).Select(group => group.First().Date).ToList();

            _lastCandle = list.Last();
            if (_selectedTimeframe != "Day")
            {
                for (var i = 0; i < dates.Count; i++)
                {
                    var date = dates[i];
                    if (i == dates.Count - 1) //for today (only open)
                    {
                        AddAnnotation("history",
                                      list.Where(item => Equals(item.TradeTime.Date, date))
                                      .Select(item => item.Open)
                                      .First(), "Open");
                    }
                    else //for other days
                    {
                        AddAnnotation("history",
                                      list.Where(item => Equals(item.TradeTime.Date, date))
                                      .Select(item => item.Open)
                                      .First(), "Open");
                        AddAnnotation("history",
                                      list.Where(item => Equals(item.TradeTime.Date, date))
                                      .Select(item => item.Close)
                                      .Last(), "Close");
                        AddAnnotation("history",
                                      list.Where(item => Equals(item.TradeTime.Date, date)).Select(item => item.High).Max(),
                                      "High");
                        AddAnnotation("history",
                                      list.Where(item => Equals(item.TradeTime.Date, date)).Select(item => item.Low).Min(),
                                      "Low");
                    }
                    _dispatcher.Invoke(() =>
                    {
                        //Vertical date annotations
                        try
                        {
                            StockChartAnnotations.Add(new VerticalLineAnnotation
                            {
                                Name             = "history",
                                Stroke           = new SolidColorBrush(Colors.DarkGray),
                                X1               = OhlcDataSeries.FindIndex(OhlcDataSeries.XValues.First(x => x.Date == date)),
                                IsEditable       = false,
                                Opacity          = 0.5,
                                AnnotationLabels =
                                    new ObservableCollection <AnnotationLabel>
                                {
                                    new AnnotationLabel
                                    {
                                        LabelPlacement = LabelPlacement.Axis,
                                        Foreground     = Brushes.White,
                                        Text           = date.ToShortDateString()
                                    }
                                }
                            });
                        }
                        catch
                        {
                        }
                    });
                }
            }
            try
            {
                SharedXRange = new IndexRange(0, OhlcDataSeries.Count + 3);
                YRange       = new DoubleRange(OhlcDataSeries.LowValues.Min(), OhlcDataSeries.HighValues.Max());
            }
            catch { }
            TXmlConnector.SendNewCandles -= ProcessCandles;
        }