WU Trading Library 0.2.0
A backtesting and trading strategy library
Loading...
Searching...
No Matches
portfolios.h File Reference
#include "types.h"
#include "data.h"
#include "positions.h"
#include "stats.h"

Go to the source code of this file.

Data Structures

struct  WU_Portfolio
 Base for a portfolio, which defines the minimal interface for updating the portfolio with signals, calculating the current value of the portfolio, and calculating the profit and loss (PnL), as well as a method to free the portfolio's resources. More...
struct  WU_ExecutionPolicy
struct  WU_BorrowParams
struct  WU_PortfolioParams
 The parameters to define a portfolio. More...
struct  WU_BasicPortfolio
 A basic portfolio implementation that supports multiple assets. More...

Macros

#define wu_portfolio_update(portfolio, signals)
 The update macro allows you to update the portfolio with new signals.
#define wu_portfolio_value(portfolio)
 Calculates the current value of the portfolio.
#define wu_portfolio_pnl(portfolio)
 Calculates the profit and loss (PnL) of the portfolio.
#define wu_portfolio_delete(portfolio)
 Frees the resources associated with the portfolio.
#define WU_PORTFOLIO(portfolio)
 Casting macro for the base portfolio type.

Enumerations

enum  WU_Direction { WU_DIRECTION_LONG , WU_DIRECTION_SHORT , WU_DIRECTION_BOTH }
enum  WU_ExecutionPolicyValue { WU_EXECUTION_POLICY_IMMEDIATE , WU_EXECUTION_POLICY_NEXT_CLOSE , WU_EXECUTION_POLICY_FIXED_SLIPPAGE , WU_EXECUTION_POLICY_RANDOM_SLIPPAGE }
enum  WU_TransactionCostType { WU_TRANSACTION_COST_FIXED , WU_TRANSACTION_COST_PROPORTIONAL }

Functions

WU_PortfolioParams wu_portfolio_params_default (void)
 Returns default portfolio parameters with reasonable values.
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.
double wu_basic_portfolio_asset_value (WU_BasicPortfolio portfolio, int asset_index)
 Get current value of a specific asset's positions.
double wu_basic_portfolio_asset_quantity (WU_BasicPortfolio portfolio, int asset_index)
 Get total quantity held for a specific asset.

Macro Definition Documentation

◆ WU_PORTFOLIO

#define WU_PORTFOLIO ( portfolio)
Value:
((WU_Portfolio)(portfolio))

Casting macro for the base portfolio type.

Definition at line 157 of file portfolios.h.

◆ wu_portfolio_delete

#define wu_portfolio_delete ( portfolio)
Value:
do { \
if ((portfolio)->delete) \
(portfolio)->delete((WU_Portfolio)(portfolio)); \
} while(0)

Frees the resources associated with the portfolio.

Usually this macro is called by the runner when it takes ownership of the portfolio.

Definition at line 52 of file portfolios.h.

◆ wu_portfolio_pnl

#define wu_portfolio_pnl ( portfolio)
Value:
(((WU_Portfolio)(portfolio))->pnl( \
(WU_Portfolio)(portfolio)))

Calculates the profit and loss (PnL) of the portfolio.

This is typically calculated as the difference between the current value of the portfolio and the initial cash invested, minus any transaction costs or fees.

Definition at line 45 of file portfolios.h.

◆ wu_portfolio_update

#define wu_portfolio_update ( portfolio,
signals )
Value:
do { \
((WU_Portfolio)(portfolio))->update((WU_Portfolio)(portfolio), \
(signals)); \
} while(0)
static double update(WU_EMA ema, double value)
Definition ema.c:4

The update macro allows you to update the portfolio with new signals.

Definition at line 28 of file portfolios.h.

◆ wu_portfolio_value

#define wu_portfolio_value ( portfolio)
Value:
(((WU_Portfolio)(portfolio))->value( \
(WU_Portfolio)(portfolio)))

Calculates the current value of the portfolio.

This includes the value of all positions plus any remaining cash.

Definition at line 37 of file portfolios.h.

Enumeration Type Documentation

◆ WU_Direction

Enumerator
WU_DIRECTION_LONG 
WU_DIRECTION_SHORT 
WU_DIRECTION_BOTH 

Definition at line 57 of file portfolios.h.

◆ WU_ExecutionPolicyValue

Enumerator
WU_EXECUTION_POLICY_IMMEDIATE 
WU_EXECUTION_POLICY_NEXT_CLOSE 
WU_EXECUTION_POLICY_FIXED_SLIPPAGE 
WU_EXECUTION_POLICY_RANDOM_SLIPPAGE 

Definition at line 63 of file portfolios.h.

◆ WU_TransactionCostType

Enumerator
WU_TRANSACTION_COST_FIXED 
WU_TRANSACTION_COST_PROPORTIONAL 

Definition at line 70 of file portfolios.h.

Function Documentation

◆ wu_basic_portfolio_asset_quantity()

double wu_basic_portfolio_asset_quantity ( WU_BasicPortfolio portfolio,
int asset_index )

Get total quantity held for a specific asset.

Get total quantity held for a specific asset.

It does bound checking.

Definition at line 827 of file basic.c.

◆ wu_basic_portfolio_asset_value()

double wu_basic_portfolio_asset_value ( WU_BasicPortfolio portfolio,
int asset_index )

Get current value of a specific asset's positions.

Get current value of a specific asset's positions.

This function does bound checking.

Definition at line 812 of file basic.c.

◆ wu_basic_portfolio_new()

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.

The array symbols should be created using the wu_symbol_list macro, which ensures it is null-terminated.

Creates a new basic portfolio instance with the specified parameters and asset symbols.

It takes in the portfolio parameters and an array of asset symbols, and initializes the portfolio's internal state accordingly. Memory is dynamically allocated for the portfolio structure and the position vectors for each asset. The function also sets up the function pointers for the portfolio operations (update, value, pnl, delete) and initializes the cash balance and trading statistics. If any memory allocation fails, it ensures that all previously allocated resources are freed to prevent memory leaks.

Definition at line 764 of file basic.c.

◆ wu_portfolio_params_default()

WU_PortfolioParams wu_portfolio_params_default ( void )

Returns default portfolio parameters with reasonable values.

Returns default portfolio parameters with reasonable values.

The portfolio can process buy and sell signals, manage positions for each asset, and calculate the total value and profit/loss (PnL) of the portfolio. It also includes features such as transaction costs, slippage, stop-loss, and take-profit mechanisms. Returns default portfolio parameters with reasonable values.

Definition at line 19 of file basic.c.