[R-SIG-Finance] using quantstrat with custom data

Kevin Dhingra kevin.dhingra at appliedacademics.com
Mon Oct 24 21:19:22 CEST 2016


Hi Jon,

As mentioned by Ilya, your arguments to SMA are incorrect. Also your
program is missing the signal function. You need to convert your indicator
into a signal using add.signal function before passing it to a rule. Try
running the modified code -

library(quantstrat)
library(quantmod)
library(tidyverse)


ts <- structure(list(times = structure(c(1477312980, 1477313580,
                                         1477314180,
                                         1477314780, 1477315380,
1477315980, 1477316580, 1477317180, 1477317780,
                                         1477318380, 1477318980,
1477319580, 1477320180, 1477320780, 1477321380,
                                         1477321980, 1477322580,
1477323180, 1477323780, 1477324380, 1477324980,
                                         1477325580, 1477326180,
1477326780, 1477327380, 1477327980, 1477328580,
                                         1477329180, 1477329780,
1477330380, 1477330980, 1477331580, 1477332180,
                                         1477332780, 1477333380,
1477333980, 1477334580), class = c("POSIXct",

                        "POSIXt"), tzone = "UTC"), NG1.Open = c(2.954,
2.93, 2.936, 2.947,

                                                                2.951,
2.941, 2.929, 2.924, 2.893, 2.871, 2.891, 2.892, 2.898,

                                                                2.89, 2.88,
2.872, 2.885, 2.884, 2.879, 2.876, 2.876, 2.859,

                                                                2.864,
2.878, 2.866, 2.886, 2.892, 2.889, 2.878, 2.861, 2.859,

                                                                2.851,
2.859, 2.84, 2.836, 2.844, 2.839), NG1.High = c(2.959,


                                           2.937, 2.954, 2.96, 2.957,
2.941, 2.932, 2.926, 2.902, 2.893,


                                           2.898, 2.9, 2.905, 2.893, 2.883,
2.885, 2.891, 2.889, 2.884,


                                           2.877, 2.877, 2.867, 2.878,
2.881, 2.887, 2.897, 2.893, 2.891,


                                           2.884, 2.865, 2.862, 2.861,
2.859, 2.843, 2.845, 2.845, 2.842

                                                                ), NG1.Low
= c(2.929, 2.916, 2.931, 2.946, 2.935, 2.924, 2.924,


   2.886, 2.871, 2.852, 2.878, 2.889, 2.886, 2.87, 2.87, 2.866,


   2.882, 2.875, 2.875, 2.871, 2.857, 2.848, 2.864, 2.863, 2.864,


   2.884, 2.886, 2.878, 2.859, 2.851, 2.849, 2.849, 2.835, 2.829,


   2.827, 2.836, 2.836), NG1.Close = c(2.93, 2.936, 2.947, 2.951,


                                       2.94, 2.93, 2.924, 2.892, 2.871,
2.891, 2.892, 2.898, 2.89, 2.879,


                                       2.872, 2.884, 2.884, 2.878, 2.876,
2.876, 2.859, 2.865, 2.878,


                                       2.865, 2.886, 2.892, 2.888, 2.879,
2.862, 2.858, 2.851, 2.858,


                                       2.839, 2.836, 2.844, 2.839, 2.842),
NG1.Volume = c(2347, 4491,



              2793, 1124, 1624, 1864, 1646, 6497, 3625, 5570, 2231, 1506,
2084,



              3443, 1372, 1464, 931, 906, 557, 1266, 2047, 3021, 1206, 938,



              1375, 1928, 582, 1203, 1810, 1309, 803, 1368, 3141, 2227,
9590,



              1067, 128)), row.names = c(NA, -37L), class = "data.frame",
.Names =
                  c("times",
                    "NG1.Open", "NG1.High", "NG1.Low", "NG1.Close",
"NG1.Volume"))

NG1 <- xts(ts[,-1],ts[,1])


initDate <- index(NG1)[1]
initEq <- 5000
n <- 14

currency("USD")
Sys.setenv(TZ="UTC")
symbols <- "NG1"
stock(symbols, currency="USD", multiplier=1)

