Quantstrat - trading not just Crossovers (e.g. Moving Average)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Quantstrat - trading not just Crossovers (e.g. Moving Average)

Igor
Hi,

I've encountered a problem in quanstrat framework, in case I want to trade
not just signalCrossovers (or better said, the modified version of a
Crossover).

Specifically i would like to e.g. go Long if Price>MovingAverage and Exit
if Price<MovingAverage.

This means, that if we have on a <b>starting </b>day Price>MovingAverage
(not a crossover, just "above"), i want the system to buy immediately on a
starting day. However, just once.

If i tried sigComparison, this solved the problem with trading immediately
on a starting day, but started trading <b>anytime</b> a
Price>MovingAverage (every day in a row).

To sum up, I need to know how to modify sigCrossover to trade also in case
"ABOVE" (not just crossover, e.g. on a start day if condition is met) or
how to modify sigComparison to make only 1 Long order (not every day the
condition is met, only on 1st and then not until Exit is made).


Thanks, best regards,

Igor





____________________________________________________________________
Informacie obsiahnute v tomto  dokumente su urcene vylucne pre
potreby jeho adresata. Dokument moze obsahovat informacie chranene
ako bankove alebo obchodne tajomstvo, pripadne informacie
podliehajuce ochrane podla inych pravnych predpisov. Preto Vas v
pripade, ak Vam bol  mylne doruceny, vyzyvame, aby ste sa zdrzali
jeho odtajnenia ci jeho pouzitia pre vlastne potreby. Zaroven si Vas
dovolujeme poziadat, aby ste nas o takomto pripade bez zbytocneho
odkladu informovali a dokument nasledne zlikvidovali.

Information stated in this document is intended only for needs of proper
addressees of this document. This document can contain information
protected as a bank or business secret, or information protected by
other legal regulations. Thus in case of receiving the document by error,  
we kindly ask you not to disclose, or use the document for your own
needs. Likewise we kindly ask you to notify us immediately upon such
a case by sending an email message to the sender's address, and
subsequently delete the document.

