On 02/09/2011 01:08 PM, Vladimir Egorin wrote:
Here is a hopefully small standalone example (a hacked script from one of the
demos.).
Running it produces an error:
[1] "setup completed"
Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1), .indexFORMAT =
indexFormat(e1),  :

   index length must match number of observations

There were a few problems with your example. A corrected version is attached.

The first and most serious was that you were doing this in the add.rule commands:

data=quote(getPrice(mktdata, prefer='FV'))

This basically took your signal columns *out* of the data object, returning only the price column. That would never work, as there is no column for ruleSignal to match on.

The prefer='FV' argument belongs more reasonably in ... in applyStrategy. I also placed it in the arguments= list for ruleSignal in add.rule, for clarity, but it shouldn't be strictly necessary there.

A second, and more minor error, is in the construction of your index.

You used POSIXct for daily data, which creates problems even after fixing the data object in the calls to ruleSignal. I modified your construction to use as.Date for the index, so that the periodicity checks and handling will work properly inside quantstrat.

With these changes, your example works, and generates trades.

> out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, parameters=list(n=2), prefer='FV' ) )
[1] "2011-01-07 FV -1000 @ 117.9922"
[1] "2011-01-13 FV 1000 @ 118.2734"
[1] "2011-01-13 FV -1000 @ 118.2734"
[1] "2011-01-19 FV 1000 @ 118.2344"
[1] "2011-01-19 FV -1000 @ 118.2344"
[1] "2011-01-20 FV 1000 @ 117.6562"
[1] "2011-01-20 FV 1000 @ 117.6562"
[1] "2011-01-25 FV -1000 @ 118.2422"
[1] "2011-01-25 FV -1000 @ 118.2422"
[1] "2011-01-27 FV 1000 @ 118.2891"
[1] "2011-01-27 FV -1000 @ 118.2891"
[1] "2011-02-01 FV 1000 @ 118.0703"
[1] "2011-02-01 FV 1000 @ 118.0703"

examine the 'out' variable to see what gets added at each step of indicators and signals, and the order book to see the orders generated from the signals.

In general, how would you specify the name of the underlying when
adding rules, in case, for example, the signal is on the underlying X
and the order is on underlying Y.

In strategies like this, you'd probably be writing custom indicator and/or signal functions that took parameters such as Prod1, Prod2 and knew the structure of your data, such as columns named for Prod1 and Prod2.

The standard functions are there to provide for common, simple, strategies, but for proprietary indicators you'll still need proprietary functions. ;)

Regards,

  - Brian

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
try(rm("order_book.RSI",pos=.strategy),silent=TRUE)
try(rm("account.RSI","portfolio.RSI",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratRSI","initDate","initEq",'start_t','end_t'),silent=TRUE)


library('xts')
library('quantstrat')
alldates <- c("2011-01-03","2011-01-04","2011-01-05","2011-01-06","2011-01-07",
"2011-01-10","2011-01-11","2011-01-12","2011-01-13","2011-01-14",
"2011-01-18","2011-01-19","2011-01-20","2011-01-21","2011-01-24",
"2011-01-25","2011-01-26","2011-01-27","2011-01-28","2011-01-31",
"2011-02-01","2011-02-02","2011-02-03","2011-02-04","2011-02-07",
"2011-02-08")
values <- 
c(117.6562,117.6875,117.0156,117.3359,117.9922,118.1172,117.9531,117.9531,
118.2734,118.1719,118.1016,118.2344,117.6562,117.8438,117.8672,118.2422,
117.9688,118.2891,118.5547,118.4141,118.0703,117.6875,117.3984,116.8516,
116.8828,116.3438)

FV = xts(values, as.Date(alldates))
colnames(FV) <- 'FV'

# Initialize a strategy object
stratRSI <- strategy("RSI")

# Add an indicator
stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = 
list(price = quote(getPrice(mktdata, prefer='FV'))), label="RSI")

# There are two signals:
# The first is when RSI is greater than 90
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = 
list(threshold=70, 
column="RSI",relationship="gt",cross=TRUE),label="RSI.gt.70") # The second is 
when RSI is less than 10
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = 
list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")

# There are two rules:
#'## we would Use osMaxPos to put trade on in layers, or to a maximum position. 

# The first is to sell when the RSI crosses above the threshold
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(sigcol="RSI.gt.70", sigval=TRUE, orderqty=-1000, ordertype='market', 
orderside='short', pricemethod='market', replace=FALSE, prefer='FV'), 
type='enter', path.dep=TRUE)
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(sigcol="RSI.lt.30", sigval=TRUE, orderqty='all', ordertype='market', 
orderside='short', pricemethod='market', replace=FALSE, prefer='FV'), 
type='exit', path.dep=TRUE)

# The second is to buy when the RSI crosses below the threshold

stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(sigcol="RSI.lt.30", sigval=TRUE, orderqty= 1000, ordertype='market', 
orderside='long', pricemethod='market', replace=FALSE, prefer='FV'), 
type='enter', path.dep=TRUE)

stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(sigcol="RSI.gt.70", sigval=TRUE, orderqty='all', ordertype='market', 
orderside='long', pricemethod='market', replace=FALSE, prefer='FV'), 
type='exit', path.dep=TRUE)

currency("USD")
symbols = c("FV")
for(symbol in symbols){ # establish trade-able instruments
    stock(symbol, currency="USD",multiplier=1)
}
initDate='2011-01-02'
initEq=100000
port.st<-'RSI' #use a string here for easier changing of parameters and 
re-trying

initPortf(port.st, symbols=symbols, initDate=initDate)
initAcct(port.st, portfolios=port.st, initDate=initDate)
initOrders(portfolio=port.st, initDate=initDate)

print("setup completed")

# Process the indicators and generate trades
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, 
parameters=list(n=2), prefer='FV' ) )
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should 
go.

Reply via email to