Search code examples
rfinancialinstrument

R: FinancialInstrument initialize cross currency instrument not working


I want to initialize a curreny pair using FinancialInstrument. The data contains exchange rates for a certain currency pair (e.g. USD_CHF, USD_EUR etc.).

But this doesn't work, why?

> currency("USD")
[1] "USD"
> instrument("USD_CHF",currency="USD",multiplier=1)
primary_id :"USD_CHF"
currency   :"USD"
multiplier :1
tick_size  : NULL
identifiers: list()
type       : NULL
> getInstrument("USD_CHF")
[1] FALSE
Warning message:
In getInstrument("USD_CHF") :
  instrument USD_CHF not found, please create it first.

Or at first it works after creating there is an output with the correct primary_id. However getInstrument doesn't work..and my code thereafter neither.


Solution

  • It didn't save the instrument because the default for the assign_i argument is FALSE.

    > instrument("USD_CHF", currency="USD", multiplier=1, assign_i=TRUE)
    [1] "USD_CHF"
    > getInstrument("USD_CHF")
    primary_id :"USD_CHF"
    currency   :"USD"
    multiplier :1
    tick_size  : NULL
    identifiers: list()
    type       : NULL
    

    You're going to make your life difficult if you use that naming convention because parse_id will not know how to make sense of that. I suggest USDCHF or USD.CHF. You could use USD_CHF as an identifier (other than the primary_id) if you want so that getInstrument (and getSymbols.FI) could still find it by that name.

    Also, you'd be better off using the exchange_rate constructor

    > currency("USD")
    [1] "USD"
    > currency("CHF")
    [1] "CHF"
    > exchange_rate("USDCHF")
    [1] "USDCHF"
    > getInstrument("USDCHF")
    primary_id      :"USDCHF"
    currency        :"CHF"
    multiplier      :1
    tick_size       :0.01
    identifiers     : list()
    type            :"exchange_rate" "currency"
    counter_currency:"USD"