Quantcast

quanstrat rule to exit same day close (using daily data)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

quanstrat rule to exit same day close (using daily data)

algotr8der
I am trying to backtest a strategy that requires an exit on the close (same day as the entry).  I have searched through Rmetrics and stackoverflow and found the following post:

http://stackoverflow.com/questions/10441614/quantstrat-in-r-setting-a-date-based-exit-signal

Brian Peterson said the following:

>quantstrat, as described in the manual, is a signals-based framework. It is not designed for filters based trades really.

>You could do what you want by using the delay argument to your exit rule. Set the delay to be one day, and the prefer
argument to prefer a different price column.

>I'm not going to write it for you, but that is enough information to solve your problem.

Now this was a solution to exit at a future time stamp. I want to exit the *same* day (I am using daily data for my test) which is why I'm not sure if setting a delay really works for what I want to do. Is this at all possible?

Appreciate the direction.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

OpenTrades
In quantstrat, if your have a signal on bar t, the rule will be executed
at bar t+1. In your case, this means that the order will be added for
the next day.

Use the 'prefer'-argument to specify which of next day's rates (open,
low, high, close) you want to use.


On 12-05-12 23:45, algotr8der wrote:

> I am trying to backtest a strategy that requires an exit on the close (same
> day as the entry).  I have searched through Rmetrics and stackoverflow and
> found the following post:
>
> http://stackoverflow.com/questions/10441614/quantstrat-in-r-setting-a-date-based-exit-signal
>
> Brian Peterson said the following:
>
>> quantstrat, as described in the manual, is a signals-based framework. It is
> not designed for filters based trades really.
>
>> You could do what you want by using the delay argument to your exit rule.
> Set the delay to be one day, and the prefer
> argument to prefer a different price column.
>
>> I'm not going to write it for you, but that is enough information to solve
> your problem.
>
> Now this was a solution to exit at a future time stamp. I want to exit the
> *same* day (I am using daily data for my test) which is why I'm not sure if
> setting a delay really works for what I want to do. Is this at all possible?
>
> Appreciate the direction.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/quanstrat-rule-to-exit-same-day-close-using-daily-data-tp4629612.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] 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.
>


--
Jan Humme - OpenTrades

WWW:     http://www.opentrades.nl
Email:   jan at opentrades dot nl
Twitter: @opentrades

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

algotr8der
okay so that means I can't use quantstrat for strategies where the entry
and exit are on the same bar (in this case daily bar).

I can do this in blotter - which I guess is fine.

Thanks for the feedback.

On Sun, May 13, 2012 at 3:46 PM, OpenTrades <[hidden email]> wrote:

