eFinancialPlanner
V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Friends
Macros
Pages
portfolio.class.cpp
Go to the documentation of this file.
1
11
class
portfolio
12
{
13
public
:
14
int
portfolio_id
;
15
string
description
;
17
float
weights
[
NBR_ASSET_CLASSES
];
18
19
bool
get_portf_from_db
(
int
id
);
20
string
get_description
();
21
// float get_risk(float alpha, float (*riskFunction)(float alpha, float (*AssetClass_mu), float (*AssetClass_varCov)), float (*AssetClass_mu), float (*AssetClass_varCov));
22
void
set_mu
(
float
(*AssetClass_mu));
23
void
set_sigma
(
float
(*AssetClass_varCov));
24
float
pMu
;
25
float
pLogR
;
26
float
pSigma
;
27
};
28
32
bool
portfolio::get_portf_from_db
(
int
id
)
33
{
34
string
s =
""
;
35
int
k;
37
try
{
38
sql::Driver *driver;
39
sql::Connection *con;
40
sql::Statement *stmt;
41
sql::ResultSet *res;
42
// sql::PreparedStatement *pstmt;
43
// Create a connection
44
driver = get_driver_instance();
45
// con = driver->connect("tcp://127.0.0.1:3306","efp","efp.cger");
46
con = driver->connect(
oConfig
.
db_host
,
oConfig
.
db_user
,
oConfig
.
db_pwd
);
47
// con->setSchema("efp");
48
con->setSchema(
db
.
db
);
49
50
stmt = con->createStatement();
51
52
// -- the portfolio_id:
53
this->
portfolio_id
= id;
54
// -- the description:
55
s =
"SELECT * FROM "
+
oConfig
.
tbl_prefix
+
"_portfolios WHERE portfolio_id = "
+ std::to_string(
id
) +
";"
;
56
res =
db
.
stmt
->executeQuery(s);
57
while
(res->next()) {this->
description
= res->getString(
"description"
);}
58
// -- the portfolio composition in terms of asset classes:
59
for
(k=0; k <
NBR_ASSET_CLASSES
; k++) {this->
weights
[k] = 0;}
60
s =
"SELECT * FROM "
+
oConfig
.
tbl_prefix
+
"_portfolio_compositions WHERE portfolio = "
+ std::to_string(
id
) +
";"
;
61
res = stmt->executeQuery(s);
62
while
(res->next())
63
{
64
k = res->getInt(
"asset_class"
);
65
this->
weights
[k-1] = atof(res->getString(
"percentage"
).c_str());
66
}
67
/*XXX if (id == 10) {int kk; for (kk=1;kk<=NBR_ASSET_CLASSES;kk++) cout << "<br>weights[" << kk << "]" << weights[kk];}
68
*/
69
}
// try
70
catch
(sql::SQLException &e) {
71
error_message
=
error_message
+
t_errMsg
[
"retrieve_portfolio_info"
];
72
#ifdef DEBUG
73
cout <<
"# ERR: SQLException in "
<< __FILE__;
74
cout <<
"("
<< __FUNCTION__ <<
") on line "
<< __LINE__ << endl;
75
cout <<
"# ERR: "
<< e.what();
76
cout <<
" (MySQL error code: "
<< e.getErrorCode();
77
cout <<
", SQLState: "
<< e.getSQLState() <<
" )"
<< endl;
78
#endif
79
}
80
return
true
;
//TODO: add checks and return true if it is indeed correctly load and if there are no definition errors (such as asymetric covar matrix)
81
}
82
86
string
portfolio::get_description
()
87
{
88
return
this->
description
;
89
}
90
94
void
portfolio::set_mu
(
float
*AssetClass_mu)
95
{
96
int
k;
97
float
m = 0;
98
for
(k = 1; k <=
NBR_ASSET_CLASSES
; k++)
99
{
100
m += *(AssetClass_mu + k) *
weights
[k-1];
101
//cout <<"<br>["<<k<<"]*(AssetClass_mu + k)="<<*(AssetClass_mu + k)<<" * weights[k-1]="<<weights[k-1]<<" ||Mu*w= " << *(AssetClass_mu + k) * weights[k-1];
102
}
103
104
this->
pMu
= pow(1 + m,0.08333333) - 1;
// 0.0833 = 1/12
105
//cout << "<BR>pMu=" << pMu;
106
this->
pLogR
= log(1+ this->
pMu
);
107
}
108
112
void
portfolio::set_sigma
(
float
*AssetClass_varCov)
113
{
114
int
i,j;
115
float
s = 0;
116
for
(i = 1; i <=
NBR_ASSET_CLASSES
; i++)
117
{
118
for
(j = 1; j <=
NBR_ASSET_CLASSES
; j++)
119
{
120
s+=
weights
[i-1] * (*(AssetClass_varCov + i *
NBR_ASSET_CLASSES
+ j)) *
weights
[j-1];
121
}
122
}
123
if
(s >= 0)
124
{
125
this->
pSigma
= (float) sqrt(s);
// / sqrt(12);
126
}
127
else
128
{
129
this->
pSigma
= 0.0;
130
error_message
=
error_message
+
"Error nbr Erno2332, invalid portfolio parameters."
;
131
}
132
// cout << "<BR>pSigma=" << pSigma;
133
}
portfolio::pLogR
float pLogR
the MONTHLY log-return
Definition:
portfolio.class.cpp:25
config::db_host
const string db_host
Database.
Definition:
config.class.cpp:33
config::db_user
const string db_user
Definition:
config.class.cpp:35
portfolio::weights
float weights[NBR_ASSET_CLASSES]
the weights of the asset classes (ordered as the asset classes)
Definition:
portfolio.class.cpp:17
portfolio::get_portf_from_db
bool get_portf_from_db(int id)
Definition:
portfolio.class.cpp:32
db_helper::stmt
sql::Statement * stmt
Definition:
db_helper.class.cpp:17
portfolio::pMu
float pMu
the MONTHLY return
Definition:
portfolio.class.cpp:24
NBR_ASSET_CLASSES
#define NBR_ASSET_CLASSES
the nunmber of asset classes (not dynamically updated!)
Definition:
efp.cpp:97
error_message
string error_message
global message variables
Definition:
efp.cpp:132
portfolio::pSigma
float pSigma
the MONTHLY volatility
Definition:
portfolio.class.cpp:26
config::tbl_prefix
const string tbl_prefix
tables start with "" + oConfig.tbl_prefix + "_" if the prefix is eg. "tbl" then we have "tbl_invesotr...
Definition:
config.class.cpp:37
db
db_helper db
Definition:
efp.cpp:183
portfolio::description
string description
Definition:
portfolio.class.cpp:15
portfolio::get_description
string get_description()
Definition:
portfolio.class.cpp:86
portfolio
Definition:
portfolio.class.cpp:11
portfolio::set_sigma
void set_sigma(float(*AssetClass_varCov))
Definition:
portfolio.class.cpp:112
t_errMsg
unordered_map< string, string > t_errMsg
Definition:
texts_Eng_UK.h:86
config::db_pwd
const string db_pwd
Definition:
config.class.cpp:36
oConfig
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition:
efp.cpp:149
db_helper::db
string db
Definition:
db_helper.class.cpp:25
portfolio::portfolio_id
int portfolio_id
Definition:
portfolio.class.cpp:14
portfolio::set_mu
void set_mu(float(*AssetClass_mu))
Definition:
portfolio.class.cpp:94
portfolio.class.cpp
Generated on Wed Nov 18 2015 22:59:22 for eFinancialPlanner.eu by
1.8.6