-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarketIndexDataContainer.cpp
319 lines (236 loc) · 8.32 KB
/
MarketIndexDataContainer.cpp
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
#include "MarketIndexDataContainer.h"
CMarketIndexDataContainer::CMarketIndexDataContainer(void)
{
}
CMarketIndexDataContainer::~CMarketIndexDataContainer(void)
{
}
void CMarketIndexDataContainer::TrimSecs (std::string& stTime)
{
stTime = stTime.substr(0, 12);
}
bool CMarketIndexDataContainer::DayKeyPresent (int nDayHour, std::string& stDay)
{
try
{
char psz[10];
itoa(nDayHour, psz, 10);
std::string stDayHour = std::string(psz);
if(stDayHour.length() < 4)
stDayHour = "0" + stDayHour;
if(stDayHour[0] == stDay[0])
if(stDayHour[1] == stDay[1])
return true;
}
catch(std::exception* ex)
{
std::cout<<std::endl<<ex->what()<<std::endl;
}
return false;
}
void CMarketIndexDataContainer::ExtractDayHourMin (std::string& stTime, int& nDayHour, int& nMin)
{
//stTime example is 200512211444 //YYYYMMDDHHMMSSSS
char pszDayHour[5];
char pszMin[3];
TrimSecs(stTime);//YYYYMMDDHHMM
stTime.copy(pszDayHour, 4, 6);
stTime.copy(pszMin, 2, 10);
nDayHour = atoi(pszDayHour);
nMin = atoi(pszMin);
}
void CMarketIndexDataContainer::AddIndex (std::string & stMarket, std::string & stTradeTime, std::string& stMktSector, std::string& stSectorIndex)
{
try
{
int nDayHour = 0;
int nMin = 0;
ExtractDayHourMin(stTradeTime, nDayHour, nMin);
double dSectorIndex = 0.0;
char* stopstring = '\0';
dSectorIndex = strtod(stSectorIndex.c_str(), &stopstring);
Map_Minute_Index mp_MinInd;//Set Minute-MarketIndex Pair
mp_MinInd[nMin] = dSectorIndex;
Map_Hour_Min_Index mp_HrMinInd;
mp_HrMinInd[nDayHour] = mp_MinInd;//Associate Hour with Minute-MarketIndex Pair
Map_SectorName_Hour_Min_Index mp_SectorHrMinInd;
mp_SectorHrMinInd[stMktSector] = mp_HrMinInd;//Associate MktSector with DayHour-Minute-MarketIndex Pair
It_MktIndexData iterFirstLevel = mp_MktIndexContainer.find(stMarket);
if(iterFirstLevel != mp_MktIndexContainer.end() )//If this Market Exists
{
Map_SectorName_Hour_Min_Index* mpSecondLevel = &(iterFirstLevel->second);
It_SectorName_Hour_Min_Index iterSecondLevel = mpSecondLevel->find(stMktSector);
if(iterSecondLevel != mpSecondLevel->end())//If this Market Sector Exists
{
Map_Hour_Min_Index* mpThirdLevel = &(iterSecondLevel->second);
It_Hour_Min_Index iterThirdLevel = mpThirdLevel->find(nDayHour);
if(iterThirdLevel != mpThirdLevel->end())//If this DayHour Exists
{
Map_Minute_Index* mpMinuteIndex = &(iterThirdLevel->second);
(*mpMinuteIndex)[nMin] = dSectorIndex;//Add the index
}
else//Add this DayHour in this MarketSector
{
(*mpThirdLevel)[nDayHour] = mp_MinInd;
}
}
else//Add this MarketSector in this Market
{
(*mpSecondLevel)[stMktSector] = mp_HrMinInd;
}
}
else//Add the Market along with its index data
{
//Now associate Market with MktSector-DayHour-Minute-MarketIndex Pair
mp_MktIndexContainer[stMarket] = mp_SectorHrMinInd;
}
}
catch(std::exception* ex)
{
std::cout<<std::endl<<ex->what()<<std::endl;
}
}
bool CMarketIndexDataContainer::GetDayHourMarketIndGraphData(std::string& stMktKey, std::string& stDayHour, std::string& stMktGraphData)
{
try
{
char pszHour[3];
char pszDay[3];
stDayHour.copy(pszHour, 2, 2);
stDayHour.copy(pszDay, 2, 0);
int nHour = atoi(pszHour);
int nDay = atoi(pszDay);
int nDayHour = atoi(stDayHour.c_str());
if(mp_MktIndexContainer.find(stMktKey) != mp_MktIndexContainer.end())
{
It_SectorName_Hour_Min_Index it_SectorName_Hour_Min_Index = mp_MktIndexContainer[stMktKey].begin();
int nSectors = 0;
const std::string* stSectorName = NULL;
bool bDayHourRecordExist = false;
//Extracting Hourly Data From Each "Sector"
for(; it_SectorName_Hour_Min_Index != mp_MktIndexContainer[stMktKey].end(); it_SectorName_Hour_Min_Index++)
{
nSectors++;
stSectorName = &(it_SectorName_Hour_Min_Index->first);
Map_Hour_Min_Index* mp_Hour_Min_Index = &(it_SectorName_Hour_Min_Index->second);
It_Hour_Min_Index it_Hour_Min_Index = mp_Hour_Min_Index->find(nDayHour);
if(it_Hour_Min_Index != mp_Hour_Min_Index->end())//If Minutely Data Exists for this hour, in this Day
{
bDayHourRecordExist = true;
bool bFirstRecordOfThisHour = false;
Map_Minute_Index* mp_Min_Index = &(it_Hour_Min_Index->second);
It_Minute_Index it_Minute_Index = mp_Min_Index->begin();
for(; it_Minute_Index != mp_Min_Index->end(); it_Minute_Index++)
{
int nMin = it_Minute_Index->first;
double dIndex = it_Minute_Index->second;
char pszMin[3];
char pszIndex[20];
itoa(nMin, pszMin, 10);
_gcvt(dIndex, 10, pszIndex);
std::string stMin = std::string(pszMin);
std::string stIndex = std::string(pszIndex);
if(stIndex[stIndex.length() - 1] == '.')
stIndex += "00";
if(!bFirstRecordOfThisHour)
{
if(nSectors > 1)
stMktGraphData += "|" + *stSectorName + "?";//adding Sector Name
else
stMktGraphData += *stSectorName + "?";//adding Sector Name
stMktGraphData += stMin + ";" + stIndex;
}
else
stMktGraphData += "?" + stMin + ";" + stIndex;
bFirstRecordOfThisHour = true;
}
}
}
if(!bDayHourRecordExist)
return false;
stMktGraphData += "~\r\n";
return true;
}
}
catch(std::exception* ex)
{
std::cout<<std::endl<<ex->what()<<std::endl;
}
return false;
}
bool CMarketIndexDataContainer::GetDayMarketIndGraphData(std::string& stMktKey, std::string& stDay, std::string& stMktGraphData)
{
try
{
if(mp_MktIndexContainer.find(stMktKey) != mp_MktIndexContainer.end())
{
It_SectorName_Hour_Min_Index it_SectorName_Hour_Min_Index = mp_MktIndexContainer[stMktKey].begin();
int nSectors = 0;
const std::string* stSectorName = NULL;
bool bDayRecordExist = false;
//Extracting Hourly Data From Each "Sector"
for(; it_SectorName_Hour_Min_Index != mp_MktIndexContainer[stMktKey].end(); it_SectorName_Hour_Min_Index++)
{
nSectors++;
stSectorName = &(it_SectorName_Hour_Min_Index->first);
Map_Hour_Min_Index* mp_Hour_Min_Index = &(it_SectorName_Hour_Min_Index->second);
It_Hour_Min_Index it_Hour_Min_Index = mp_Hour_Min_Index->begin();
bool bFirstRecordOfThisSector = false;
for(; it_Hour_Min_Index != mp_Hour_Min_Index->end(); it_Hour_Min_Index++)
{
int nDayHour = it_Hour_Min_Index->first;
if(DayKeyPresent(nDayHour, stDay))
{
bDayRecordExist = true;
Map_Minute_Index* mp_Min_Index = &(it_Hour_Min_Index->second);
It_Minute_Index it_Minute_Index = mp_Min_Index->begin();
double dIndex = 0.0;
int count = 0;
for(; it_Minute_Index != mp_Min_Index->end(); it_Minute_Index++)
{
dIndex += it_Minute_Index->second;
count++;
}
if (count == 0)
continue;
dIndex = dIndex/count;
char pszIndex[20];
char pszHour[20];
itoa(nDayHour, pszHour, 10);
_gcvt(dIndex, 10, pszIndex);
std::string stIndex = std::string(pszIndex);
std::string stHour = std::string(pszHour);
if(stHour.length() == 4)
stHour = stHour.substr(2, 2);
else if(stHour.length() == 3)
stHour = stHour.substr(1, 2);
if(stIndex[stIndex.length() - 1] == '.')
stIndex += "00";
if(!bFirstRecordOfThisSector)
{
if(nSectors > 1)
stMktGraphData += "|" + *stSectorName + "?";//adding Sector Name
else
stMktGraphData += *stSectorName + "?";//adding Sector Name
stMktGraphData += stHour + ";" + stIndex;
}
else
stMktGraphData += "?" + stHour + ";" + stIndex;
bFirstRecordOfThisSector = true;
}
}
}
if(nSectors == 0)
return false;
if(!bDayRecordExist)
return false;
stMktGraphData += "~\r\n";
return true;
}
}
catch(std::exception* ex)
{
std::cout<<std::endl<<ex->what()<<std::endl;
}
return false;
}