WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
mean_downside.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <math.h>
3#include "wu/indicators.h"
4
5static double update(WU_Downside self, double value) {
6 if (isnan(value)) return NAN;
7 self->count++;
8 if (value < 0.0) {
9 self->downside_m2 += value * value;
10 }
11 if (self->count == 0) {
12 self->value = NAN;
13 } else {
14 self->value = sqrt(self->downside_m2 / self->count);
15 }
16 return self->value;
17}
18
19static void delete(WU_Downside self) {
20 free(self);
21}
22
23WU_Downside wu_downside_new(void) {
24 WU_Downside d = malloc(sizeof(struct WU_Downside_));
25 if (!d) return NULL;
26 d->downside_m2 = 0.0;
27 d->count = 0;
28 d->value = NAN;
29 d->update = update;
30 d->delete = delete;
31 return d;
32}
static double update(WU_EMA ema, double value)
Definition ema.c:4
static double update(WU_Downside self, double value)
WU_Downside wu_downside_new(void)
Creates a new WU_Downside indicator.
Tracks the average downside, calculate as the square root of the average of the squared negative valu...
Definition indicators.h:250