/
TradeOverlayExampleViewModel.cs
115 lines (97 loc) · 4.65 KB
/
TradeOverlayExampleViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2022. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// TradeOverlayExampleViewModel.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SciChart.Charting.Model.ChartSeries;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.ViewportManagers;
using SciChart.Charting.Visuals.Annotations;
using SciChart.Examples.ExternalDependencies.Common;
using SciChart.Examples.ExternalDependencies.Data;
namespace SciChart.Examples.Examples.AnnotateAChart.OverlayTradeMarkers
{
public class TradeOverlayExampleViewModel : BaseViewModel
{
private IDataSeries<DateTime, double> _chartDataSeries;
private ObservableCollection<IAnnotationViewModel> _annotations;
private DefaultViewportManager _viewportManager;
public TradeOverlayExampleViewModel()
{
// Create some data to show on the chart
_chartDataSeries = new XyDataSeries<DateTime, double>();
_chartDataSeries.SeriesName = "CL FUT JUN15 2013";
// Get some price data, trades
List<Trade> trades;
List<NewsEvent> newsEvents;
DataManager.Instance.SetRandomSeed(0); // required for UIAutomationTesting
var priceData = DataManager.Instance.GetRandomTrades(out trades, out newsEvents);
// Buffer above and append all in one go to avoid multiple recalculations of series range
_chartDataSeries.Append(priceData.TimeData, priceData.CloseData);
// Create annotations
TradeAnnotations = CreateAnnotations(trades, newsEvents);
_chartDataSeries.InvalidateParentSurface(RangeMode.ZoomToFit);
}
public ObservableCollection<IAnnotationViewModel> TradeAnnotations
{
get { return _annotations; }
set
{
_annotations = value;
OnPropertyChanged("TradeAnnotations");
}
}
public IDataSeries<DateTime, double> ChartDataSeries
{
get { return _chartDataSeries; }
set
{
_chartDataSeries = value;
OnPropertyChanged("ChartDataSeries");
}
}
private static ObservableCollection<IAnnotationViewModel> CreateAnnotations(IEnumerable<Trade> trades, List<NewsEvent> newsEvents)
{
var annotations = new ObservableCollection<IAnnotationViewModel>();
foreach (var trade in trades)
{
var annotation = trade.BuySell == BuySell.Buy ? new BuyMarkerAnnotationViewModel() : (IBuySellAnnotationViewModel)new SellMarkerAnnotationViewModel();
// The datacontext allows the tooltip inside the buy or sell marker to bind to elements of the Trade
annotation.TradeData = trade;
// X1,Y1 we set up manually
annotation.X1 = trade.TradeDate;
annotation.Y1 = trade.DealPrice;
annotations.Add(annotation);
}
foreach(var newsEvent in newsEvents)
{
var annotation = new NewsBulletAnnotationViewModel();
// The datacontext allows the tooltip to bind to news data
annotation.NewsData = newsEvent;
// X1 is equal to the news date
annotation.X1 = newsEvent.EventDate;
// Y1 is set to 0.99, which is the just inside the vertical bottom edge of the chart in relative mode
annotation.Y1 = 0.99;
// Finally we use CoordinateMode.RelativeY to have a fractional coordinate on Y-Axis
// this ensures the news bullets always appear at the bottom of the chart regardless
// of YAxis.VisibleRange
annotation.CoordinateMode = AnnotationCoordinateMode.RelativeY;
annotations.Add(annotation);
}
return annotations;
}
}
}