-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal_proj.cpp
350 lines (305 loc) · 10.9 KB
/
universal_proj.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
#include "universal_proj.h"
using namespace std;
void BCRPEngine::parseCsvData(const string& stockName,const string& csvFileName)
{
ifstream csvFile;
FloatArray priceRelVec,priceVec;
StringArray dates;
int lineNum = 0;
float quote = -1;
float prevQuote = -1;
csvFile.open(string(tableDir_ + csvFileName + ".csv").c_str());
if (!csvFile.is_open())
myThrow ("Couldn't open file '" + string(tableDir_ + csvFileName + ".csv")+ "'!");
if (stockNum_ != 0)
lineNum = stockDates_[0].size()-1;
string line;
puts ("Parsing csv data for " + stockName +"...");
getline(csvFile,line); // Skip header line.
while(!csvFile.eof()) {
StringArray lineVec;
getline(csvFile,line);
if (line.empty() || line == ",,,,,,")
continue;
size_t pos = line.find_first_of(",");
size_t prevPos = 0;
while(pos != string::npos) {
if (line.substr(prevPos,pos-prevPos) == "")
myThrow("Issue with line in the csv file, found line with insufficient num of cells");
lineVec.push_back(line.substr(prevPos,pos-prevPos));
prevPos = pos+1;
pos = line.find_first_of(",",prevPos);
if (pos == string::npos)
lineVec.push_back(line.substr(prevPos));
}
string date = lineVec[0];
quote = atof(lineVec[6].c_str());
if (prevQuote == -1)
prevQuote = quote;
float relQuote = prevQuote/quote;
if (stockNum_ != 0) {
if (date != stockDates_[0][lineNum]) {
puts ("Notice: Removing redundant date '" + date + "' from stock's price vector.");
continue;
}
}
priceRelVec.insert(priceRelVec.begin(),relQuote);
priceVec.insert(priceVec.begin(),quote);
dates.insert(dates.begin(),date);
prevQuote = quote;
if (stockNum_ != 0)
--lineNum;
}
puts ("finished parsing, updating database...");
stockNameToIndx_.insert(make_pair(stockName,stockNum_));
indxToStockName_.push_back(stockName);
stockArray_.push_back(priceRelVec);
stockQuoteArray_.push_back(priceVec);
stockDates_.push_back(dates);
stockNum_ = stockArray_.size();
bcrpCalcStartRanges_.push_back(0);
bcrpCalcEndRanges_.push_back(0);
puts("closing I/O file handler... ");
puts("Checking stock data integrity...");
int prevDataSize = stockArray_[0].size();
numOfDays_ = prevDataSize;
for (int j=1; j<stockNum_; j++) {
int currDataSize = stockArray_[j].size();
if (currDataSize != prevDataSize)
myThrow ("The data size for some size didn't fit the size of the data of the other stocks. stock0 size is " + toStr(prevDataSize)
+ " and stock"+toStr(j)+" data size is" + toStr(currDataSize));
}
puts ("All data found OK");
csvFile.close();
}
void BCRPEngine::calcAvgIndex()
{
float avgSn = 0;
for (int j=0; j<stockNum_; j++) {
avgSn += stockQuoteArray_[j][numOfDays_-1]/stockQuoteArray_[j][0];
}
avgSn = avgSn/stockNum_;
puts("Average Index is:" + toStr(avgSn));
}
void BCRPEngine::findDeltaAndPortfoliosRecursive(int stockIndex,float prevPortfolioSum, float prevPortfolioNegSum)
{
float portfolioSum=prevPortfolioSum;
float portfolioNegSum=prevPortfolioNegSum;
for (float i=rangeStart_; i <= rangeEnd_; i+=step_) {
if (i>0)
portfolioSum = prevPortfolioSum+i;
else
portfolioNegSum = prevPortfolioNegSum-i;
currPortfolio_[stockIndex] = i;
if (portfolioSum > 1.0001)
continue;
if (portfolioNegSum > 1.0001)
continue;
if (stockIndex == stockNum_-1) {
if (portfolioSum > 0.9999 && portfolioSum < 1.00001 ) {
++numOfPortfolios_;
CRP crp;
crp.setB(currPortfolio_);
allPortfolios_.push_back(crp);
}
} else {
findDeltaAndPortfoliosRecursive(stockIndex+1,portfolioSum,portfolioNegSum);
}
}
}
void BCRPEngine::findDeltaAndPortfolios()
{
puts("Calculation integral delta and setting all valid portfolios...");
puts("stockNum:" + toStr(stockNum_));
numOfPortfolios_=0;
currPortfolio_.insert(currPortfolio_.begin(),stockNum_,0);
eodPortfolio_.insert(eodPortfolio_.begin(),stockNum_,0);
findDeltaAndPortfoliosRecursive(0,0,0);
puts("finished recursive calculation...");
delta_ = (1/(float)numOfPortfolios_);
puts("Number of portfoilois (segments) is:" + toStr(numOfPortfolios_));
puts("Delta is:" + toStr(delta_));
}
void BCRPEngine::printCurrCRP()
{
cout << "current CRP is: (";
for (int i=0; i < currPortfolio_.size(); i++) {
cout << currPortfolio_[i] << ",";
}
cout << ")" << endl;
}
void BCRPEngine::printBCRP()
{
cout << "Best CRP is: (";
for (int i=0; i < bestCrpPortfolio_.size(); i++) {
cout << bestCrpPortfolio_[i] << ",";
}
cout << ")" << endl;
}
void BCRPEngine::calcEodPortfolio(float currDayProfit)
{
FloatArray eodProtfolio(stockNum_,0);
for (int j=0; j<stockNum_; j++)
eodPortfolio_[j] = eodPortfolio_[j]/currDayProfit;
}
float BCRPEngine::calcDayProfitWithCommission(FloatArray nextDayPortfolio,float currDayProfit,int n)
{
float commissionFactor = 0;
for (int j=0; j<stockNum_; j++) {
float stockPrice = stockQuoteArray_[j][n];
float bjDiff = nextDayPortfolio[j]-eodPortfolio_[j];
if (bjDiff < 0)
bjDiff = bjDiff*(-1);
commissionFactor += bjDiff*c_/stockPrice;
}
float currDayProfitWithCommission = currDayProfit*(1-commissionFactor);
return currDayProfitWithCommission;
}
float BCRPEngine::calcCrpS(FloatArray& sVec)
{
sVec.clear();
float s = 1;
for (int i = 0; i < numOfDays_; i++) {
float currDayProfit = 0;
for (int j=0; j<stockNum_; j++) {
float bj = currPortfolio_[j];
float xij = stockArray_[j][i];
eodPortfolio_[j] = bj*xij;
if (bj>0) {
currDayProfit += bj*xij;
} else {
currDayProfit += (-1)*bj*(1-xij);
}
}
if (withCommission_ && i != numOfDays_-1) {
calcEodPortfolio(currDayProfit);
currDayProfit = calcDayProfitWithCommission(currPortfolio_,currDayProfit,i);
}
s = s*currDayProfit;
sVec.push_back(s);
}
return s;
}
const FloatArray& BCRPEngine::calcBestCRP()
{
ofstream csvFile;
FloatArray sVec,bestSVec;
csvFile.open(string(resultDir_ + "r_bcrp.csv").c_str());
puts("calculating best CRP...");
long int currPortfolioNum = 0;
if (withCommission_)
puts("calculating BCRP with transaction costs...");
for (CRPArray::const_iterator crpIt = allPortfolios_.begin();
crpIt != allPortfolios_.end(); crpIt++) {
currPortfolio_ = crpIt->getB();
float currS = calcCrpS(sVec);
if (currS > bestCrpS_) {
bestCrpS_ = currS;
bestCrpPortfolio_ = currPortfolio_;
bestSVec = sVec;
}
++currPortfolioNum;
if ((currPortfolioNum%10000) == 0)
puts("Finished checking " + toStr(currPortfolioNum/10000) + "0k out of " + toStr(numOfPortfolios_) + "...");
}
printBCRP();
for (int i=0; i<bestSVec.size(); i++) {
csvFile << i << "," << bestSVec[i] << endl;
}
csvFile.close();
puts("Done. Best CRP Sn is " + toStr(bestCrpS_));
return bestCrpPortfolio_;
}
void BCRPEngine::calcUniversalProfit()
{
ofstream csvFile;
csvFile.open(string(resultDir_ + "r_universal.csv").c_str());
float s = 1;
for (int n=0; n<numOfDays_; n++) {
puts("Day " + toStr(n));
float currDayProfit = 0;
float bSum = 0;
for (int j=0; j<stockNum_; j++) {
float bj = nDayPortfolio_[n][j];
float xij = stockArray_[j][n];
if (bj>0) {
bSum += bj;
currDayProfit += bj*xij;
} else {
currDayProfit += (-1)*(bj)*(1-xij);
}
eodPortfolio_[j] = bj*xij;
}
if (withShorts_)
currDayProfit += 1-bSum;
puts("currDProfit:" + toStr(currDayProfit));
if (withCommission_ && n != numOfDays_-1) {
calcEodPortfolio(currDayProfit);
currDayProfit= calcDayProfitWithCommission(nDayPortfolio_[n+1],currDayProfit,n);
}
s = s*currDayProfit;
puts(" s is:" + toStr(s) + "bSum is:" + toStr(bSum));
universalSVec_.push_back(s);
}
universalS_ = s;
for (int i=0; i<universalSVec_.size(); i++) {
csvFile << i << "," << universalSVec_[i] << endl;
}
csvFile.close();
}
float BCRPEngine::calcSnIncr(int n)
{
float s = 1;
float currDayProfit = 0;
for(int j=0; j<stockNum_; j++) {
float bj = currPortfolio_[j];
float xij = stockArray_[j][n];
if (bj>0)
currDayProfit += bj*xij;
else
currDayProfit += (-bj)*(1-xij);
}
return currDayProfit;
}
void BCRPEngine::calcDayIntegral(int n)
{
FloatArray currDayPortfolio(stockNum_,0);
float sIntegral = 0;
for (CRPArray::iterator crpIt = allPortfolios_.begin();
crpIt != allPortfolios_.end(); crpIt++) {
currPortfolio_ = crpIt->getB();
float sn = crpIt->getS();
float sIncr = calcSnIncr(n);
sn = sn*sIncr;
crpIt->setS(sn);
sIntegral += sn*delta_;
for (int j=0; j<stockNum_; j++) {
currDayPortfolio[j] += currPortfolio_[j]*sn*delta_;
}
}
nDayPortfolio_.push_back(currDayPortfolio);
cout << " sIntegral: " + toStr(sIntegral);
cout << " nPortfolio:(";
for (int j=0; j<stockNum_; j++) {
nDayPortfolio_[n+1][j] = (nDayPortfolio_[n+1][j])/sIntegral;
cout << toStr(nDayPortfolio_[n+1][j]) << ",";
}
cout << ") ";
}
const FloatArray& BCRPEngine::calcUniversal()
{
puts("Start calculating Universal-Portfolio...");
nDayPortfolio_.push_back(FloatArray(stockNum_,1/(float)stockNum_));
for (int n=0; n<numOfDays_-1; n++) {
cout << "calculating integral for day" + toStr(n) + "...";
calcDayIntegral(n);
puts(" finished calculation for day " + toStr(n));
}
puts("Finished calculating Universal-Portfolio.");
puts("\nStart Calculating Universal-Portfolio profit...");
calcUniversalProfit();
puts("Finished Calculating Universal-Portfolio profit.");
puts ("****************************************");
puts("Universal S:" + toStr(universalS_));
return universalSVec_;
}