[R-SIG-Finance] [quantstrat] trading a synthetic asset -- example

Anass Mouhsine anass.mouhsine at gmail.com
Fri Dec 24 14:40:03 CET 2010


Hi all,

here is an example code for a basic strategy on a synthetic asset. (with 
error :-( )
Let us assume  I already calculated the synthetic asset.
You will find it attached.

require(quantstrat)

try(rm("order_book.testP",pos=.strategy),silent=TRUE)
try(rm("account.testP","portfolio.testP",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","synth.str","stratComp","initDate","initEq",'start_t','end_t'),silent=TRUE)


### Here I prepare the asset####
synth.str='test'
testdata<-read.csv("test.csv",colClasses=c("character","numeric"),sep=";")
test<-as.xts(testdata[,2],order.by=as.POSIXct(testdata[,1],format="%Y-%m-%d"),tzone="America/New_York", 
src="csv", updated=Sys.time())
colnames(test)<-"test"

####Then I will define it####
currency('USD')
synthetic(synth.str,currency='USD',multiplier=1,tick_size=0.000001)
# BTW, I have to specify a multiplier and a tick_size otherwise there is 
an error

initDate='2007-11-01'
initEq=1000000
portfolio.st='testP'
account.st='testP'

initPortf(portfolio.st,symbols=synth.str, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)

buyin=-1.5*sd(C15)

#Define function stand
stand<-function(x)
{
   t<-cbind(x,x)
   colnames(t)<-c(colnames(x),"signal")
   return(t)
}


stratComp <- strategy(portfolio.st)

stratComp <- add.indicator(strategy = stratComp, name = "stand", 
arguments = list(x=quote(mktdata)) )

stratComp <- add.signal(strategy = 
stratComp,name="sigThreshold",arguments = 
list(column="signal",relationship="gt",threshold=buyin,cross=TRUE),label="signal.gt.buyin")

#buy rule
stratComp <- add.rule(strategy = stratComp,name='ruleSignal', arguments 
= list(sigcol="signal.gt.buyin",sigval=TRUE, orderqty=1, 
ordertype='market', orderside='long', threshold=NULL),type='enter')
stratComp <- add.rule(strategy = stratComp,name='ruleSignal', arguments 
= list(sigcol="signal.gt.buyin",sigval=TRUE, orderqty=-1, 
ordertype='stoplimit', orderside='long', 
threshold=.90,tmult=TRUE),type='risk')

out<-try(applyStrategy(strategy=stratComp , portfolios=portfolio.st))

I have then the following error

Error in getPrice(x = data, prefer = prefer) :
   subscript out of bounds, no price was discernible from the data
De plus : Message d'avis :
In max(i) : aucun argument pour max ; -Inf est renvoyé
Error in if (price == 0) stop("price", price, "must be positive or 
negative") :
   valeur manquante là où TRUE / FALSE est requis

I understand that the getPrice function works on OHLC prices.
the synthetic asset, as such, is just a one column price time-series.

Is there a way to use the applyStrategy function on non-OHLC time-series?

Thx in advance

Anass
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20101224/31cd0cd6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.csv
Type: application/vnd.ms-excel
Size: 29369 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20101224/31cd0cd6/attachment.xlb>


More information about the R-SIG-Finance mailing list