WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
indicators.h
Go to the documentation of this file.
1#ifndef WU_INDICATOR_H
2#define WU_INDICATOR_H
3
4/**
5 * Header file for technical indicators. Defines the structures and
6 * function prototypes for various indicators. All the indicator should
7 * follow the same interface:
8 *
9 * - `update`: A function that takes a new value (or candle) and updates
10 * the indicator's state, returning the new indicator value.
11 * - `delete`: A function that frees any resources allocated by the
12 * indicator.
13 * - `value`: A field that holds the current value of the indicator.
14 *
15 * Once the indicator has been created, it should only be accessed using
16 * the provided macros:
17 *
18 * - `wu_indicator_update(indicator, value)`: Updates the indicator with
19 * a new value and returns the updated indicator value.
20 * - `wu_indicator_get(indicator)`: Retrieves the current value of the
21 * indicator.
22 * - `wu_indicator_delete(indicator)`: Frees any resources allocated by
23 * the indicator.
24 *
25 * This design allows for a consistent interface across different types
26 * of indicators, making it easier to use them interchangeably in
27 * trading strategies.
28 */
29
30#include "types.h"
31#include "data.h"
32#include <stdlib.h>
33#include <math.h>
34
35/**
36 * Update the indicator state with a new value a return the updated
37 * indicator state. This macro assumes that the indicator has an
38 * `update` function that takes the indicator itself and a new value,
39 * and returns the updated indicator value.
40 */
41#define wu_indicator_update(indicator, value) \
42 (indicator)->update((indicator), (value))
43
44/**
45 * Get the current value of the indicator. This macro assume that the
46 * indicator has a `value` field that holds the current state of the
47 * indicator.
48 */
49#define wu_indicator_get(indicator) ((indicator)->value)
50
51/**
52 * Delete the indicator and free any resources allocated by it. This
53 * macro assumes that the indicator has a `delete` function that takes
54 * the indicator itself and frees any resources allocated by it.
55 */
56#define wu_indicator_delete(indicator) (indicator)->delete((indicator))
57
58/**
59 * MovingAverage is a simple moving average indicator that calculates
60 * the average of the last N values, where N is the window size.
61 */
62typedef struct WU_SMA_ {
63 double (*update)(struct WU_SMA_ *self, double value);
64 void (*delete)(struct WU_SMA_ *self);
65 double value;
66 double* prev_values;
68 int pos;
69 int len;
70 double sum;
71}* WU_SMA;
72
73/**
74 * Creates a new WU_SMA (Simple Moving Average) indicator with the
75 * specified window size.
76 */
77WU_SMA wu_sma_new(int window_size);
78
79/**
80 * The exponential moving average (WU_EMA) is a type of moving average that
81 * gives more weight to recent values, making it more responsive to
82 * recent value changes. The WU_EMA is calculated using a smoothing factor
83 * that determines how much weight is given to the most recent value.
84 */
85typedef struct WU_EMA_ {
86 double (*update)(struct WU_EMA_ *self, double value);
87 void (*delete)(struct WU_EMA_ *self);
88 double value;
89 double prev_value;
90 double alpha;
91 int len;
92 int period;
93}* WU_EMA;
94
95/**
96 * Creates a new WU_EMA (Exponential Moving Average) indicator with the
97 * specified period and smoothing factor. Usually, 2.0 is used as the
98 * smoothing factor.
99 */
100WU_EMA wu_ema_new(int window_size, double smoothing);
101
102/**
103 * The WU_MVar (Moving Variance) is an indicator that calculates the variance
104 * of the last N values, where N is the window size. It is used to measure
105 * the volatility of a time series. `dof` stands for "degree of freedom"
106 * and is used to adjust the variance calculation. If `dof` is 0, the
107 * variance is calculated using the population variance formula. If
108 * `dof` is 1, the variance is calculated using the sample variance
109 * formula.
110 */
111typedef struct WU_MVar_ *WU_MVar;
112
113struct WU_MVar_ {
114 double (*update)(struct WU_MVar_ *self, double value);
115 void (*delete)(struct WU_MVar_ *self);
116 double value;
117 WU_SMA sma;
118 double* prev_values;
119 double sum2;
121 int pos;
122 int len;
123 int dof;
124};
125
126/**
127 * Creates a new WU_MVar (Moving Variance) indicator with the specified
128 * window size and degree of freedom.
129 */
130WU_MVar wu_mvar_new(int window_size, int dof);
131
132/**
133 * The WU_MStDev (Moving Standard Deviation) is an indicator that calculates
134 * the standard deviation of the last N values, where N is the window size.
135 * It is used to measure the volatility of a time series. `dof` stands for
136 * "degree of freedom" and is used to adjust the standard deviation
137 * calculation. If `dof` is 0, the standard deviation is calculated using
138 * the population standard deviation formula. If `dof` is 1, the standard
139 * deviation is calculated using the sample standard deviation formula.
140 */
141typedef struct WU_MStDev_ {
142 double (*update)(struct WU_MStDev_ *self, double value);
143 void (*delete)(struct WU_MStDev_ *self);
144 double value;
145 WU_MVar mvar;
146}* WU_MStDev;
147
148/**
149 * Creates a new WU_MStDev (Moving Standard Deviation) indicator with the
150 * specified window size and degree of freedom.
151 */
152WU_MStDev wu_mstdev_new(int window_size, int dof);
153
154/**
155 * The WU_RSI (Relative Strength Index) is a momentum oscillator that measures
156 * the speed and change of price movements. It is calculated using the
157 * average gain and average loss over a specified period. The RSI ranges
158 * from 0 to 100, with values above 70 typically indicating overbought
159 * conditions and values below 30 indicating oversold conditions.
160 */
161typedef struct WU_RSI_ {
162 double (*update)(struct WU_RSI_ *self, const WU_Candle* candle);
163 void (*delete)(struct WU_RSI_ *self);
164 double value;
165 WU_EMA gain;
166 WU_EMA loss;
167}* WU_RSI;
168
169/**
170 * Creates a new WU_RSI (Relative Strength Index) indicator with the
171 * specified window size.
172 */
173WU_RSI wu_rsi_new(int window_size);
174
175/**
176 * The WU_MACDResult structure holds the current values of the MACD line,
177 * signal line, and histogram. The MACD line is the difference between the
178 * short-term EMA and long-term EMA. The signal line is an EMA of the MACD
179 * line, and the histogram is the difference between the MACD line and the
180 * signal line.
181 */
182typedef struct MACDResult_ {
183 double macd;
184 double signal;
185 double histogram;
187
188/**
189 * The WU_MACD (Moving Average Convergence Divergence) is a
190 * trend-following momentum indicator that shows the relationship
191 * between two moving averages of a security's price. The MACD is
192 * calculated by subtracting the long-term EMA from the short-term EMA.
193 * A signal line, which is an EMA of the MACD, is then plotted on top of
194 * the MACD to identify buy and sell signals. The histogram represents
195 * the difference between the MACD and the signal line, providing a
196 * visual representation of the momentum of the price movement.
197 */
198typedef struct WU_MACD_ {
199 WU_MACDResult (*update)(struct WU_MACD_ *self, double value);
200 void (*delete)(struct WU_MACD_ *self);
202 WU_EMA ema_short;
203 WU_EMA ema_long;
205 int len;
206 int start;
207}* WU_MACD;
208
209/**
210 * Creates a new WU_MACD (Moving Average Convergence Divergence) indicator
211 * with the specified short and long window sizes, and signal line window
212 * size.
213 */
214WU_MACD wu_macd_new(int short_window, int long_window, int signal_window,
215 double smoothing);
216
217/**
218 * Maximum Drawdown - tracks the largest peak-to-trough decline.
219 * Updates with portfolio values and returns the drawdown as a negative
220 * percentage. Calculated as (current_value - peak) / peak when current
221 * is below peak.
222 */
223typedef struct WU_MaxDrawdown_ {
224 double (*update)(struct WU_MaxDrawdown_* self, double portfolio_value);
225 void (*delete)(struct WU_MaxDrawdown_* self);
226 double value;
227 double peak;
228}* WU_MaxDrawdown;
229
230/**
231 * Creates a new WU_MaxDrawdown indicator. The initial value is set to 0
232 * and the initial peak is set to the first portfolio value observed.
233 */
234WU_MaxDrawdown wu_max_drawdown_new(void);
235
236/**
237 * Performance Update - value and timestamp for performance metric calculations.
238 */
239typedef struct {
240 double portfolio_value; // Current portfolio value
241 WU_TimeStamp timestamp; // Timestamp of this value
243
244
245/**
246 * Tracks the average downside, calculate as the square root of the
247 * average of the squared negative values. It is used with returns to
248 * measure the volatility of negative returns.
249 */
250typedef struct WU_Downside_ {
251 double (*update)(struct WU_Downside_ *self, double value);
252 void (*delete)(struct WU_Downside_ *self);
253 double value;
254 double downside_m2; // Sum of squared negative returns
255 int64_t count; // Number of returns observed (for denominator)
256}* WU_Downside;
257
258/**
259 * Creates a new WU_Downside indicator.
260 */
261WU_Downside wu_downside_new(void);
262
263/**
264 * A global mean calculator that updates with new values and maintains
265 * the current mean.
266 */
267typedef struct WU_Mean_ {
268 double (*update)(struct WU_Mean_ *self, double value);
269 void (*delete)(struct WU_Mean_ *self);
270 double value;
271 int count;
272 double accum;
273}* WU_Mean;
274
275/**
276 * Creates a new WU_Mean indicator.
277 */
278WU_Mean wu_mean_new(void);
279
280/**
281 * A glabal variance calculator. It reports the variance for all passed
282 * values
283 */
284typedef struct WU_Var_ {
285 double (*update)(struct WU_Var_ *self, double value);
286 void (*delete)(struct WU_Var_ *self);
287 double value;
288 WU_Mean mean;
289 double sum2;
290 int dof;
291 int count;
292}* WU_Var;
293
294/**
295 * Createas a new variance indicator
296 */
297WU_Var wu_var_new(int dof);
298
299/** A global standard deviation calcular. It reports the standard
300 * deviation for all passed values.
301 */
302typedef struct WU_StDev_ {
303 double (*update)(struct WU_StDev_ *self, double value);
304 void (*delete)(struct WU_StDev_ *self);
305 double value;
306 WU_Var var;
307}* WU_StDev;
308
309/**
310 * Creates a new standard deviation indicator
311 */
312WU_StDev wu_stdev_new(int dof);
313
314#endif // WU_INDICATOR_H
WU_Var wu_var_new(int dof)
Createas a new variance indicator.
Definition var.c:25
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
WU_StDev wu_stdev_new(int dof)
Creates a new standard deviation indicator.
Definition var.c:49
WU_Downside wu_downside_new(void)
Creates a new WU_Downside indicator.
WU_Mean wu_mean_new(void)
Creates a new WU_Mean indicator.
Definition mean.c:15
WU_MaxDrawdown wu_max_drawdown_new(void)
Creates a new WU_MaxDrawdown indicator.
Definition maxdrawdown.c:20
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_MStDev wu_mstdev_new(int window_size, int dof)
Creates a new WU_MStDev (Moving Standard Deviation) indicator with the specified window size and degr...
Definition mstdev.c:17
WU_SMA wu_sma_new(int window_size)
Creates a new WU_SMA (Simple Moving Average) indicator with the specified window size.
Definition sma.c:22
WU_MACD wu_macd_new(int short_window, int long_window, int signal_window, double smoothing)
Creates a new WU_MACD (Moving Average Convergence Divergence) indicator with the specified short and ...
Definition macd.c:27
WU_MVar wu_mvar_new(int window_size, int dof)
Creates a new WU_MVar (Moving Variance) indicator with the specified window size and degree of freedo...
Definition mvar.c:32
WU_Candle represents an aggregated data point to represent how prices moved within a specific time pe...
Definition data.h:15
Tracks the average downside, calculate as the square root of the average of the squared negative valu...
Definition indicators.h:250
double(* update)(struct WU_Downside_ *self, double value)
Definition indicators.h:251
double downside_m2
Definition indicators.h:254
int64_t count
Definition indicators.h:255
The exponential moving average (WU_EMA) is a type of moving average that gives more weight to recent ...
Definition indicators.h:85
int period
Definition indicators.h:92
double(* update)(struct WU_EMA_ *self, double value)
Definition indicators.h:86
int len
Definition indicators.h:91
double value
Definition indicators.h:88
double alpha
Definition indicators.h:90
double prev_value
Definition indicators.h:89
The WU_MACDResult structure holds the current values of the MACD line, signal line,...
Definition indicators.h:182
double histogram
Definition indicators.h:185
The WU_MACD (Moving Average Convergence Divergence) is a trend-following momentum indicator that show...
Definition indicators.h:198
WU_EMA signal_line
Definition indicators.h:204
WU_EMA ema_short
Definition indicators.h:202
WU_MACDResult value
Definition indicators.h:201
WU_MACDResult(* update)(struct WU_MACD_ *self, double value)
Definition indicators.h:199
WU_EMA ema_long
Definition indicators.h:203
The WU_MStDev (Moving Standard Deviation) is an indicator that calculates the standard deviation of t...
Definition indicators.h:141
WU_MVar mvar
Definition indicators.h:145
double value
Definition indicators.h:144
double(* update)(struct WU_MStDev_ *self, double value)
Definition indicators.h:142
The WU_MVar (Moving Variance) is an indicator that calculates the variance of the last N values,...
Definition indicators.h:113
WU_SMA sma
Definition indicators.h:117
double value
Definition indicators.h:116
double sum2
Definition indicators.h:119
int window_size
Definition indicators.h:120
double(* update)(struct WU_MVar_ *self, double value)
Definition indicators.h:114
double * prev_values
Definition indicators.h:118
Maximum Drawdown - tracks the largest peak-to-trough decline.
Definition indicators.h:223
double(* update)(struct WU_MaxDrawdown_ *self, double portfolio_value)
Definition indicators.h:224
A global mean calculator that updates with new values and maintains the current mean.
Definition indicators.h:267
double(* update)(struct WU_Mean_ *self, double value)
Definition indicators.h:268
double accum
Definition indicators.h:272
double value
Definition indicators.h:270
Performance Update - value and timestamp for performance metric calculations.
Definition indicators.h:239
WU_TimeStamp timestamp
Definition indicators.h:241
The WU_RSI (Relative Strength Index) is a momentum oscillator that measures the speed and change of p...
Definition indicators.h:161
double(* update)(struct WU_RSI_ *self, const WU_Candle *candle)
Definition indicators.h:162
WU_EMA gain
Definition indicators.h:165
double value
Definition indicators.h:164
WU_EMA loss
Definition indicators.h:166
MovingAverage is a simple moving average indicator that calculates the average of the last N values,...
Definition indicators.h:62
double * prev_values
Definition indicators.h:66
double value
Definition indicators.h:65
int pos
Definition indicators.h:68
int window_size
Definition indicators.h:67
double sum
Definition indicators.h:70
int len
Definition indicators.h:69
double(* update)(struct WU_SMA_ *self, double value)
Definition indicators.h:63
A global standard deviation calcular.
Definition indicators.h:302
WU_Var var
Definition indicators.h:306
double value
Definition indicators.h:305
double(* update)(struct WU_StDev_ *self, double value)
Definition indicators.h:303
A timestamp represent a mark in time given relative to unix epoch.
Definition timeutils.h:21
A glabal variance calculator.
Definition indicators.h:284
double(* update)(struct WU_Var_ *self, double value)
Definition indicators.h:285
double sum2
Definition indicators.h:289
double value
Definition indicators.h:287
WU_Mean mean
Definition indicators.h:288
int count
Definition indicators.h:291