Advertisement
entreprnr

duplicate column in mktdata, but don't see it

Dec 6th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.62 KB | None | 0 0
  1. #rm(list=ls())
  2.  
  3. require(quantstrat)
  4. suppressWarnings(rm("order_book.macd",pos=.strategy))
  5. suppressWarnings(rm("account.macd","portfolio.macd",pos=.blotter))
  6. suppressWarnings(rm("accnt","p","ticker","stratMACD","initDate","initEq",'start_t','end_t'))
  7.  
  8. ticker='QQQ'
  9. getSymbols("QQQ", src='yahoo', index.class=c("POSIXt","POSIXct"), from='2012-01-01')
  10.  
  11. #Loads QQQ DATA
  12. initDate = '1990-01-02'
  13.  
  14. currency(c('USD'))
  15. stock(ticker,'USD')
  16.  
  17. p='macd'
  18. accnt='macd'
  19. qty = 10000
  20.  
  21. initPortf(p,symbols=ticker, initDate=initDate)
  22. initAcct(accnt,portfolios=p, initDate=initDate)
  23. initOrders(portfolio=p,initDate=initDate)
  24.  
  25. strat<-p
  26.  
  27. # define the strategy
  28. strategy(strat, store=TRUE)
  29.  
  30. # *********************************************************
  31. # ATR Functions
  32.  
  33. #profit
  34. test1 <- function(HLC, n=2, maType, mmstp=2){test1 <- 2 * ATR(HLC=HLC, n=n, maType=maType)}
  35.  
  36. #stop loss
  37. # must use the [,1] in order to apply the multiplication only to the "tr" column. without this,
  38. #  the entire frame gets multiplied by -1.  Also, this prevents creating another trueHigh and
  39. #  trueLow column
  40. test2 <- function(HLC, n=2, maType, mmstp=2){test2 <- -1* ATR(HLC=HLC, n=n, maType=maType)[,1]}
  41.  
  42. # *********************************************************
  43. # INDICATORS
  44.  
  45. # ATR used for stop (see notes above)
  46. add.indicator(strat, name = "test1", arguments = list(HLC=quote(HLC(mktdata)[,1:3]), n=2, maType="SMA", mmstp=2))
  47.  
  48. # must use the [,c(1,3,5)] to get the real HLC data. When the first ATR indicator is run, it populates trueHigh and trueLow
  49. #  data fields. the code doesn't know which column to look at for "low" and "high", since there are multiple instances of it
  50. #  now due to the first run of the function on HLC data.  Thus, this bracketing tells it what columns to look for.  if you
  51. #  had another function to the same call, you may need to run a "head(HLC(mktdata))" to understand what the columns look like
  52. #  before the 3rd indicator call.  this solution is from: https://stat.ethz.ch/pipermail/r-sig-finance/2012q4/011103.html
  53. add.indicator(strat, name = "test2", arguments = list(HLC=quote(HLC(mktdata)[,c(1,3,5)]), n=2, maType="SMA", mmstp=2))
  54.  
  55. #Highest close of the last n days
  56. add.indicator(strat,name="runMax", arguments=list(x=quote(Cl(mktdata)),n=50,cumulative=FALSE), label="RmxrCompare")
  57.  
  58.  
  59. # *********************************************************
  60. # SIGNALS
  61.  
  62. # BUY if highest close of last n days is greater than today's close
  63. add.signal(strat,name='sigComparison', arguments=list(columns=c("Close","RmxrCompare"), relationship="gte"),label='long')   # Signal to BUY
  64.  
  65.  
  66. # *********************************************************
  67. # RULES
  68.  
  69. #Enters LONG position
  70. add.rule(strat, name = 'ruleSignal', arguments=list(sigcol='long', sigval=TRUE, orderside='long', ordertype='market',
  71.         orderqty=qty, orderset='ocolong'),
  72.         type='enter', label='EnterLONG', storefun=FALSE)
  73.  
  74. # Stop Loss for LONG position. Stops if beyond some multiple of ATR (see notes above)
  75. add.rule(strat,name='ruleSignal', arguments = list(sigcol='long',sigval=TRUE, orderqty='all', ordertype='stoplimit',
  76.         orderside='long', threshold='test1.ind.tr', tmult=FALSE, orderset='ocolong'),
  77.         type='risk',label='stoplimitexit-LONG',storefun=FALSE)
  78.  
  79. #end rules
  80. ####
  81. #### ***************************************************************************************************************************************************************
  82.  
  83. applyStrategy(strat, portfolios=p,prefer='Open', verbose=FALSE)
  84. updatePortf(p, Symbols=ticker, ,Dates=paste('::',as.Date(Sys.time()),sep=''))
  85. print("Complete")
  86.  
  87. print("********")
  88. print("********")
  89. print("********")
  90. print(head(mktdata))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement