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
<mailto:hennecke.andreas at web.de>> wrote:
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( ...