4static double update(WU_SMA ma,
double value) {
5 if (isnan(value))
return ma->value;
6 if (ma->len < ma->window_size)
9 ma->sum -= ma->prev_values[ma->pos];
11 ma->prev_values[ma->pos] = value;
12 ma->pos = (ma->pos + 1) % ma->window_size;
13 ma->value = ma->len < ma->window_size ? NAN : ma->sum / ma->window_size;
17static void delete(WU_SMA ma) {
18 free(ma->prev_values);
23 WU_SMA ma = malloc(
sizeof(
struct WU_SMA_));
26 ma->prev_values = malloc(window_size *
sizeof(
double));
27 if (!ma->prev_values) {
33 ma->window_size = window_size;
34 for (
int i = 0; i < window_size; i++) {
35 ma->prev_values[i] = NAN;
static double update(WU_EMA ema, double value)
WU_SMA wu_sma_new(int window_size)
Creates a new WU_SMA (Simple Moving Average) indicator with the specified window size.
static double update(WU_SMA ma, double value)
MovingAverage is a simple moving average indicator that calculates the average of the last N values,...