Running your strategy
Congratulations on creating a strategy in quantstrat
! To review, your strategy uses three separate indicators and five separate signals. The strategy requires both the threshold of the DVO_2_126
indicator to be under 20 and the SMA50
to be greater than the SMA200
. The strategy sells when the DVO_2_126
crosses above 80, or the SMA50
crosses under the SMA200
.
For this strategy to work properly, you specified five separate signals:
sigComparison
forSMA50
being greater thanSMA200
;sigThreshold
withcross
set toFALSE
forDVO_2_126
less than 20;sigFormula
to tie them together and setcross
toTRUE
;sigCrossover
withSMA50
less thanSMA200
; andsigThreshold
withcross
set toTRUE
forDVO_2_126
greater than 80.
The strategy invests $100,000 (your initeq
) into each trade, and may have some small dollar cost averaging if the DVO_2_126
oscillates around 20 (though the effect is mostly negligible compared to the initial allocation).
In this final chapter, you will learn how to view the actual results of your portfolio. But first, in order to generate the results, you need to run your strategy and fill in some more boilerplate code to make sure quantstrat
records everything. The code in this exercise is code you will have to copy and paste in the future.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- Use applyStrategy() to apply your strategy (
strategy.st
) to your portfolio (portfolio.st
). Save this to the objectout
. - Run necessary functions to record the results of your strategy, including
updatePortf()
and setting thedaterange
as well asupdateAcct()
andupdateEndEq()
. - Based on this information, see if you can find the date of the last trade.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use applyStrategy() to apply your strategy. Save this to out
out <- applyStrategy(strategy = ___, portfolios = ___)
# Update your portfolio (portfolio.st)
updatePortf(___)
daterange <- time(getPortfolio(___)$summary)[-1]
# Update your account (account.st)
updateAcct(___, daterange)
updateEndEq(___)