WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
var.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include "wu/indicators.h"
3
4/**
5 * The variance is compute based on the difference between
6 * \sum x^2 - n * mean. Degrees of freedom are considered to decided
7 * when valid values start generating.
8 */
9static double var_update(WU_Var self, double value) {
10 if (isnan(value)) return NAN;
11 self->count++;
12 self->sum2 += value * value;
13 double mean = wu_indicator_update(self->mean, value);
14 if (self->count <= self->dof) return NAN;
15 self->value = (self->sum2 - self->count * mean * mean)
16 / (self->count - self->dof);
17 return self->value;
18}
19
20static void var_delete(struct WU_Var_* self) {
22 free(self);
23}
24
25WU_Var wu_var_new(int dof) {
26 WU_Var var = malloc(sizeof(struct WU_Var_));
27 if (!var) return NULL;
28 var->update = var_update;
29 var->delete = var_delete;
30 var->value = NAN;
31 var->mean = wu_mean_new();
32 var->sum2 = 0.0;
33 var->dof = dof;
34 var->count = 0;
35 return var;
36}
37
38static double stdev_update(WU_StDev self, double value) {
39 double var = wu_indicator_update(self->var, value);
40 self->value = isnan(var) ? NAN : sqrt(var);
41 return self->value;
42}
43
44static void stdev_delete(struct WU_StDev_* self) {
46 free(self);
47}
48
49WU_StDev wu_stdev_new(int dof) {
50 WU_StDev stdev = malloc(sizeof(struct WU_StDev_));
51 if (!stdev) return NULL;
52 stdev->var = wu_var_new(dof);
53 if (!stdev->var) {
54 free(stdev);
55 return NULL;
56 }
57 stdev->update = stdev_update;
58 stdev->delete = stdev_delete;
59 stdev->value = NAN;
60 return stdev;
61}
#define wu_indicator_delete(indicator)
Delete the indicator and free any resources allocated by it.
Definition indicators.h:56
#define wu_indicator_update(indicator, value)
Header file for technical indicators.
Definition indicators.h:41
WU_Mean wu_mean_new(void)
Creates a new WU_Mean indicator.
Definition mean.c:15
A global standard deviation calcular.
Definition indicators.h:302
WU_Var var
Definition indicators.h:306
A glabal variance calculator.
Definition indicators.h:284
WU_Mean mean
Definition indicators.h:288
WU_Var wu_var_new(int dof)
Createas a new variance indicator.
Definition var.c:25
static double var_update(WU_Var self, double value)
The variance is compute based on the difference between \sum x^2 - n * mean.
Definition var.c:9
static void stdev_delete(struct WU_StDev_ *self)
Definition var.c:44
static double stdev_update(WU_StDev self, double value)
Definition var.c:38
WU_StDev wu_stdev_new(int dof)
Creates a new standard deviation indicator.
Definition var.c:49
static void var_delete(struct WU_Var_ *self)
Definition var.c:20