Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (103 loc) · 4.41 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (103 loc) · 4.41 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
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
struct Stock{
std::string symbol;
std::string exchange;
double currentPrice;
double prevClose;
double changePercent;
std::string lastTradeDay;
long long volume;
void deriveExchange(){
if(symbol.find(".NS") != std::string::npos) exchange = "NSE";
else if(symbol.find(".BSE") != std::string::npos) exchange = "BSE";
else exchange = "UNKOWN";
}
bool isBullish() const{
return changePercent > 0;
}
void display() const{
std::cout << "================================" << '\n';
std::cout << "TICKER: " << symbol << "(" << exchange << ")" << '\n';
std::cout << "PRICE: " << currentPrice << '\n';
std::cout << "CHANGE: " << (isBullish() ? "+" : "Bearish") << changePercent << "%" << '\n';
std::cout << "VOL: " << volume << '\n';
std::cout << "AS OF: " << lastTradeDay << '\n';
std::cout << "================================" << '\n';
}
};
std::string getInternetData(std::string url){
HINTERNET hInternet = InternetOpenA("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hInternet) return "FATAL_ERROR: Internet Open Failed";
HINTERNET hConnect = InternetOpenUrlA(hInternet, url.c_str(), NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);
if(!hConnect){
DWORD err = GetLastError();
InternetCloseHandle(hInternet);
return "FATAL_ERROR: Connection failed. Error Code: " + std::to_string(err);
}
char buffer[4096];
DWORD bytesRead;
std::string response;
while(InternetReadFile(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0){
response.append(buffer, bytesRead);
}
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);
return response;
}
int main(){
std::cout << "Starting Stock Screener..." << '\n';
std::vector<std::string> watchList = {"RELIANCE.BSE", "TCS.BSE", "HDFCBANK.BSE", "ICICIBANK.BSE", "SBIN.BSE", "INFY.BSE", "ITC.BSE", "TATAMOTORS.BSE"};
std::vector<Stock> screenedStocks;
std::string myKey = "ADOHUY9QOJ1XLCXD";
for(const std::string& ticker : watchList){
std::cout << "FETching Data for: " << ticker << "..." << '\n';
std::string url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=" + ticker + "&apikey=" + myKey;
std::cout << "\nREQUESTING URL: " << url << std::endl;
std::string rawData = getInternetData(url);
try{
auto j = json::parse(rawData);
if(j.contains("Global Quote") && !j["Global Quote"].empty()){
auto quote = j["Global Quote"];
Stock s;
s.symbol = quote["01. symbol"];
s.currentPrice = std::stod(quote["05. price"].get<std::string>());
std::string pcStr = quote["10. change percent"];
pcStr.pop_back();
s.changePercent = std::stod(pcStr);
s.volume = std::stoll(quote["06. volume"].get<std::string>());
s.lastTradeDay = quote["07. latest trading day"];
s.prevClose = std::stod(quote["08. previous close"].get<std::string>());
s.deriveExchange();
screenedStocks.push_back(s);
} else {
std::cout << "SKIPPING " << ticker << " (API limit or error unknown bruh)" << '\n';
std::cout << "REASON FROM API: " << rawData << '\n';
}
}
catch (const std::exception& e) {
std::cout << "ERROR PROCESSING" << ticker << ":" << e.what() << '\n';
}
if(ticker != watchList.back()) {
std::cout << "Im gonna wait 15 secs to respect API Limit" << '\n';
Sleep(15000);
}
}
std::cout << "\n\nKHATAM SCREENING, HERE ARE THE RESULTS\n" << '\n';
for(const auto& s : screenedStocks) {
s.display();
}
std::ofstream file("screener_results.csv", std::ios::app);
file << "Ticker,Price,Change,Volume,Date\n";
for(const auto& s : screenedStocks){
file << s.symbol << "," << s.currentPrice << "," << s.changePercent << "," << s.volume << "," << s.lastTradeDay << "\n";
}
file.close();
return 0;
}