-
Notifications
You must be signed in to change notification settings - Fork 0
/
PAIRS.cpp
179 lines (144 loc) · 5.25 KB
/
PAIRS.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
struct StockData {
string date;
double closePrice;
};
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 == 7) stock.closePrice = 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();
}
int main() {
string filename1, filename2;
cout << "Enter CSV file-1 name: ";
cin >> filename1;
cout << "Enter CSV file-2 name: ";
cin >> filename2;
double x;
cout<<"X :";
cin >> x;
double n;
cout<<"n :";
cin >> n;
double threshold;
cout<<"Threshold :";
cin >> threshold;
string startDate, endDate;
cout << "Enter start date (YYYY-MM-DD): ";
cin >> startDate;
cout << "Enter end date (YYYY-MM-DD): ";
cin >> endDate;
vector<StockData> file1Data = readCSV(filename1+".csv",startDate, endDate);
vector<StockData> file2Data = readCSV(filename2+".csv",startDate, endDate);
// Assuming both files have the same number of rows
if (file1Data.size() != file2Data.size()) {
cerr << "Error: Unequal number of rows in the files." << endl;
return 1;
}
ofstream file1("order_statistics1.csv");
ofstream file2("order_statistics2.csv");
ofstream file3("daily_pnl.csv");
double size = file1Data.size();
// cout << "\nSize :"<< size << endl;
double total_shares1=0;
double total_shares2=0;
double net_profit=0;
for(int i=0;i<size;i++){
double current_spread = file1Data[i].closePrice - file2Data[i].closePrice;
//cout <<"Spread: " << current_spread << endl;
double count = 0;
for (int j= 0; j < n; j++) {
double spread = file1Data[j].closePrice - file2Data[j].closePrice;
count = count+spread;
}
double mean = count/(n);
//cout<<"Rolling Mean: " << mean << endl;
double sumOfSquares = 0;
for (int j = 0; j < n; j++) {
double spread = file1Data[j].closePrice - file2Data[j].closePrice;
double deviation = spread - mean;
sumOfSquares = sumOfSquares + (deviation*deviation);
}
double std_dev = sqrt(sumOfSquares/(n));
//cout<<"Rolling Standard Deviation: " << std_dev << endl;
double z_score = (current_spread - mean)/(std_dev);
//cout<<"z_score: " << z_score << endl;
double close1=file1Data[i].closePrice;
double close2=file2Data[i].closePrice;
if (z_score>threshold){
if (total_shares1<=x && total_shares1>(x*(-1))){
file1<<formatDate(file1Data[i].date)<<","<<"SELL"<<","<<1<<","<<close1<<"\n";
net_profit=net_profit+close1;
total_shares1-=1;
}
if (total_shares2<x && total_shares2>=(x*(-1))){
file2<<formatDate(file2Data[i].date)<<","<<"BUY"<<","<<1<<","<<close2<<"\n";
net_profit=net_profit-close2;
total_shares2+=1;
}
}else if (z_score<-threshold){
if (total_shares1<x && total_shares1>=(x*(-1))){
file1<<formatDate(file1Data[i].date)<<","<<"BUY"<<","<<1<<","<<close1<<"\n";
net_profit=net_profit-close1;
total_shares1+=1;
}
if (total_shares2<=x && total_shares2>(x*(-1))){
file2<<formatDate(file2Data[i].date)<<","<<"SELL"<<","<<1<<","<<close2<<"\n";
net_profit=net_profit+close2;
total_shares2-=1;
}
}
file3<<formatDate(file1Data[i].date)<<","<<net_profit<<"\n";
}
file1.close();
file2.close();
file3.close();
//Writing final_pnl.txt
ofstream file4("final_pnl.txt");
net_profit+=(total_shares1)*file1Data[size-1].closePrice;
net_profit+=(total_shares2)*file2Data[size-1].closePrice;
file4<<net_profit;
file4.close();
return 0;
}