rxtsquantstratblotterfinancialinstrument

R - FinancialInstrument Package Changing Symbol Names when using stock


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"

Solution

  • 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)