R: backtesting with path dependencies
R: backtesting with path dependencies
Loading saved threads...
Darrell Berry · External communityPost link
External question — Quantitative Finance Stack Exchange
Author: Darrell Berry
Original post: https://quant.stackexchange.com/questions/42842
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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.
Quote
Report
Enrico Schumann · External communityPost link
External answer — Quantitative Finance Stack Exchange
Author: Enrico Schumann
Original post: https://quant.stackexchange.com/a/42843
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Quantitative Finance Stack Exchange Author: Enrico Schumann Source score (net votes, not local likes): 1 Original post: https://quant.stackexchange.com/a/42843 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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
Checking account access…