forked from silahian/VisualHFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperPosition.cs
186 lines (170 loc) · 7.7 KB
/
HelperPosition.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using VisualHFT.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Threading;
using System.Timers;
using System.Threading.Tasks;
using System.Threading;
using System.Data.Entity;
using System.Windows;
namespace VisualHFT.Helpers
{
public enum ePOSITION_LOADING_TYPE
{
WEBSOCKETS,
DATABASE
}
public class HelperPosition : IDisposable
{
private const int POLLING_INTERVAL = 5000; // Interval for polling the database
private long? _LAST_POSITION_ID = null;
private List<VisualHFT.Model.Position> _positions;
private DateTime? _sessionDate = null;
private readonly System.Timers.Timer _timer;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); // Added cancellation token source
private readonly HFTEntities _DB = null;
private readonly object _lock = new object();
public event EventHandler<IEnumerable<VisualHFT.Model.Position>> OnInitialLoad;
public event EventHandler<IEnumerable<VisualHFT.Model.Position>> OnDataReceived;
protected virtual void RaiseOnInitialLoad(IEnumerable<VisualHFT.Model.Position> pos) => OnInitialLoad?.Invoke(this, pos);
protected virtual void RaiseOnDataReceived(IEnumerable<VisualHFT.Model.Position> pos) => OnDataReceived?.Invoke(this, pos);
public HelperPosition(ePOSITION_LOADING_TYPE loadingType)
{
_positions = new List<VisualHFT.Model.Position>();
this.LoadingType = loadingType;
if (loadingType == ePOSITION_LOADING_TYPE.DATABASE)
{
_timer = new System.Timers.Timer(POLLING_INTERVAL);
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
_timer_Elapsed(null, null);
_DB = new HFTEntities();
_DB.Database.CommandTimeout = 6000;
_DB.Configuration.ValidateOnSaveEnabled = false;
_DB.Configuration.AutoDetectChangesEnabled = false;
_DB.Configuration.LazyLoadingEnabled = false;
}
}
private async void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Stop(); // Stop the timer while the operation is running
if (_cancellationTokenSource.IsCancellationRequested) return; // Check for cancellation
var res = await GetPositionsAsync();
if (res != null && res.Any())
{
foreach (var p in res)
{
if (!p.PipsPnLInCurrency.HasValue || p.PipsPnLInCurrency == 0)
{
p.PipsPnLInCurrency = (p.GetCloseQuantity * p.GetCloseAvgPrice) - (p.GetOpenQuantity * p.GetOpenAvgPrice);
if (p.Side == ePOSITIONSIDE.Sell)
{
p.PipsPnLInCurrency *= -1;
}
}
HelperSymbol.Instance.UpdateData(p.Symbol);
}
if (this.Positions == null || !this.Positions.Any())
{
_positions = new List<VisualHFT.Model.Position>(res);
RaiseOnInitialLoad(this.Positions);
}
else
{
foreach (var p in res)
_positions.Insert(0, p);
RaiseOnDataReceived(res);
}
}
_timer.Start(); // Restart the timer once the operation is complete
}
public List<VisualHFT.Model.Position> Positions
{
get { return _positions; }
}
public ePOSITION_LOADING_TYPE LoadingType { get; set; }
public DateTime? SessionDate {
get { return _sessionDate; }
set
{
if (value != _sessionDate)
{
_sessionDate = value;
this.Positions.Clear();
RaiseOnInitialLoad(this.Positions);
_LAST_POSITION_ID = null;
_timer_Elapsed(null, null);
}
}
}
private async Task<IEnumerable<VisualHFT.Model.Position>> GetPositionsAsync()
{
if (!SessionDate.HasValue || _cancellationTokenSource.IsCancellationRequested) return null;
if (!SessionDate.HasValue)
return null;
return await Task.Run(() =>
{
lock (_lock)
{
try
{
var targetDate = SessionDate.Value.Date;
var simpleCheckOfNewRecords = _DB.Positions
.Where(x => DbFunctions.TruncateTime(x.CreationTimeStamp) == targetDate &&
(!_LAST_POSITION_ID.HasValue || x.ID > _LAST_POSITION_ID.Value))
.ToList();
if (!simpleCheckOfNewRecords.Any()) { return null; }
var allProviders = _DB.Providers.ToList();
var result = _DB.Positions.Include("OpenExecutions").Include("CloseExecutions").Where(x => DbFunctions.TruncateTime(x.CreationTimeStamp) == targetDate && (!_LAST_POSITION_ID.HasValue || x.ID > _LAST_POSITION_ID.Value)).ToList();
if (result.Any())
{
_LAST_POSITION_ID = result.Max(x => x.ID);
//DEPRECIATED
/*var ret = result.Select(x => new VisualHFT.Model.Position(x)).ToList(); //convert to our model
//find provider's name
ret.ForEach(x =>
{
x.CloseProviderName = allProviders.Where(p => p.ProviderCode == x.CloseProviderId).DefaultIfEmpty(new Provider()).FirstOrDefault().ProviderName;
x.OpenProviderName = allProviders.Where(p => p.ProviderCode == x.OpenProviderId).DefaultIfEmpty(new Provider()).FirstOrDefault().ProviderName;
x.CloseExecutions.ForEach(ex => ex.ProviderName = x.CloseProviderName);
x.OpenExecutions.ForEach(ex => ex.ProviderName = x.OpenProviderName);
});
return ret;*/
return new List<Position>();
}
return null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
}
});
//return null;
}
public void LoadNewPositions(IEnumerable<VisualHFT.Model.Position> positions)
{
if (positions == null || !positions.Any() || _cancellationTokenSource.IsCancellationRequested) return;
foreach (var p in positions)
{
var posToUpdate = this.Positions.Where(x => x.PositionID == p.PositionID).FirstOrDefault();
if (posToUpdate == null)
{
/*foreach (var ex in p.AllExecutions)
ex.Symbol = p.Symbol;
this.Positions.Add(p);*/
}
}
}
public void Dispose()
{
_timer?.Stop();
_timer?.Dispose();
_cancellationTokenSource?.Cancel(); // Cancel any ongoing operations
_cancellationTokenSource?.Dispose();
_DB.Dispose();
}
}
}