Tatra banka, a.s.
http://www.tatrabanka.sk
Hodzovo namestie 3,  811 06 Bratislava 1
ICO: 00 686 930
Zapisana v obchodnom registri Okresneho sudu Bratislava I
Oddiel: Sa, vlozka cislo: 71/B
        [[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
|

Re: Quantstrat - trading not just Crossovers (e.g. Moving Average)

braverock
On Mon, 2011-08-22 at 17:19 +0200, [hidden email]
wrote:

> Hi,
>
> I've encountered a problem in quanstrat framework, in case I want to trade
> not just signalCrossovers (or better said, the modified version of a
> Crossover).
>
> Specifically i would like to e.g. go Long if Price>MovingAverage and Exit
> if Price<MovingAverage.
>
> This means, that if we have on a <b>starting </b>day Price>MovingAverage
> (not a crossover, just "above"), i want the system to buy immediately on a
> starting day. However, just once.
>
> If i tried sigComparison, this solved the problem with trading immediately
> on a starting day, but started trading <b>anytime</b> a
> Price>MovingAverage (every day in a row).
>
> To sum up, I need to know how to modify sigCrossover to trade also in case
> "ABOVE" (not just crossover, e.g. on a start day if condition is met) or
> how to modify sigComparison to make only 1 Long order (not every day the
> condition is met, only on 1st and then not until Exit is made).

use osMaxPos and set a maximum position instead of the default osNoOp
order sizing function.

Details in the documentation, or I can work up a quick example later in
the week.

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
|

Re: Quantstrat - trading not just Crossovers (e.g. Moving Average)

Joshua Ulrich
On Mon, Aug 22, 2011 at 10:33 AM, Brian G. Peterson <[hidden email]> wrote:

> On Mon, 2011-08-22 at 17:19 +0200, [hidden email]
> wrote:
>> Hi,
>>
>> I've encountered a problem in quanstrat framework, in case I want to trade
>> not just signalCrossovers (or better said, the modified version of a
>> Crossover).
>>
>> Specifically i would like to e.g. go Long if Price>MovingAverage and Exit
>> if Price<MovingAverage.
>>
>> This means, that if we have on a <b>starting </b>day Price>MovingAverage
>> (not a crossover, just "above"), i want the system to buy immediately on a
>> starting day. However, just once.
>>
>> If i tried sigComparison, this solved the problem with trading immediately
>> on a starting day, but started trading <b>anytime</b> a
>> Price>MovingAverage (every day in a row).
>>
>> To sum up, I need to know how to modify sigCrossover to trade also in case
>> "ABOVE" (not just crossover, e.g. on a start day if condition is met) or
>> how to modify sigComparison to make only 1 Long order (not every day the
>> condition is met, only on 1st and then not until Exit is made).
>
> use osMaxPos and set a maximum position instead of the default osNoOp
> order sizing function.
>
> Details in the documentation, or I can work up a quick example later in
> the week.
>

I just wrote a blog post with an example strategy that uses osMaxPos
and addPosLimit to do this.  Here's the link: http://goo.gl/yBKDr

Best,
Josh

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

--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.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.
Reply | Threaded
Open this post in threaded view
|

Re: Quantstrat - trading not just Crossovers (e.g. Moving Average)

Igor
In reply to this post by Igor
Thanks, works fine for me in this case.

I am just wondering, however, how to use this solution in case i want to
set my own order sizing function (e.g. order size based on a % of current
portfolio equity).

In other words, how to trade my system (above Moving Average, not
crossover) and use flexible order quantity as well.

Igor Vilèek




Joshua Ulrich <[hidden email]>
25.08.2011 03:16

To
"Brian G. Peterson" <[hidden email]>
cc
[hidden email], [hidden email]
Subject
Re: [R-SIG-Finance] Quantstrat - trading not just Crossovers (e.g. Moving
Average)






On Mon, Aug 22, 2011 at 10:33 AM, Brian G. Peterson <[hidden email]>
wrote:
> On Mon, 2011-08-22 at 17:19 +0200, [hidden email]
> wrote:
>> Hi,
>>
>> I've encountered a problem in quanstrat framework, in case I want to
trade
>> not just signalCrossovers (or better said, the modified version of a
>> Crossover).
>>
>> Specifically i would like to e.g. go Long if Price>MovingAverage and
Exit
>> if Price<MovingAverage.
>>
>> This means, that if we have on a <b>starting </b>day
Price>MovingAverage
>> (not a crossover, just "above"), i want the system to buy immediately
on a
>> starting day. However, just once.
>>
>> If i tried sigComparison, this solved the problem with trading
immediately
>> on a starting day, but started trading <b>anytime</b> a
>> Price>MovingAverage (every day in a row).
>>
>> To sum up, I need to know how to modify sigCrossover to trade also in
case
>> "ABOVE" (not just crossover, e.g. on a start day if condition is met)
or
>> how to modify sigComparison to make only 1 Long order (not every day
the
>> condition is met, only on 1st and then not until Exit is made).
>
> use osMaxPos and set a maximum position instead of the default osNoOp
> order sizing function.
>
> Details in the documentation, or I can work up a quick example later in
> the week.
>

I just wrote a blog post with an example strategy that uses osMaxPos
and addPosLimit to do this.  Here's the link: http://goo.gl/yBKDr

Best,
Josh

> Regards,
>
>  - Brian
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
>
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com





____________________________________________________________________
Informacie obsiahnute v tomto  dokumente su urcene vylucne pre
potreby jeho adresata. Dokument moze obsahovat informacie chranene
ako bankove alebo obchodne tajomstvo, pripadne informacie
podliehajuce ochrane podla inych pravnych predpisov. Preto Vas v
pripade, ak Vam bol  mylne doruceny, vyzyvame, aby ste sa zdrzali
jeho odtajnenia ci jeho pouzitia pre vlastne potreby. Zaroven si Vas
dovolujeme poziadat, aby ste nas o takomto pripade bez zbytocneho
odkladu informovali a dokument nasledne zlikvidovali.

Information stated in this document is intended only for needs of proper
addressees of this document. This document can contain information
protected as a bank or business secret, or information protected by
other legal regulations. Thus in case of receiving the document by error,  
we kindly ask you not to disclose, or use the document for your own
needs. Likewise we kindly ask you to notify us immediately upon such
a case by sending an email message to the sender's address, and
subsequently delete the document.

Tatra banka, a.s.
http://www.tatrabanka.sk
Hodzovo namestie 3,  811 06 Bratislava 1
ICO: 00 686 930
Zapisana v obchodnom registri Okresneho sudu Bratislava I
Oddiel: Sa, vlozka cislo: 71/B
        [[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
|

Re: Quantstrat - trading not just Crossovers (e.g. Moving Average)

Kenton Russell
See if this helps
http://www.google.com/m/url?ei=ULhXTtiYCqOIsgeo54kY&q=http://timelyportfolio.blogspot.com/2011/07/quantstrat-to-build-on-part-6.html&ved=0CCcQFjAE&usg=AFQjCNHmJsPa1uDfZKwfqPsHy42kxK7nlg


Kent

On Aug 26, 2011, at 7:39 AM, [hidden email] wrote:

> Thanks, works fine for me in this case.
>
> I am just wondering, however, how to use this solution in case i want to
> set my own order sizing function (e.g. order size based on a % of current
> portfolio equity).
>
> In other words, how to trade my system (above Moving Average, not
> crossover) and use flexible order quantity as well.
>
> Igor Vilèek
>
>
>
>
> Joshua Ulrich <[hidden email]>
> 25.08.2011 03:16
>
> To
> "Brian G. Peterson" <[hidden email]>
> cc
> [hidden email], [hidden email]
> Subject
> Re: [R-SIG-Finance] Quantstrat - trading not just Crossovers (e.g. Moving
> Average)
>
>
>
>
>
>
> On Mon, Aug 22, 2011 at 10:33 AM, Brian G. Peterson <[hidden email]>
> wrote:
>> On Mon, 2011-08-22 at 17:19 +0200, [hidden email]
>> wrote:
>>> Hi,
>>>
>>> I've encountered a problem in quanstrat framework, in case I want to
> trade
>>> not just signalCrossovers (or better said, the modified version of a
>>> Crossover).
>>>
>>> Specifically i would like to e.g. go Long if Price>MovingAverage and
> Exit
>>> if Price<MovingAverage.
>>>
>>> This means, that if we have on a <b>starting </b>day
> Price>MovingAverage
>>> (not a crossover, just "above"), i want the system to buy immediately
> on a
>>> starting day. However, just once.
>>>
>>> If i tried sigComparison, this solved the problem with trading
> immediately
>>> on a starting day, but started trading <b>anytime</b> a
>>> Price>MovingAverage (every day in a row).
>>>
>>> To sum up, I need to know how to modify sigCrossover to trade also in
> case
>>> "ABOVE" (not just crossover, e.g. on a start day if condition is met)
> or
>>> how to modify sigComparison to make only 1 Long order (not every day
> the
>>> condition is met, only on 1st and then not until Exit is made).
>>
>> use osMaxPos and set a maximum position instead of the default osNoOp
>> order sizing function.
>>
>> Details in the documentation, or I can work up a quick example later in
>> the week.
>>
>
> I just wrote a blog post with an example strategy that uses osMaxPos
> and addPosLimit to do this.  Here's the link: http://goo.gl/yBKDr
>
> Best,
> Josh
>
>> Regards,
>>
>> - Brian
>>
>> --
>> Brian G. Peterson
>> http://braverock.com/brian/
>> Ph: 773-459-4973
>> IM: bgpbraverock
>>
>>
>
> --
> Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
>
>
>
>
>
> ____________________________________________________________________
> Informacie obsiahnute v tomto  dokumente su urcene vylucne pre
> potreby jeho adresata. Dokument moze obsahovat informacie chranene
> ako bankove alebo obchodne tajomstvo, pripadne informacie
> podliehajuce ochrane podla inych pravnych predpisov. Preto Vas v
> pripade, ak Vam bol  mylne doruceny, vyzyvame, aby ste sa zdrzali
> jeho odtajnenia ci jeho pouzitia pre vlastne potreby. Zaroven si Vas
> dovolujeme poziadat, aby ste nas o takomto pripade bez zbytocneho
> odkladu informovali a dokument nasledne zlikvidovali.
>
> Information stated in this document is intended only for needs of proper
> addressees of this document. This document can contain information
> protected as a bank or business secret, or information protected by
> other legal regulations. Thus in case of receiving the document by error,  
> we kindly ask you not to disclose, or use the document for your own
> needs. Likewise we kindly ask you to notify us immediately upon such
> a case by sending an email message to the sender's address, and
> subsequently delete the document.
>
> Tatra banka, a.s.
> http://www.tatrabanka.sk
> Hodzovo namestie 3,  811 06 Bratislava 1
> ICO: 00 686 930
> Zapisana v obchodnom registri Okresneho sudu Bratislava I
> Oddiel: Sa, vlozka cislo: 71/B
>    [[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.

_______________________________________________
[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
|

Re: Quantstrat - trading not just Crossovers (e.g. Moving Average)

braverock
In reply to this post by Igor
On Fri, 2011-08-26 at 14:39 +0200, [hidden email]
wrote:
>
> Thanks, works fine for me in this case.
>
> I am just wondering, however, how to use this solution in case i want
> to set my own order sizing function

*Everything* in quantstrat is modular.  You can define your own
functions for indicators, signals, rules, order sizing, even the fill
simulator or P&L.  We've tried to provide reasonable default functions.

It  is typical when I am modeling 'real' strategies for me to write
custom indicators, signals, and order sizing functions.

We've provided two example order sizing functions.  osNoOp, and
osMaxPos.  You can use these as templates to code your own custom order
sizing function.

> (e.g. order size based on a % of current portfolio equity).
>
We'll get around to an equity-aware order sizing function, as Josh
pointed out in his most recent blog post:

http://blog.fosstrading.com/2011/08/tactical-asset-allocation-using.html

The challenge for us in doing this is making it fast.  On daily OHLC
data, speed is less of an issue, but on tick data, it would be very easy
to take a huge performance hit in recalculating account equity.

There are some possible 'shortcuts' to this,

1> you could use realized transaction P&L as a proxy for account equity,
and not have to update all the statistics in the portfolio and account.

2> you could repeatedly call applyStrategy on different non-overlapping
time periods (e.g. months for daily data), calling updatePortf and and
updateAcct at the end of each period, and updating your maxPos for each
asset.  osMaxPos is already aware of the maximum position at any given
timestamp, so changing it periodically should be fine unless you're
levered up to nosebleed levels.

I've used variations on both the methods with quantstrat on tick data,
but haven't gotten around to reworking those things in a generic fashion
to put in to the package.

> In other words, how to trade my system (above Moving Average, not
> crossover) and use flexible order quantity as well.

Hope this helps,

  - 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.