> In quantstrat, if your have a signal on bar t, the rule will be executed
> at bar t+1. In your case, this means that the order will be added for the
> next day.
>
> Use the 'prefer'-argument to specify which of next day's rates (open, low,
> high, close) you want to use.
>
>
>
> On 12-05-12 23:45, algotr8der wrote:
>
>> I am trying to backtest a strategy that requires an exit on the close
>> (same
>> day as the entry).  I have searched through Rmetrics and stackoverflow and
>> found the following post:
>>
>> http://stackoverflow.com/**questions/10441614/quantstrat-**
>> in-r-setting-a-date-based-**exit-signal<http://stackoverflow.com/questions/10441614/quantstrat-in-r-setting-a-date-based-exit-signal>
>>
>> Brian Peterson said the following:
>>
>>  quantstrat, as described in the manual, is a signals-based framework. It
>>> is
>>>
>> not designed for filters based trades really.
>>
>>  You could do what you want by using the delay argument to your exit rule.
>>>
>> Set the delay to be one day, and the prefer
>> argument to prefer a different price column.
>>
>>  I'm not going to write it for you, but that is enough information to
>>> solve
>>>
>> your problem.
>>
>> Now this was a solution to exit at a future time stamp. I want to exit the
>> *same* day (I am using daily data for my test) which is why I'm not sure
>> if
>> setting a delay really works for what I want to do. Is this at all
>> possible?
>>
>> Appreciate the direction.
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/**
>> quanstrat-rule-to-exit-same-**day-close-using-daily-data-**tp4629612.html<http://r.789695.n4.nabble.com/quanstrat-rule-to-exit-same-day-close-using-daily-data-tp4629612.html>
>> Sent from the Rmetrics mailing list archive at Nabble.com.
>>
>> ______________________________**_________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-sig-finance<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.
>>
>>
>
> --
> Jan Humme - OpenTrades
>
> WWW:     http://www.opentrades.nl
> Email:   jan at opentrades dot nl
> Twitter: @opentrades
>
>
> ______________________________**_________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-sig-finance<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]]

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

gsee
Extending on what Jan said, perhaps you could add a "PrevClose" column
to your data and use prefer='PrevClose'

Garrett

On Sun, May 13, 2012 at 2:55 PM, s p <[hidden email]> wrote:

> okay so that means I can't use quantstrat for strategies where the entry
> and exit are on the same bar (in this case daily bar).
>
> I can do this in blotter - which I guess is fine.
>
> Thanks for the feedback.
>
> On Sun, May 13, 2012 at 3:46 PM, OpenTrades <[hidden email]> wrote:
>
>> In quantstrat, if your have a signal on bar t, the rule will be executed
>> at bar t+1. In your case, this means that the order will be added for the
>> next day.
>>
>> Use the 'prefer'-argument to specify which of next day's rates (open, low,
>> high, close) you want to use.
>>
>>
>>

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

braverock
In reply to this post by algotr8der
On Sun, 2012-05-13 at 15:55 -0400, s p wrote:
> okay so that means I can't use quantstrat for strategies where the
> entry and exit are on the same bar (in this case daily bar).

Sure you can, if the signal is on the same observation.  

I'm not sure how realistic a backtest of market on open and market on
close is going to be, of course, because of slippage, but it should
'work' in quantstrat.

Your entry rule would use prefer='Open' and your exit rule would use
prefer='Close'.

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

gsee
Brian,

I can't get that to work.  It used to be that exit orders would fire
before enter orders and if you did not have a position, they were
effectively not sent.

I tried to test this by making a tiny change to the maCross.R demo.  I
changed the second add.signal call so that it had
    relationship="gte"
just like the first add.signal call.  In other words, you should get
an enter and exit at the same time.  So, you should never have a
position.  Nevertheless,  you end up with a position of short 600
shares.  If that's not right, then I doubt that adding prefer="Open"
to one and prefer="Close" to the other will work.

Garrett
On Sun, May 13, 2012 at 3:20 PM, Brian G. Peterson <[hidden email]> wrote:

> On Sun, 2012-05-13 at 15:55 -0400, s p wrote:
>> okay so that means I can't use quantstrat for strategies where the
>> entry and exit are on the same bar (in this case daily bar).
>
> Sure you can, if the signal is on the same observation.
>
> I'm not sure how realistic a backtest of market on open and market on
> close is going to be, of course, because of slippage, but it should
> 'work' in quantstrat.
>
> Your entry rule would use prefer='Open' and your exit rule would use
> prefer='Close'.
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

braverock
Garrett is correct, sorry, I'm still a little sleep-deprived from
R/Finance.  

Rule execution order is discussed in the documentation for add.rule, and
'enter' rules are processed last.

I wonder if we should consider moving 'rebalance' rules to the end of
the order, or adding a exit rule type that is specifically evaluated
after entry rules.   The code change would be trivial, as that is
processed in a switch statement.


On Sun, 2012-05-13 at 15:46 -0500, G See wrote:

> Brian,
>
> I can't get that to work.  It used to be that exit orders would fire
> before enter orders and if you did not have a position, they were
> effectively not sent.
>
> I tried to test this by making a tiny change to the maCross.R demo.  I
> changed the second add.signal call so that it had
>     relationship="gte"
> just like the first add.signal call.  In other words, you should get
> an enter and exit at the same time.  So, you should never have a
> position.  Nevertheless,  you end up with a position of short 600
> shares.  If that's not right, then I doubt that adding prefer="Open"
> to one and prefer="Close" to the other will work.
>
> Garrett
> On Sun, May 13, 2012 at 3:20 PM, Brian G. Peterson <[hidden email]> wrote:
> > On Sun, 2012-05-13 at 15:55 -0400, s p wrote:
> >> okay so that means I can't use quantstrat for strategies where the
> >> entry and exit are on the same bar (in this case daily bar).
> >
> > Sure you can, if the signal is on the same observation.
> >
> > I'm not sure how realistic a backtest of market on open and market on
> > close is going to be, of course, because of slippage, but it should
> > 'work' in quantstrat.
> >
> > Your entry rule would use prefer='Open' and your exit rule would use
> > prefer='Close'.
> >
> > --
> > Brian G. Peterson
> > http://braverock.com/brian/
> > Ph: 773-459-4973
> > IM: bgpbraverock
> >
> > _______________________________________________
> > [hidden email] 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.
>
> _______________________________________________
> [hidden email] 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

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

algotr8der
Hi Brian - I think that change would be desirable. Not sure what everyone in your development team thinks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

braverock


On Tue, 2012-05-15 at 09:45 -0700, algotr8der wrote:
> Hi Brian - I think that change would be desirable. Not sure what everyone in
> your development team thinks.

OK, it's a one or two line change (order of rule execution) , I'll work
on it.

Jan Humme (opentrades) has a patch basically completed that will allow
multiple trades on the same bar, though things may of course happen 'out
of order' depending on the order of rule evaluation operations.

Regards,

   - Brian

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: quanstrat rule to exit same day close (using daily data)

OpenTrades
Garrett,

On 15-05-12 19:02, Brian G. Peterson wrote:

>
> On Tue, 2012-05-15 at 09:45 -0700, algotr8der wrote:
>> Hi Brian - I think that change would be desirable. Not sure what everyone in
>> your development team thinks.
> OK, it's a one or two line change (order of rule execution) , I'll work
> on it.
>
> Jan Humme (opentrades) has a patch basically completed that will allow
> multiple trades on the same bar, though things may of course happen 'out
> of order' depending on the order of rule evaluation operations.

Yep, patch committed now, rev 1025. There was a problem with the new
orderset feature (committed a couple of weeks ago), causing the
enter-orders to be canceled.

This leaves us with the following situation:

=> if you use a hard-coded order position for the exit-order (eg, -100)
this will still be ruthlessly followed; the order size will not be
adjusted to match the (max) current position. As a result, you will end
up with a -100 position after the first exit order gets executed.
However with your modification (lt changed to gte) the enter order will
be executed at the same bar, leaving you with a 0-position again.
> [1] "2001-06-26 AAPL -100 @ 11.88"
> [1] "2001-06-26 AAPL 100 @ 11.88"

=> if you use the 'all'-argument for the exit-order, the first
exit-order will be skipped due to the 0-position, but the first
enter-order (on the same bar) will be executed. In this case you end up
with a 100-position, which will be exited and immediately entered again
at the next bar that has a signal.

> [1] "2001-06-26 AAPL 100 @ 11.88"
> [1] "2002-01-04 AAPL -100 @ 11.85"
> [1] "2002-01-04 AAPL 100 @ 11.85"
> [1] "2003-05-15 AAPL -100 @ 9.36"
> [1] "2003-05-15 AAPL 100 @ 9.36"

Best regards,

--
Jan Humme - OpenTrades

WWW:     http://www.opentrades.nl
Email:   [hidden email]
Twitter: @opentrades

_______________________________________________
[hidden email] 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.
Loading...