Get startedGet started for free

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:

  1. sigComparison for SMA50 being greater than SMA200;
  2. sigThreshold with cross set to FALSE for DVO_2_126 less than 20;
  3. sigFormula to tie them together and set cross to TRUE;
  4. sigCrossover with SMA50 less than SMA200; and
  5. sigThreshold with cross set to TRUE for DVO_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

View Course

Exercise instructions

  • Use applyStrategy() to apply your strategy (strategy.st) to your portfolio (portfolio.st). Save this to the object out.
  • Run necessary functions to record the results of your strategy, including updatePortf() and setting the daterange as well as updateAcct() and updateEndEq().
  • 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(___)
Edit and Run Code