[R-SIG-Finance] [quantstrat] trading a synthetic asset -- example
Brian G. Peterson
brian at braverock.com
Fri Dec 24 14:59:48 CET 2010
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:
> 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
>
>
>
> _______________________________________________
> R-SIG-Finance at r-project.org 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.
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
More information about the R-SIG-Finance
mailing list