Skip to content

R quantstrat - filter consecutive entries

4 messages · Andreas Henneck, Jasen Mackie

#
Dear all, being new to this list, kindly allow me to introduce myself: I
am an engineer by education, marketer of automation products by
profession and R fan of a few years. i am currently learning to program
technical trading rules for backtesting. The purpose is to stablize
investments and learn to trade. As neither a search through the last
three years of e-mail from this list nor a search through stackexchange
et al revealed an answer I would like to dare a post. I hope I? am not
being to basic with my request.

This first self-written strategy on EOD data is looking to follow trends
utilizing Bollinger Bands for indicating direction and Stochastik fastD
to detect entry points and exits with overbought and oversold positions.
My first version codes long only trades. The rules work all fine, the
problem is this:

Stochastic crosses a 0.25 threshold from below more than once, i.e. in a
slight downward trend thus triggering multiple consecutive entry orders,
before any exits or stops have triggered. This essentially increases
position size.

The essential question: What is the solution to entering a trade more
than once?

Looking forward to your responses and insight. Any additional tips for
quantstrat thinking or coding are appreciated. Below find some hopefully
illustrating code snippets.

Kind regards,
Andreas

##Not run <- not sure that I am using this properly here.

# Create indicators

BBTMDir = function(HLC, n = 10, sd = 1, nFastK = 5, nFastD = 2) {

 ?bb_dir <- # rules not essential to the problem, values look something
like: round(runif(5))

# Stochastic overbought and oversold indicator. Symmetry for simplicity:

 ? sto_sig <- sigThreshold(label = "StochSig", data = sto, column =
"fastD",
 ????????????????????????? threshold = 0.25, relationship = "gt", cross
= TRUE) * 1 +
 ??? sigThreshold(label = "StochSig", data = sto, column = "fastD",
 ???????????????? threshold = 0.75, relationship = "lt", cross = TRUE) * -1
 ? merge(bb_dir, sto_sig)? # return the two columns

# ... standard boiler plate quantstrat setup ...

s <- add.indicator( s, 'BBTMDir', label='BBTM',
 ??????????????????? arguments=list( HLC = quote(HLC(mktdata))) )

# Entry and exit rules based on bb_dir => BBDIR.BBTM as filter and
StochSig.BBTM like this:

s <- add.signal( s, 'sigFormula', label='StochEntryLong',
 ???????????????? arguments = list(cross = TRUE, formula = "BBDir.BBTM
== 1 & StochSig.BBTM == 1"))
s <- add.signal( s, 'sigFormula', label='StochExitLong',
 ???????????????? arguments = list(cross = TRUE, formula = "BBDir.BBTM
== 1 & StochSig.BBTM == -1"))

# as I am still learning and testing this, there are only these two
simple rules for now:

s <- add.rule(
 ? s, name='ruleSignal', type='enter', label='ChgDirLong',
 ? arguments = list(
 ??? sigcol="StochEntryLong", sigval=TRUE, orderqty=orderQ,
 ??? TxnFees=.txnfees,
 ??? ordertype='market', orderside='long', orderset = "ocolong"
 ? )
)

s <- add.rule(
 ? s, name='ruleSignal', type='exit', label='ExitChgDir',
 ? arguments = list(
 ??? sigcol="StochExitLong", sigval=TRUE, replace = TRUE, orderqty="all",
 ??? ordertype='market', orderside='long', orderset = "ocolong"
 ? )
)

# ...

applyStrategy( ...
#
Hi Andreas

Thanks for the question. In future, feel free to ask a question by creating
an issue on the project repo -
https://github.com/braverock/quantstrat/issues.

You are looking for the ?sigCrossover function.

The project README
<https://github.com/braverock/quantstrat/blob/master/README.md> on GitHub
can also help with more learning materials and as usual look into the demo
folder for concrete examples.

Good luck.

Regards
Jasen

On Thu, 28 May 2020 at 12:18, Andreas Henneck <hennecke.andreas at web.de>
wrote:

  
  
#
Hi Jasen et al,

thanks for the quick response and invite. I'll be glad to do post
questions on github.

sigThreshold, cross = TRUE responds as programmed and the same as I
would expect sigCrossover to work, when passing one parameter as a
constant. Just wording for the long side: Two consecutive entry signals
occur, i.e. stoch crosses the 0.25 threshold from below on a second
lower low, while the BB crossing has not switched to "short" yet. It is
more a question of the strategy not detecting a lower low well and thus
filtering a bad order to begin with. So the ways I am looking to explore
are:

 ? - objective for quantstrat programing:? filter consecutive entries
after an order has already been placed and filled.? How can quantstrat
solve this?

 ? - objective for strategy: improve the filter to be more responsive to
detect the change of direction. The literature suggests MAs for filters.
Any tips on specific I should explore?

Thanks and kind regards,
Andreas


Am 28.05.2020 um 18:36 schrieb Jasen Mackie:

  
  
#
Right i see. A minimum reproducible example will be helpful to understand
the questions properly and the repo on GH would be the ideal place to
continue the discussion.

Thanks
Jasen

On Fri, 29 May 2020 at 03:45, Andreas Henneck <hennecke.andreas at web.de>
wrote: