I'm learning a blotter
and have implemented a tidal strategy. (by the way, please check if I did everything correctly). The strategy can have only one position
1 buy
-1 sell
library(blotter)
library(xts)
set.seed(1)
times <- seq(as.POSIXct("2016-01-01 00:00:00"), length = 30, by = "sec")
price <- round(cumsum(rnorm(30))+999,2)
signal <- rep(0,30)
signal[5] <- 1 # open long
signal[15] <- -1 # close long open short
dat <- xts(cbind(price,signal),order.by = times)
initDate='1997-12-31'
initEq=1000
currency("USD")
stock("dat",currency="USD",multiplier=1)
ltportfolio='my'
ltaccount='my'
initPortf(ltportfolio,'dat', initDate=initDate)
initAcct(ltaccount,portfolios='my', initDate=initDate, initEq=initEq)
verbose=TRUE
for(i in 1:nrow(dat)){
CurrentDate <- time(dat)[i]
# getEndEq(ltaccount, CurrentDate)
# get current open position
open_position <- getPosQty(ltportfolio, Symbol='dat', Date=CurrentDate)
#####################
##### open long #####
#####################
if(dat$signal[i]==1){
# if there is an open short position, close it
if(open_position < 0){
addTxn(ltaccount,
Symbol='dat',
TxnDate=CurrentDate,
TxnPrice=dat$price[i],
TxnQty = -open_position ,
TxnFees=0,
verbose=verbose)
}
# open long position
addTxn(ltaccount,
Symbol='dat',
TxnDate=CurrentDate,
TxnPrice=dat$price[i],
TxnQty = 1 ,
TxnFees=0,
verbose=verbose)
}
######################
##### open short #####
######################
if(dat$signal[i]==-1){
# if there is an open long position, close it
if(open_position > 0){
addTxn(ltaccount,
Symbol='dat',
TxnDate=CurrentDate,
TxnPrice=dat$price[i],
TxnQty = -open_position ,
TxnFees=0,
verbose=verbose)
}
# open short position
addTxn(ltaccount,
Symbol='dat',
TxnDate=CurrentDate,
TxnPrice=dat$price[i],
TxnQty = -1 ,
TxnFees=0,
verbose=verbose)
}
}
updatePortf(ltportfolio, Dates = CurrentDate)
updateAcct(ltaccount, Dates = CurrentDate)
updateEndEq(ltaccount, Dates = CurrentDate)
the problem is with this chart
chart.Posn(ltportfolio, Symbol = 'dat')
It does not show either accumulated profit or drawdown, but it should do so by default, at least in all examples it does so.
Great job on providing a reproducible example! The issue is that you're providing
Dates = CurrentDate
in the call toupdatePorf()
. That means you're only valuing your trades on one date (CurrentDate
). You probably mean to value them on every date. You can do that by omitting theDates
argument.Now the chart has cumulative PnL and drawdown.