strategy.st <- 'teststrat'
portfolio.st <- 'teststrat'
account.st <- 'teststrat'
initPortf(portfolio.st, symbols = symbols, initDate=initDate,currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
         initEq=initEq, currency='USD')
initOrders(portfolio=portfolio.st, initDate=initDate)



strategy(name = strategy.st,
         store = TRUE)

add.indicator(
  strategy = strategy.st,
  name = 'RSI',
  arguments = list(price=quote(Cl(mktdata)),n=n),
  label = "rsi")



add.signal(strategy.st, name="sigThreshold",
           arguments=list(column="rsi", threshold= 40,
                          relationship="lt", cross=FALSE),
           label="RSI_long")

add.rule(
  strategy=strategy.st,
  name='ruleSignal',
  arguments=list(
    sigcol='RSI_long',
    sigval = TRUE,
    orderside = 'long',
    ordertype='market',
    orderqty=1,
    replace=FALSE
  ),
  type='enter',
  label='Enterlong',
  path.dep = TRUE
)




t1<-Sys.time()
out<-applyStrategy(strategy=strategy.st,portfolios = portfolio.st)
t2<-Sys.time()
print(t2-t1)

On Mon, Oct 24, 2016 at 3:07 PM, Ilya Kipnis <ilya.kipnis at gmail.com> wrote:

