-
Notifications
You must be signed in to change notification settings - Fork 0
/
MACD.cpp
166 lines (139 loc) · 4.3 KB
/
MACD.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
int signal=0;
/*Indexes
DATE 0
SERIES 1
OPEN 2
HIGH 3
LOW 4
PREV. CLOSE 5
LTP 6
CLOSE 7
VWAP 8
52W H 9
52W L 10
VOLUME 11
VALUE 12
NO OF TRADES 13
SYMBOL 14*/
void readCSV(const string& filename,vector<vector<string>>& data) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename <<endl;
return;
}
string line;
while (getline(file, line)) {
vector<string> row;
istringstream iss(line);
string cell;
while (getline(iss, cell, ',')) {
row.push_back(cell);
}
data.push_back(row);
}
file.close();
}
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();
}
double get_EWM(vector<vector<string>> data,int t,int start_index,int n){
double a=2/static_cast<double>(n+1);
if (t==0){
return stod(data[start_index][7]);
}else{
double EWM=(a*(stod(data[start_index+t][7])))+((1-a)*get_EWM(data,t-1,start_index,n));
return EWM;
}
}
double get_MACD(vector<vector<string>> data,int t,int start_index){
double short_EWM=get_EWM(data,t,start_index,12);
double long_EWM=get_EWM(data,t,start_index,26);
double MACD=short_EWM-long_EWM;
return MACD;
}
double get_signal(vector<vector<string>> data,int t,int start_index,int n){
double a=2/static_cast<double>(n+1);
if(t==0){
signal=get_MACD(data,t,start_index);
}else{
signal=((1-a)*signal)+(a*get_MACD(data,t,start_index));
}
return signal;
}
void MACD(string input_file,string output_file1,string output_file2,int x,string start_date){
//reading the csv file
vector<vector<string>> data;
readCSV(input_file,data);
//Writing output csv files as given
ofstream file1(output_file1);
ofstream file2(output_file2);
if (file1.is_open() && file2.is_open()){
file1<<"Date,Order_dir,Quantity,Price"<<"\n";
file2<<"Date,Cashflow"<<"\n";
//Getting the index of start_date
int start_index=1;
for (int row_index=1;row_index<data.size();row_index++){
if (data[row_index][0]==start_date){
start_index=row_index;
break;
}
}
//Getting signals
double total_shares=0;
double net_profit=0;
for (int row_index=start_index;row_index<data.size();row_index++){
double close=std::stod(data[row_index][7]);
double MACD=get_MACD(data,row_index-start_index,start_index);
double signal=get_signal(data,row_index-start_index,start_index,9);
if (MACD>signal){
if (total_shares<x && total_shares>=(x*(-1))){
file1<<formatDate(data[row_index][0])<<","<<"BUY"<<","<<1<<","<<close<<"\n";
net_profit-=close;
total_shares+=1;
}
}else if (MACD<signal){
if (total_shares<=x && total_shares>(x*(-1))){
file1<<formatDate(data[row_index][0])<<","<<"SELL"<<","<<1<<","<<close<<"\n";
net_profit+=close;
total_shares-=1;
}
}
file2<<formatDate(data[row_index][0])<<","<<net_profit<<"\n";
}
file1.close();
file2.close();
//Writing final_pnl.txt
ofstream file3("final_pnl.txt");
net_profit+=(total_shares)*(stod(data[data.size()-1][7]));
file3<<net_profit;
file3.close();
}
}
int main(){
//taking the required inputs for Basic Strategy
string end_date,start_date,strategy,symbol;
int x;
cin>>strategy>>symbol>>x>>start_date>>end_date;
if (start_date<=end_date){
//writing order_statisics
string output_file1="order_statistics.csv";
string output_file2="daily_pnl.csv";
string input_file=symbol+".csv";
MACD(input_file,output_file1,output_file2,x,start_date);
return 0;
}
}