2

I'm currently in the process of building a strategy using quantstrat/blotter. The price data that I'm using uses numbers as the security identifiers and these numbers are therefore the column names as well as what I use for the synbol names in functions such as stock() in order to import the financial instruments. However as shown in the reproducible code below, using a very small portion of my dataset, whenever stock() is used on these numerical identifiers, the FinancialInstrument package modifies them in a strange manner, by appending an "X" and removing the leading digit. Based upon this, are there any restrictions on symbol names for use with the FinancialInstrument package?

structure(c(9.17000007629395, 9.17000007629395, 9.17000007629395, 
9.17000007629395, 9.17000007629395, 9.17000007629395, 41.0999984741211, 
40.7599983215332, 40.4599990844727, 40.1500015258789, 40.5299987792969, 
40.5299987792969, 41.9900016784668, 41.7449989318848, 42.0299987792969, 
41.7200012207031, 42.25, 41.7000007629395, 29.3199996948242, 
29.3199996948242, 29.3199996948242, 29.3199996948242, 29.3199996948242, 
29.3199996948242), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1403481600, 
1403568000, 1403654400, 1403740800, 1403827200, 1404086400), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 
4L), .Dimnames = list(NULL, c("10078", "10104", "10107", "10108"
)))

colnames(x)
# "10078" "10104" "10107" "10108"

for(i in colnames(x)){
  stock(i,currency="USD",multiplier=1)
}

ls_stocks()
# "X0078" "X0104" "X0107" "X0108"
Brian G. Peterson
  • 3,531
  • 2
  • 20
  • 21
user6893
  • 143
  • 1
  • 2
  • 9
  • 2
    What it's doing isn't strange. Symbol/object names should start with a letter or a dot followed by a letter. Use a different identifier for each stock. – Joshua Ulrich Jan 30 '15 at 18:51
  • 1
    hmm. It's happening because of this code in `instrument()`: `if (substr(primary_id, 1, 1) == 1) primary_id <- substr(primary_id, 2, nchar(primary_id))`, but I don't remember why that's there. :-/ (perhaps a hack for Interactive Brokers single stock futures symbols) As Joshua said, you could name your instruments better (and use identifiers so that getInstrument() can still find them if you look for the numbers) – GSee Jan 30 '15 at 18:56
  • 1
    to demonstrate: `stock("X1234", currency("USD"), identifiers=list(num="1234")); getInstrument("1234")` – GSee Jan 30 '15 at 18:57
  • @GSee The "numbers" that I'm using are actually PERMNOs since the price series that I'm using are very long in nature and tickers can change over time but your workaround works for me. If you want, expand your comment into an answer and I'll accept it – user6893 Jan 30 '15 at 19:03
  • The Details section of `?stock` says "The ‘primary_id’ will be coerced within reason to a valid R variable name by using ‘make.names’. We also remove any leading '1' digit (a simple workaround to account for issues with the Reuters API)." – GSee Jan 31 '15 at 00:27

1 Answers1

2

instrument names need to begin with a letter or a dot. The instrument function uses make.names to ensure this. If it's important to be able to find your instruments by a number, then you can add it as an identifier.

stock("X1234", currency("USD"), identifiers=list(num=1234))
getInstrument("1234")
#primary_id :"X1234"
#currency   :"USD"
#multiplier :1
#tick_size  :0.01
#identifiers:List of 1
# ..$ num:1234
#type       :"stock"

Another way to add an identifier

add.identifier("X1234", id2=42)
GSee
  • 48,880
  • 13
  • 125
  • 145