Skip to content

Commit

Permalink
Added correlation func to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
evanwporter committed Sep 24, 2023
1 parent 6254f45 commit d6e7acc
Show file tree
Hide file tree
Showing 21 changed files with 35 additions and 25 deletions.
25 changes: 16 additions & 9 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\build\\backtest.exe",
"/Fe${fileDirname}\\backtest.exe",
"C:\\Users\\evanw\\OneDrive\\Documents\\GitHub\\CAT\\DataHandler\\dh.cpp",
"${fileDirname}\\cov.cpp",
"C:\\Users\\evanw\\OneDrive\\Documents\\GitHub\\CAT\\Libraries\\**.cpp",
"/IC:\\Users\\evanw\\OneDrive\\Documents\\GitHub\\CAT\\Libraries\\Eigen",

"${fileDirname}\\**.cpp",
"${fileDirname}\\Strategy\\**.cpp",
"${fileDirname}\\DataHandler\\**.cpp",
"${fileDirname}\\RiskHandler\\**.cpp",
"${fileDirname}\\Portfolio\\**.cpp",
"${fileDirname}\\Libraries\\**.cpp",
"${fileDirname}\\Metrics\\**.cpp",
"/I${fileDirname}\\Libraries\\Eigen",


// "/Fe${fileDirname}\\backtest.exe",
// "${fileDirname}\\**.cpp",
// "${fileDirname}\\Strategy\\**.cpp",
// "${fileDirname}\\DataHandler\\**.cpp",
// "${fileDirname}\\RiskHandler\\**.cpp",
// "${fileDirname}\\Portfolio\\**.cpp",
// "${fileDirname}\\Libraries\\**.cpp",
// "${fileDirname}\\Metrics\\**.cpp",
// "/I${fileDirname}\\Libraries\\Eigen",

"/std:c++20",
],
Expand Down
1 change: 0 additions & 1 deletion Data/AAPL.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
2147
Date,Open,High,Low,Close,Adj Close,Volume
1092873600000,0.562679,0.568929,0.542143,0.548393,0.465474,388920000
1092960000000,0.548393,0.553393,0.544464,0.55,0.466838,316780800
1093219200000,0.551071,0.558393,0.546429,0.555,0.471082,254660000
1093305600000,0.558214,0.570536,0.556964,0.570536,0.484269,374136000
Expand Down
1 change: 0 additions & 1 deletion Data/Dated/AAPL.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Date,Open,High,Low,Close,Adj Close,Volume
2004-08-19,0.562679,0.568929,0.542143,0.548393,0.465474,388920000
2004-08-20,0.548393,0.553393,0.544464,0.550000,0.466838,316780800
2004-08-23,0.551071,0.558393,0.546429,0.555000,0.471082,254660000
2004-08-24,0.558214,0.570536,0.556964,0.570536,0.484269,374136000
Expand Down
10 changes: 5 additions & 5 deletions Metrics/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Metrics::Metrics(Portfolio *p) {

// std::cout<<"4";

RETURNS = calculate_percent_change(TOTAL_EQUITY);
RETURNS = pct_change(TOTAL_EQUITY);
// Total Returns
// RETURNS = Eigen::MatrixXd(holdings.rows(), 1);
// RETURNS(0) = 1;
Expand Down Expand Up @@ -78,12 +78,12 @@ Metrics::Metrics(Portfolio *p) {
}

double Metrics::SHARPE_RATIO(int periods) {
return std::sqrt(periods) * RETURNS.mean() / std(RETURNS);
return std::sqrt(periods) * RETURNS.mean() / stdev(RETURNS);
}

double Metrics::std(Eigen::VectorXd vec) {
return std::sqrt((vec.array() - vec.mean()).square().sum() / (vec.size() - 1));
}
// double Metrics::std(Eigen::VectorXd vec) {
// return std::sqrt((vec.array() - vec.mean()).square().sum() / (vec.size() - 1));
// }


void Metrics::calculate_drawdown() {
Expand Down
15 changes: 14 additions & 1 deletion Metrics/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace Eigen;


VectorXd calculate_percent_change(VectorXd vec) {
VectorXd pct_change(VectorXd vec) {
VectorXd ret = MatrixXd(vec.rows(), 1);
ret(0) = 1;

Expand All @@ -21,3 +21,16 @@ VectorXd calculate_percent_change(VectorXd vec) {
double stdev(VectorXd vec) {
return std::sqrt((vec.array() - vec.mean()).square().sum() / (vec.size() - 1));
}

// This currently does not work.
MatrixXd correlation(MatrixXd RETURNS){

MatrixXd centered = RETURNS.rowwise() - RETURNS.colwise().mean();
MatrixXd cov = (centered.adjoint() * centered) / double(RETURNS.rows() - 1);

auto Dinv = cov.inverse();

// std::cout << cov;

return Dinv * cov * Dinv;
}
Binary file modified backtest.exe
Binary file not shown.
Binary file removed build/backtest.exe
Binary file not shown.
Binary file removed build/backtest.ilk
Binary file not shown.
Binary file removed build/backtest.pdb
Binary file not shown.
Binary file removed cash.obj
Binary file not shown.
8 changes: 0 additions & 8 deletions cov.cpp

This file was deleted.

Binary file removed cov.obj
Binary file not shown.
Binary file removed dh.obj
Binary file not shown.
Binary file removed engine.obj
Binary file not shown.
Binary file removed metrics.obj
Binary file not shown.
Binary file removed portfolio.obj
Binary file not shown.
Binary file removed position.obj
Binary file not shown.
Binary file removed risk.obj
Binary file not shown.
Binary file removed simdjson.obj
Binary file not shown.
Binary file removed strategy.obj
Binary file not shown.
Binary file removed vc140.pdb
Binary file not shown.

0 comments on commit d6e7acc

Please sign in to comment.