开始使用免费开始使用

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.

本练习是课程的一部分

Financial Trading in R

查看课程

练习说明

  • 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.

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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(___)
编辑并运行代码