[R-SIG-Finance] quantstrat & custom indicators

Brian G. Peterson brian at braverock.com
Tue May 3 21:45:32 CEST 2011


If you look a the strategy object, you'll see that you have four sets of
rules by the time you're done.  This is likely not what you want.
Strategies should normally be agnostic to the instrument to trade, and
you're not really doing that.

I don't have time to rewrite this for you right now, but here's what I 
suggest.

build two portfolios, one for your longs, and one for your shorts
(alternately, you could have a portfolio for each instrument, but this
shouldn't be necessary)

build one account to hold it all

Have *two* strategy objects, ignoring symbols for now.  One will be your
'long' strategy, and one will be your 'short' strategy.  use some
arbitrarily large order quantity for your entries and 'all' for your
exits. (I'll explain this in a minute). have the rules use
osFUN='osMaxPos'

Use setPosLimit to set your maximum long and short position limits by
symbol.  

This should then work as you expect when you apply the long strategy to
the longs and the short strategy to the shorts.

#############

Now...  onto something more like what I suspect you're really trying to
do ...

Normally, what you'd really want to do is have a only a single strategy
object, regardless of symbol. So forget all about the symbols when
specifying the indicators, signals, and rules.

The sophisticated part lies in (maybe) using setPosLimit or some other
function to store things like limits/direction by symbol, and *then*
writing a custom order sizing function.  The order sizing function gets
a rule fire on a symbol, checks your custom metadata, and decides how
large (if any) an order to place.

You'll notice that several times in the documentation we refer to
signals as places where you *may* want to trade.  Rules and order sizing
functions take the *may* and can then decide to turn that into an
affirmative trade action and also decide how much.

HTH,

   - Brian

On Tue, 2011-05-03 at 12:28 -0700, algotr8der wrote:
> So I changed the following:
> 
> 1) orderqty = all 
> 
> in all of the exit rules to 
> 
> 2) orderqty = -numshares
> 
> So essentially reversing the original trade. This allowed the code to
> generate trades. I also lengthened the date range to include data from
> 2007-07-02 to 2007-12-30 to get more data points for the test. 
> 
> http://r.789695.n4.nabble.com/file/n3493332/basketStrategy.R
> basketStrategy.R 
> 
> As you can see the trades for IBM are generated based on the rule specified.
> However, one of the trades for CAT is using the rule added to the strategy
> for IBM (it's trading 65 shares). I'm not sure if this is the entry trade
> because the entry trade should be a short sale of 348 shares. Instead a
> purchase of 65 shares is made as the first ever trade for CAT. You can see
> the same theme progress for UTX and MSFT.
> 
> I think there is some rule overlapping going on here and its likely because
> I am not using the features of this package correctly. If there was a way to
> remove a rule once processing of a symbol has completed I think that might
> work. But you guys might have more 'clean' and 'efficient' ways to think
> about this problem.
> 
> [1] "2007-08-10 IBM 65 @ 112.64"
> [1] "2007-08-14 IBM -65 @ 112.05"
> [1] "2007-08-23 IBM 65 @ 111.45"
> [1] "2007-09-12 IBM -65 @ 116"
> [1] "2007-09-18 IBM 65 @ 116.63"
> [1] "2007-10-03 IBM -65 @ 116.4"
> [1] "2007-10-08 IBM 65 @ 117.77"
> [1] "2007-10-17 IBM -65 @ 115.78"
> [1] "2007-10-29 IBM 65 @ 114.8"
> [1] "2007-11-01 IBM -65 @ 113.65"
> [1] "2007-11-02 IBM 65 @ 114.59"
> [1] "2007-11-05 IBM -65 @ 113.4"
> [1] "2007-11-23 IBM 65 @ 104.05"
> [1] "2007-11-26 IBM -65 @ 101.97"
> [1] "2007-11-27 IBM 65 @ 103.83"
> [1] "2007-12-11 IBM -65 @ 106.99"
> [1] "2007-12-12 IBM 65 @ 108.47"
> [1] "2007-12-14 IBM -65 @ 105.77"
> [1] "2007-12-20 IBM 65 @ 108.84"
> [1] "2007-08-02 CAT 65 @ 80.67"
> [1] "2007-08-02 CAT -348 @ 80.67"
> [1] "2007-08-03 CAT -65 @ 78.96"
> [1] "2007-08-03 CAT 348 @ 78.96"
> [1] "2007-08-06 CAT 65 @ 81"
> [1] "2007-08-06 CAT -348 @ 81"
> [1] "2007-08-09 CAT -65 @ 78.48"
> [1] "2007-08-09 CAT 348 @ 78.48"
> [1] "2007-08-23 CAT 65 @ 75.17"
> [1] "2007-08-23 CAT -348 @ 75.17"
> [1] "2007-08-28 CAT -65 @ 74.16"
> [1] "2007-08-28 CAT 348 @ 74.16"
> [1] "2007-08-29 CAT 65 @ 75.04"
> [1] "2007-08-29 CAT -348 @ 75.04"
> [1] "2007-08-30 CAT -65 @ 74.66"
> [1] "2007-08-30 CAT 348 @ 74.66"
> [1] "2007-08-31 CAT 65 @ 75.77"
> [1] "2007-08-31 CAT -348 @ 75.77"
> [1] "2007-09-07 CAT -65 @ 73.44"
> [1] "2007-09-07 CAT 348 @ 73.44"
> [1] "2007-09-18 CAT 65 @ 77.46"
> [1] "2007-09-18 CAT -348 @ 77.46"
> [1] "2007-10-11 CAT -65 @ 79.44"
> [1] "2007-10-11 CAT 348 @ 79.44"
> [1] "2007-10-12 CAT 65 @ 80.3"
> [1] "2007-10-12 CAT -348 @ 80.3"
> [1] "2007-10-15 CAT -65 @ 78.84"
> [1] "2007-10-15 CAT 348 @ 78.84"
> [1] "2007-11-02 CAT 65 @ 74.76"
> [1] "2007-11-02 CAT -348 @ 74.76"
> [1] "2007-11-05 CAT -65 @ 73.5"
> [1] "2007-11-05 CAT 348 @ 73.5"
> [1] "2007-11-06 CAT 65 @ 74.92"
> [1] "2007-11-06 CAT -348 @ 74.92"
> [1] "2007-11-07 CAT -65 @ 73.6"
> [1] "2007-11-07 CAT 348 @ 73.6"
> [1] "2007-11-28 CAT 65 @ 71.19"
> [1] "2007-11-28 CAT -348 @ 71.19"
> [1] "2007-12-14 CAT -65 @ 73.39"
> [1] "2007-12-14 CAT 348 @ 73.39"
> [1] "2007-12-24 CAT 65 @ 72.7"
> [1] "2007-12-24 CAT -348 @ 72.7"
> [1] "2007-08-02 UTX -348 @ 74.76"
> [1] "2007-08-02 UTX 648 @ 74.76"
> [1] "2007-08-03 UTX 348 @ 73.93"
> [1] "2007-08-03 UTX -648 @ 73.93"
> [1] "2007-08-06 UTX -348 @ 74.85"
> [1] "2007-08-06 UTX 648 @ 74.85"
> [1] "2007-08-07 UTX 348 @ 74.15"
> [1] "2007-08-07 UTX -648 @ 74.15"
> [1] "2007-08-13 UTX -348 @ 74.01"
> [1] "2007-08-13 UTX 648 @ 74.01"
> [1] "2007-08-14 UTX 348 @ 72.78"
> [1] "2007-08-14 UTX -648 @ 72.78"
> [1] "2007-08-17 UTX -348 @ 73.82"
> [1] "2007-08-17 UTX 648 @ 73.82"
> [1] "2007-08-21 UTX 348 @ 72.94"
> [1] "2007-08-21 UTX -648 @ 72.94"
> [1] "2007-08-22 UTX -348 @ 73.87"
> [1] "2007-08-22 UTX 648 @ 73.87"
> [1] "2007-08-28 UTX 348 @ 72"
> [1] "2007-08-28 UTX -648 @ 72"
> [1] "2007-08-29 UTX -348 @ 74.35"
> [1] "2007-08-29 UTX 648 @ 74.35"
> [1] "2007-09-05 UTX 348 @ 73.45"
> [1] "2007-09-05 UTX -648 @ 73.45"
> [1] "2007-09-06 UTX -348 @ 75.01"
> [1] "2007-09-06 UTX 648 @ 75.01"
> [1] "2007-09-07 UTX 348 @ 73.79"
> [1] "2007-09-07 UTX -648 @ 73.79"
> [1] "2007-09-10 UTX -348 @ 74.49"
> [1] "2007-09-10 UTX 648 @ 74.49"
> [1] "2007-10-03 UTX 348 @ 79.67"
> [1] "2007-10-03 UTX -648 @ 79.67"
> [1] "2007-10-05 UTX -348 @ 80.84"
> [1] "2007-10-05 UTX 648 @ 80.84"
> [1] "2007-10-08 UTX 348 @ 80.36"
> [1] "2007-10-08 UTX -648 @ 80.36"
> [1] "2007-10-09 UTX -348 @ 81.2"
> [1] "2007-10-09 UTX 648 @ 81.2"
> [1] "2007-10-10 UTX 348 @ 80.5"
> [1] "2007-10-10 UTX -648 @ 80.5"
> [1] "2007-10-31 UTX -348 @ 76.59"
> [1] "2007-10-31 UTX 648 @ 76.59"
> [1] "2007-11-01 UTX 348 @ 74.72"
> [1] "2007-11-01 UTX -648 @ 74.72"
> [1] "2007-11-05 UTX -348 @ 76.11"
> [1] "2007-11-05 UTX 648 @ 76.11"
> [1] "2007-11-07 UTX 348 @ 74.46"
> [1] "2007-11-07 UTX -648 @ 74.46"
> [1] "2007-11-13 UTX -348 @ 75.33"
> [1] "2007-11-13 UTX 648 @ 75.33"
> [1] "2007-11-14 UTX 348 @ 74.92"
> [1] "2007-11-14 UTX -648 @ 74.92"
> [1] "2007-11-20 UTX -348 @ 74.32"
> [1] "2007-11-20 UTX 648 @ 74.32"
> [1] "2007-11-21 UTX 348 @ 72.78"
> [1] "2007-11-21 UTX -648 @ 72.78"
> [1] "2007-11-27 UTX -348 @ 74"
> [1] "2007-11-27 UTX 648 @ 74"
> [1] "2007-12-14 UTX 348 @ 76.69"
> [1] "2007-12-14 UTX -648 @ 76.69"
> [1] "2007-12-21 UTX -348 @ 77.83"
> [1] "2007-12-21 UTX 648 @ 77.83"
> [1] "2007-08-07 MSFT 648 @ 29.55"
> [1] "2007-08-07 MSFT -1000 @ 29.55"
> [1] "2007-08-09 MSFT -648 @ 29.3"
> [1] "2007-08-09 MSFT 1000 @ 29.3"
> [1] "2007-08-23 MSFT 648 @ 28.3"
> [1] "2007-08-23 MSFT -1000 @ 28.3"
> [1] "2007-08-28 MSFT -648 @ 27.93"
> [1] "2007-08-28 MSFT 1000 @ 27.93"
> [1] "2007-08-29 MSFT 648 @ 28.59"
> [1] "2007-08-29 MSFT -1000 @ 28.59"
> [1] "2007-09-05 MSFT -648 @ 28.48"
> [1] "2007-09-05 MSFT 1000 @ 28.48"
> [1] "2007-09-06 MSFT 648 @ 28.91"
> [1] "2007-09-06 MSFT -1000 @ 28.91"
> [1] "2007-09-07 MSFT -648 @ 28.44"
> [1] "2007-09-07 MSFT 1000 @ 28.44"
> [1] "2007-09-11 MSFT 648 @ 28.93"
> [1] "2007-09-11 MSFT -1000 @ 28.93"
> [1] "2007-09-17 MSFT -648 @ 28.73"
> [1] "2007-09-17 MSFT 1000 @ 28.73"
> [1] "2007-09-18 MSFT 648 @ 28.93"
> [1] "2007-09-18 MSFT -1000 @ 28.93"
> [1] "2007-09-19 MSFT -648 @ 28.67"
> [1] "2007-09-19 MSFT 1000 @ 28.67"
> [1] "2007-09-24 MSFT 648 @ 29.08"
> [1] "2007-09-24 MSFT -1000 @ 29.08"
> [1] "2007-10-19 MSFT -648 @ 30.17"
> [1] "2007-10-19 MSFT 1000 @ 30.17"
> [1] "2007-10-22 MSFT 648 @ 30.51"
> [1] "2007-10-22 MSFT -1000 @ 30.51"
> [1] "2007-11-07 MSFT -648 @ 35.52"
> [1] "2007-11-07 MSFT 1000 @ 35.52"
> [1] "2007-11-20 MSFT 648 @ 34.58"
> [1] "2007-11-20 MSFT -1000 @ 34.58"
> [1] "2007-11-26 MSFT -648 @ 32.97"
> [1] "2007-11-26 MSFT 1000 @ 32.97"
> [1] "2007-12-05 MSFT 648 @ 34.15"
> [1] "2007-12-05 MSFT -1000 @ 34.15"
> [1] "2007-12-17 MSFT -648 @ 34.39"
> [1] "2007-12-17 MSFT 1000 @ 34.39"
> [1] "2007-12-18 MSFT 648 @ 34.74"
> [1] "2007-12-18 MSFT -1000 @ 34.74"
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/quantstrat-custom-indicators-tp3491259p3493332.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
> 
> _______________________________________________
> 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