WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
portfolios.h
Go to the documentation of this file.
1#ifndef WU_PORTFOLIO_H
2#define WU_PORTFOLIO_H
3
4#include "types.h"
5#include "data.h"
6#include "positions.h"
7#include "stats.h"
8
9/**
10 * Base for a portfolio, which defines the minimal interface for
11 * updating the portfolio with signals, calculating the current
12 * value of the portfolio, and calculating the profit and loss (PnL), as
13 * well as a method to free the portfolio's resources. The delete method
14 * should be called by the runner taking ownership of the portfolio. It
15 * is expected that specific portfolio implementations will extend this
16 * base structure and implement the defined methods.
17 */
18typedef struct WU_Portfolio_ {
19 void (*update)(struct WU_Portfolio_* portfolio, const WU_Signal* signals);
20 double (*value)(const struct WU_Portfolio_* portfolio);
21 double (*pnl)(const struct WU_Portfolio_* portfolio);
22 void (*delete)(struct WU_Portfolio_* portfolio);
23}* WU_Portfolio;
24
25/**
26 * The update macro allows you to update the portfolio with new signals.
27 */
28#define wu_portfolio_update(portfolio, signals) do { \
29 ((WU_Portfolio)(portfolio))->update((WU_Portfolio)(portfolio), \
30 (signals)); \
31} while(0)
32
33/**
34 * Calculates the current value of the portfolio. This includes the
35 * value of all positions plus any remaining cash.
36 */
37#define wu_portfolio_value(portfolio) (((WU_Portfolio)(portfolio))->value( \
38 (WU_Portfolio)(portfolio)))
39
40/**
41 * Calculates the profit and loss (PnL) of the portfolio. This is typically
42 * calculated as the difference between the current value of the portfolio and
43 * the initial cash invested, minus any transaction costs or fees.
44 */
45#define wu_portfolio_pnl(portfolio) (((WU_Portfolio)(portfolio))->pnl( \
46 (WU_Portfolio)(portfolio)))
47
48/**
49 * Frees the resources associated with the portfolio. Usually this macro
50 * is called by the runner when it takes ownership of the portfolio.
51 */
52#define wu_portfolio_delete(portfolio) do { \
53 if ((portfolio)->delete) \
54 (portfolio)->delete((WU_Portfolio)(portfolio)); \
55} while(0)
56
62
69
74
84
85typedef struct {
86 double rate;
87 double limit;
89
90/**
91 * The parameters to define a portfolio.
92 */
101
102/**
103 * A basic portfolio implementation that supports multiple assets. It
104 * tracks cash, positions for each asset, and calculates portfolio value
105 * and PnL based on the current market prices and the positions held. It
106 * also keeps track of accumulated transaction costs and trading
107 * statistics. The portfolio is initialized with a set of parameters and
108 * an array of asset symbols, and it dynamically manages the positions
109 * for each asset.
110 */
111
122
123/**
124 * Returns default portfolio parameters with reasonable values.
125 */
127
128/**
129 * Creates a new basic portfolio instance with the specified parameters
130 * and asset symbols. The array symbols should be created using the
131 * `wu_symbol_list` macro, which ensures it is null-terminated.
132 */
133WU_BasicPortfolio wu_basic_portfolio_new(
134 WU_PortfolioParams params,
135 const char* symbols[]
136);
137
138/**
139 * Get current value of a specific asset's positions
140 */
142 WU_BasicPortfolio portfolio,
143 int asset_index
144);
145
146/**
147 * Get total quantity held for a specific asset
148 */
150 WU_BasicPortfolio portfolio,
151 int asset_index
152);
153
154/**
155 * Casting macro for the base portfolio type.
156 */
157#define WU_PORTFOLIO(portfolio) ((WU_Portfolio)(portfolio))
158
159#endif // WU_PORTFOLIO_H
WU_BasicPortfolio wu_basic_portfolio_new(WU_PortfolioParams params, const char *symbols[])
Creates a new basic portfolio instance with the specified parameters and asset symbols.
Definition basic.c:764
double wu_basic_portfolio_asset_quantity(WU_BasicPortfolio portfolio, int asset_index)
Get total quantity held for a specific asset.
Definition basic.c:827
WU_TransactionCostType
Definition portfolios.h:70
@ WU_TRANSACTION_COST_PROPORTIONAL
Definition portfolios.h:72
@ WU_TRANSACTION_COST_FIXED
Definition portfolios.h:71
WU_PortfolioParams wu_portfolio_params_default(void)
Returns default portfolio parameters with reasonable values.
Definition basic.c:19
WU_ExecutionPolicyValue
Definition portfolios.h:63
@ WU_EXECUTION_POLICY_IMMEDIATE
Definition portfolios.h:64
@ WU_EXECUTION_POLICY_RANDOM_SLIPPAGE
Definition portfolios.h:67
@ WU_EXECUTION_POLICY_FIXED_SLIPPAGE
Definition portfolios.h:66
@ WU_EXECUTION_POLICY_NEXT_CLOSE
Definition portfolios.h:65
double wu_basic_portfolio_asset_value(WU_BasicPortfolio portfolio, int asset_index)
Get current value of a specific asset's positions.
Definition basic.c:812
WU_Direction
Definition portfolios.h:57
@ WU_DIRECTION_BOTH
Definition portfolios.h:60
@ WU_DIRECTION_SHORT
Definition portfolios.h:59
@ WU_DIRECTION_LONG
Definition portfolios.h:58
A basic portfolio implementation that supports multiple assets.
Definition portfolios.h:112
WU_PositionVector ** positions
Definition portfolios.h:118
WU_TimeStamp last_update_time
Definition portfolios.h:116
WU_Signal * pending_orders
Definition portfolios.h:120
WU_PortfolioParams params
Definition portfolios.h:117
WU_PortfolioStats stats
Definition portfolios.h:119
struct WU_Portfolio_ base
Definition portfolios.h:113
WU_ExecutionPolicyValue policy
Definition portfolios.h:76
WU_TransactionCostType tx_cost_type
Definition portfolios.h:79
The parameters to define a portfolio.
Definition portfolios.h:93
WU_BorrowParams borrow_params
Definition portfolios.h:98
WU_ExecutionPolicy execution_policy
Definition portfolios.h:97
WU_PositionSizingParams position_sizing
Definition portfolios.h:99
WU_Direction direction
Definition portfolios.h:94
Base for a portfolio, which defines the minimal interface for updating the portfolio with signals,...
Definition portfolios.h:18
void(* update)(struct WU_Portfolio_ *portfolio, const WU_Signal *signals)
Definition portfolios.h:19
double(* value)(const struct WU_Portfolio_ *portfolio)
Definition portfolios.h:20
double(* pnl)(const struct WU_Portfolio_ *portfolio)
Definition portfolios.h:21
Position sizing policy.
Definition positions.h:70
WU_PositionVector is a data structure that holds multiple positions for a single asset.
Definition positions.h:24
WU_Signal represents a trading signal generated by a strategy.
Definition types.h:38
A timestamp represent a mark in time given relative to unix epoch.
Definition timeutils.h:21