Moved script inside the function. Fixed temporarily error when building package:
Error in has.Op(x) : object 'SPX' not found