-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal_proj.h
142 lines (126 loc) · 3.65 KB
/
universal_proj.h
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
// **
// Utility functions
// **
inline void puts(const string& msg)
{
cout << msg << endl;
}
inline void myThrow(const string& msg)
{
cout << "ERROR: " + msg << endl;
throw;
}
inline string toStr(float num)
{
ostringstream stream;
stream << num;
return stream.str();
}
inline string toStr(long num)
{
ostringstream stream;
stream << num;
return stream.str();
}
inline string toStr(int num)
{
ostringstream stream;
stream << num;
return stream.str();
}
// **
// Constant Rebalanced Portfolio
// **
struct CRP
{
// **
// The variable naming follow the convetions of portfolio theory,
// and are the same as used in the Universal Portfolio formula from Thomas Cover's paper.
// S - factor of wealth increase.
// B - wealth allocation vector. B = (b_1, b_2,...b_m), where b_i is the propotion of wealth invested in stock i
//
// **
float s_;
vector<float> b_;
CRP(): s_(1) {};
void setB(const vector<float>& b) {b_ = b;};
void setS(float s) {s_ = s;};
float getS() const { return s_;};
vector<float> getB() const { return b_;};
};
typedef vector<CRP> CRPArray;
typedef vector<int> IntArray;
typedef vector<float> FloatArray;
typedef vector<string> StringArray;
typedef map<string,int> StringIntMap;
typedef vector<IntArray> IntArrayVec;
typedef vector<FloatArray> FloatArrayVec;
typedef vector<StringArray> StringArrayVec;
// **
// BCRP - Best Constant Rebalanced Portfolio
// **
class BCRPEngine
{
public:
BCRPEngine(const string& tableDir,const string& resultDir,float step,bool withShorts,
bool commission = false,float c = 0.005):
tableDir_(tableDir),resultDir_(resultDir),step_(step),stockNum_(0),bestCrpS_(0),rangeStart_(0),rangeEnd_(1),
withShorts_(withShorts),withCommission_(commission),c_(c) {
if (withShorts)
rangeStart_ = -1;
};
void parseCsvData(const string& stockName,const string& csvFile);
void findDeltaAndPortfolios();
void calcAvgIndex();
const FloatArray& calcBestCRP();
const FloatArray& calcUniversal();
const FloatArray& calcBestCrpProfit();
private:
void findDeltaAndPortfoliosRecursive(int stockIndex,float prevPortfolioSum,float prevPortfolioNegSum);
void printBCRP();
void printCurrCRP();
void calcEodPortfolio(float currDayProfit);
float calcDayProfitWithCommission(FloatArray nextDayPortfolio,float currDayProfit,int n);
float calcCrpS(FloatArray& sVec);
FloatArray calcBestCrp();
void calcDayIntegral(int n);
float calcSnIncr(int n);
void calcUniversalProfit();
private:
int stockNum_;
int numOfDays_;
long int numOfPortfolios_;
StringIntMap stockNameToIndx_;
StringArray indxToStockName_;
FloatArrayVec stockArray_;
FloatArrayVec stockQuoteArray_;
StringArrayVec stockDates_;
float bestCrpS_;
float universalS_;
float step_;
float delta_;
float rangeStart_;
float rangeEnd_;
FloatArray bestCrpPortfolio_;
FloatArray currPortfolio_;
FloatArray eodPortfolio_;
FloatArray universalSVec_;
CRPArray allPortfolios_;
FloatArrayVec nDayPortfolio_;
FloatArray bcrpCalcStartRanges_;
FloatArray bcrpCalcEndRanges_;
string tableDir_;
string resultDir_;
bool withCommission_;
bool withShorts_;
float c_;
};