# NOT RUN {
## For more examples, please see the Manual
## http://enricoschumann.net/R/packages/PMwR/manual/PMwR.html
## 1 - a simple rule
timestamp <- structure(c(16679L, 16680L, 16681L, 16682L,
16685L, 16686L, 16687L, 16688L,
16689L, 16692L, 16693L),
class = "Date")
prices <- c(3182, 3205, 3272, 3185, 3201,
3236, 3272, 3224, 3194, 3188, 3213)
data.frame(timestamp, prices)
signal <- function() ## buy when last price is
if (Close() < 3200) ## below 3200, else sell
1 else 0 ## (more precisely: build position of 1
## when price < 3200, else reduce
## position to 0)
solution <- btest(prices = prices, signal = signal)
journal(solution)
## with Date timestamps
solution <- btest(prices = prices, signal = signal,
timestamp = timestamp)
journal(solution)
## 2 - a simple MA model
# }
# NOT RUN {
library("PMwR")
library("NMOF")
dax <- DAX[[1]]
n <- 5
ma <- MA(dax, n, pad = NA)
ma_strat <- function(ma) {
if (Close() > ma[Time()])
1
else
0
}
plot(as.Date(row.names(DAX)), dax, type = "l", xlab = "", ylab = "DAX")
lines(as.Date(row.names(DAX)), ma, type = "l")
res <- btest(prices = dax,
signal = ma_strat,
b = n, ma = ma)
par(mfrow = c(3, 1))
plot(as.Date(row.names(DAX)), dax, type = "l",
xlab = "", ylab = "DAX")
plot(as.Date(row.names(DAX)), res$wealth, type = "l",
xlab = "", ylab = "Equity")
plot(as.Date(row.names(DAX)), position(res), type = "s",
xlab = "", ylab = "Position")
# }
Run the code above in your browser using DataLab