The objective is to create a tracker that seek a return that is -1x the return of Bitcoin in order to simulate BTC/USD short position in a portoflio.
For example I would like the btcInv
object goes up +3.96% after price of Bitcoin felt on 05-feb, then loses -1.42% after price rise on 06-feb.
Date Dernier Ouv. + Haut + Bas Variation %
06/02/2015 218.80 215.73 223.00 213.16 1.42%
05/02/2015 215.73 224.62 225.00 212.00 -3.96%
The code bellow return a line that oscillate around 100 but it doesn't do the job because btcInv
always equals to 100.
What would be a proper way to minus the daily return to the previous BtcInv
value (i.e. the day n-1 value) ?
btcInv <- 100
# Use tryCatch() in case we try to get data too far in the past that
# Oanda doesn't provide. Return NULL if there is an error, and Filter
# to only include data that has at least 1 row.
btc <- do.call(rbind, Filter(NROW, lapply(0:5, function(i) {
tryCatch(getFX("BTC/USD",
from = Sys.Date() -499 * (i + 1),
to = Sys.Date() - 499 * i,
env=NULL), error=function(e) NULL)
})))
btcInv <- btcInv - (dailyReturn(btc) * 100)
Thank you