Using PMwR Portfolio State in Path-Dependent Backtests
Summary
This document explains how to use the PMwR backtesting framework when trade sizing depends on portfolio state at each decision point. The example defines a signal function that reads current wealth, cash, and holdings, then uses that information while selecting the next position. These values are accessed through PMwR's state functions within the signal calculation, so the strategy can base decisions on the evolving backtest rather than maintaining a separate external ledger.
A simple synthetic price series and random position choices demonstrate the workflow, with printed portfolio values and resulting positions showing that cash, wealth, and holdings update over time. The example establishes how to access path-dependent state; it does not present a realistic sizing rule or discuss transaction costs, constraints, or performance. Researchers adapting it should supply their own trading logic and account for the assumptions in their backtest.
Key ideas
- A PMwR signal function can query portfolio state during a backtest.
- Wealth, cash, and current positions can inform decisions at each timestamp.
- The example demonstrates state access with a simple synthetic series and random position choices.
- The example does not specify a practical sizing rule or cover trading costs and constraints.
Tags
Full text
# R: backtesting with path dependencies
# R: backtesting with path dependencies
I have a historical PMwR journal of trades (one for each side of position open/close) in R.
I wish to backtest trade sizing algorithms, one of the inputs to which calculation will be, on-the-day total value of the portfolio prior to execution of each open.
I would prefer to do this within PMwR.
From the docs, I can't see how to access total portfolio value (or related 'path-dependent' numbers for example 'cash position', 'on the day' inside a backtest. Is this available within the framework, or do I need to maintain P&L etc externally in a Global via some explicit loop?
Does anyone have an example of backtesting trade sizing using PMwR, in a situation where pre-trade total portfolio value, current holdings in each instrument, etc are inputs for the sizing algorithm?
I am also open to non-PMwR solutions, but I appreciate its clarity and elegance and would prefer to stay within it if possible.
## Answer by Enrico Schumann (score 1, accepted)
https://quant.stackexchange.com/a/42843
It is described in the PMwR manual.
An example: I make up a trivial price series.
```
library("PMwR")
prices <- 1:5
```
The `signal` function instructs the algorithm to buy a random quantity at each timestamp. And `signal` also prints the current values of total wealth, cash and the position.
```
signal <- function() {
cat("Time", Time(), "\n")
cat("Total portfolio value", round(Wealth(), 2),
" cash", round(Cash(), 2), "\n")
cat("Position ", round(Portfolio(), 3), "\n\n")
runif(1) ## a random position
}
```
Calling `btest`:
```
bt <- btest(prices, signal, initial.cash = 100)
## Time 1
## Total portfolio value 100 cash 100
## Position 0
##
## Time 2
## Total portfolio value 100 cash 99.65
## Position 0.173
##
## Time 3
## Total portfolio value 100.17 cash 98.4
## Position 0.59
##
## Time 4
## Total portfolio value 100.76 cash 99.34
## Position 0.355
position(bt)
## [,1]
## [1,] 0.0000000
## [2,] 0.1725948
## [3,] 0.5902009
## [4,] 0.3549475
## [5,] 0.7121020
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.