WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
ema.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include "wu.h"
3
4static double update(WU_EMA ema, double value) {
5 if (isnan(value)) {
6 return ema->value;
7 }
8 ema->len++;
9 if (ema->len < ema->period) {
10 ema->prev_value += value;
11 } else if (ema->len == ema->period) {
12 ema->prev_value += value;
13 ema->prev_value /= ema->period;
14 ema->value = ema->prev_value;
15 } else {
16 ema->prev_value = ema->alpha * value + (1 - ema->alpha)
17 * ema->prev_value;
18 ema->value = ema->prev_value;
19 }
20 return ema->value;
21}
22
23static void delete(WU_EMA ema) {
24 free(ema);
25}
26
27WU_EMA wu_ema_new(int period, double smoothing) {
28 WU_EMA ema = malloc(sizeof(struct WU_EMA_));
29 ema->value = NAN;
30 ema->period = period;
31 ema->alpha = smoothing / (period + 1);
32 ema->prev_value = 0.0;
33 ema->len = 0;
34 ema->update = update;
35 ema->delete = delete;
36 return ema;
37}
38
39
WU_EMA wu_ema_new(int period, double smoothing)
Creates a new WU_EMA (Exponential Moving Average) indicator with the specified period and smoothing f...
Definition ema.c:27
static double update(WU_EMA ema, double value)
Definition ema.c:4
The exponential moving average (WU_EMA) is a type of moving average that gives more weight to recent ...
Definition indicators.h:85