0

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

Florent
  • 1,791
  • 22
  • 40
  • It would be easier to help if you had a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. As far as I can tell your programming question doesn't have anything to do with bitcoin specifically. Maybe you're just looking for the `diff()` function? – MrFlick Feb 10 '15 at 22:19
  • To give more precision to my question I will add an example. – Florent Feb 10 '15 at 23:13
  • I'm not sure the diff() function will help. I hope the description of the question is better now. – Florent Feb 10 '15 at 23:34

1 Answers1

1

With arithmetic returns, you can multiply the first price by cumprod(1+r) to recreate the series. If you want the inverse, you can use -r. i.e. cumprod(1-r)

# sample data
btc <- getFX("BTC/USD", from="2015-02-01", to="2015-02-10", auto.assign=FALSE)
btcInv <- cumprod(1-dailyReturn(btc)) * as.numeric(btc[1])
all.equal(dailyReturn(btc), -dailyReturn(btcInv))
#[1] TRUE
GSee
  • 48,880
  • 13
  • 125
  • 145