forked from visualHFT/VisualHFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmChartStudy.cs
126 lines (111 loc) · 3.79 KB
/
vmChartStudy.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
116
117
118
119
120
121
122
123
124
125
126
using System.Collections.ObjectModel;
using VisualHFT.Studies;
using VisualHFT.Model;
using Prism.Mvvm;
using VisualHFT.Helpers;
using System.Windows.Threading;
using System;
using System.Windows;
using System.Collections.Generic;
using VisualHFT.Commons.Studies;
using VisualHFT.UserSettings;
using VisualHFT.ViewModel;
using System.Windows.Input;
namespace VisualHFT.ViewModels
{
public class vmChartStudy : BindableBase, IDisposable
{
private bool _disposed = false; // to track whether the object has been disposed
private IStudy _study;
private ISetting _settings;
private string _selectedSymbol;
private VisualHFT.ViewModel.Model.Provider _selectedProvider;
private AggregatedCollection<BaseStudyModel> _rollingValues;
private readonly object _locker = new object();
private int _MAX_ITEMS = 500;
private UIUpdater uiUpdater;
public vmChartStudy(IStudy study)
{
_study = study;
_settings = ((PluginManager.IPlugin)_study).Settings;
InitializeData();
StudyAxisTitle = _study.TileTitle;
RaisePropertyChanged(nameof(StudyAxisTitle));
OpenSettingsCommand = new RelayCommand<vmTile>(OpenSettings);
_study.OnCalculated += _study_OnCalculated;
uiUpdater = new UIUpdater(uiUpdaterAction);
}
~vmChartStudy()
{
Dispose(false);
}
public string StudyTitle { get; set; }
public string StudyAxisTitle { get; set; }
public ICommand OpenSettingsCommand { get; set; }
public IReadOnlyList<BaseStudyModel> ChartData
{
get
{
lock (_locker)
return _rollingValues.AsReadOnly();
}
}
private void _study_OnCalculated(object? sender, BaseStudyModel e)
{
lock (_locker)
{
_rollingValues.Add(e);
}
}
private void uiUpdaterAction()
{
RaisePropertyChanged(nameof(ChartData));
}
private void OpenSettings(object obj)
{
PluginManager.PluginManager.SettingPlugin((PluginManager.IPlugin)_study);
_settings = ((PluginManager.IPlugin)_study).Settings;
InitializeData();
}
private void InitializeData()
{
_rollingValues = new AggregatedCollection<BaseStudyModel>(_settings.AggregationLevel,
_MAX_ITEMS,
x => x.Timestamp,
(BaseStudyModel existing, BaseStudyModel newItem) =>
{
existing.MarketMidPrice = newItem.MarketMidPrice;
existing.Value = newItem.Value;
});
StudyTitle = _study.TileTitle + " " +
_settings.Symbol + "-" +
_settings.Provider?.ProviderName + " " +
"[" + _settings.AggregationLevel.ToString() + "]";
RaisePropertyChanged(nameof(StudyTitle));
_selectedSymbol = _settings.Symbol;
_selectedProvider = new ViewModel.Model.Provider(_settings.Provider);
uiUpdaterAction();
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
uiUpdater.Dispose();
if (_study != null)
{
_study.OnCalculated -= _study_OnCalculated;
_study.Dispose();
}
}
_disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}