joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
325 stars 102 forks source link

The Symbol for Nano Labs Ltd (NASDAQ), NA, conflicts with NA in R #128

Closed boryeumao closed 10 months ago

boryeumao commented 10 months ago

The entry for Nano Labs Ltd via function stockSymbols() is not properly treated, and getSymbols() fails to fetch the data, as expected. For read.csv there is na.strings to get around the special NA - Where should I look for anything similar for quantmod? Thank you.

joshuaulrich commented 10 months ago

Can you please provide a reproducible example that demonstrates the issue? Otherwise I have to make educated guesses about what code you're running...

boryeumao commented 10 months ago

Yes - I had to step away but will get back on github to send relevant data to the issue.  Thanks!

boryeumao commented 10 months ago

An example of stockSymbols() not reading "Nano Labs Ltd's" symbol NA:

library(quantmod)

s <- stockSymbols(exchange = "NASDAQ")
# Symbol NA should've been row 3151
print(s[3148:3153, c("Symbol","Name")], right = FALSE)
##      Symbol Name
## 3148 MYPSW  PLAYSTUDIOS, Inc.  - Warrant
## 3149 MYRG   MYR Group, Inc. - Common Stock
## 3150 MYSZ   My Size, Inc. - Common Stock
## 3151 NAAS   NaaS Technology Inc. - American Depositary Shares
## 3152 NAII   Natural Alternatives International, Inc. - Common Stock
## 3153 NAMS   NewAmsterdam Pharma Company N.V. - Ordinary Shares

# But it is misplaced at the end, row 5147
# with Symbol NA mis-interpreted to be <NA>
print(s[5142:5147, c("Symbol","Name")], right = FALSE)
##      Symbol Name
## 5142 ZXYZ-A Nasdaq Symbology Test Common Stock
## 5143 ZXZZT  NASDAQ TEST STOCK
## 5144 ZYME   Zymeworks Inc. - Common Stock
## 5145 ZYNE   Zynerba Pharmaceuticals, Inc. - Common Stock
## 5146 ZYXI   Zynex, Inc. - Common Stock
## 5147 <NA>   Nano Labs Ltd - American Depositary Shares

An example of getSymbols() failing to read quotes for "Nano Labs Ltd":

# Symbol NA fails
nn <- getSymbols("NA")
nn
## [1] "NA"

Thank you.

joshuaulrich commented 10 months ago

Thanks for the example and the report! This is indeed an issue with stockSymbols(), so I'm going to transfer this issue to the TTR repo.


getSymbols() imports ticker "NA" correctly. The issue is that getSymbols() loads the data into the global environment and returns the ticker symbol. So it's expected that nn would be "NA".

You can get your expected result by setting auto.assign = FALSE.

(nn <- getSymbols("NA", auto.assign = FALSE))
##            NA.Open NA.High NA.Low NA.Close NA.Volume NA.Adjusted
## 2022-07-12    7.61  13.390  7.510    12.05   1486800       12.05
## 2022-07-13    9.77  12.000  8.800     8.80    264400        8.80
## 2022-07-14    8.01   8.260  6.321     7.35    134100        7.35
## 2022-07-15    7.64   7.820  6.800     6.80     47000        6.80
## 2022-07-18    6.45   8.490  6.300     6.70     69400        6.70
## 2022-07-19    6.72   7.515  6.710     7.42     39200        7.42
## 2022-07-20    6.54   8.300  6.330     7.73    190700        7.73
## 2022-07-21    7.70   8.000  7.080     8.00     28000        8.00
## 2022-07-22    7.91   7.910  6.688     6.79     37500        6.79
## 2022-07-25    7.10  13.000  7.010    10.56   4532500       10.56
##        ...
## 2023-08-03    1.67   1.670  1.400     1.44     55700        1.44
## 2023-08-04    1.48   1.520  1.380     1.44    124000        1.44
## 2023-08-07    1.41   1.440  1.300     1.30     83100        1.30
## 2023-08-08    1.25   1.460  1.250     1.38    117200        1.38
## 2023-08-09    1.35   1.420  1.310     1.33     39100        1.33
## 2023-08-10    1.33   1.394  1.280     1.28     34000        1.28
## 2023-08-11    1.35   1.350  1.190     1.22    100400        1.22
## 2023-08-14    1.19   1.328  1.140     1.29     81600        1.29
## 2023-08-15    1.29   1.290  1.080     1.20     48600        1.20

And getSymbols("NA") works. But NA is a reserved word in R, so you have to use backquotes to access the object returned by getSymbols()

getSymbols("NA")
print(NA)    # NA
## [1] NA
print(`NA`)  # xts object
##            NA.Open NA.High NA.Low NA.Close NA.Volume NA.Adjusted
## 2022-07-12    7.61  13.390  7.510    12.05   1486800       12.05
## 2022-07-13    9.77  12.000  8.800     8.80    264400        8.80
## 2022-07-14    8.01   8.260  6.321     7.35    134100        7.35
## 2022-07-15    7.64   7.820  6.800     6.80     47000        6.80
## 2022-07-18    6.45   8.490  6.300     6.70     69400        6.70
## 2022-07-19    6.72   7.515  6.710     7.42     39200        7.42
## 2022-07-20    6.54   8.300  6.330     7.73    190700        7.73
## 2022-07-21    7.70   8.000  7.080     8.00     28000        8.00
## 2022-07-22    7.91   7.910  6.688     6.79     37500        6.79
## 2022-07-25    7.10  13.000  7.010    10.56   4532500       10.56
##        ...
## 2023-08-03    1.67   1.670  1.400     1.44     55700        1.44
## 2023-08-04    1.48   1.520  1.380     1.44    124000        1.44
## 2023-08-07    1.41   1.440  1.300     1.30     83100        1.30
## 2023-08-08    1.25   1.460  1.250     1.38    117200        1.38
## 2023-08-09    1.35   1.420  1.310     1.33     39100        1.33
## 2023-08-10    1.33   1.394  1.280     1.28     34000        1.28
## 2023-08-11    1.35   1.350  1.190     1.22    100400        1.22
## 2023-08-14    1.19   1.328  1.140     1.29     81600        1.29
## 2023-08-15    1.29   1.290  1.080     1.20     48600        1.20
boryeumao commented 10 months ago

Thanks for the lesson on NA in R for getSymbols(). And for forwarding stockSymbols() as well.

boryeumao commented 10 months ago

Thank you!!

 On Friday, August 25, 2023 at 09:02:13 AM PDT, Joshua Ulrich ***@***.***> wrote:  

Closed #128 as completed via f99ce2c.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>