veepsirtt,

You need to add an indicator for BBands(sd=1).

pairStrat <- add.indicator(strategy = pairStrat, name = "BBands", arguments
= list(HLC=quote(Ratio), sd=1, n=N, maType='SMA'))

the BBands function returns an object with columns: "up","mavg","dn", and
"pctB"
Since you added a second BBands function, it will use column names
"up.1","mavg.1","dn.1","pctB.1"

Edit your exits to look at columns from the second BBand function;

pairStrat <- add.signal(strategy = pairStrat, name = "sigCrossover",
arguments= list(columns=c("Ratio","up.1"), relationship="lt"),
label="cross.mid.fa")
pairStrat <- add.signal(strategy = pairStrat, name = "sigCrossover",
arguments= list(columns=c("Ratio","dn.1"), relationship="gt"),
label="cross.mid.fb")

Please pay attention to the "relationship" arguments. Currently, it will
sell when it cross the upper 2 stdev band FROM ABOVE, and it will take that
short off when it crosses the 1 stdev band FROM ABOVE.  You may want to
switch the relationships for entries.

HTH,
Garrett

On Wed, Jun 8, 2011 at 4:19 AM, veepsirtt <veepsirtt@gmail.com> wrote:

> Dear Daniel Cegiełka,
>
>  I iinstalled and run the  demo.
>
>
> https://r-forge.r-project.org/scm/viewvc.php/pkg/quantstrat/demo/pair_trade.R?view=markup&revision=605&root=blotter
>
> Now I want to change the pair trading strategy as follows.
>
> ## given 2 stocks, calculate the ratio of their notional values.  If the
> ratio falls below it's
> # 2 stdev band, then when it crosses back above it, buy stock 1 and sell
> stock 2.
>
> # If the ratio rises above it's 1 stdev band, then  flatten any open
> positions.
>
> For this condition how to modify the following lines,
>
> # Create signals -*** BBands for 2 Stdev
>
> pairStrat <- add.signal(strategy = pairStrat, name = "sigCrossover",
> arguments= list(columns=c("Ratio","up"), relationship="lt"),
> label="cross.up")
> pairStrat <- add.signal(strategy = pairStrat, name = "sigCrossover",
> arguments= list(columns=c("Ratio","dn"), relationship="gt"),
> label="cross.dn")
>
> # Create entry and exit rules for longs  and for shorts..****here BBands
> for
> 1 stdev
>
> pairStrat <- add.rule(strategy = pairStrat, name='ruleSignal', arguments =
> list(sigcol="cross.up", sigval=TRUE, orderqty=-1e6, ordertype='market',
> orderside=NULL, osFUN='osSpreadMaxPos'), type='enter')
>
>  How to exit ?
>
> Thanking you
> veepsirtt
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-to-test-pairs-trading-strategy-tp3558776p3582036.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance@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.
>

	[[alternative HTML version deleted]]

