1

When I tried to use the EMA function on currencies pair, on the closing price, it seems to return me an error. The code and the error is as follows:

getSymbols('JPY=X', src= 'yahoo')     
JPY.EMA.10 <- EMA(`JPY=X`.Close, n=10)  
Error: unexpected symbol in "JPY.EMA.10 <- EMA(`JPY=X`.Close"

ps: Im a beginner in the area of R. Sorry if my question seems too trivial.

Regards
Harvey

r2evans
  • 141,215
  • 6
  • 77
  • 149
Harvey
  • 11
  • 2
  • 1
    Please have a read through [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [minimal/complete examples](https://stackoverflow.com/help/mcve). Please include all non-base packages you've loaded. Is there really a ticker symbol with an equal sign in it? Don't use backticks (they are not interchangeable with single-quotes). Lastly, a property or field within a variable is typically accessed using the dollar-sign, not dot-separated. – r2evans Jun 14 '17 at 07:03
  • I guess you are using `quantmod` library – KenHBS Jun 14 '17 at 07:06
  • @r2evans Thank you for the advice. I will look through the article and revised my questions. – Harvey Jun 14 '17 at 07:28

1 Answers1

1

It does indeed exist a ticker symbol with an equal sign in it.

Because of this the notation is going to be a little weird, but we can make it work easily:

library(quantmod)

getSymbols('JPY=X', src= 'yahoo')
#> Warning: JPY=X contains missing values. Some functions will not work if
#> objects contain missing values in the middle of the series. Consider using
#> na.omit(), na.approx(), na.fill(), etc to remove or replace them.
#> [1] "JPY=X"

EMA(`JPY=X`$`JPY=X.Close`, n = 10, na.rm = T)
#> Error in naCheck(x, n): Series contains non-leading NAs

We still get an error, but this is relative to the EMA function, not some syntax error.

You can fix the error with using na.omit(`JPY=X`) as suggested in the warnings.

We can also simplify our life assigning the variable to a more standard name:

JPY_Close <- na.omit(`JPY=X`$`JPY=X.Close`)
EMA(JPY_Close, n = 10)
#>                  EMA
#> 2007-01-01        NA
#> 2007-01-02        NA
#> 2007-01-03        NA
#> 2007-01-04        NA
#> 2007-01-05        NA
#> 2007-01-08        NA
#> 2007-01-09        NA
#> 2007-01-10        NA
#> 2007-01-11        NA
#> 2007-01-12 119.26700
#> 2007-01-15 119.47664
#> 2007-01-16 119.71543
#> 2007-01-17 119.87990
#> 2007-01-18 120.13083
#> 2007-01-19 120.32704
#> 2007-01-22 120.56212
#> ...
GGamba
  • 13,140
  • 3
  • 38
  • 47