Advertisement
Guest User

quantstrat-I code

a guest
Jan 3rd, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.72 KB | None | 0 0
  1. > library(blotter)
  2. Loading required package: xts
  3. Loading required package: zoo
  4.  
  5. Attaching package: ‘zoo’
  6.  
  7. The following object(s) are masked from ‘package:base’:
  8.  
  9.     as.Date, as.Date.numeric
  10.  
  11. Loading required package: FinancialInstrument
  12. Loading required package: quantmod
  13. Loading required package: Defaults
  14. Loading required package: TTR
  15. > search()
  16.  [1] ".GlobalEnv"                  "package:blotter"            
  17.  [3] "package:FinancialInstrument" "package:quantmod"          
  18.  [5] "package:TTR"                 "package:Defaults"          
  19.  [7] "package:xts"                 "package:zoo"                
  20.  [9] "package:stats"               "package:graphics"          
  21. [11] "package:grDevices"           "package:utils"              
  22. [13] "package:datasets"            "package:methods"            
  23. [15] "Autoloads"                   "package:base"              
  24. > ls(all=T)
  25. [1] ".blotter"
  26. > args(currency)
  27. function (primary_id, identifiers = NULL, assign_i = TRUE, ...)
  28. NULL
  29. > args(stock)
  30. function (primary_id, currency = NULL, multiplier = 1, tick_size = 0.01,
  31.     identifiers = NULL, assign_i = TRUE, ...)
  32. NULL
  33. > currency("USD")
  34. [1] "USD"
  35. > get("USD",envir=FinancialInstrument.instrument)
  36. Error in get("USD", envir = FinancialInstrument.instrument) :
  37.   object 'FinancialInstrument.instrument' not found
  38. > get("USD",envir,FinancialInstrument.instrument)
  39. Error in get("USD", envir, FinancialInstrument.instrument) :
  40.   object 'FinancialInstrument.instrument' not found
  41. > get("USD",envir,FinancialInstrument:::.instrument)
  42. primary_id :"USD"
  43. currency   :"USD"
  44. multiplier :1
  45. tick_size  :0.01
  46. identifiers: list()
  47. type       :"currency"
  48. > stock("SPY",currency="USD",multiplier=1)
  49. [1] "SPY"
  50. > (get("SPY",enfir=FinancialInstrument:::.instrument)
  51.  
  52. + > get("SPY",envir=FinancialInstrument:::.instrument)
  53. primary_id :"SPY"
  54. currency   :"USD"
  55. multiplier :1
  56. tick_size  :0.01
  57. identifiers: list()
  58. type       :"stock"
  59. > getSymbols('SPY , from='1998-01-01', to='2012-12-31', adjust=T)
  60. Error: unexpected numeric constant in "getSymbols('SPY , from='1998"
  61. > getSymbols('SPY , from='1998-01-01', to='2012-12-31', adjust=T)
  62. Error: unexpected numeric constant in "getSymbols('SPY , from='1998"
  63. > getSymbols('SPY' , from=1998-01-01', to='2012-12-31', adjust=T)
  64. Error: unexpected string constant in "getSymbols('SPY' , from=1998-01-01', to='"
  65. > getSymbols('SPY' , from='1998-01-01', to='2012-12-31', adjust=T)
  66. [1] "SPY"
  67. > SPY=to.monthly(SPY, indexAt='endof')
  68. > SPY$SMA10m <- SMA(Cl(SPY), 10)
  69. > tail(SPY)
  70.           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted  SMA10m
  71. 2012-07-31   136.48   139.34  132.60    137.71 2864743300       136.01 133.029
  72. 2012-08-31   138.70   143.09  135.58    141.16 2413590900       139.42 134.595
  73. 2012-09-28   141.04   148.11  140.13    143.97 2391233500       142.96 136.493
  74. 2012-10-31   144.52   147.16  140.39    141.35 2719915500       140.35 138.078
  75. 2012-11-30   141.65   143.72  134.70    142.15 3032769100       141.15 139.161
  76. 2012-12-31   142.80   145.58  139.54    142.41 2889875900       142.41 139.700
  77. > args(initPortf)
  78. function (name = "default", symbols, initPosQty = 0, initDate = "1950-01-01",
  79.    currency = "USD", ...)
  80. NULL
  81. > args(initAcct)
  82. function (name = "default", portfolios, initDate = "1950-01-01",
  83.    initEq = 0, currency = "USD", ...)
  84. NULL
  85. > b.strategy <- "bFaber"
  86. > initPortf(b.strategy,'SPY' , initDate='1997-12-31')
  87. [1] "bFaber"
  88. > initAcct(b.strategy,portfolios=b.strategy, initDate='1997-12-31', initEq=1e6)
  89. [1] "bFaber"
  90. > ls()
  91. [1] "b.strategy" "SPY"      
  92. > ls(.blotter)
  93. [1] "account.bFaber"   "portfolio.bFaber"
  94. > ls(.instrument)
  95. Error in as.environment(pos) :
  96.  no item called ".instrument" on the search list
  97. In addition: Warning message:
  98. In ls(.instrument) : ‘.instrument’ converted to character string
  99. > ls(FinancialInstrument:::.instrument)
  100. [1] "SPY" "USD"
  101. > theme<-chart_theme()
  102. > theme$col$up.col<-'lightgreen')
  103. Error: unexpected ')' in "theme$col$up.col<-'lightgreen')"
  104. > theme$col$up.col<-'lightgreen'
  105. > theme$col$up.border<-'lightgreen'
  106. > theme$col$dn.col<-'pink'
  107. > theme$col$dn.border<-'pink'
  108. > chart_Series(SPY,theme=theme,name="SPY")
  109. > plot(add_SMA(n=10,col=4,lwd=2))
  110. > for( i in 1:nrow(SPY) ) {
  111. + CurrentDate <- time(SPY)[i]
  112. + ClosePrice <- as.numeric(Cl(SPY[i,]))
  113. + Posn <- getPosQty(b.strategy, Symbol='SPY' , Date=CurrentDate)
  114. + if( !is.na(as.numeric(SPY[i,'SMA10m'])) ) {
  115. + if( Posn == 0 ) { # No position, test to go Long
  116. + if( ClosePrice > as.numeric(SPY[i,'SMA10m']) ) {
  117. + # enter long position
  118. + addTxn(b.strategy, Symbol='SPY' , TxnDate=CurrentDate,
  119. + TxnPrice=ClosePrice, TxnQty = 1000 , TxnFees=0) }
  120. + } else { # Have a position, so check exit
  121. + if( ClosePrice < as.numeric(SPY[i,'SMA10m']) ) {
  122. + # exit position
  123. + addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
  124. + TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0) }
  125. + }
  126. + }
  127. + # Calculate P&L and resulting equity with blotter
  128. + updatePortf(b.strategy, Dates = CurrentDate)
  129. + updateAcct(b.strategy, Dates = CurrentDate)
  130. + updateEndEq(b.strategy, Dates = CurrentDate)
  131. + } # End dates loop
  132. [1] "1998-10-30 00:00:00 SPY 1000 @ 110"
  133. [1] "1999-09-30 00:00:00 SPY -1000 @ 128.75"
  134. [1] "1999-10-29 00:00:00 SPY 1000 @ 137"
  135. [1] "2000-09-29 00:00:00 SPY -1000 @ 143.63"
  136. [1] "2002-03-28 00:00:00 SPY 1000 @ 114.52"
  137. [1] "2002-04-30 00:00:00 SPY -1000 @ 107.86"
  138. [1] "2003-04-30 00:00:00 SPY 1000 @ 91.91"
  139. [1] "2004-07-30 00:00:00 SPY -1000 @ 110.84"
  140. [1] "2004-10-29 00:00:00 SPY 1000 @ 113.2"
  141. [1] "2005-04-29 00:00:00 SPY -1000 @ 115.75"
  142. [1] "2005-05-31 00:00:00 SPY 1000 @ 119.48"
  143. [1] "2007-12-31 00:00:00 SPY -1000 @ 146.21"
  144. [1] "2009-06-30 00:00:00 SPY 1000 @ 91.95"
  145. [1] "2010-05-28 00:00:00 SPY -1000 @ 109.37"
  146. [1] "2010-07-30 00:00:00 SPY 1000 @ 110.27"
  147. [1] "2010-08-31 00:00:00 SPY -1000 @ 105.31"
  148. [1] "2010-09-30 00:00:00 SPY 1000 @ 114.13"
  149. [1] "2011-08-31 00:00:00 SPY -1000 @ 122.22"
  150. [1] "2012-01-31 00:00:00 SPY 1000 @ 131.32"
  151. > args(updatePortf)
  152. function (Portfolio, Symbols = NULL, Dates = NULL, Prices = NULL,
  153.    ...)
  154. NULL
  155. > args(updateAcct)
  156. function (name = "default", Dates = NULL)
  157. NULL
  158. > args(updateEndEq)
  159. function (Account, Dates = NULL)
  160. NULL
  161. > source("chart_Posn.R")
  162. Error in file(filename, "r", encoding = encoding) :
  163.  cannot open the connection
  164. In addition: Warning message:
  165. In file(filename, "r", encoding = encoding) :
  166.  cannot open file 'chart_Posn.R': No such file or directory
  167. > source("chart_Posn")
  168. Error in file(filename, "r", encoding = encoding) :
  169.  cannot open the connection
  170. In addition: Warning message:
  171. In file(filename, "r", encoding = encoding) :
  172.  cannot open file 'chart_Posn': No such file or directory
  173. > chart_Posn(b.strategy, Symbol = 'SPY' , Dates = '1998::')
  174. Error: could not find function "chart_Posn"
  175. >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement