Skip to content

[quantstrat] trading a synthetic asset -- example

3 messages · Anass Mouhsine, Brian G. Peterson

#
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>
#
Either pass

prefer='test'

since that's what you called the price column, or better yet, name the 
column 'Price', or 'price' , or 'spread.price' (basically anything with 
'price' in it.

getPrice will then be able to figure it out.

I'll run your example later to confirm, but this is almost certainly the 
issue.

Regards,

   - Brian
On 12/24/2010 07:40 AM, Anass Mouhsine wrote:

  
    
#
On 12/24/2010 07:40 AM, Anass Mouhsine wrote:
I figure I should probably clarify this too.

getPrice tries to figure things out from the structure of the data.  As 
such, it tests for OHLC, TBBO (tick), or univariate 'price' series and 
makes its best guess.

Beyond that, if the best guess isn't good enough, as in the proffered 
test case, the 'prefer=' argument will tell getPrice what column to go 
looking for in your data.
Yes.  We do it all the time on tick data, including synthetic 
instruments that may be univariate price series, or (for example) 
bid/ask/mid columns for a spread (to show execution slippage when 
crossing markets to hit the other side of the spread, for example).

Regards,

   - Brian