1. Visualizing your strategy
In addition to the trade statistics table, one other highly
2. The chart.Posn function
illuminating feature in quantstrat is to look at your positions over time. You do this with the chart-dot-Posn function. Let's have a look at the chart-dot-Posn function for our strategy.
3. What it looks like
Using this code brings up a chart of the price, along with the positions you took over time. Furthermore, it will display the size of your positions over the course of the strategy--which are the blue and yellow rectangles, along with a cash, or profit-and-loss based equity curve. This is the green line, and has a drawdown chart under it, which is a red line.
To elaborate, each blue rectangle is a transaction. When its size increases -- that is, as the blue quantity topped by a yellow dot moves away from zero, that means that the strategy took a position--in other words, made a bet. A position greater than zero is a bet on increasing prices, and a position less than zero is a bet on decreasing prices. As you can see in this strategy, we never have a position less than zero.
Moving onto the green line, which is the equity curve based on profit and loss, this green line shows you the overall value of the account over time. The higher the line goes, the better the strategy performs. Ideally, this green line should continue going up and to the right, which would mean a consistently profitable system.
In contrast to the equity curve, the red line is the drawdown chart. To review, drawdown is the amount of money lost since the portfolio made a new high. In quantstrat, the chart-dot-Posn function will include a drawdown chart based on the profit and loss right under the equity curve, which we just discussed. Unlike the equity curve, which anyone wants to see diverging away from zero, you want to see the drawdown chart have as little action as possible. That is, the red line can only stay below zero, and you want it to stay as close to zero as possible as often as possible. When the drawdown chart moves away from zero, that always signifies a strategy losing money.
4. Adding indicators to charts
In addition to the basic functionality of the chart-dot-Posn display, it is possible to overlay indicators on top of it. In order to do this, you have to recalculate the indicators outside the scope of your original strategy, and then add them to the chart using the add-underscore-TA function. In order to plot an indicator on top of the price chart, you will need to use the on = 1 argument in the add-underscore-TA function. Otherwise, do not add this argument and the indicator will be plotted in a new window.
5. Zoomed in
Lastly, using the zoom-underscore-Chart functionality, you can zoom in on various dates in your strategy. Here, you have a closer look at how the system places its trades. The green triangles are buy orders, and the red triangles are sell orders. In this particular image, you can notice that a potential weakness of this system is that good trades may be missed because price crosses over the SMA200 a fair bit of time before the SMA50 does. That is, the black and white price bars cross above the red line before the blue line does. This has to be the case, as the blue line is the average of the past 50 bars, including some that didn't cross the red line.
6. Let's practice!
In the next exercises, you will bring up the chart-dot-Posn chart for your own strategy and add indicators to it.