[R-SIG-Finance] Guy's example osFixedDollar not working in latest quantstrat

Jim Green student.northwestern at gmail.com
Fri Apr 13 03:15:27 CEST 2012


Good Evening!

I was studying the Guy Yollin's presentation on quantstrat
http://www.r-programming.org/files/quantstrat-II.pdf  and can't
reproduce the result in latest quantstrat. Actually osFixedDollar was
not even called per my basic logging. I've attached the pdf chart and
the runnable code and the results on my machine. Hope someone could
help..

Thanks!
Jim.


### code ###

#!/usr/bin/env Rscript
library(quantstrat)
library(logging)
basicConfig()

# define stock list
stock.str=c("SPY")
# inz currency and stocks
dummy <- currency('USD')
for(symbol in stock.str){
         stock(symbol, currency="USD",multiplier=1)
}
# download stocks
start.date <- as.Date("2001-01-01")
initDate <- start.date-1
end.date <-as.Date("2011-08-06")
getSymbols(stock.str,from=start.date,to=end.date,adjust=T)

# inz portfolio, account, orders, strategy
strat.name <- "MAX"
initEq=1000000
trade.percent <- 0.01
tradeSize <- initEq * trade.percent
dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate)
dummy <- initAcct(name=strat.name,portfolios=strat.name,
initDate=initDate, initEq=initEq)
initOrders(portfolio=strat.name,initDate=initDate)
strat <- strategy(strat.name)

# indicators:
strat <- add.indicator(strategy = strat, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
strat <- add.indicator(strategy = strat, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=200),label= "ma200")
# signals:
strat <- add.signal(strategy = strat,name="sigCrossover", arguments =
list(columns=c("ma50","ma200"), relationship="gte"),
label="ma50.gt.ma200")
strat <- add.signal(strategy = strat,name="sigCrossover", arguments =
list(column=c("ma50","ma200"),relationship="lt"),
label="ma50.lt.ma200")

osFixedDollar <- function(timestamp,orderqty, portfolio, symbol, ruletype, ...)
{
   logwarn("started osFixedDollar")
   ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
   orderqty <- sign(orderqty)*round(tradeSize/ClosePrice)
   return(orderqty)
}

# rules:
strat <- add.rule(
   strategy = strat,
   name='ruleSignal',
   arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100,
ordertype='market', orderside='long', osFUN='osFixedDollar'),
   type='enter'
)

strat <- add.rule(
   strategy = strat,
   name='ruleSignal',
   arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE,
orderqty='all', ordertype='market', orderside='long'),
   type='exit'
)

out <- try(applyStrategy(strategy=strat , portfolios=strat.name, verbose=TRUE))
getTxns(Portfolio=strat.name, Symbol=stock.str)
getPortfolio(strat.name)$summary
dummy <- updatePortf(Portfolio=strat.name,
   Dates=paste('::',as.Date(Sys.time()),sep='')
)

pdf("quantstrat2_position_sizing_fixed.pdf")
chart.Posn(Portfolio=strat.name,Symbol=stock.str)
add_SMA(n=50 , on=1,col='blue',lwd=2)
add_SMA(n=200, on=1,col='red',lwd=2)

### output ###
[1] "SPY"
[1] "2002-04-24 SPY 100 @ 91.9933301492183"
[1] "2002-04-29 SPY -100 @ 89.8492574695683"
[1] "2002-04-30 SPY 100 @ 90.6900702851173"
[1] "2002-05-14 SPY -100 @ 92.674388529813"
[1] "2003-05-12 SPY 100 @ 81.1354076349475"
[1] "2004-08-25 SPY -100 @ 96.8566013672453"
[1] "2004-10-27 SPY 100 @ 98.8180303441752"
[1] "2006-07-25 SPY -100 @ 114.662711057191"
[1] "2006-08-29 SPY 100 @ 118.211406993905"
[1] "2007-12-28 SPY -100 @ 137.165700378348"
[1] "2009-06-18 SPY 100 @ 88.5716570825431"
[1] "2010-07-06 SPY -100 @ 100.84667390332"
[1] "2010-10-15 SPY 100 @ 116.002709490181"
                   Txn.Qty Txn.Price Txn.Fees  Txn.Value Txn.Avg.Cost
2000-12-30 19:00:00       0   0.00000        0      0.000      0.00000
2002-04-24 00:00:00     100  91.99333        0   9199.333     91.99333
2002-04-29 00:00:00    -100  89.84926        0  -8984.926     89.84926
2002-04-30 00:00:00     100  90.69007        0   9069.007     90.69007
2002-05-14 00:00:00    -100  92.67439        0  -9267.439     92.67439
2003-05-12 00:00:00     100  81.13541        0   8113.541     81.13541
2004-08-25 00:00:00    -100  96.85660        0  -9685.660     96.85660
2004-10-27 00:00:00     100  98.81803        0   9881.803     98.81803
2006-07-25 00:00:00    -100 114.66271        0 -11466.271    114.66271
2006-08-29 00:00:00     100 118.21141        0  11821.141    118.21141
2007-12-28 00:00:00    -100 137.16570        0 -13716.570    137.16570
2009-06-18 00:00:00     100  88.57166        0   8857.166     88.57166
2010-07-06 00:00:00    -100 100.84667        0 -10084.667    100.84667
2010-10-15 00:00:00     100 116.00271        0  11600.271    116.00271
                   Net.Txn.Realized.PL
2000-12-30 19:00:00              0.0000
2002-04-24 00:00:00              0.0000
2002-04-29 00:00:00           -214.4073
2002-04-30 00:00:00              0.0000
2002-05-14 00:00:00            198.4318
2003-05-12 00:00:00              0.0000
2004-08-25 00:00:00           1572.1194
2004-10-27 00:00:00              0.0000
2006-07-25 00:00:00           1584.4681
2006-08-29 00:00:00              0.0000
2007-12-28 00:00:00           1895.4293
2009-06-18 00:00:00              0.0000
2010-07-06 00:00:00           1227.5017
2010-10-15 00:00:00              0.0000
                   Long.Value Short.Value Net.Value Gross.Value Realized.PL
2000-12-30 19:00:00          0           0         0           0           0
                   Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL
2000-12-30 19:00:00             0                0        0              0


### sessioninfo()###
> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base

other attached packages:
 [1] PerformanceAnalytics_1.0.4.4 quantstrat_0.6.4
 [3] blotter_0.8.4                FinancialInstrument_0.13.6
 [5] RPostgreSQL_0.3-2            DBI_0.2-5
 [7] quantmod_0.3-17              TTR_0.21-1
 [9] Defaults_1.1-1               xts_0.8-6
[11] zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.14.2    lattice_0.20-0
-------------- next part --------------
A non-text attachment was scrubbed...
Name: quantstrat2_position_sizing_fixed.pdf
Type: application/pdf
Size: 351907 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120412/91c9678b/attachment.pdf>


More information about the R-SIG-Finance mailing list