> Hi Jon,
>
> First off, check the parameters on your add.indicator call for your SMA
> function.
>
> Secondly, your data is only 37 observations long and you have a setting of
> 200.
>
> Please fix these two issues.
>
> -Ilya
>
> On Mon, Oct 24, 2016 at 2:55 PM, Jon Golenbock <jongolenbock at gmail.com>
> wrote:
>
> > Hi folks,
> >
> > new to this mailing list and not very proficient with R in general yet,
> so
> > apologies in advance.
> >
> > all of the demos I could see were using stock data pulled from
> > yahoo/google, etc -- mine is coming from bloomberg. I did my best to
> > replicate the type of object that getsymbols() seems to create.
> >
> > everything seems to be working alright until I actually get to
> > applystrategy(), where I am getting an error that I imagine, while not
> > particularly helpful to me, will be completely obvious to somebody with a
> > bit more experience. the error i'm receiving is
> >
> > Error in n < 1 :
> >   comparison (3) is possible only for atomic and list types
> >
> > code below. Any help is greatly appreciated and apologies for all the
> rules
> > I am surely breaking, I'll plead first-timer on this one.
> >
> >
> >
> > library(quantstrat)
> > library(quantmod)
> > library(tidyverse)
> >
> >
> > ts <- structure(list(times = structure(c(1477312980, 1477313580,
> > 1477314180,
> > 1477314780, 1477315380, 1477315980, 1477316580, 1477317180, 1477317780,
> > 1477318380, 1477318980, 1477319580, 1477320180, 1477320780, 1477321380,
> > 1477321980, 1477322580, 1477323180, 1477323780, 1477324380, 1477324980,
> > 1477325580, 1477326180, 1477326780, 1477327380, 1477327980, 1477328580,
> > 1477329180, 1477329780, 1477330380, 1477330980, 1477331580, 1477332180,
> > 1477332780, 1477333380, 1477333980, 1477334580), class = c("POSIXct",
> > "POSIXt"), tzone = "UTC"), NG1.Open = c(2.954, 2.93, 2.936, 2.947,
> > 2.951, 2.941, 2.929, 2.924, 2.893, 2.871, 2.891, 2.892, 2.898,
> > 2.89, 2.88, 2.872, 2.885, 2.884, 2.879, 2.876, 2.876, 2.859,
> > 2.864, 2.878, 2.866, 2.886, 2.892, 2.889, 2.878, 2.861, 2.859,
> > 2.851, 2.859, 2.84, 2.836, 2.844, 2.839), NG1.High = c(2.959,
> > 2.937, 2.954, 2.96, 2.957, 2.941, 2.932, 2.926, 2.902, 2.893,
> > 2.898, 2.9, 2.905, 2.893, 2.883, 2.885, 2.891, 2.889, 2.884,
> > 2.877, 2.877, 2.867, 2.878, 2.881, 2.887, 2.897, 2.893, 2.891,
> > 2.884, 2.865, 2.862, 2.861, 2.859, 2.843, 2.845, 2.845, 2.842
> > ), NG1.Low = c(2.929, 2.916, 2.931, 2.946, 2.935, 2.924, 2.924,
> > 2.886, 2.871, 2.852, 2.878, 2.889, 2.886, 2.87, 2.87, 2.866,
> > 2.882, 2.875, 2.875, 2.871, 2.857, 2.848, 2.864, 2.863, 2.864,
> > 2.884, 2.886, 2.878, 2.859, 2.851, 2.849, 2.849, 2.835, 2.829,
> > 2.827, 2.836, 2.836), NG1.Close = c(2.93, 2.936, 2.947, 2.951,
> > 2.94, 2.93, 2.924, 2.892, 2.871, 2.891, 2.892, 2.898, 2.89, 2.879,
> > 2.872, 2.884, 2.884, 2.878, 2.876, 2.876, 2.859, 2.865, 2.878,
> > 2.865, 2.886, 2.892, 2.888, 2.879, 2.862, 2.858, 2.851, 2.858,
> > 2.839, 2.836, 2.844, 2.839, 2.842), NG1.Volume = c(2347, 4491,
> > 2793, 1124, 1624, 1864, 1646, 6497, 3625, 5570, 2231, 1506, 2084,
> > 3443, 1372, 1464, 931, 906, 557, 1266, 2047, 3021, 1206, 938,
> > 1375, 1928, 582, 1203, 1810, 1309, 803, 1368, 3141, 2227, 9590,
> > 1067, 128)), row.names = c(NA, -37L), class = "data.frame", .Names =
> > c("times",
> > "NG1.Open", "NG1.High", "NG1.Low", "NG1.Close", "NG1.Volume"))
> >
> > NG1 <- xts(ts[,-1],ts[,1])
> >
> >
> > initDate <- Sys.Date()-1
> > initEq <- 5000
> > n <- 14
> > nSMA <- 200
> >
> > currency("USD")
> > Sys.setenv(TZ="UTC")
> > symbols <- "NG1"
> > stock(symbols, currency="USD", multiplier=1)
> >
> > strategy.st <- 'teststrat'
> > portfolio.st <- 'teststrat'
> > account.st <- 'teststrat'
> > initPortf(portfolio.st, symbols = symbols, initDate=initDate,currency='
> > USD')
> > initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
> > initEq=initEq, currency='USD')
> > initOrders(portfolio=portfolio.st, initDate=initDate)
> >
> >
> >
> > strategy(name = strategy.st,
> >          store = TRUE)
> >
> >
> >
> >
> >
> >
> >
> > add.indicator(
> >   strategy = strategy.st,
> >   name = 'RSI',
> >   arguments = list(price=quote(Cl(mktdata)),n=n),
> >   label = "rsi")
> >
> > add.indicator(
> >   strategy = strategy.st,
> >   name = 'SMA',
> >   arguments = list(x=quote(Cl(mktdata)),n=SMA),
> >   label = 'sma'
> > )
> >
> >
> >
> > add.rule(
> >   strategy=strategy.st,
> >   name='ruleSignal',
> >   arguments=list(
> >     sigcol='RSI_long',
> >     sigval = TRUE,
> >     orderside = 'long',
> >     ordertype='market',
> >     orderqty=1,
> >     replace=FALSE
> >   ),
> >   type='enter',
> >   label='Enterlong',
> >   path.dep = TRUE
> > )
> >
> > add.rule(
> >   strategy=strategy.st,
> >   name='ruleSignal',
> >   arguments=list(
> >     sigcol='RSI_short',
> >     sigval = TRUE,
> >     orderside = 'short',
> >     ordertype='market',
> >     orderqty=1,
> >     replace=TRUE
> >   ),
> >   type='exit',
> >   label='Exitlong',
> >   path.dep = TRUE
> >
> > )
> >
> >
> > t1<-Sys.time()
> > out<-applyStrategy(strategy=strategy.st,portfolios = portfolio.st)
> > t2<-Sys.time()
> > print(t2-t1)
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only. If you want to post, subscribe first.
> > -- Also note that this is not the r-help list where general R questions
> > should go.
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list