Monocloud
Monocloud

Reputation: 41

R // QuantStrat package - hasTsp(x): invalid time series parameters specified Error

I'm trying to use historical chart data in .csv format for simple backtesting with the quantstrat package in R. I've tried to use different sources - daily OHLC charts, tick data etc. , but I always get this error:

hasTsp(x): invalid time series parameters specified

It's important to note that everything works fine when I'm using getSymbols() instead of the .csv file.

First I import the data set, clean it and transform it to the xts format.

1) Importing the historical tick data.

Sys.setenv(TZ="UTC")
library(quantstrat)
GBPUSD.2015.01->GBPUSD
head(GBPUSD) ## how it looks before transforming 

           V1                    V2      V3      V4
    1 GBP/USD 20150101 21:43:43.344 1.55677 1.55966
    2 GBP/USD 20150101 21:44:15.708 1.55647 1.55987
    3 GBP/USD 20150101 21:45:11.580 1.55692 1.55886
    4 GBP/USD 20150101 21:46:07.579 1.55732 1.55871
    5 GBP/USD 20150101 21:46:41.545 1.55773 1.55858
    6 GBP/USD 20150101 21:46:41.804 1.55772 1.55959

2) Cleaning and adjusting of time.

GBPUSD$V1 <- NULL
GBPUSD$V2 <- strptime(GBPUSD$V2,"%Y%m%d %H:%M:%S") 

3) Transforming to xts.

GBPUSD <- xts(GBPUSD[,-1], order.by=GBPUSD[,1])
head(GBPUSD)
                        V3      V4
2015-01-01 21:43:43 1.55677 1.55966
2015-01-01 21:44:15 1.55647 1.55987
2015-01-01 21:45:11 1.55692 1.55886
2015-01-01 21:46:07 1.55732 1.55871
2015-01-01 21:46:41 1.55773 1.55858
2015-01-01 21:46:41 1.55772 1.55959

is.xts(GBPUSD)
[1] TRUE


GBPUSD <- to.period(GBPUSD,"hours",4) ## Transforming to a 4 hour OHLC chart

                    GBPUSD.Open GBPUSD.High GBPUSD.Low GBPUSD.Close
2015-01-02 00:59:58     1.55677     1.55852    1.55613      1.55634
2015-01-02 04:59:54     1.55627     1.55682    1.55410      1.55441
2015-01-02 08:59:57     1.55442     1.55509    1.55147      1.55297
2015-01-02 12:59:58     1.55236     1.55371    1.54120      1.54259
2015-01-02 16:59:58     1.54253     1.54393    1.53559      1.53601
2015-01-02 20:59:59     1.53602     1.53632    1.53273      1.53333 


initDate = '2015-01-01' ## setting start/end date
.from=initDate
.to="2015-01-31"

currency(c('GBP', 'USD'))

4) Then I specify my strategy. Since it works with getSymbols() the problem should be in the transformation process and not in the definition of the portfolio,indicators,rules etc.

5) Applying the strategy

out <- applyStrategy(strategy.st, portfolio.st)
Error in hasTsp(x): invalid time series parameters specified

Here is the output of traceback() and sessionInfo():

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantstrat_0.9.1669           foreach_1.4.2                 blotter_0.9.1666             
[4] PerformanceAnalytics_1.4.3579 FinancialInstrument_1.2.0     quantmod_0.4-3               
[7] TTR_0.22-0                    xts_0.9-7                     zoo_1.7-11                   

loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_3.1.1      iterators_1.0.7 lattice_0.20-29 tools_3.1.1    


> traceback()
6: hasTsp(x)
5: time.default(Dates)
4: time(Dates)
3: first(time(Dates))
2: applyRules(portfolio = portfolio, symbol = symbol, strategy = strategy, 
       mktdata = mktdata, Dates = NULL, indicators = sret$indicators, 
       signals = sret$signals, parameters = parameters, ..., path.dep = TRUE, 
       debug = debug)
1: applyStrategy(strategy.st, portfolio.st)

Thanks in advance for your help!

Upvotes: 1

Views: 1608

Answers (1)

Monocloud
Monocloud

Reputation: 41

Finally I have found an answer to my question. The problem is that strptime() converts my chart to POSIXlt time format. Apparently only POSIXct format is working.

Therefore you just have to use as.POSIXct() to solve the problem. Modified example: as.POSIXct(strptime(GBPUSD$V2,"%Y%m%d %H:%M:%S")).

Upvotes: 3

Related Questions