eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 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:
15  string description;
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 
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 
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 }
float pLogR
the MONTHLY log-return
const string db_host
Database.
const string db_user
float weights[NBR_ASSET_CLASSES]
the weights of the asset classes (ordered as the asset classes)
bool get_portf_from_db(int id)
sql::Statement * stmt
float pMu
the MONTHLY return
#define NBR_ASSET_CLASSES
the nunmber of asset classes (not dynamically updated!)
Definition: efp.cpp:97
string error_message
global message variables
Definition: efp.cpp:132
float pSigma
the MONTHLY volatility
const string tbl_prefix
tables start with "" + oConfig.tbl_prefix + "_" if the prefix is eg. "tbl" then we have "tbl_invesotr...
db_helper db
Definition: efp.cpp:183
string description
string get_description()
void set_sigma(float(*AssetClass_varCov))
unordered_map< string, string > t_errMsg
Definition: texts_Eng_UK.h:86
const string db_pwd
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition: efp.cpp:149
void set_mu(float(*AssetClass_mu))