-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMA.cpp
162 lines (138 loc) · 4.34 KB
/
DMA.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
using namespace std;
/*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();
}
// Calculating the mean and Standard deviation sd
double get_sd(vector<vector<string>> data , int start_index ,int end_index){
double sum=0,sum_of_squares=0;
for (int row_index=start_index;row_index<=end_index;row_index++){
double entry=std::stod(data[row_index][7]);
sum+=entry;
sum_of_squares+=std::pow(entry,2);
}
double mean=sum/(data.size()-1);
double sd=sqrt((sum_of_squares/(data.size()-1)) -(mean*mean));
return sd;
}
double get_mean(vector<vector<string>> data , int start_index ,int end_index){
double sum=0;
for (int row_index=start_index;row_index<=end_index;row_index++){
double entry=std::stod(data[row_index][7]);
sum+=entry;
}
double mean=sum/(data.size()-1);
return mean;
}
void DMA(string input_file,string output_file1,string output_file2,int n,int x,int p,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++){
// Initiating start and end for acalculatind men and standard deviation
int start=row_index-n+1;
int end=row_index;
double close=std::stod(data[row_index][7]);
double sd=get_sd(data,start,end);
double mean=get_mean(data,start,end);
if (close-mean>p*sd){
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 (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 n,x,p;
cin>>strategy>>symbol>>n>>x>>p>>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";
DMA(input_file,output_file1,output_file2,n,x,p,start_date);
return 0;
}
}