Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > library(blotter)
- Loading required package: xts
- Loading required package: zoo
- Attaching package: ‘zoo’
- The following object(s) are masked from ‘package:base’:
- as.Date, as.Date.numeric
- Loading required package: FinancialInstrument
- Loading required package: quantmod
- Loading required package: Defaults
- Loading required package: TTR
- > search()
- [1] ".GlobalEnv" "package:blotter"
- [3] "package:FinancialInstrument" "package:quantmod"
- [5] "package:TTR" "package:Defaults"
- [7] "package:xts" "package:zoo"
- [9] "package:stats" "package:graphics"
- [11] "package:grDevices" "package:utils"
- [13] "package:datasets" "package:methods"
- [15] "Autoloads" "package:base"
- > ls(all=T)
- [1] ".blotter"
- > args(currency)
- function (primary_id, identifiers = NULL, assign_i = TRUE, ...)
- NULL
- > args(stock)
- function (primary_id, currency = NULL, multiplier = 1, tick_size = 0.01,
- identifiers = NULL, assign_i = TRUE, ...)
- NULL
- > currency("USD")
- [1] "USD"
- > get("USD",envir=FinancialInstrument.instrument)
- Error in get("USD", envir = FinancialInstrument.instrument) :
- object 'FinancialInstrument.instrument' not found
- > get("USD",envir,FinancialInstrument.instrument)
- Error in get("USD", envir, FinancialInstrument.instrument) :
- object 'FinancialInstrument.instrument' not found
- > get("USD",envir,FinancialInstrument:::.instrument)
- primary_id :"USD"
- currency :"USD"
- multiplier :1
- tick_size :0.01
- identifiers: list()
- type :"currency"
- > stock("SPY",currency="USD",multiplier=1)
- [1] "SPY"
- > (get("SPY",enfir=FinancialInstrument:::.instrument)
- + > get("SPY",envir=FinancialInstrument:::.instrument)
- primary_id :"SPY"
- currency :"USD"
- multiplier :1
- tick_size :0.01
- identifiers: list()
- type :"stock"
- > getSymbols('SPY , from='1998-01-01', to='2012-12-31', adjust=T)
- Error: unexpected numeric constant in "getSymbols('SPY , from='1998"
- > getSymbols('SPY , from='1998-01-01', to='2012-12-31', adjust=T)
- Error: unexpected numeric constant in "getSymbols('SPY , from='1998"
- > getSymbols('SPY' , from=1998-01-01', to='2012-12-31', adjust=T)
- Error: unexpected string constant in "getSymbols('SPY' , from=1998-01-01', to='"
- > getSymbols('SPY' , from='1998-01-01', to='2012-12-31', adjust=T)
- [1] "SPY"
- > SPY=to.monthly(SPY, indexAt='endof')
- > SPY$SMA10m <- SMA(Cl(SPY), 10)
- > tail(SPY)
- SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted SMA10m
- 2012-07-31 136.48 139.34 132.60 137.71 2864743300 136.01 133.029
- 2012-08-31 138.70 143.09 135.58 141.16 2413590900 139.42 134.595
- 2012-09-28 141.04 148.11 140.13 143.97 2391233500 142.96 136.493
- 2012-10-31 144.52 147.16 140.39 141.35 2719915500 140.35 138.078
- 2012-11-30 141.65 143.72 134.70 142.15 3032769100 141.15 139.161
- 2012-12-31 142.80 145.58 139.54 142.41 2889875900 142.41 139.700
- > args(initPortf)
- function (name = "default", symbols, initPosQty = 0, initDate = "1950-01-01",
- currency = "USD", ...)
- NULL
- > args(initAcct)
- function (name = "default", portfolios, initDate = "1950-01-01",
- initEq = 0, currency = "USD", ...)
- NULL
- > b.strategy <- "bFaber"
- > initPortf(b.strategy,'SPY' , initDate='1997-12-31')
- [1] "bFaber"
- > initAcct(b.strategy,portfolios=b.strategy, initDate='1997-12-31', initEq=1e6)
- [1] "bFaber"
- > ls()
- [1] "b.strategy" "SPY"
- > ls(.blotter)
- [1] "account.bFaber" "portfolio.bFaber"
- > ls(.instrument)
- Error in as.environment(pos) :
- no item called ".instrument" on the search list
- In addition: Warning message:
- In ls(.instrument) : ‘.instrument’ converted to character string
- > ls(FinancialInstrument:::.instrument)
- [1] "SPY" "USD"
- > theme<-chart_theme()
- > theme$col$up.col<-'lightgreen')
- Error: unexpected ')' in "theme$col$up.col<-'lightgreen')"
- > theme$col$up.col<-'lightgreen'
- > theme$col$up.border<-'lightgreen'
- > theme$col$dn.col<-'pink'
- > theme$col$dn.border<-'pink'
- > chart_Series(SPY,theme=theme,name="SPY")
- > plot(add_SMA(n=10,col=4,lwd=2))
- > for( i in 1:nrow(SPY) ) {
- + CurrentDate <- time(SPY)[i]
- + ClosePrice <- as.numeric(Cl(SPY[i,]))
- + Posn <- getPosQty(b.strategy, Symbol='SPY' , Date=CurrentDate)
- + if( !is.na(as.numeric(SPY[i,'SMA10m'])) ) {
- + if( Posn == 0 ) { # No position, test to go Long
- + if( ClosePrice > as.numeric(SPY[i,'SMA10m']) ) {
- + # enter long position
- + addTxn(b.strategy, Symbol='SPY' , TxnDate=CurrentDate,
- + TxnPrice=ClosePrice, TxnQty = 1000 , TxnFees=0) }
- + } else { # Have a position, so check exit
- + if( ClosePrice < as.numeric(SPY[i,'SMA10m']) ) {
- + # exit position
- + addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
- + TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0) }
- + }
- + }
- + # Calculate P&L and resulting equity with blotter
- + updatePortf(b.strategy, Dates = CurrentDate)
- + updateAcct(b.strategy, Dates = CurrentDate)
- + updateEndEq(b.strategy, Dates = CurrentDate)
- + } # End dates loop
- [1] "1998-10-30 00:00:00 SPY 1000 @ 110"
- [1] "1999-09-30 00:00:00 SPY -1000 @ 128.75"
- [1] "1999-10-29 00:00:00 SPY 1000 @ 137"
- [1] "2000-09-29 00:00:00 SPY -1000 @ 143.63"
- [1] "2002-03-28 00:00:00 SPY 1000 @ 114.52"
- [1] "2002-04-30 00:00:00 SPY -1000 @ 107.86"
- [1] "2003-04-30 00:00:00 SPY 1000 @ 91.91"
- [1] "2004-07-30 00:00:00 SPY -1000 @ 110.84"
- [1] "2004-10-29 00:00:00 SPY 1000 @ 113.2"
- [1] "2005-04-29 00:00:00 SPY -1000 @ 115.75"
- [1] "2005-05-31 00:00:00 SPY 1000 @ 119.48"
- [1] "2007-12-31 00:00:00 SPY -1000 @ 146.21"
- [1] "2009-06-30 00:00:00 SPY 1000 @ 91.95"
- [1] "2010-05-28 00:00:00 SPY -1000 @ 109.37"
- [1] "2010-07-30 00:00:00 SPY 1000 @ 110.27"
- [1] "2010-08-31 00:00:00 SPY -1000 @ 105.31"
- [1] "2010-09-30 00:00:00 SPY 1000 @ 114.13"
- [1] "2011-08-31 00:00:00 SPY -1000 @ 122.22"
- [1] "2012-01-31 00:00:00 SPY 1000 @ 131.32"
- > args(updatePortf)
- function (Portfolio, Symbols = NULL, Dates = NULL, Prices = NULL,
- ...)
- NULL
- > args(updateAcct)
- function (name = "default", Dates = NULL)
- NULL
- > args(updateEndEq)
- function (Account, Dates = NULL)
- NULL
- > source("chart_Posn.R")
- Error in file(filename, "r", encoding = encoding) :
- cannot open the connection
- In addition: Warning message:
- In file(filename, "r", encoding = encoding) :
- cannot open file 'chart_Posn.R': No such file or directory
- > source("chart_Posn")
- Error in file(filename, "r", encoding = encoding) :
- cannot open the connection
- In addition: Warning message:
- In file(filename, "r", encoding = encoding) :
- cannot open file 'chart_Posn': No such file or directory
- > chart_Posn(b.strategy, Symbol = 'SPY' , Dates = '1998::')
- Error: could not find function "chart_Posn"
- >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement