saving and loading blotter portfolio

340 views Asked by At

I am using blotter to hold and do the accounting of some transactions, but I would need to save and load them on a daily basis.

I haven´t been able to save my transactions, I believe that that happens because they are in a different enviroment created by blotter (.blotter) - from what I´ve been able to pick up by googling my questions.

I have set up an example of a transaction:

require(quantstrat)
currency("USD")
stock(primary_id = "SB1", currency = "USD", multiplier=1120, tick_size = 0.01)
initPortf(name="testport", symbols="SB1", initDate = "2017-11-01")
initAcct(name="testacct", portfolio="testport", initDate = "2017-11-01", initEq = 100000)
ls_instruments()
addTxn(Portfolio="testport", Symbol="SB1", TxnDate="2017-11-22", TxnPrice=15.00, TxnQty = 2 , verbose=TRUE)
getPos(Portfolio="testport", Symbol="SB1", "2017-11-22", Columns=c("Pos.Qty"))

Then I try to save it (which didn´t work) and was thinking about loading it like in the code below:

save("testport", file="C:/Users/augus/Dropbox/Trading/R/Trading/Dados/test.RData", envir=.blotter)
load(file="C:/Users/augus/Dropbox/Trading/R/Trading/Dados/test.RData", verbose=TRUE)

I am pretty knew to R and stackoverflow, so please let me know if I am missing any information in my question, and thanks a lot for the help.

All the best,

Augusto

1

There are 1 answers

0
FXQuantTrader On BEST ANSWER

The object you wanted was actually "portfolio.testport", not "testport" (this is designed this way in blotter). You can check by looking at what is in the .blotter environment:

ls(.blotter)
#[1] "account.testacct"   "portfolio.testport"

You could do this instead:

save("portfolio.testport", file="test.RData", envir=.blotter)
save("testport", file="C:/Users/augus/Dropbox/Trading/R/Trading/Dados/test.RData", envir=.blotter)
load(file="test.RData", verbose=TRUE)

You may not want to store all of the stuff in this portfolio though, so it helps to understand what makes up the portfolio.

p <- getPortfolio("testport")
class(p)
#[1] "blotter_portfolio" "portfolio"   

It is basically a list, containing a summary (xts) and a symbols object (another list):

ls(p)
#[1] "summary" "symbols"

The contents of the symbols list is:

ls(p$symbols)
#[1] "SB1"

And each symbol object is also a list, containing 3 xts objects:

ls(p$symbols$SB1)
#[1] "posPL"     "posPL.USD" "txn"  

txn is one of the objects (an xts object itself):

head(p$symbols$SB1$txn)
# Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees Net.Txn.Realized.PL Con.Mult
# 2017-11-01       0         0         0            0       0            0                     0        0                   0        0
# 2017-11-22       2        15     33600           15       2           15                     0        0                   0     1120

You may wish to save only sub parts of the above portfolio object.

Here is another approach you might find useful for saving:

p <- getPortfolio("testport")
saveRDS(p, "test2.rds")
p <- readRDS("test2.rds")