WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
rsi.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include "wu.h"
3
4static double update(WU_RSI rsi, const WU_Candle* candle) {
5 if (!candle) {
6 return rsi->value;
7 }
8 double diff = candle->close - candle->open;
9 wu_indicator_update(rsi->gain, diff > 0 ? diff : 0.0);
10 wu_indicator_update(rsi->loss, diff < 0 ? -diff : 0.0);
11 rsi->value = isnan(wu_indicator_get(rsi->loss))
12 ? NAN
13 : 100.0 - (100.0 / (1.0 + (wu_indicator_get(rsi->gain) /
14 wu_indicator_get(rsi->loss))));
15 return rsi->value;
16}
17
18static void delete(WU_RSI rsi) {
19 wu_indicator_delete(rsi->gain);
20 wu_indicator_delete(rsi->loss);
21 free(rsi);
22}
23
24WU_RSI wu_rsi_new(int window_size) {
25 WU_RSI rsi = malloc(sizeof(struct WU_RSI_));
26 rsi->gain = wu_ema_new(window_size, 1.0);
27 rsi->loss = wu_ema_new(window_size, 1.0);
28 rsi->value = NAN;
29 rsi->update = update;
30 rsi->delete = delete;
31 return rsi;
32}
static double update(WU_EMA ema, double value)
Definition ema.c:4
#define wu_indicator_get(indicator)
Get the current value of the indicator.
Definition indicators.h:49
#define wu_indicator_delete(indicator)
Delete the indicator and free any resources allocated by it.
Definition indicators.h:56
WU_EMA wu_ema_new(int window_size, double smoothing)
Creates a new WU_EMA (Exponential Moving Average) indicator with the specified period and smoothing f...
Definition ema.c:27
#define wu_indicator_update(indicator, value)
Header file for technical indicators.
Definition indicators.h:41
static double update(WU_RSI rsi, const WU_Candle *candle)
Definition rsi.c:4
WU_RSI wu_rsi_new(int window_size)
Creates a new WU_RSI (Relative Strength Index) indicator with the specified window size.
Definition rsi.c:24
WU_Candle represents an aggregated data point to represent how prices moved within a specific time pe...
Definition data.h:15
double close
Definition data.h:20
double open
Definition data.h:17
The WU_RSI (Relative Strength Index) is a momentum oscillator that measures the speed and change of p...
Definition indicators.h:161