-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLINEAR_REGRESSION.cpp
367 lines (292 loc) · 10.5 KB
/
LINEAR_REGRESSION.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
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
struct StockData {
string date;
double closePrice;
double vwap;
double low;
double high;
double noOfTrades;
double open;
};
vector<StockData> readCSV(const string& filename, const string& startDate, const string& endDate) {
vector<StockData> data;
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return data;
}
string line;
getline(file, line); // Skip header line
while (getline(file, line)) {
stringstream ss(line);
string token;
StockData stock;
for (int i = 0; getline(ss, token, ','); ++i) {
if (i == 0) stock.date = token;
else if(i == 2) stock.open = stod(token);
else if(i == 3) stock.high = stod(token);
else if(i == 4) stock.low = stod(token);
else if(i == 7) stock.closePrice = stod(token);
else if(i == 8) stock.vwap = stod(token);
else if(i == 13) stock.noOfTrades = stod(token);
}
// Check if the date is within the desired range
if (stock.date >= startDate && stock.date <= endDate) {
data.push_back(stock);
}
// Break the loop if we reached the end date
if (stock.date > endDate) {
break;
}
}
file.close();
return data;
}
//Formatting date
string formatDate(const string& inputDate) {
istringstream iss(inputDate);
int year, month, day;
char dash1, dash2;
iss >> year >> dash1 >> month >> dash2 >> day;
ostringstream oss;
oss << setw(2) << setfill('0') << day << '/' << setw(2) << setfill('0') << month << '/' << year;
return oss.str();
}
// Function to print a matrix
void printMatrix(const vector<vector<double>>& matrix) {
for (const auto& row : matrix) {
for (double elem : row) {
cout << elem << "\t";
}
cout << endl;
}
}
// Function to find the transpose of a matrix
vector<vector<double>> transpose(const vector<vector<double>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
// Create a matrix to store the transpose
vector<vector<double>> transposed(cols, vector<double>(rows));
// Populate the transposed matrix
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
transposed[j][i] = matrix[i][j];
}
}
return transposed;
}
// Function to multiply two matrices
vector<vector<double>> matrixMultiply(const vector<vector<double>>& mat1, const vector<vector<double>>& mat2) {
int rows1 = mat1.size();
int cols1 = mat1[0].size();
int cols2 = mat2[0].size();
// Create a intermediate matrix
vector<vector<double>> intermediate(rows1, vector<double>(cols2, 0.0));
// Perform matrix multiplication
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
intermediate[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return intermediate;
}
// Function to find the determinant of a square matrix
double determinant(const vector<vector<double>>& matrix);
// Function to find the inverse of a square matrix
vector<vector<double>> inverse(const vector<vector<double>>& matrix) {
int n = matrix.size();
// Create a matrix to store the inverse
vector<vector<double>> inverseMatrix(n, vector<double>(n));
// Find determinant of the matrix
double det = determinant(matrix);
if (det == 0) {
cout << "Inverse does not exist for this matrix." << endl;
return inverseMatrix;
}
// Find adjoint of the matrix
vector<vector<double>> adjoint(n, vector<double>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
// Get the cofactor of element (i, j)
vector<vector<double>> cofactor(n - 1, vector<double>(n - 1));
int x = 0, y = 0;
for (int row = 0; row < n; ++row) {
for (int col = 0; col < n; ++col) {
if (row != i && col != j) {
cofactor[x][y++] = matrix[row][col];
if (y == n - 1) {
y = 0;
++x;
}
}
}
}
// Sign of adjoint matrix positive if sum of indices is even.
// Otherwise negative.
double sign = ((i + j) % 2 == 0) ? 1 : -1;
adjoint[j][i] = sign * determinant(cofactor);
}
}
// Find inverse of the matrix by dividing adjoint by determinant
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
inverseMatrix[i][j] = adjoint[i][j] / det;
}
}
return inverseMatrix;
}
// Function to find the determinant of a square matrix using recursion
double determinant(const vector<vector<double>>& matrix) {
int n = matrix.size();
if (n == 1) {
return matrix[0][0];
}
if (n == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
double det = 0;
for (int i = 0; i < n; ++i) {
vector<vector<double>> cofactor(n - 1, vector<double>(n - 1));
int x = 0, y = 0;
for (int row = 1; row < n; ++row) {
for (int col = 0; col < n; ++col) {
if (col != i) {
cofactor[x][y++] = matrix[row][col];
if (y == n - 1) {
y = 0;
++x;
}
}
}
}
det += (i % 2 == 0 ? 1 : -1) * matrix[0][i] * determinant(cofactor);
}
return det;
}
int main() {
string filename;
cout << "Enter CSV file name: ";
cin >> filename;
double x;
cout<<"X :";
cin >> x;
double p;
cout<<"P :";
cin >> p;
string train_startDate, train_endDate;
cout << "Enter train start date (YYYY-MM-DD): ";
cin >> train_startDate;
cout << "Enter train end date (YYYY-MM-DD): ";
cin >> train_endDate;
vector<StockData> trainData = readCSV(filename+".csv",train_startDate,train_endDate);
double days = trainData.size();
//cout<<"\nDays: "<< days<<endl;
vector<vector<double>>y_matrix(days-1,vector<double>(1));
int count1 = days-1;
for(int i =0;i<days-1;i++){
if(count1==0){
break;
}else{
y_matrix[i][0] = trainData[count1].closePrice;
count1--;
}
}
// cout<< "\ny_matrix: " << endl;
// printMatrix(y_matrix);
vector<vector<double>>x_matrix(days-1,vector<double>(8));
int count = days-1;
for(int i=0;i<=days;i++){
if(count==0){
break;
}else{
x_matrix[i][0] = 1;
x_matrix[i][1] = trainData[count-1].closePrice;
x_matrix[i][2] = trainData[count-1].open;
x_matrix[i][3] = trainData[count-1].vwap;
x_matrix[i][4] = trainData[count-1].low;
x_matrix[i][5] = trainData[count-1].high;
x_matrix[i][6] = trainData[count-1].noOfTrades;
x_matrix[i][7] = trainData[count].open;
count--;
}
}
// cout<< "\nx_matrix: " << endl;
// printMatrix(x_matrix);
vector<vector<double>> transposed = transpose(x_matrix);
// cout<< "\ntrans_matrix: " << endl;
// printMatrix(transposed);
vector<vector<double>> intermediate = matrixMultiply(transposed, x_matrix);
// cout<< "\nmulti_matrix: " << endl;
// printMatrix(intermediate);
vector<vector<double>> invMatrix = inverse(intermediate);
// cout<< "\ninv_matrix: " << endl;
// printMatrix(invMatrix);
vector<vector<double>> final = matrixMultiply(invMatrix,transposed);
// cout<< "\nfinal: " << endl;
// printMatrix(final);
vector<vector<double>> result = matrixMultiply(final,y_matrix);
// cout<< "\nResult: " << endl;
// printMatrix(result);
string startDate, endDate;
cout << "Enter start date (YYYY-MM-DD): ";
cin >> startDate;
cout << "Enter end date (YYYY-MM-DD): ";
cin >> endDate;
vector<StockData> fileData = readCSV(filename+".csv",startDate,endDate);
double size = fileData.size();
// cout<<"\nSize: "<< days<<endl;
ofstream file1("order_statistics.csv");
ofstream file2("daily_pnl.csv");
double total_shares=0;
double net_profit=0;
for(int i=1;i<size;i++){
vector<vector<double>>new_x_matrix(1,vector<double>(8));
new_x_matrix[0][0] = 1;
new_x_matrix[0][1] = trainData[i].closePrice;
new_x_matrix[0][2] = trainData[i].open;
new_x_matrix[0][3] = trainData[i].vwap;
new_x_matrix[0][4] = trainData[i].low;
new_x_matrix[0][5] = trainData[i].high;
new_x_matrix[0][6] = trainData[i].noOfTrades;
new_x_matrix[0][7] = trainData[i-1].open;
// cout<< "\nnew_x_matrix: " << endl;
// printMatrix(new_x_matrix);
vector<vector<double>> estimated_close = matrixMultiply(new_x_matrix,result);
//cout << "\n Answer:" << endl;
//printMatrix(estimated_close);
double percentage = ((estimated_close[0][0] - fileData[i-1].closePrice)/(fileData[i-1].closePrice))*100;
//cout<<"\nPercentage: "<<percentage<<endl;
double close=fileData[i-1].closePrice;
if (percentage>=p){
if (total_shares<x && total_shares>=(x*(-1))){
file1<<formatDate(fileData[i-1].date)<<","<<"BUY"<<","<<1<<","<<close<<"\n";
net_profit-=close;
total_shares+=1;
}
}else if (percentage<=-p){
if (total_shares<=x && total_shares>(x*(-1))){
file1<<formatDate(fileData[i-1].date)<<","<<"SELL"<<","<<1<<","<<close<<"\n";
net_profit+=close;
total_shares-=1;
}
}
file2<<formatDate(fileData[i-1].date)<<","<<net_profit<<"\n";
}
file1.close();
file2.close();
//Writing final_pnl.txt
ofstream file3("final_pnl.txt");
net_profit+=(total_shares)*fileData[size-1].closePrice;
file3<<net_profit;
file3.close();
return 0;
}