-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalTrading.cpp
289 lines (225 loc) · 7.64 KB
/
FinalTrading.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
#include <iostream>
#include <list>
#include <map>
#include <vector>
#include <string.h>
#include <time.h>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
typedef struct{
string ID;
string type;
string name;
float rate;
int num;
string expiry;
string date;
}Scrip;
typedef list<Scrip> ScripList;
typedef map<string,ScripList> TradeList;
void checkExpiry (TradeList &, TradeList & );
void doTransaction (TradeList &, TradeList & );
void populateList (TradeList &, TradeList &, string req);
string Current_Time(time_t *Current_Time_time_t);
vector<string> tokenize(string sent, char sep);
string tostring(int i);
int ConvertStringToNumber(string num);
bool isAlive(string, string);
int ConvertTimeToSeconds(string);
void PrintScrip (Scrip);
int main(int argc, char *argv[]) {
ifstream FIFO;
string Order = "";
TradeList SellListAll, BuyListAll;
while (Order != "x@x@EXCHCLOSED@x@x@x") {
FIFO.open("FIFO");
while (FIFO >> Order){
time_t CT;
time(&CT);
cout << "Current Time Is: " << Current_Time(&CT) << endl;
cout << Order << endl;
populateList (BuyListAll,SellListAll,Order);
checkExpiry (BuyListAll,SellListAll);
//doTransaction (BuyListAll, SellListAll);
}
FIFO.close();
}
}
void PrintScrip(Scrip s){
cout << " " << s.name << s.rate << s.type << endl;
}
int ConvertTimeToSeconds(string s){
vector<string> ts = tokenize(s,':');
int sec = 0;
sec = (ConvertStringToNumber(ts[0]))*60*60 + (ConvertStringToNumber(ts[1]))*60 + ConvertStringToNumber(ts[2]);
return sec;
}
bool isAlive(string ptime, string ctime){
if ( ConvertTimeToSeconds(ptime) > ConvertTimeToSeconds(ctime) )
return true;
else
return false;
}
string Current_Time(time_t *Current_Time_time_t) {
struct tm *Current_Time_tm;
string tmp_hour;
string tmp_min;
string tmp_sec;
string tmp_Time;
Current_Time_tm = localtime(Current_Time_time_t);
mktime(Current_Time_tm);
tmp_hour = tostring(Current_Time_tm->tm_hour);
tmp_min = tostring(Current_Time_tm->tm_min);
tmp_sec = tostring(Current_Time_tm->tm_sec);
tmp_Time = tmp_hour + ":" + tmp_min + ":" + tmp_sec;
return tmp_Time;
}
// Converts int to string
string tostring(int i) {
stringstream converter_stream;
string converted_string;
converter_stream << i;
converter_stream >> converted_string;
return converted_string;
}
int ConvertStringToNumber(string num){
char *chararr1;
int pos = 0;
chararr1 = new char[num.length()];
for ( int i = 0; i < num.length() ; i++){
if(num[i]!='.'){
chararr1[pos] = num[i];
pos++;
}
}
int number = atoi (chararr1);
return number;
}
void populateList (TradeList &BuyListAll, TradeList &SellListAll, string req){
vector<string> tokenized_string = tokenize (req,'@');
TradeList::iterator it;
Scrip s;
s.ID = tokenized_string[0];
s.type = tokenized_string[1];
s.name = tokenized_string[2];
s.rate = ConvertStringToNumber(tokenized_string[3]);
s.num = ConvertStringToNumber(tokenized_string[4]);
vector<string> expiryTime = tokenize(tokenized_string[5],'$');
s.expiry = expiryTime[1];
s.date = expiryTime[0];
if ( (s.type).compare("B") == 0 ){
cout << "entering this part B" <<endl;
//PrintScrip(s);
it = BuyListAll.find(s.name);
if ( it != BuyListAll.end() ) // key exists already and needs to be updated
(it -> second).push_back(s);
else{
ScripList SL;
SL.push_back(s);
BuyListAll[s.name] = SL;
}
//BuyListAll[s.name] = s;
}
else{
cout << "entering this part S" <<endl;
//PrintScrip(s);
it = SellListAll.find(s.name);
if ( it != SellListAll.end() ) // key exists already and needs to be updated
(it -> second).push_back(s);
else{
ScripList SL;
SL.push_back(s);
SellListAll[s.name] = SL;
}
//SellListAll[s.name] = s;
}
}
vector<string> tokenize(string sent, char sep){
vector<string> word_collect;
string::iterator it_start,it_end;
it_start = sent.begin();
it_end = sent.begin();
int mark_start = 0, mark_end = 0;
while ( it_end < sent.end() ){
if ( *it_end == sep ){
if ( mark_start != mark_end )
word_collect.push_back(sent.substr(mark_start,mark_end-mark_start));
it_start = it_end+1;
mark_start = mark_end+1;
}
it_end++;
mark_end++;
}
if ( mark_start != mark_end )
word_collect.push_back(sent.substr(mark_start,mark_end-mark_start));
return word_collect;
}
void checkExpiry ( TradeList &BuyListAll, TradeList &SellListAll ){
cout << "Checking expiry once " << endl;
TradeList::iterator buy,sell;
ScripList::iterator b1,s1;
ScripList blist, slist;
time_t CT;
time(&CT);
string CurrentTime = Current_Time(&CT);
for ( sell = SellListAll.begin() ; sell != SellListAll.end() ; sell++ ){
slist = sell->second;
for ( s1 = slist.begin(); s1 != slist.end() ; s1++ ){
if ( !isAlive ( (*s1).expiry, CurrentTime ) ){
//print to file
PrintScrip(*s1);
(sell->second).erase(s1);
cout << "Erasing a record once since it is consumed" << endl;
}
}
}
for ( buy = BuyListAll.begin() ; buy != BuyListAll.end() ; buy++ ){
blist = buy->second;
for ( b1 = blist.begin(); b1 != blist.end() ; b1++ ){
if ( !isAlive ( (*b1).expiry, CurrentTime ) ){
//print to file
PrintScrip(*b1);
(buy->second).erase(b1);
cout << "Erasing a record once since it is consumed" << endl;
}
}
}
}
void doTransaction ( TradeList &BuyListAll, TradeList &SellListAll ){
TradeList::iterator buy,sell;
bool tradeflag = true;
for ( sell = SellListAll.begin() ; sell != SellListAll.end() ; sell++ ){
buy = BuyListAll.find(sell->first);
if ( buy != BuyListAll.end() ){ // there is a buyer
ScripList blist = buy->second;
ScripList slist = sell->second;
ScripList::iterator b1,s1;
s1 = slist.begin();//check the first sale item
for ( b1 = blist.begin(); b1 != blist.end() && tradeflag ; b1++ ){//check the buyer list entirely
if ( (*b1).rate > (*s1).rate ){
if ( (*b1).num >= (*s1).num ){//all sold
// *************print to file*************** //
PrintScrip(*s1);
PrintScrip(*b1);
(*b1).num -= (*s1).num;
(sell->second).erase(s1);
if ( (*b1).num == 0 )
(buy->second).erase(b1);
tradeflag = false;
}
if ( (*b1).num < (*s1).num ){//partially sold
// *************print to file*************** //
PrintScrip(*s1);
PrintScrip(*b1);
(*s1).num -= (*b1).num;
(buy->second).erase(b1);
}
}
}
}
}
}