can download and save data but quantstrat is not finding symbol

Issue #1 resolved
Murthy Suresh created an issue

I can download the data and save it as .csv. However quantstrat cannot find the symbol when using applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

Here is the reproducable example. The one doubt i have is if the environmement that i loaded the data needs to match the environment that quantstrat is using. But i am a bit of a rookie in this to determine the dependency.

``` r
# use rtsdata for now

# load `rtsdata` package
require(quantstrat)
#> Loading required package: quantstrat
#> Loading required package: quantmod
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Loading required package: blotter
#> Loading required package: FinancialInstrument
#> Loading required package: PerformanceAnalytics
#> 
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#> 
#>     legend
#> Loading required package: foreach
require(PerformanceAnalytics)
library(rtsdata)
#> 
#> Attaching package: 'rtsdata'
#> The following object is masked from 'package:quantmod':
#> 
#>     getSymbols
env = new.env()
initDate="1990-01-01"
from="2003-01-01"
to="2021-01-29"
options(width=70)
currency('USD')
#> [1] "USD"
Sys.setenv(TZ="UTC")


# tickers to load data
env = new.env()



symbols = c('AAON')



# change the 'yahoo' data source to use CSV files to store historical data
# data is stored in the 'C:/Data/stocks' folder 
# and disable check for updates
register.data.source(src = 'yahoo', 
                     storage = ds.storage.file.csv(getwd(), custom.folder = TRUE),
                     functionality = ds.functionality.default(check.update = TRUE)
)


getSymbols(symbols, env,  src = 'yahoo', from = initDate, verbose=TRUE)
#> ***Full Update for AAON

  print(head(env$'AAON'))
#>            AAON.Open AAON.High AAON.Low AAON.Close AAON.Volume
#> 1992-12-16  0.099763  0.099763 0.099763   0.099763        2506
#> 1992-12-17  0.119716  0.129692 0.099763   0.119716      164139
#> 1992-12-18  0.119716  0.119716 0.119716   0.119716       15036
#> 1992-12-21  0.099763  0.099763 0.089787   0.099763       33830
#> 1992-12-22  0.099763  0.119716 0.099763   0.119716       62648
#> 1992-12-23  0.099763  0.099763 0.099763   0.099763       15036
#>            AAON.Adjusted
#> 1992-12-16      0.083761
#> 1992-12-17      0.100514
#> 1992-12-18      0.100514
#> 1992-12-21      0.083761
#> 1992-12-22      0.100514
#> 1992-12-23      0.083761


stock(symbols, currency="USD", multiplier=1)
#> [1] "AAON"




strategy.st <- portfolio.st <- account.st <- "nonDerivedData"
rm.strat(portfolio.st)
rm.strat(account.st)
.blotter <- new.env()
.strategy <- new.env()

initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
#> [1] "nonDerivedData"
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD')
#> [1] "nonDerivedData"
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 50),
              label = "nFast")
#> [1] "nonDerivedData"

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "Close"),
                            relationship = "gte"),
           label = "longentry")
#> [1] "nonDerivedData"

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("Close", "nFast"),
                            relationship = "gte"),
           label = "longexit")
#> [1] "nonDerivedData"








add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="longentry", sigval=TRUE, ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open", orderqty = 1), 
         type="enter", path.dep=TRUE)
#> [1] "nonDerivedData"

add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="longexit", sigval=TRUE, orderqty="all", 
                        ordertype="market", orderside="long", 
                        replace=FALSE, prefer="Open"), 
         type="exit", path.dep=TRUE)
#> [1] "nonDerivedData"

#apply strategy

out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
#> Error in get(symbol, envir = envir): object 'AAON' not found
```

<sup>Created on 2022-01-29 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>

print(head(env$'AAON'))
#> AAON.Open AAON.High AAON.Low AAON.Close AAON.Volume
#> 1992-12-16 0.099763

however,

out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
#> Error in get(symbol, envir = envir): object 'AAON' not found

Comments (1)

  1. Murthy Suresh reporter

    creating teh new env for the download caused some grief. and so i commented it

    tickers to load data

    env = new.env()

  2. Log in to comment