-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
219 lines (186 loc) · 7.02 KB
/
main.cpp
File metadata and controls
219 lines (186 loc) · 7.02 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include "Share.h"
#include "HashTable.h"
#include "PriorityQueue.h"
using namespace std;
// Constants
const unsigned int TICKERS = 504;
const unsigned int HASHSIZE = 1500;
// Function Prototypes
bool parseData(ifstream &, string &, string &, double &, double &, string &, double &,
double &, double &, double &, double &, double &, double &, double &);
void printBuy(PriorityQueue &, double &);
void stockInfo(Share);
void stockInfo(string, HashTable &, double); // Overloaded
int main()
{
cout << "Would you like to use latest data file saved? (Y/y)";
char entry;
cin >> entry;
// Run Python script to obtain stock data
if (entry != 'y' && entry != 'Y')
{
string command = "FetchStockData.py";
system(command.c_str());
}
// Read stock data from CSV file
ifstream stockData("stockData.csv");
// Error in opening file
if (!stockData.is_open())
{
cout << "Error! File did not open correctly.";
return -1;
}
// Variables
string ticker, compName, cap;
double price, yearLow, yearHigh, fiftyDMA, twoHundDMA, macd, signal, hist, rsi, vol;
int count = 0;
HashTable hashTable(HASHSIZE); // To store ticker-data pairs
PriorityQueue maxHeap(TICKERS); // Binary tree to sort stocks by "customFactor"
bool filePass;
// Parse Data
filePass = parseData(stockData, ticker, compName, price, vol, cap, yearLow,
yearHigh, fiftyDMA, twoHundDMA, macd, signal, hist, rsi);
// While input stream is not empty
while (filePass)
{
// Create Share object with information
Share stock(ticker, compName, price, vol, cap, yearLow, yearHigh,
fiftyDMA, twoHundDMA, macd, signal, hist, rsi);
// Insert share object to HashTable
hashTable.insert(stock);
// Insert share object to priority queue (max-heap) based on customFactor value
if (stock.getFactor() >= 0)
maxHeap.enqueue(stock);
// Parse Data
filePass = parseData(stockData, ticker, compName, price, vol, cap, yearLow,
yearHigh, fiftyDMA, twoHundDMA, macd, signal, hist, rsi);
}
stockData.close(); // Close file
double avgCustomFactor; // Store average custom factor for top 15 stocks
printBuy(maxHeap, avgCustomFactor); // Print Top 15 Stocks to BUY
cout << endl << "Enter Stock Ticker for Information or 0 to exit." << endl;
string viewData;
cin >> viewData;
// Run loop until user prompts to exit
while (viewData != "0")
{
stockInfo(viewData, hashTable, avgCustomFactor); // Print Stock Data
cout << endl << "Enter Stock Ticker for Information or 0 to exit." << endl;
cin >> viewData;
}
system("pause");
return 0;
}
// Function Definitions
bool parseData(ifstream &fin, string &ticker, string &compName, double &price, double &vol,
string &cap, double &yearLow, double &yearHigh, double &fiftyDMA, double &twoHundDMA,
double &macd, double &signal, double &hist, double &rsi)
{
string line, field;
getline(fin, line); // Get the entire line in the CSV file
istringstream ss(line);
// Check if last line in file
if (line == "")
return false;
// Parse data from line to variables
while (!ss.eof())
{
getline(ss, field, ',');
ticker = field;
getline(ss, field, ',');
compName = field;
getline(ss, field, ',');
price = atof(field.c_str());
getline(ss, field, ',');
vol = atof(field.c_str());
getline(ss, field, ',');
cap = field;
getline(ss, field, ',');
yearLow = atof(field.c_str());
getline(ss, field, ',');
yearHigh = atof(field.c_str());
getline(ss, field, ',');
fiftyDMA = atof(field.c_str());
getline(ss, field, ',');
twoHundDMA = atof(field.c_str());
getline(ss, field, ',');
macd = atof(field.c_str());
getline(ss, field, ',');
signal = atof(field.c_str());
getline(ss, field, ',');
hist = atof(field.c_str());
getline(ss, field, ',');
rsi = atof(field.c_str());
}
return true;
}
void printBuy(PriorityQueue &maxHeap, double &avgCustomFactor)
{
avgCustomFactor = 0;
Share buyStock;
int num = maxHeap.size();
if (num > 15)
num = 15;
cout << "Top 15 Stocks to BUY:" << endl << endl;
// Obtain Top 15 stocks to "Buy", unless there are less than 15 ideal stocks
for (int i = 1; i <= num; i++)
{
buyStock = maxHeap.max();
maxHeap.dequeue();
cout << i << ") " << endl;
stockInfo(buyStock);
avgCustomFactor += buyStock.getFactor();
}
avgCustomFactor /= num;
}
void stockInfo(Share stock)
{
cout << "Ticker Symbol: " << stock.getTicker() << endl;
cout << "Company Name: " << stock.getCompanyName() << endl;
cout << fixed << setprecision(2) << "Current Price: $" << stock.getPrice() << endl;
cout << "Average Volume: " << stock.getVolume() << endl;
cout << "Market Cap: $" << stock.getCap() << endl;
cout << fixed << setprecision(2) << "Year Low: $" << stock.getYearLow() << endl;
cout << fixed << setprecision(2) << "Year High: $" << stock.getYearHigh() << endl;
cout << fixed << setprecision(2) << "50 DMA: $" << stock.get50DMA() << endl;
cout << fixed << setprecision(2) << "200 DMA: $" << stock.get200DMA() << endl;
cout << fixed << setprecision(2) << "MACD: " << stock.getMACD() << endl;
cout << fixed << setprecision(2) << "MACD Signal: " << stock.getSignal() << endl;
cout << fixed << setprecision(2) << "MACD Histogram: " << stock.getHist() << endl;
cout << fixed << setprecision(2) << "RSI: " << stock.getRSI() << endl << endl;
}
void stockInfo(string ticker, HashTable &table, double avgCustomFactor)
{
Share stock;
bool exists = table.search(ticker, stock); // Check to see if ticker exists in table
if (exists) // If exists, then print out data
{
cout << "Ticker Symbol: " << stock.getTicker() << endl;
cout << "Company Name: " << stock.getCompanyName() << endl;
cout << fixed << setprecision(2) << "Current Price: $" << stock.getPrice() << endl;
cout << "Average Volume: " << stock.getVolume() << endl;
cout << "Market Cap: $" << stock.getCap() << endl;
cout << fixed << setprecision(2) << "Year Low: $" << stock.getYearLow() << endl;
cout << fixed << setprecision(2) << "Year High: $" << stock.getYearHigh() << endl;
cout << fixed << setprecision(2) << "50 DMA: $" << stock.get50DMA() << endl;
cout << fixed << setprecision(2) << "200 DMA: $" << stock.get200DMA() << endl;
cout << fixed << setprecision(2) << "MACD: " << stock.getMACD() << endl;
cout << fixed << setprecision(2) << "MACD Signal: " << stock.getSignal() << endl;
cout << fixed << setprecision(2) << "MACD Histogram: " << stock.getHist() << endl;
cout << fixed << setprecision(2) << "RSI: " << stock.getRSI() << endl;
double factor = stock.getFactor();
if (factor < 0)
factor = 0;
else if (factor > avgCustomFactor)
factor = avgCustomFactor;
double rating = factor / avgCustomFactor * 100;
cout << fixed << setprecision(2) << "Buy Rating: " << rating << "%" << endl << endl;
}
else // Otherwise, display error
cout << "The stock symbol entered does not exist in the database." << endl;
}