[R-SIG-Finance] R quantstrat - filter consecutive entries
Andreas Henneck
hennecke@@ndre@@ @end|ng |rom web@de
Thu May 28 18:18:04 CEST 2020
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( ...
More information about the R-SIG-Finance
mailing list