0
$\begingroup$

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.

$\endgroup$

1 Answer 1

1
$\begingroup$

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
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.