Search code examples
rquantmod

How to solve an error of calculated variable after adding new trade row into quantmod data set?


I used a code (link is attached) to add new row of trade data into the data set:

 gspc<-getSymbols(("^GSPC") , src = 'yahoo', from = '2008-01-01',  auto.assign = T)
    gspc<-cbind(GSPC)
    q <- getQuote("^GSPC") # adds the current trade row
    qCols <- c("Open","High","Low","Last","Volume","Last")
    qx <- xts(q[,qCols], as.Date(q[,"Trade Time"]))
    gspc <- rbind(gspc, qx)

I got a fine output:

tail(gspc)

               GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCIsuP GSPCIsUp1day GSPCOpAbOp GSPCMA5Op GSPCMA5High GSPCHiLo1d GSPCMAHiLo5d GSPCSTdVHiLo5d GSPCMaOpCl5d GSPCMA10Op
2015-11-03   2102.63   2116.48  2097.51    2109.79  4272060000       2109.79        1            1  21.869873  2085.644    2090.752  25.439941     11.88403      11.720686    -6.943994   2068.559
2015-11-04   2110.60   2114.59  2096.98    2102.31  4078870000       2102.31        0            1   7.970215  2094.468    2099.974  13.850097     14.33003      10.223758    -8.948047   2076.272
2015-11-05   2101.68   2108.78  2090.41    2099.93  4051890000       2099.93        0            0  -8.920166  2097.134    2104.822   3.989990     10.35400       9.420192    -2.516015   2084.252
2015-11-06   2098.60   2101.91  2083.74    2099.20  4369020000       2099.20        1            0  -3.079834  2098.854    2108.074   7.100097     10.94004       9.022028    -1.954053   2088.293
2015-11-09   2096.56   2096.56  2068.24    2078.58  3882350000       2078.58        0            0  -2.040039  2102.014    2109.592   3.309814     10.73799       9.216563    -4.202002   2090.441
2015-11-10   2077.19   2077.98  2073.35    2077.12    34835429       2077.12       NA           NA         NA        NA          NA         NA           NA             NA           NA         NA

But when I tried to add two calculated fields, the first one worked fine but the other produce an error

gspc$GSPCHiLo1d<-Lag(Hi(GSPC),k=1)-Lag(Op(GSPC),k=1) # that's fine
gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d,n=10)
#I got an error "Error in runSum(x, n) : Series contains non-leading NAs"

How can I overcome this problem? link to the code I used: source code link for adding new row to quantmod data set


Solution

  • You can replace your last line with :

    gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d[-nrow(gspc)]) # the n=10 is optional as it is the default option
    
    tail(gspc)
               GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d GSPCMAHiLo10d
    2015-11-03   2102.63   2116.48  2097.51    2109.79  4272060000       2109.79  25.439941     12.483997
    2015-11-04   2110.60   2114.59  2096.98    2102.31  4078870000       2102.31  13.850097     13.270007
    2015-11-05   2101.68   2108.78  2090.41    2099.93  4051890000       2099.93   3.989990     13.219006
    2015-11-06   2098.60   2101.91  2083.74    2099.20  4369020000       2099.20   7.100097     10.597021
    2015-11-09   2096.56   2096.56  2068.24    2078.58  3882350000       2078.58   3.309814      8.772998
    2015-11-09   2096.56   2096.56  2068.24    2078.58   664847995       2078.58         NA            NA
    

    as you can see it will put a NA in your last cell at the bottom right. The thing is, your formula with the two Lag is shorter by one element from gspc so the last element is a NA which will cause a problem for your calculations. After running the command SMA you can (if you want) replace the NA values.

    You can also replace the last value of gspc$GSPCHiLo1d with what you want and then run the SMA command without problem.

    Ok I think I found one of the problem : use

    gspc$GSPCHiLo1d<-Lag(Hi(gspc),k=1)-Lag(Op(gspc),k=1)
    

    instead of

    gspc$GSPCHiLo1d<-Lag(Hi(GSPC),k=1)-Lag(Op(GSPC),k=1)
    

    and then

    gspc$GSPCMAHiLo10d<-SMA(gspc$GSPCHiLo1d,n=10)
    
    tail(gspc)
               GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d
    2015-11-03   2102.63   2116.48  2097.51    2109.79  4272060000       2109.79  25.439941
    2015-11-04   2110.60   2114.59  2096.98    2102.31  4078870000       2102.31  13.850097
    2015-11-05   2101.68   2108.78  2090.41    2099.93  4051890000       2099.93   3.989990
    2015-11-06   2098.60   2101.91  2083.74    2099.20  4369020000       2099.20   7.100097
    2015-11-09   2096.56   2096.56  2068.24    2078.58  3882350000       2078.58   3.309814
    2015-11-10   2077.19   2079.70  2069.91    2071.69   109144559       2071.69   0.000000
               GSPCMAHiLo10d
    2015-11-03     12.483997
    2015-11-04     13.270007
    2015-11-05     13.219006
    2015-11-06     10.597021
    2015-11-09      8.772998
    2015-11-10      8.767016
    

    from your comment you want High - Low but in your code it is High - Open. ReplaceOp by Lo and you will have

    tail(gspc)
               GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted GSPCHiLo1d
    2015-11-03   2102.63   2116.48  2097.51    2109.79  4272060000       2109.79   25.43994
    2015-11-04   2110.60   2114.59  2096.98    2102.31  4078870000       2102.31   18.96997
    2015-11-05   2101.68   2108.78  2090.41    2099.93  4051890000       2099.93   17.61011
    2015-11-06   2098.60   2101.91  2083.74    2099.20  4369020000       2099.20   18.37012
    2015-11-09   2096.56   2096.56  2068.24    2078.58  3882350000       2078.58   18.16992
    2015-11-10   2077.19   2079.70  2069.91    2071.01   110450050       2071.01   28.32007
               GSPCMAHiLo10d
    2015-11-03      18.58199
    2015-11-04      19.22799
    2015-11-05      18.91400
    2015-11-06      17.41902
    2015-11-09      17.08101
    2015-11-10      19.05203
    

    with the desired 28.32

    If that is not what you wanted then please tell me