joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
813 stars 224 forks source link

imported symbol data from text file cant be used with getSymbols #415

Closed Theo1996 closed 4 months ago

Theo1996 commented 4 months ago

Description

I am importing a txt file,with read.delim() . the text file contains this but verically:

Symbol
AZN.L
HSBA.L
ULVR.L

I can get it to work if use this loop:


for ( i in row(importedtxt)) {

    b[i]=importedtxt[i,1] 

}

But if I use as.list or as.character to convert it, the output in the console of this commands shows the correct format:

$Symbol
 [1] "AZN.L"  "HSBA.L" "ULVR.L" "RIO.L"

but its not a character value, and if I use it in getSymbols(importedtxt) it throws the error

Error in getSymbols.AZN.L(Symbols = "Symbol1", env = <environment>, verbose = FALSE,  : 
  could not find function "getSymbols.AZN.L"

Expected behavior

It should take each row or element in the data and get stock prices for that symbol.

Minimal, reproducible example

Symbol
AZN.L
HSBA.L
ULVR.L
importedtxt=read.delim("importedtxt.txt",sep = "\n")
for ( i in row(importedtxt)) {

    b[i]=importedtxt[i,1] 

}

Session Info

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Greek_Greece.1253  LC_CTYPE=Greek_Greece.1253   
[3] LC_MONETARY=Greek_Greece.1253 LC_NUMERIC=C                 
[5] LC_TIME=Greek_Greece.1253    
system code page: 932

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

other attached packages:
[1] tidyquant_1.0.7            PerformanceAnalytics_2.0.4
[3] lubridate_1.9.3            quantmod_0.4.26           
[5] TTR_0.24.4                 xts_0.13.2                
[7] zoo_1.8-12                

loaded via a namespace (and not attached):
 [1] rstudioapi_0.16.0 magrittr_2.0.3    tidyselect_1.2.1 
 [4] munsell_0.5.1     timechange_0.3.0  colorspace_2.1-0 
 [7] lattice_0.20-41   R6_2.5.0          rlang_1.1.3      
[10] quadprog_1.5-8    fansi_0.4.1       stringr_1.5.1    
[13] dplyr_1.1.4       httr_1.4.7        tools_4.0.2      
[16] grid_4.0.2        gtable_0.3.5      utf8_1.1.4       
[19] cli_3.6.2         tibble_3.2.1      lifecycle_1.0.4  
[22] ggplot2_3.5.1     vctrs_0.6.5       curl_5.2.1       
[25] Quandl_2.11.0     glue_1.7.0        stringi_1.5.3    
[28] pillar_1.9.0      compiler_4.0.2    generics_0.1.3   
[31] scales_1.3.0      jsonlite_1.8.8    pkgconfig_2.0.3 
joshuaulrich commented 4 months ago

Your example doesn't use quantmod, and it doesn't run because b isn't defined.

I'm guessing you want something like: quantmod::getSymbols(importedtxt$Symbol).

Theo1996 commented 4 months ago

I omitted both package and b variable, but they do exist properly. Though I did make a mistake the output of getsymbols(importedtxt) is NULL even , Your guess was right, I was mainly tryingimportedtxt[0] and importedtxt[1]etc and now I tried importedtxt[,1] and it works too. Thank you!