WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
crossover.c
Go to the documentation of this file.
1#include <assert.h>
2#include <stdlib.h>
3#include "wu.h"
4
5#define NUM_INPUTS 1
6#define NUM_OUTPUTS 1
7
10
11static WU_Signal* update(WU_Strategy strat_, const void* inputs[]) {
12 WU_CrossOverStrat strat = (WU_CrossOverStrat)strat_;
13 const WU_Single* val = (const WU_Single*)inputs[0];
15 double short_val = wu_indicator_update(strat->short_ma, val->value);
16 double long_val = wu_indicator_update(strat->long_ma, val->value);
18 .timestamp = val->timestamp,
19 .side = WU_SIDE_HOLD,
20 .price = val->value,
21 .quantity = 1.0
22 };
23 if (isnan(short_val) || isnan(long_val)) {
24 return signal_buffer;
25 }
26 if ((short_val > long_val * (1.0 + strat->threshold)) &&
27 strat->last_signal != WU_SIDE_BUY) {
28 strat->last_signal = signal_buffer[0].side = WU_SIDE_BUY;
29 } else if ((short_val < long_val * (1.0 - strat->threshold)) &&
30 strat->last_signal != WU_SIDE_SELL) {
31 strat->last_signal = signal_buffer[0].side = WU_SIDE_SELL;
32 }
33 return signal_buffer;
34}
35
36static void delete(struct WU_Strategy_* strategy) {
37 WU_CrossOverStrat strat = (WU_CrossOverStrat)strategy;
38 wu_indicator_delete(strat->short_ma);
39 wu_indicator_delete(strat->long_ma);
40 free(strat);
41}
42
43WU_CrossOverStrat wu_crossover_strat_new(int short_window, int long_window,
44 double threshold) {
45 WU_CrossOverStrat strat = malloc(sizeof(struct WU_CrossOverStrat_));
46 strat->base.update = update;
47 strat->base.delete = delete;
48 strat->base.input_types = input_types;
49 strat->base.num_inputs = NUM_INPUTS;
50 strat->base.num_outputs = NUM_OUTPUTS;
51 strat->base.signal_buffer = signal_buffer;
52 strat->short_ma = wu_sma_new(short_window);
53 strat->long_ma = wu_sma_new(long_window);
54 strat->threshold = threshold;
55 strat->last_signal = WU_SIDE_HOLD;
56 return strat;
57}
58
59
static WU_Signal * update(WU_Strategy strat_, const void *inputs[])
Definition crossover.c:11
#define NUM_OUTPUTS
Definition crossover.c:6
static WU_Signal signal_buffer[NUM_OUTPUTS]
Definition crossover.c:9
static const WU_DataType input_types[]
Definition crossover.c:8
#define NUM_INPUTS
Definition crossover.c:5
WU_CrossOverStrat wu_crossover_strat_new(int short_window, int long_window, double threshold)
Definition crossover.c:43
static double update(WU_EMA ema, double value)
Definition ema.c:4
#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_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_CrossOverStrat is a simple crossover strategy that generates buy and sell signals based on the cro...
Definition strategies.h:39
WU_Signal represents a trading signal generated by a strategy.
Definition types.h:38
WU_Single represents a single value with a timestamp, which can be a price, an indicator value,...
Definition data.h:47
WU_DataType data_type
Definition data.h:50
WU_TimeStamp timestamp
Definition data.h:48
double value
Definition data.h:49
@ WU_SIDE_HOLD
Definition types.h:12
@ WU_SIDE_SELL
Definition types.h:14
@ WU_SIDE_BUY
Definition types.h:13
WU_DataType
WU_DataType represents the type of input data, which can be a candle, a trade, or a single value.
Definition types.h:24
@ WU_DATA_TYPE_SINGLE_VALUE
Definition types.h:27