Skip to content

Commit

Permalink
Fixed bug in the calculation of drawdown and drawdown duration
Browse files Browse the repository at this point in the history
  • Loading branch information
evanwporter committed Sep 26, 2023
1 parent 5a2bb31 commit 2a56e86
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 66 deletions.
3 changes: 1 addition & 2 deletions DataHandler/dh.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DataHandler {
std::map<std::string, int> s;

std::vector<datetime64> total_symbol_dates;
std::vector<std::string> symbols;// = {"GOOG", "AAPL"};
std::vector<std::string> symbols;

sd::ondemand::document settings;
bool quiet = true;
Expand All @@ -45,7 +45,6 @@ class DataHandler {
unsigned int total_bars, current, data_size;

DataHandler();
// DataHandler(std::vector<std::string> sym);

MoneyMatrixX getLatestBarsN(std::string symbol, int N);
};
Expand Down
81 changes: 22 additions & 59 deletions Metrics/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Metrics::Metrics(Portfolio *p) {

// std::cout<<"7";

// calculate_drawdown();
calculate_drawdown();

// std::cout << max_dd << std::endl;

Expand All @@ -83,70 +83,33 @@ double Metrics::SHARPE_RATIO(int periods) {
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));
// }


// void Metrics::calculate_drawdown() {

// MoneyVectorX high_water_mark = MoneyMatrixX(EQUITY_CURVE.size(), 1);
// MoneyVectorX drawdown = MoneyMatrixX(EQUITY_CURVE.size(), 1);
// MoneyVectorX drawdown_duration = MoneyMatrixX(EQUITY_CURVE.size(), 1);

// for (int t = 1; t < EQUITY_CURVE.size(); t++) {
// high_water_mark(t) = std::max(high_water_mark(t - 1), EQUITY_CURVE(t));
// drawdown(t) = high_water_mark(t) - EQUITY_CURVE(t);
// if (drawdown(t) == 0) {
// drawdown_duration(t) = 0;
// }
// else {
// drawdown_duration(t) = drawdown_duration(t-1) + 1;
// };
// // high_water_mark[t] = max(high_water_mark[t-1], self.eq_curve[t])
// // drawdown[t]= high_water_mark[t] - self.eq_curve[t]
// // drawdown_duration[t]= 0 if drawdown[t] == 0 else drawdown_duration[t-1] + 1
// };
void Metrics::calculate_drawdown() {

PercentVectorX high_water_mark = PercentVectorX(EQUITY_CURVE.size(), 1);
PercentVectorX drawdown = PercentVectorX(EQUITY_CURVE.size(), 1);
PercentVectorX drawdown_duration = PercentVectorX(EQUITY_CURVE.size(), 1);

for (int t = 1; t < EQUITY_CURVE.size(); t++) {
high_water_mark(t) = std::max(high_water_mark(t - 1), EQUITY_CURVE(t));
drawdown(t) = high_water_mark(t) - EQUITY_CURVE(t);
if (drawdown(t) == 0) {
drawdown_duration(t) = 0;
}
else {
drawdown_duration(t) = drawdown_duration(t-1) + 1;
};
};

// MAX_DRAWDOWN = drawdown.maxCoeff();
// DRAWDOWN_DURATION = drawdown_duration.maxCoeff();

// };

// void Metrics::cumulative_product(MoneyVectorX vec, MoneyVectorX& make_vec) {
// // Note this function automatically adds one to whatever its cumulatively multiplying.

// make_vec = Eigen::MatrixXd(vec.size(), 1);
// // make_vec(0) = vec(0);

// // std::cout << "TOTAL RETURNS" << std::endl << vec.head(10) << std::endl;

// make_vec(0) = vec(0);
// vec.array() += 1.f;

// // std::cout << "TOTAL RETURNS + 1" << std::endl << vec.head(10) << std::endl;
MAX_DRAWDOWN = drawdown.maxCoeff();
DRAWDOWN_DURATION = drawdown_duration.maxCoeff();

// for (int j = 1; j < vec.size(); j++) {
// make_vec(j) = (vec(j) * make_vec(j - 1));
// };
};

// }

void Metrics::printf_csv(std::string path, MoneyMatrixX matrix) {
const static Eigen::IOFormat CSVFormat(StreamPrecision, DontAlignCols, ", ", "\n");
std::ofstream file(path);
file << matrix.format(CSVFormat);


// std::cout << matrix;

// for (int row = 0; row < matrix.rows(); row++) {
// for (int col = 0; col < matrix.cols(); col++) std::cout << col << ',';
// std::cout << '\n';
// }

// for (auto row : matrix)

}

void Metrics::display_metrics() {
Expand All @@ -155,8 +118,8 @@ void Metrics::display_metrics() {
vt.addRow("Time Taken", TIME_TAKEN);
vt.addRow("Sharpe Ratio", SHARPE_RATIO(252));
vt.addRow("Total Return", TOTAL_RETURN);
vt.addRow("Max DD", 100);//MAX_DRAWDOWN);
vt.addRow("DD Duration", 100);//DRAWDOWN_DURATION);
vt.addRow("Max DD", MAX_DRAWDOWN);
vt.addRow("DD Duration", DRAWDOWN_DURATION);
// vt.addRow("Profit and Loss", PnL);

vt.print(std::cout);
Expand Down
6 changes: 1 addition & 5 deletions Metrics/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ class Metrics {

MoneyMatrixX holdings, TOTAL_EQUITY;

Matrix< double, Dynamic, 1 > RETURNS, EQUITY_CURVE;
PercentVectorX RETURNS, EQUITY_CURVE;


double SHARPE_RATIO(int periods);

// static double std(VectorXd vec);
// static void cumulative_product(Eigen::VectorXd vec, VectorXd& make_vec);

double MAX_DRAWDOWN, TOTAL_RETURN, PnL;
double TIME_TAKEN;
Expand Down
Binary file modified backtest.exe
Binary file not shown.
1 change: 1 addition & 0 deletions utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef long quantity_t;

typedef Eigen::Matrix< money, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MoneyMatrixX;
typedef Eigen::Matrix< money, Eigen::Dynamic, 1 > MoneyVectorX;
typedef Eigen::Matrix< double, Eigen::Dynamic, 1 > PercentVectorX;


#endif

0 comments on commit 2a56e86

Please sign in to comment.