WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
strategies.h
Go to the documentation of this file.
1#ifndef WU_STRATEGY_H
2#define WU_STRATEGY_H
3
4#include "types.h"
5#include "data.h"
6#include "indicators.h"
7
8typedef struct WU_Strategy_ {
9 WU_Signal* (*update)(struct WU_Strategy_* strategy,
10 const void* inputs[]);
11 void (*delete)(struct WU_Strategy_* strategy);
13 // const WU_AssetSymbol* output_symbols;
17}* WU_Strategy;
18
19#define WU_STRATEGY(strategy) ((WU_Strategy)(strategy))
20
21#define wu_strategy_update(strategy, inputs) \
22 ((strategy)->update((strategy), (inputs)))
23
24#define wu_strategy_delete(strategy) do { \
25 if ((strategy)->delete) \
26 (strategy)->delete((WU_Strategy)(strategy)); \
27} while(0)
28
29#define wu_strategy_num_inputs(strategy) ((strategy)->num_inputs)
30#define wu_strategy_input_type(strategy, idx) ((strategy)->input_types[idx])
31#define wu_strategy_num_outputs(strategy) ((strategy)->num_outputs)
32#define wu_strategy_output_symbol(strategy, idx) \
33 ((strategy)->output_symbols[idx])
34
35/**
36 * WU_CrossOverStrat is a simple crossover strategy that generates buy and
37 * sell signals based on the crossover of two moving averages.
38 */
39typedef struct WU_CrossOverStrat_ {
41 WU_SMA short_ma;
42 WU_SMA long_ma;
43 double threshold;
45}* WU_CrossOverStrat;
46
47WU_CrossOverStrat wu_crossover_strat_new(int short_window, int long_window,
48 double threshold);
49
50/**
51 * WU_PairsTradingStrat implements a classic pairs trading strategy that
52 * trades the spread between two correlated assets (statistical
53 * arbitrage).
54 *
55 * Strategy Logic:
56 * - Calculates the spread: spread = asset_a - (ratio * asset_b)
57 * - Tracks the mean and standard deviation of the spread using a
58 * rolling window
59 * - Generates BUY signal when spread falls below (mean - threshold *
60 * stdev) → Asset A is undervalued relative to Asset B, so buy A and
61 * sell B
62 * - Generates SELL signal when spread rises above (mean + threshold *
63 * stdev) → Asset A is overvalued relative to Asset B, so sell A and
64 * buy B
65 * - Generates CLOSE signal when spread returns to mean (exit position)
66 *
67 * Typical use cases:
68 * - Coca-Cola (KO) vs PepsiCo (PEP)
69 * - ExxonMobil (XOM) vs Chevron (CVX)
70 * - Gold (GLD) vs Gold Miners (GDX)
71 *
72 * Parameters:
73 * - window: lookback period for calculating spread statistics
74 * - threshold: number of standard deviations for entry signals
75 * (typically 1.5-2.5)
76 * - ratio: hedge ratio between the two assets (typically 1.0 or
77 * calculated via regression)
78 */
79typedef struct WU_PairsTradingStrat_ {
81 WU_SMA spread_ma;
82 WU_MStDev spread_std;
83 double threshold;
84 double ratio;
86}* WU_PairsTradingStrat;
87
88/**
89 * Creates a new pairs trading strategy.
90 *
91 * @param window Lookback period for spread statistics
92 * @param threshold Number of standard deviations for entry (e.g., 2.0)
93 * @param ratio Hedge ratio between assets
94 * @return New pairs trading strategy instance
95 */
96WU_PairsTradingStrat wu_pairs_trading_strat_new(int window, double threshold,
97 double ratio);
98
99#endif // WU_STRATEGY_H
WU_PairsTradingStrat wu_pairs_trading_strat_new(int window, double threshold, double ratio)
Creates a new pairs trading strategy.
WU_CrossOverStrat wu_crossover_strat_new(int short_window, int long_window, double threshold)
Definition crossover.c:43
WU_CrossOverStrat is a simple crossover strategy that generates buy and sell signals based on the cro...
Definition strategies.h:39
struct WU_Strategy_ base
Definition strategies.h:40
WU_PairsTradingStrat implements a classic pairs trading strategy that trades the spread between two c...
Definition strategies.h:79
struct WU_Strategy_ base
Definition strategies.h:80
WU_Signal represents a trading signal generated by a strategy.
Definition types.h:38
WU_Signal * signal_buffer
Definition strategies.h:14
const WU_DataType * input_types
Definition strategies.h:12
WU_Side
WU_Side represents the direction of a signal or a trade.
Definition types.h:11
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