-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstockSimulation.cpp
More file actions
385 lines (324 loc) · 11 KB
/
Copy pathstockSimulation.cpp
File metadata and controls
385 lines (324 loc) · 11 KB
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
384
385
/*
Name: Carmela Coreen Pilande
Program: The user inputs a stock file, price file, number of days
for simulation, and amount to be transferred into the account. Using this info,
it finds the profit per day, the remaining balance, and the amount in dividends.
*/
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
struct stockType
{
std::string name;
int timesPurchased;
double dividend;
double price;
stockType() // Default Constructor
{
name = "";
timesPurchased = dividend = price = 0;
}
stockType(int price) // Constructor with inputted price
{
name = "";
timesPurchased = dividend = price = 0;
}
// Checks if the left-hand value is less than the right-hand value
bool operator<(const stockType &rhs)
{
if (price < rhs.price)
return true;
else
return false;
}
// Copies the stockType object information
void operator=(const stockType &rhs)
{
this->name = rhs.name;
timesPurchased = rhs.timesPurchased;
dividend = rhs.dividend;
price = rhs.price;
}
};
template <class Type>
class priorityQ
{
public:
priorityQ(int cap) // Constructor
{
capacity = cap;
heapArray = new stockType[cap]; // allocates heap array
size = 0; // set the size variable
};
priorityQ(std::vector<Type> vec) // Constructor, w/ vector passed in
{
capacity = size = vec.size() + 1;
for (int i = 0; i < vec.size(); i++) // assigns elements from vector to heapArray
heapArray[i + 1] = vec[i];
for (int i = size - 1; i > 0; i--) // buildHeap
bubbleDown(i);
};
priorityQ(const priorityQ<Type> &rhs) // Copy constructor
{
for (int i = 0; i < size; i++)
heapArray[i] = rhs[i];
};
~priorityQ() // Destructor
{
delete[] heapArray;
};
const priorityQ<Type> &operator=(const priorityQ<Type> &rhs) // Assignment Operator
{
for (int i = 0; i < size; i++)
heapArray[i] = rhs[i];
};
/*
% void insert(Type e)
% Description: This inserts a given element of any given type into the heapArray
by incrementing the size, pushing an element to the end of the heapArray, and
bubbling up that element
*/
void insert(Type e)
{
size++;
if (size == capacity) // if the heapArray is maxed out...
{
capacity *= 2; // increase the capacity
Type *newArray; // create a new array
newArray = new Type[capacity];
for (int i = 1; i < size; i++) // coopy elements of the old array
newArray[i] = heapArray[i];
delete[] heapArray; // clear the heapArray
heapArray = newArray; // point the heapArray to the newArray
newArray = NULL;
}
heapArray[size] = e;
bubbleUp(size);
};
/*
% void deletePriority()
% Description: Deletes the priority element, in this case, the minimum element
*/
void deletePriority()
{
heapArray[1] = heapArray[size]; // Sets the root element to the back elemetn
size--; // Decrement size
bubbleDown(1); // Bubble the root down
};
/*
% Type getPriority() const
% Description: Returns the root priority element, in this case, the minimum
*/
Type getPriority() const
{
return heapArray[1];
};
/*
% bool isEmpty()
% Description: Returns true if the heapArray is empty
*/
bool isEmpty() const
{
if (size == 0)
return true;
else
return false;
};
/*
% void bubbleUp(int i)
% Description: Takes the element at a given index and bubbles it up into its correct location
in a minimum heap
*/
void bubbleUp(int i)
{
// if there is only one element or the child is less than the parent...
if (size == 1 || heapArray[i / 2] < heapArray[i])
return;
// while the parent is less than the child...
while (heapArray[i] < heapArray[i / 2])
{
std::swap(heapArray[i], heapArray[i / 2]); // swap the elemnents
i = i / 2; // update the index
}
};
/*
% void bubbleDown(int i)
% Description: Takes an element at a given index and bubbles it down
to its correct location in a minimum heap.
*/
void bubbleDown(int i)
{
// if there are 2 elements and the child is less than the parent...
if (size == 2 && heapArray[i * 2] < heapArray[i])
{
std::swap(heapArray[i], heapArray[i * 2]);
return;
}
// if there are no elements, 1 element, or the node is a leaf node
if (size == 0 || size == 1 || 2 * i > size || 2 * i + 1 > size)
return;
// while the left or right children are greater than the parent
while (heapArray[2 * i] < heapArray[i] || heapArray[2 * i + 1] < heapArray[i])
{
if (i > size || i * 2 > size || 2 * i + 1 > size) // check for leaf nodes
return;
// if both children are less than the parent...
if (heapArray[2 * i] < heapArray[i] && heapArray[2 * i + 1] < heapArray[i])
{
if (heapArray[2 * i] < heapArray[2 * i + 1]) // check the left child
{
std::swap(heapArray[i], heapArray[2 * i]);
i = 2 * i;
}
else
{
std::swap(heapArray[i], heapArray[2 * i + 1]); // check the right child
i = 2 * i + 1;
}
}
else if (heapArray[2 * i] < heapArray[i]) // if the left child is less thn the parent
{
std::swap(heapArray[i], heapArray[2 * i]);
i = 2 * i;
}
else if (heapArray[2 * i + 1] < heapArray[i]) // if the right child is less than the parent
{
std::swap(heapArray[i], heapArray[2 * i + 1]);
i = 2 * i + 1;
}
else
return;
}
return;
};
/*
% int getSize()
% Description: Returns the capacity of the heapArray
*/
int getSize() const
{
return capacity;
};
private:
int capacity;
int size;
Type *heapArray;
};
int main()
{
// stock file name, simulation file name, line, and part of the line
std::string stockFile, simFile, line, partOfLine;
std::ifstream infile; // file processing
std::vector<stockType> stocks; // vector of stocks
std::vector<bool> stockBought; // vector keeping track of which stocks were bought
int days; // number of days for simulation
double balance, dblNum; // account balance, value being converted to double
// Output Formatting
std::cout << std::fixed;
std::cout.precision(2);
// User prompt for the stock file
std::cout << "\nStocks file: ";
std::cin >> stockFile;
std::cout << std::endl;
infile.open(stockFile);
while (getline(infile, line)) // Stock file processing
{
std::stringstream ss(line);
stockType newStock;
// Parses the string for the stock name and dividend
getline(ss, partOfLine, ',');
newStock.name = partOfLine;
getline(ss, partOfLine);
dblNum = strtod(partOfLine.c_str(), NULL);
newStock.dividend = dblNum;
// Adds stock element to vectors
stocks.push_back(newStock);
stockBought.push_back(false);
}
infile.close();
std::cout << "Sim file: ";
std::cin >> simFile;
std::cout << "\nAmount of days to simulate: ";
std::cin >> days;
std::cout << "\nAmount you wish to transfer into brokerage account: ";
std::cin >> balance;
infile.open(simFile);
int count = 0; // vector index count
int dayCount = 0; // count of how many days in simulation
std::cout << std::endl;
while (dayCount != days)
{
count = 0;
priorityQ<stockType> heap(10);
std::cout << "Day " << dayCount + 1 << " Current Balance $ " << balance << std::endl
<< std::endl;
getline(infile, line); // prices at the beginning of the day
std::stringstream ss(line);
while (getline(ss, partOfLine, ',')) // parses line
{
dblNum = strtod(partOfLine.c_str(), NULL);
stocks[count].price = dblNum;
heap.insert(stocks[count]); // inserts individuals tocks into the heap
count++;
}
stockType min = heap.getPriority();
double profit = 0; // reset day's profit variable
count = 0; // reset vector index variable
// Process the minimum heap
while (!heap.isEmpty() && balance > min.price)
{
balance -= min.price; // subtract price from the profit & balance
profit -= min.price;
for (int i = 0; i < stocks.size(); i++)
{
if (min.price == stocks[i].price) // find the minimum element in the vector
{
stocks[i].timesPurchased++; // increment timesPurchased and set bought to true
stockBought[i] = true;
}
}
// delete priority element and output the stock bought
heap.deletePriority();
std::cout << "Buying one share of " << min.name << " valued at $ " << min.price << " per share" << std::endl;
min = heap.getPriority();
count++;
}
getline(infile, line); // second line, prices at the end of the day
std::stringstream sp(line);
count = 0;
while (getline(sp, partOfLine, ','))
{
dblNum = strtod(partOfLine.c_str(), NULL);
if (stockBought[count] == true) // if the stock was bought
{
balance += dblNum; // add the price at the end of the day to the balance and profit
profit += dblNum;
}
count++;
}
// Output the day's profit
std::cout << std::endl;
std::cout << "Profit made today $ " << profit << std::endl
<< std::endl;
// Reset the stockBought vector for the next day
for (int i = 0; i < stockBought.size(); i++)
{
stockBought[i] = false;
}
dayCount++;
}
// Output the balance & amount in dividends
std::cout << "Balance after " << dayCount << " days $ " << balance << std::endl;
double sum = 0;
for (int i = 0; i < stocks.size(); i++) // Dividenc calculation
{
sum += stocks[i].dividend * stocks[i].timesPurchased;
}
std::cout << "Amount in dividends $ " << sum << std::endl
<< std::endl;
return 0;
}