WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
maxdrawdown.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <math.h>
3#include "wu.h"
4
5static double update(WU_MaxDrawdown self, double portfolio_value) {
6 if (portfolio_value > self->peak)
7 self->peak = portfolio_value;
8 if (self->peak > 0.0) {
9 double curr = (portfolio_value - self->peak) / self->peak;
10 if (curr < self->value)
11 self->value = curr;
12 }
13 return self->value;
14}
15
16static void wu_max_drawdown_free(WU_MaxDrawdown self) {
17 free(self);
18}
19
20WU_MaxDrawdown wu_max_drawdown_new(void) {
21 WU_MaxDrawdown md = malloc(sizeof(struct WU_MaxDrawdown_));
22 if (!md) return NULL;
23 md->value = 0.0;
24 md->peak = 0.0;
25 md->update = update;
26 md->delete = wu_max_drawdown_free;
27 return md;
28}
static double update(WU_EMA ema, double value)
Definition ema.c:4
static double update(WU_MaxDrawdown self, double portfolio_value)
Definition maxdrawdown.c:5
static void wu_max_drawdown_free(WU_MaxDrawdown self)
Definition maxdrawdown.c:16
WU_MaxDrawdown wu_max_drawdown_new(void)
Creates a new WU_MaxDrawdown indicator.
Definition maxdrawdown.c:20
Maximum Drawdown - tracks the largest peak-to-trough decline.
Definition indicators.h:223