Skip to content

setting quantstrat stoplimit threshold to a signal value

2 messages · bb01100100, Brian G. Peterson

#
Hello,

I am testing a simple strategy in quantstrat, whereby I would like to set a
'stoplimit' order equal to 75% of the Donchian Channel range.

If the Donchian Channel is as follows:

 a. low = 92.20
 b. high = 110.50

Then I would like to set my 'stoplimit' to: low + (high - low) * 0.75,
giving a price of 105.925

My question is: How can I correctly access mktdata's signal information,
e.g. mktdata$mySignalValue for use in the threshold parameter of add.rule()?

If my approach is wrong, then a backup question is: In quantstrat, how do I
set a long stop / stoplimit order above the market at a price derived from
the signals added to the strategy via add.signal()?



I have tried to use formulas in the add.rule() function's 'threshold'
parameter as below:

myStrat = add.rule(myStrat, name='ruleSignal',
 arguments=list(
   sigcol='trade.long',
   sigval=TRUE,
   orderqty=1,
   ordertype='stoplimit',
   orderside='long',
   threshold=(mktdata$tradeSignal.low + ((mktdata$tradeSignal.high -
mktdata$tradeSignal.low)*0.75) - mktdata$SPY.High),
   tmult=FALSE,
   TxnFees=-5
 ),
type='enter',
replace=TRUE
)


but I get an error:



[1] "Setup completed"
Added a position limit for  SPY 
Error in NextMethod(.Generic) : 
  dims [product 1] do not match the length of object [504]
In addition: Warning messages:
1: In max(i) : no non-missing arguments to max; returning -Inf
2: In max(getOrders(portfolio = portfolio, symbol = symbol, status = "open", 
:
  no non-missing arguments, returning NA
3: In if (!is.null(ordertype) & is.na(charmatch(ordertype, c("market",  :
  the condition has length > 1 and only the first element will be used
4: In if (is.na(charmatch(ordertype, c("market", "limit", "stoplimit",  :
  the condition has length > 1 and only the first element will be used

Which, I assume, means that I'm passing a big list of values from my
threshold formula instead of a specific value.

I have tried using last() on the above formula, but this uses the last value
of the entire mktdata series.

Also, I have searched quite a lot, but all of the examples I have seen look
for a signal and then buy at market, so I'm bit stuck. Any help, pointers or
guidance to a relevant example would be much appreciated.


Thanks,
Kel.


--
View this message in context: http://r.789695.n4.nabble.com/setting-quantstrat-stoplimit-threshold-to-a-signal-value-tp3635067p3635067.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
On Thu, 2011-06-30 at 01:15 -0700, bb01100100 wrote:
Please provide a reproducible example, per the posting guide. (put
another way, I'm not going to write an entire strategy to figure out
your bug).

Now, some general advice for when you rework your strategy:

You could do what you want with sigFormula, but it will be slow, because
of its flexibility.

Instead, look at how the other demo scipts make use of indicators and
signals.

What you've described is 
- one indicator which is the Donchian Channel
- another indicator which is 75% of the Donchian Channel range
- a rule which enters limit orders at your second indicator, likely
adjusting the limits whenever the indicator changes

Also, take a look at how the demos and documentation use the quote()
function.

If you post a complete example, odds are someone here will help you sort
out the details; and wouldn't a Donchian Channel demo strategy be a nice
addition to the package? ;)

Regards,

   - Brian