Get startedGet started for free

Setting up a strategy II

1. Setting up a strategy II

In addition to the previous lecture, quantstrat needs more setup still. The good thing is that past this video, this will be all the initialization you'll need, so this chapter is fairly short. However, it's important to get these things correct, and stored away in a place that can be easily changed for future strategies.

2. Trade size and initial equity

In quantstrat, trade size and initial equity are important if one hopes to be able to work in returns space, as opposed to simply profit and loss space (aka P&L space). The trade size is the base line amount for risking on one trade, and the initial equity is your total account equity. Working in returns space allows for many more functions from the PerformanceAnalytics library which use returns as an input (and not P&L) to analyze your strategy.

3. Three important objects

Furthermore, one needs to initialize names for the account, the portfolio, and the strategy. The way to think of this hierarchy is this: an account may contain one or multiple portfolios, and each portfolio may contain one or multiple strategies.

4. Naming account, portfolio, and strategy

In this case, we will be working with one account, one portfolio, and one strategy. The way I prefer doing this is displayed here.

5. Removing existing strategy

Furthermore, if a strategy already exists in your working environment, it cannot be re-run. Therefore, you need to remove the strategy if one wishes to re-run a slightly modified variant of the strategy.

6. Initialize…

Lastly, you need to formally initialize the portfolio, account, orders, and strategy objects.

7. Initializing portfolio

In order, initializing the portfolio is the first step in this process. A portfolio, before running your strategy, is a set of assets, and the initialization date for your strategy. In order to initialize your portfolio, you need to pass in your portfolio name (in this case, portfolio-dot-st), the assets in this portfolio (in this case, LQD), the initialization date, and the currency which you defined earlier on.

8. Initializing account

Next, you will initialize the account. The account in quantstrat holds all portfolios. The arguments to the initAcct command are the account name (in this case, account-dot-st), the names of all the portfolios inside it (in this case, just portfolio-dot-st), the initialization date, the currency, and the initial equity, both defined earlier.

9. Initializing orders

Next, you need to initialize orders. This command is simpler in that it only requires a name of the portfolio which will hold the orders, and the initialization date after which these orders begin.

10. Initializing strategy

Finally, you initialize the strategy, which just takes in a strategy name, and the store = TRUE command.

11. Overview

Essentially, you'll need to have all of these commands stored in an initialization master file that you can modify. To put it succinctly, at the highest level, you have an account. An account in quantstrat is, like in real life, something that contains portfolios, which themselves contain assets. On the lower level, quantstrat needs to initialize orders--that is, a container for holding the history of transactions to buy or sell assets. Lastly, a strategy is a set of instructions on how to buy and sell these various assets.

12. Let's practice!

In the next exercises, you'll be tasked with completing the initialization for your strategy.