-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostAggContextProc.cs
383 lines (349 loc) · 16.7 KB
/
PostAggContextProc.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
namespace StockAnalysis
{
internal class PostAggContextProc: ThreadProcRequest
{
private const string CRITERIA_FILTER_DETAILS_FILE_NAME = "CriteriaFilteredDetail.csv";
private const string CRITERIA_FILTER_FILE_NAME = "CriteriaFiltered.csv";
private const string CRITERA_THRESHOLD_FILE_NAME = "CriteriaThresholdOutput.csv";
private const string FILTER_FILE_HEADING = "Aggregation Key,sharesheld,percentagesharesheld,value";
private StreamWriter criteriaFilterDetailsWriter;
private StreamWriter criteriaFilterWriter;
private StreamWriter criteriaThresholdWriter;
private static int postAggContextCount = 0;
private int m_CriteriaIndex;
private StringBuilder aggKeyContainer = new StringBuilder(); // these will the keys that will be part of one of the output files.
//private OutputFileWriter fileWriter;
private string writeDirectory;
public PostAggContextProc(int criteriaObjIndex)
{
m_CriteriaIndex = criteriaObjIndex;
Interlocked.Add(ref postAggContextCount, 1);
//fileWriter = new OutputFileWriter(outputDirectoryPath);
}
public override void Execute(ref bool destory)
{
string correctedCriteriaName;
int outstandingPostAggContext;
CriteriaSetObj aggCriteriaObj = Global.criteriaSets[m_CriteriaIndex];
ThresholdComparison typeOfComparison = aggCriteriaObj.postAggObj.comparison;
correctedCriteriaName = Regex.Replace(aggCriteriaObj.criteriaSetName, "[^a-zA-Z0-9]", "");
writeDirectory = Path.Combine(Global.outputDirectory, correctedCriteriaName);
Directory.CreateDirectory(writeDirectory);
criteriaFilterDetailsWriter = new StreamWriter(Path.Combine(writeDirectory, CRITERIA_FILTER_DETAILS_FILE_NAME));
criteriaFilterWriter = new StreamWriter(Path.Combine(writeDirectory, CRITERIA_FILTER_FILE_NAME));
criteriaThresholdWriter = new StreamWriter(Path.Combine(writeDirectory, CRITERA_THRESHOLD_FILE_NAME));
criteriaFilterWriter.WriteLine(FILTER_FILE_HEADING);
// TODO: write header lines for details and threshold files
switch (typeOfComparison)
{
case ThresholdComparison.Undefined:
{
// shouldn't EVER come in here, checks before this in CriteriaObj should prevent this.
break;
}
case ThresholdComparison.Max:
{
// TODO: call associated method.
break;
}
case ThresholdComparison.Crosses:
{
CrossesYesterdayValueComparison(aggCriteriaObj, typeOfComparison);
break;
}
case ThresholdComparison.GreaterThan:
case ThresholdComparison.GreaterThanOrEqualTo:
case ThresholdComparison.LessThan:
case ThresholdComparison.LessThanOrEqualTo:
{
ValueOfColumnComparison(aggCriteriaObj, typeOfComparison);
break;
}
default:
{
// TODO: check criteria set text file he gave us with all criteria sets.
break;
}
}
outstandingPostAggContext = Interlocked.Decrement(ref postAggContextCount);
if(outstandingPostAggContext == 0)
{
Global.mainThreadWait.Set();
}
}
private void CrossesYesterdayValueComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
{
int rawDataChunkIndex;
int stockDataIndex;
double columnValue = 0.0, thresholdCrossed = 0.0;
ThresholdComparison comparison = compare;
CriteriaSetObj criteria = criteriaObj;
ThresholdColumn crossesColumn = criteria.postAggObj.thresholdColumn;
IReadOnlyList<double> thresholds = criteria.postAggObj.GetRefToThresholdValues();
IReadOnlyDictionary<string, AggValues> todaysParsedDataDictionary = criteria.GetRefToAggResults();
IReadOnlyDictionary<string, AggValues> yesterdaysParsedDataDictionary = criteria.GetRefToYesterdayAggResults();
AggValues todaysAgg;
AggValues yestedayAgg;
bool keyExistsYesterday, todayGreaterYesterday;
if (crossesColumn == ThresholdColumn.Undefiend)
{
// TODO: do something else
return;
}
WriteThresholdHeader(criteria);
foreach(string key in todaysParsedDataDictionary.Keys)
{
todaysParsedDataDictionary.TryGetValue(key, out todaysAgg); // should always find something
keyExistsYesterday = yesterdaysParsedDataDictionary.TryGetValue(key, out yestedayAgg);
if(!keyExistsYesterday)
{
yestedayAgg = new AggValues(); // all values are 0 then.
}
switch (crossesColumn)
{
case ThresholdColumn.PrecentageSharesHeld:
{
columnValue = todaysAgg.percentageSharesHeld;
if(columnValue > yestedayAgg.percentageSharesHeld)
{
todayGreaterYesterday = true;
}
else
{
todayGreaterYesterday = false;
}
thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
break;
}
case ThresholdColumn.SharesHeld:
{
columnValue = todaysAgg.sharesHeld;
if (columnValue > yestedayAgg.sharesHeld)
{
todayGreaterYesterday = true;
}
else
{
todayGreaterYesterday = false;
}
thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
break;
}
case ThresholdColumn.Value:
{
columnValue = todaysAgg.value;
if (columnValue > yestedayAgg.value)
{
todayGreaterYesterday = true;
}
else
{
todayGreaterYesterday = false;
}
thresholdCrossed = PreciseThresholdCrossed(todayGreaterYesterday, columnValue, thresholds);
break;
}
default:
{
break;
}
}
// TODO: write to files
criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, todaysAgg));
IReadOnlyList<uint> unaggregatedDictEntry = todaysAgg.GetRefToRowsInChunk();
for (int i = 0; i < unaggregatedDictEntry.Count; i++)
{
rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
stockDataIndex = (int)unaggregatedDictEntry[i] & 0xffff;
StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
}
criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdCrossed));
}
criteriaFilterWriter.Dispose();
criteriaFilterDetailsWriter.Dispose();
criteriaThresholdWriter.Dispose();
}
private double PreciseThresholdCrossed(bool todayGreater, double todaysColumnValue, IReadOnlyList<double> thresholds)
{
double lastThresholdCrossed = 0.0;
if(todayGreater)
{
// lowest to highest threshold
for(int i = 0; i < thresholds.Count; i++)
{
if(todaysColumnValue > thresholds[i])
{
lastThresholdCrossed = thresholds[i];
}
else
{
return lastThresholdCrossed;
}
}
}
else
{
// highest to lowest threshold
for(int i = thresholds.Count - 1; i >= 0; i--)
{
if(todaysColumnValue < thresholds[i])
{
lastThresholdCrossed = thresholds[i];
}
else
{
return lastThresholdCrossed;
}
}
}
return lastThresholdCrossed;
}
private void ValueOfColumnComparison(CriteriaSetObj criteriaObj, ThresholdComparison compare)
{
int rawDataChunkIndex;
int stockDataIndex;
ThresholdComparison comparison = compare;
CriteriaSetObj criteria = criteriaObj;
ThresholdColumn column = criteria.postAggObj.thresholdColumn;
IReadOnlyDictionary<string, AggValues> readOnlyAggResults = criteria.GetRefToAggResults();
AggValues aggValue;
IReadOnlyList<double> thresholdValues = criteria.postAggObj.GetRefToThresholdValues(); // should only contain 1 value.
double thresholdValue = thresholdValues[0]; // should be only 1 value here'
double columnValue = 0.0;
bool thresholdCheck = false;
if(column == ThresholdColumn.Undefiend)
{
// TODO: Do something if it is undefined, shouldn't be.
return;
}
WriteThresholdHeader(criteria);
foreach (string key in readOnlyAggResults.Keys)
{
readOnlyAggResults.TryGetValue(key, out aggValue);
switch(column)
{
case ThresholdColumn.PrecentageSharesHeld:
{
columnValue = aggValue.percentageSharesHeld;
thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
break;
}
case ThresholdColumn.SharesHeld:
{
columnValue = aggValue.sharesHeld;
thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
break;
}
case ThresholdColumn.Value:
{
columnValue = aggValue.value;
thresholdCheck = ValuePassesComparison(columnValue, thresholdValue, comparison);
break;
}
default:
{
break;
}
}
if (thresholdCheck)
{
criteriaFilterWriter.WriteLine(GetAggValuesCommaSeparatedString(key, aggValue));
IReadOnlyList<uint> unaggregatedDictEntry = aggValue.GetRefToRowsInChunk();
for (int i = 0; i < unaggregatedDictEntry.Count; i++)
{
rawDataChunkIndex = (int)unaggregatedDictEntry[i] >> 16;
stockDataIndex = (int)unaggregatedDictEntry[i] & 0xffff;
StockDataEntry sde = Global.parsedRawData[rawDataChunkIndex].chunkDataParsed[stockDataIndex];
criteriaFilterDetailsWriter.WriteLine(GetCommaSeparatedStockDataEntryWithKey(sde, key));
}
criteriaThresholdWriter.WriteLine(GetCommaSeparatedThresholdEntry(key, columnValue, thresholdValue));
}
}
criteriaFilterWriter.Dispose();
criteriaFilterDetailsWriter.Dispose();
criteriaThresholdWriter.Dispose();
}
private string GetCommaSeparatedThresholdEntry(string key, double columnValue, double thresholdValue)
{
return key + "," + Convert.ToString(columnValue) + "," + Convert.ToString(thresholdValue);
}
private void WriteThresholdHeader(CriteriaSetObj criteriaSet)
{
// TODO: create constants
criteriaThresholdWriter.WriteLine("Set," + criteriaSet.criteriaSetName);
criteriaThresholdWriter.WriteLine("Date," + DateTime.Today.ToString("MM/dd/yyyy"));
if(criteriaSet.aggSpecsObj.stockCode)
{
criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.STOCK_CODE);
}
if(criteriaSet.aggSpecsObj.stockType)
{
criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.STOCK_TYPE);
}
if(criteriaSet.aggSpecsObj.holderId)
{
criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.HOLDER_ID);
}
if(criteriaSet.aggSpecsObj.direction)
{
criteriaThresholdWriter.WriteLine("Agg Key Column," + CriteriaConstants.DIRECTION);
}
criteriaThresholdWriter.WriteLine();
criteriaThresholdWriter.WriteLine("Agg Key,Column Value,Threshold Crossed");
}
private string GetCommaSeparatedStockDataEntryWithKey(StockDataEntry sde, string key)
{
return sde.stockCode + "," + sde.stockType + "," + sde.holderId + "," + sde.holderCountry + "," + Convert.ToString(sde.sharesHeld) + "," + Convert.ToString(sde.precentageSharesHeld) + "," + sde.direction + "," + Convert.ToString(sde.value) + "," + key;
}
private string GetAggValuesCommaSeparatedString(string key, AggValues agg)
{
return key + "," + Convert.ToString(agg.percentageSharesHeld) + "," + Convert.ToString(agg.sharesHeld) + "," + Convert.ToString(agg.value);
}
private bool ValuePassesComparison(double columnValue, double thresholdValue, ThresholdComparison tc)
{
switch(tc)
{
case ThresholdComparison.GreaterThan:
{
if (columnValue > thresholdValue)
{
return true;
}
break;
}
case ThresholdComparison.GreaterThanOrEqualTo:
{
if (columnValue >= thresholdValue)
{
return true;
}
break;
}
case ThresholdComparison.LessThan:
{
if (columnValue < thresholdValue)
{
return true;
}
break;
}
case ThresholdComparison.LessThanOrEqualTo:
{
if(columnValue <= thresholdValue)
{
return true;
}
break;
}
}
return false;
}
}
}