<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/an-introduction-to-stock-market-data-analysis-with-r-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ntguardian.wordpress.com/2017/04/03/introduction-stock-market-data-r-2/"> R – Curtis Miller's Personal Website</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>Around September of 2016 I wrote two articles on using Python for accessing, visualizing, and evaluating trading strategies (see <a target="_blank" rel="nofollow" href="https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1/">part 1</a> and <a target="_blank" rel="nofollow" href="https://ntguardian.wordpress.com/2016/09/26/introduction-stock-market-data-python-2/">part 2</a>). These have been my most popular posts, up until I published my article <a target="_blank" rel="nofollow" href="https://ntguardian.wordpress.com/2017/03/13/on-programming-languages-why-my-dad-went-from-programming-to-driving-a-bus/">on learning programming languages</a> (featuring my dad’s story as a programmer), and has been translated into both Russian (which used to be on backtest.ru at a <a target="_blank" rel="nofollow" href="https://backtest.ru/blog/algotrejding-na-python/">link</a> that now appears to no longer work) and Chinese (<a target="_blank" rel="nofollow" href="https://yq.aliyun.com/articles/66878">here</a> and <a target="_blank" rel="nofollow" href="https://yq.aliyun.com/articles/66817">here</a>). R has excellent packages for analyzing stock data, so I feel there should be a “translation” of the post for using R for stock data analysis.</em></p>
<p><em>This post is the second in a two-part series on stock data analysis using R, based on a lecture I gave on the subject for <a target="_blank" rel="nofollow" href="http://datasciencecourse.net/2016/index.html">MATH 3900 (Data Science) at the University of Utah</a>. (You can read the first post <a target="_blank" rel="nofollow" href="https://ntguardian.wordpress.com/2017/03/27/introduction-stock-market-data-r-1/">here</a>.) In these posts, I discuss basics such as obtaining the data from Yahoo! Finance using pandas, visualizing stock data, moving averages, developing a moving-average crossover strategy, backtesting, and benchmarking. The final post will include practice problems. This post discusses moving average crossover strategies,backtesting, and benchmarking.</em></p>
<p><strong>NOTE: The information in this post is of a general nature containing information and opinions from the author’s perspective. None of the content of this post should be considered financial advice. Furthermore, any code written here is provided without any form of guarantee. Individuals who choose to use it do so at their own risk.</strong></p>
<p><span></span></p>
<h2>Trading Strategy</h2>
<p>Call an <strong>open position</strong> a trade that will be terminated in the future when a condition is met. A <strong>long</strong> position is one in which a profit is made if the financial instrument traded increases in value, and a <strong>short</strong> position is on in which a profit is made if the financial asset being traded decreases in value. When trading stocks directly, all long positions are bullish and all short position are bearish. That said, a bullish attitude need not be accompanied by a long position, and a bearish attitude need not be accompanied by a short position (this is particularly true when trading stock options).</p>
<p>Here is an example. Let’s say you buy a stock with the expectation that the stock will increase in value, with a plan to sell the stock at a higher price. This is a long position: you are holding a financial asset for which you will profit if the asset increases in value. Your potential profit is unlimited, and your potential losses are limited by the price of the stock since stock prices never go below zero. On the other hand, if you expect a stock to decrease in value, you may borrow the stock from a brokerage firm and sell it, with the expectation of buying the stock back later at a lower price, thus earning you a profit. This is called <strong>shorting a stock</strong>, and is a short position, since you will earn a profit if the stock drops in value. The potential profit from shorting a stock is limited by the price of the stock (the best you can do is have the stock become worth nothing; you buy it back for free), while the losses are unlimited, since you could potentially spend an arbitrarily large amount of money to buy the stock back. Thus, a broker will expect an investor to be in a very good financial position before allowing the investor to short a stock.</p>
<p>Any trader must have a set of rules that determine how much of her money she is willing to bet on any single trade. For example, a trader may decide that under no circumstances will she risk more than 10% of her portfolio on a trade. Additionally, in any trade, a trader must have an <strong>exit strategy</strong>, a set of conditions determining when she will exit the position, for either profit or loss. A trader may set a <strong>target</strong>, which is the minimum profit that will induce the trader to leave the position. Likewise, a trader must have a maximum loss she is willing to tolerate; if potential losses go beyond this amount, the trader will exit the position in order to prevent any further loss (this is usually done by setting a <strong>stop-loss order</strong>, an order that is triggered to prevent further losses).</p>
<p>We will call a plan that includes trading signals for prompting trades, a rule for deciding how much of the portfolio to risk on any particular strategy, and a complete exit strategy for any trade an overall <strong>trading strategy</strong>. Our concern now is to design and evaluate trading strategies.</p>
<p>We will suppose that the amount of money in the portfolio involved in any particular trade is a fixed proportion; 10% seems like a good number. We will also say that for any trade, if losses exceed 20% of the value of the trade, we will exit the position. Now we need a means for deciding when to enter position and when to exit for a profit.</p>
<p>Here, I will be demonstrating a <a target="_blank" rel="nofollow" href="http://www.investopedia.com/university/movingaverage/movingaverages4.asp">moving average crossover strategy</a>. We will use two moving averages, one we consider “fast”, and the other “slow”. The strategy is:</p>
<ul>
<li>Trade the asset when the fast moving average crosses over the slow moving average.</li>
<li>Exit the trade when the fast moving average crosses over the slow moving average again.</li>
</ul>
<p>A long trade will be prompted when the fast moving average crosses from below to above the slow moving average, and the trade will be exited when the fast moving average crosses below the slow moving average later. A short trade will be prompted when the fast moving average crosses below the slow moving average, and the trade will be exited when the fast moving average later crosses above the slow moving average.</p>
<p>We now have a complete strategy. But before we decide we want to use it, we should try to evaluate the quality of the strategy first. The usual means for doing so is <strong>backtesting</strong>, which is looking at how profitable the strategy is on historical data. For example, looking at the above chart’s performance on Apple stock, if the 20-day moving average is the fast moving average and the 50-day moving average the slow, this strategy does not appear to be very profitable, at least not if you are always taking long positions.</p>
<p>(You may not see all of these packages being loaded in the session; they may get loaded when other packages are loaded.)</p>
<p>First, let’s recreate some of our earlier charts. (I’m going to use different methods for recreating these charts, both for diversity and because some functions we’ll be using later will be introduced this way. The method used is actually more general than what was used in part 1.)</p>
<pre><code>if (!require(&quot;TTR&quot;)) {
  install.packages(&quot;TTR&quot;)
  library(TTR)
}
if (!require(&quot;quantstrat&quot;)) {
  install.packages(&quot;quantstrat&quot;, repos=&quot;http://R-Forge.R-project.org&quot;)
  library(quantstrat)
}
if (!require(&quot;IKTrading&quot;)) {
  if (!require(&quot;devtools&quot;)) {
    install.packages(&quot;devtools&quot;)
  }
  library(devtools)
  install_github(&quot;IKTrading&quot;, username = &quot;IlyaKipnis&quot;)
  library(IKTrading)
}
library(quantmod)

start &lt;- as.Date(&quot;2010-01-01&quot;)
end &lt;- as.Date(&quot;2016-10-01&quot;)

# Let's get Apple stock data; Apple's ticker symbol is AAPL. We use the quantmod function getSymbols, and pass a string as a first argument to identify the desired ticker symbol, pass &quot;yahoo&quot; to src for Yahoo! Finance, and from and to specify date ranges

# The default behavior for getSymbols is to load data directly into the global environment, with the object being named after the loaded ticker symbol. This feature may become deprecated in the future, but we exploit it now.

getSymbols(&quot;AAPL&quot;, src=&quot;yahoo&quot;, from = start, to = end)
</code></pre>
<pre>## [1] &quot;AAPL&quot;
</pre>
<pre><code># What is AAPL?
class(AAPL)
</code></pre>
<pre>## [1] &quot;xts&quot; &quot;zoo&quot;
</pre>
<pre><code># Let's see the first few rows
head(AAPL)
</code></pre>
<pre>##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2010-01-04    213.43    214.50   212.38     214.01   123432400
## 2010-01-05    214.60    215.59   213.25     214.38   150476200
## 2010-01-06    214.38    215.23   210.75     210.97   138040000
## 2010-01-07    211.75    212.00   209.05     210.58   119282800
## 2010-01-08    210.30    212.00   209.06     211.98   111902700
## 2010-01-11    212.80    213.00   208.45     210.11   115557400
##            AAPL.Adjusted
## 2010-01-04      27.72704
## 2010-01-05      27.77498
## 2010-01-06      27.33318
## 2010-01-07      27.28265
## 2010-01-08      27.46403
## 2010-01-11      27.22176
</pre>
<pre><code>candleChart(AAPL, up.col = &quot;black&quot;, dn.col = &quot;red&quot;, theme = &quot;white&quot;, subset = &quot;2016-01-04/&quot;)

AAPL_sma_20 &lt;- SMA(
  Cl(AAPL),  # The closing price of AAPL, obtained by quantmod's Cl() function
  n = 20     # The number of days in the moving average window
  )

AAPL_sma_50 &lt;- SMA(
  Cl(AAPL),
  n = 50
  )

AAPL_sma_200 &lt;- SMA(
  Cl(AAPL),
  n = 200
  )

zoomChart(&quot;2016&quot;)  # Zoom into the year 2016 in the chart
addTA(AAPL_sma_20, on = 1, col = &quot;red&quot;)  # on = 1 plots the SMA with price
addTA(AAPL_sma_50, on = 1, col = &quot;blue&quot;)
addTA(AAPL_sma_200, on = 1, col = &quot;green&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-1-1.png?w=456" data-recalc-dims="1"></p>
<p>We will refer to the sign of this difference as the <strong>regime</strong>; that is, if the fast moving average is above the slow moving average, this is a bullish regime (the bulls rule), and a bearish regime (the bears rule) holds when the fast moving average is below the slow moving average. I identify regimes with the following code.</p>
<pre><code>AAPL_trade &lt;- AAPL
AAPL_trade$`20d` &lt;- AAPL_sma_20
AAPL_trade$`50d` &lt;- AAPL_sma_50

regime_val &lt;- sigComparison(&quot;&quot;, data = AAPL_trade,
                            columns = c(&quot;20d&quot;, &quot;50d&quot;), relationship = &quot;gt&quot;) -
              sigComparison(&quot;&quot;, data = AAPL_trade,
                            columns = c(&quot;20d&quot;, &quot;50d&quot;), relationship = &quot;lt&quot;)

plot(regime_val[&quot;2016&quot;], main = &quot;Regime&quot;, ylim = c(-2, 2))
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<pre><code>plot(regime_val, main = &quot;Regime&quot;, ylim = c(-2, 2))
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>I could visualize the regime along with the main series with the following code:</p>
<pre><code>candleChart(AAPL, up.col = &quot;black&quot;, dn.col = &quot;red&quot;, theme = &quot;white&quot;, subset = &quot;2016-01-04/&quot;)
addTA(regime_val, col = &quot;blue&quot;, yrange = c(-2, 2))
addLines(h = 0, col = &quot;black&quot;, on = 3)
addSMA(n = c(20, 50), on = 1, col = c(&quot;red&quot;, &quot;blue&quot;))
zoomChart(&quot;2016&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<pre><code>candleChart(AAPL, up.col = &quot;black&quot;, dn.col = &quot;red&quot;, theme = &quot;white&quot;, subset = &quot;2016-01-04/&quot;)
addTA(regime_val, col = &quot;blue&quot;, yrange = c(-2, 2))
addLines(h = 0, col = &quot;black&quot;, on = 3)
addSMA(n = c(20, 50), on = 1, col = c(&quot;red&quot;, &quot;blue&quot;))
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-5-4.png?w=456" data-recalc-dims="1"></p>
<pre><code>table(as.vector(regime_val))
</code></pre>
<pre>## 
##  -1   1 
## 663 987
</pre>
<p>The call above indicates that the market was bullish on Apple stock for 987 days, and bearish for 663 days. (If you were following along on the Python version, you may notice there is no 0. This may be due to different behavior by <code>sigComparison()</code>.)</p>
<p>Trading signals appear at regime changes. When a bullish regime begins, a buy signal is triggered, and when it ends, a sell signal is triggered. Likewise, when a bearish regime begins, a sell signal is triggered, and when the regime ends, a buy signal is triggered (this is of interest only if you ever will short the stock, or use some derivative like a stock option to bet against the market).</p>
<p>It’s simple to obtain signals. Let <img title="r_t" alt="r_t" class="latex" src="https://s0.wp.com/latex.php?latex=r_t&bg=ffffff&%23038;fg=444444&%23038;s=0"> indicate the regime at time <img title="t" alt="t" class="latex" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0">, and <img title="s_t" alt="s_t" class="latex" src="https://s0.wp.com/latex.php?latex=s_t&bg=ffffff&%23038;fg=444444&%23038;s=0"> the signal at time <img title="t" alt="t" class="latex" src="https://s0.wp.com/latex.php?latex=t&bg=ffffff&%23038;fg=444444&%23038;s=0">. Then:</p>
<p><img title="s_t = \text{sign}(r_t - r_{t - 1})" alt="s_t = \text{sign}(r_t - r_{t - 1})" class="latex" src="https://s0.wp.com/latex.php?latex=s_t+%3D+%5Ctext%7Bsign%7D%28r_t+-+r_%7Bt+-+1%7D%29&bg=ffffff&%23038;fg=444444&%23038;s=0"></p>
<p><img title="s_t \in \{-1, 0, 1\}" alt="s_t \in \{-1, 0, 1\}" class="latex" src="https://s0.wp.com/latex.php?latex=s_t+%5Cin+%5C%7B-1%2C+0%2C+1%5C%7D&bg=ffffff&%23038;fg=444444&%23038;s=0">, with <img title="-1" alt="-1" class="latex" src="https://s0.wp.com/latex.php?latex=-1&bg=ffffff&%23038;fg=444444&%23038;s=0"> indicating “sell”, <img title="1" alt="1" class="latex" src="https://s0.wp.com/latex.php?latex=1&bg=ffffff&%23038;fg=444444&%23038;s=0"> indicating “buy”, and <img title="0" alt="0" class="latex" src="https://s0.wp.com/latex.php?latex=0&bg=ffffff&%23038;fg=444444&%23038;s=0"> no action. We can obtain signals like so:</p>
<pre><code>sig &lt;- diff(regime_val) / 2
plot(sig, main = &quot;Signal&quot;, ylim = c(-2, 2))
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-7-1.png?w=456" data-recalc-dims="1"></p>
<pre><code>table(sig)
</code></pre>
<pre>## sig
##   -1    0    1 
##   19 1611   19
</pre>
<p>We would buy Apple stock 19 times and sell Apple stock 19 times. If we only go long on Apple stock, only 19 trades will be engaged in over the 6-year period, while if we pivot from a long to a short position every time a long position is terminated, we would engage in 19 trades total. (Bear in mind that trading more frequently isn’t necessarily good; trades are never free.)</p>
<p>You may notice that the system as it currently stands isn’t very robust, since even a fleeting moment when the fast moving average is above the slow moving average triggers a trade, resulting in trades that end immediately (which is bad if not simply because realistically every trade is accompanied by a fee that can quickly erode earnings). Additionally, every bullish regime immediately transitions into a bearish regime, and if you were constructing trading systems that allow both bullish and bearish bets, this would lead to the end of one trade immediately triggering a new trade that bets on the market in the opposite direction, which again seems finnicky. A better system would require more evidence that the market is moving in some particular direction. But we will not concern ourselves with these details for now.</p>
<p>Let’s now try to identify what the prices of the stock is at every buy and every sell.</p>
<pre><code># The Cl function from quantmod pulls the closing price from the object
# holding a stock's data
# Buy prices
Cl(AAPL)[which(sig == 1)]
</code></pre>
<pre>##            AAPL.Close
## 2010-06-18     274.07
## 2010-09-20     283.23
## 2011-05-12     346.57
## 2011-07-14     357.77
## 2011-12-28     402.64
## 2012-06-25     570.77
## 2013-05-17     433.26
## 2013-07-31     452.53
## 2013-10-16     501.11
## 2014-03-26     539.78
## 2014-04-25     571.94
## 2014-08-18      99.16
## 2014-10-28     106.74
## 2015-02-05     119.94
## 2015-04-28     130.56
## 2015-10-27     114.55
## 2016-03-11     102.26
## 2016-07-01      95.89
## 2016-07-25      97.34
</pre>
<pre><code># Sell prices
Cl(AAPL)[sig == -1]
</code></pre>
<pre>##            AAPL.Close
## 2010-06-11     253.51
## 2010-07-22     259.02
## 2011-03-31     348.51
## 2011-05-27     337.41
## 2011-11-17     377.41
## 2012-05-09     569.18
## 2012-10-17     644.61
## 2013-06-26     398.07
## 2013-10-03     483.41
## 2014-01-28     506.50
## 2014-04-22     531.70
## 2014-06-11      93.86
## 2014-10-17      97.67
## 2015-01-05     106.25
## 2015-04-16     126.17
## 2015-06-25     127.50
## 2015-12-18     106.03
## 2016-05-05      93.24
## 2016-07-08      96.68
</pre>
<pre><code># Since these are of the same dimension, computing profit is easy
as.vector(Cl(AAPL)[sig == 1])[-1] - Cl(AAPL)[sig == -1][-table(sig)[[&quot;1&quot;]]]
</code></pre>
<pre>##             AAPL.Close
## 2010-06-11   29.720012
## 2010-07-22   87.549988
## 2011-03-31    9.259998
## 2011-05-27   65.230011
## 2011-11-17  193.360020
## 2012-05-09 -135.920013
## 2012-10-17 -192.080017
## 2013-06-26  103.040009
## 2013-10-03   56.369995
## 2014-01-28   65.440003
## 2014-04-22 -432.540016
## 2014-06-11   12.879997
## 2014-10-17   22.270004
## 2015-01-05   24.309998
## 2015-04-16  -11.619995
## 2015-06-25  -25.239998
## 2015-12-18  -10.140000
## 2016-05-05    4.099998
</pre>
<p>Above, we can see that on May 22nd, 2013, there was a massive drop in the price of Apple stock, and it looks like our trading system would do badly. But this price drop is not because of a massive shock to Apple, but simply due to a stock split. And while dividend payments are not as obvious as a stock split, they may be affecting the performance of our system.</p>
<pre><code>candleChart(AAPL, up.col = &quot;black&quot;, dn.col = &quot;red&quot;, theme = &quot;white&quot;)
addTA(regime_val, col = &quot;blue&quot;, yrange = c(-2, 2))
addLines(h = 0, col = &quot;black&quot;, on = 3)
addSMA(n = c(20, 50), on = 1, col = c(&quot;red&quot;, &quot;blue&quot;))
zoomChart(&quot;2014-05/2014-07&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-12-5.png?w=456" data-recalc-dims="1"></p>
<p>We don’t want our trading system to be behaving poorly because of stock splits and dividend payments. How should we handle this? One approach would be to obtain historical stock split and dividend payment data and design a trading system for handling these. This would most realistically represent the behavior of the stock and could be considered the best solution, but it is more complicated. Another solution would be to adjust the prices to account for stock splits and dividend payments.</p>
<p>Yahoo! Finance only provides the adjusted closing price of a stock, but this is all we need to get adjusted opening, high, and low prices. The adjusted close is computed like so:</p>
<p><img title="\text{price}^{\text{adj}}_t = m_t \times \text{price}_t" alt="\text{price}^{\text{adj}}_t = m_t \times \text{price}_t" class="latex" src="https://s0.wp.com/latex.php?latex=%5Ctext%7Bprice%7D%5E%7B%5Ctext%7Badj%7D%7D_t+%3D+m_t+%5Ctimes+%5Ctext%7Bprice%7D_t&bg=ffffff&%23038;fg=444444&%23038;s=0"></p>
<p>where <img title="m_t" alt="m_t" class="latex" src="https://s0.wp.com/latex.php?latex=m_t&bg=ffffff&%23038;fg=444444&%23038;s=0"> is the multiplier used for the adjustment. Solving for <img title="m_t" alt="m_t" class="latex" src="https://s0.wp.com/latex.php?latex=m_t&bg=ffffff&%23038;fg=444444&%23038;s=0"> requires only division and thus we can use the closing price and the adjusted closing price to adjust all prices in the series.</p>
<p>Let’s go back, adjust the apple data, and reevaluate our trading system using the adjusted data.</p>
<pre><code>candleChart(adjustOHLC(AAPL), up.col = &quot;black&quot;, dn.col = &quot;red&quot;, theme = &quot;white&quot;)
addLines(h = 0, col = &quot;black&quot;, on = 3)
addSMA(n = c(20, 50), on = 1, col = c(&quot;red&quot;, &quot;blue&quot;))
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-13-2.png?w=456" data-recalc-dims="1"></p>
<p>As you can see, adjusting for dividends and stock splits makes a big difference. We will use this data from now on.</p>
<p>Let’s now create a simulated portfolio of $1,000,000, and see how it would behave, according to the rules we have established. This includes:</p>
<ul>
<li>Investing only 10% of the portfolio in any trade</li>
<li>Exiting the position if losses exceed 20% of the value of the trade.</li>
</ul>
<p>When simulating, bear in mind that:</p>
<ul>
<li>Trades are done in batches of 100 stocks (unfortunately, <code>osMaxDollar()</code> will not round to batches of 100, so for the sake of simplicity, this will be ignored).</li>
<li>Our stop-loss rule involves placing an order to sell the stock the moment the price drops below the specified level. Thus we need to check whether the lows during this period ever go low enough to trigger the stop-loss. Realistically, unless we buy a put option, we cannot guarantee that we will sell the stock at the price we set at the stop-loss, but we will use this as the selling price anyway for the sake of simplicity.</li>
<li>Every trade is accompanied by a commission to the broker, which should be accounted for. I do not do so here.</li>
</ul>
<p><strong>quantmod</strong> is good for visualizing stock data, but if we want to start developing and testing strategies, we will need to rely more on other packages:</p>
<ul>
<li><strong>TTR</strong> contains functions for computing technical indicators (simple moving averages, or SMAs, are included).</li>
<li><strong>blotter</strong> is intended to manage portfolios and positions created while developing trading strategies. It provides environments intended to help simplify portfolio tracking tasks.</li>
<li><strong>FinancialInstruments</strong> is a companion package to <strong>blotter</strong> and stores meta-data about different financial instruments (stocks, options contracts, etc.).</li>
<li><strong>quantstrat</strong> may be the work horse package here; it is used for defining and testing trading strategies.</li>
<li><strong>IKTrading</strong> contains some additional useful functions for trading (in particular, <code>osMaxDollar()</code>).</li>
</ul>
<p><strong>quantstrat</strong> and its companion packages provides a flexible framework for backtesting that, in principle, should allow for any trading strategy to be backtested. The following code must be executed for every testing session.</p>
<p>For the sake of simplicity, I’m going to not force orders to be in batches of 100 shares, nor will I enforce the stop-loss rule mentioned above. It is possible to implement these rules, but doing so is not simple. Perhaps in a later article I will revisit these topics. Additionally, I am allowing the strategy to further enter a position in “bullish” markets. I would not recommend this in the real world, since this will drive up transaction costs for little profit. (Rule of thumb; the fewer transactions, the better.)</p>
<pre><code>rm(list = ls(.blotter), envir = .blotter)  # Clear blotter environment
currency(&quot;USD&quot;)  # Currency being used
</code></pre>
<pre>## [1] &quot;USD&quot;
</pre>
<pre><code>Sys.setenv(TZ = &quot;MDT&quot;)  # Allows quantstrat to use timestamps
</code></pre>
<p>Next, we declare the stocks we are going to be working with.</p>
<pre><code>AAPL_adj &lt;- adjustOHLC(AAPL)
stock(&quot;AAPL_adj&quot;, currency = &quot;USD&quot;, multiplier = 1)
</code></pre>
<pre>## [1] &quot;AAPL_adj&quot;
</pre>
<pre><code>initDate &lt;- &quot;1990-01-01&quot;  # A date prior to first close price; needed (why?)
</code></pre>
<p>Now we create a portfolio and account object that will be used in the simulation.</p>
<pre><code>strategy_st &lt;- portfolio_st &lt;- account_st &lt;- &quot;SMAC-20-50&quot;  # Names of objects
rm.strat(portfolio_st)  # Need to remove portfolio from blotter env
rm.strat(strategy_st)   # Ensure no strategy by this name exists either
initPortf(portfolio_st, symbols = &quot;AAPL_adj&quot;,  # This is a simple portfolio
                                               # trading AAPL only
          initDate = initDate, currency = &quot;USD&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>initAcct(account_st, portfolios = portfolio_st,  # Uses only one portfolio
         initDate = initDate, currency = &quot;USD&quot;,
         initEq = 1000000)  # Start with a million dollars
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>initOrders(portfolio_st, store = TRUE)  # Initialize the order container; will
                                        # contain all orders made by strategy
</code></pre>
<p>Now we define the strategy.</p>
<pre><code>strategy(strategy_st, store = TRUE)  # store = TRUE tells function to store in
                                     # the .strategy environment

# Now define trading rules
# Indicators are used to construct signals
add.indicator(strategy = strategy_st, name = &quot;SMA&quot;,     # SMA is a function
              arguments = list(x = quote(Cl(mktdata)),  # args of SMA
                               n = 20),
              label = &quot;fastMA&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>add.indicator(strategy = strategy_st, name = &quot;SMA&quot;,
              arguments = list(x = quote(Cl(mktdata)),
                               n = 50),
              label = &quot;slowMA&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code># Next comes trading signals
add.signal(strategy = strategy_st, name = &quot;sigComparison&quot;,  # Remember me?
           arguments = list(columns = c(&quot;fastMA&quot;, &quot;slowMA&quot;),
                            relationship = &quot;gt&quot;),
           label = &quot;bull&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>add.signal(strategy = strategy_st, name = &quot;sigComparison&quot;,
           arguments = list(columns = c(&quot;fastMA&quot;, &quot;slowMA&quot;),
                            relationship = &quot;lt&quot;),
           label = &quot;bear&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code># Finally, rules that generate trades
add.rule(strategy = strategy_st, name = &quot;ruleSignal&quot;,  # Almost always this one
         arguments = list(sigcol = &quot;bull&quot;,  # Signal (see above) that triggers
                          sigval = TRUE,
                          ordertype = &quot;market&quot;,
                          orderside = &quot;long&quot;,
                          replace = FALSE,
                          prefer = &quot;Open&quot;,
                          osFUN = osMaxDollar,
                          # The next parameter, which is a parameter passed to
                          # osMaxDollar, will ensure that trades are about 10%
                          # of portfolio equity
                          maxSize = quote(floor(getEndEq(account_st,
                                                   Date = timestamp) * .1)),
                          tradeSize = quote(floor(getEndEq(account_st,
                                                   Date = timestamp) * .1))),
         type = &quot;enter&quot;, path.dep = TRUE, label = &quot;buy&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>add.rule(strategy = strategy_st, name = &quot;ruleSignal&quot;,
         arguments = list(sigcol = &quot;bear&quot;,
                          sigval = TRUE,
                          orderqty = &quot;all&quot;,
                          ordertype = &quot;market&quot;,
                          orderside = &quot;long&quot;,
                          replace = FALSE,
                          prefer = &quot;Open&quot;),
         type = &quot;exit&quot;, path.dep = TRUE, label = &quot;sell&quot;)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<p>With the strategy set up, we now execute it. (I’m allowing the function’s intermediate output to be included; I believe, in this case, it’s nice to see what is going on.)</p>
<pre><code>applyStrategy(strategy_st, portfolios = portfolio_st)
</code></pre>
<pre>## [1] &quot;2010-03-17 00:00:00 AAPL_adj 3410 @ 29.4145214710173&quot;
## [1] &quot;2010-03-19 00:00:00 AAPL_adj 1 @ 29.4001360824518&quot;
## [1] &quot;2010-03-23 00:00:00 AAPL_adj 56 @ 29.5113078050153&quot;
## [1] &quot;2010-06-14 00:00:00 AAPL_adj -3467 @ 33.4768405519892&quot;
## [1] &quot;2010-06-21 00:00:00 AAPL_adj 2808 @ 36.3188897412688&quot;
## [1] &quot;2010-06-23 00:00:00 AAPL_adj 1 @ 35.9121377755245&quot;
## [1] &quot;2010-06-25 00:00:00 AAPL_adj 12 @ 35.3209704881171&quot;
## [1] &quot;2010-06-28 00:00:00 AAPL_adj 10 @ 34.9115992042882&quot;
## [1] &quot;2010-06-29 00:00:00 AAPL_adj 33 @ 34.5440814243432&quot;
## [1] &quot;2010-06-30 00:00:00 AAPL_adj 30 @ 33.5749295477998&quot;
## [1] &quot;2010-07-01 00:00:00 AAPL_adj 84 @ 33.2597286804071&quot;
## [1] &quot;2010-07-02 00:00:00 AAPL_adj 28 @ 32.761422084999&quot;
## [1] &quot;2010-07-06 00:00:00 AAPL_adj 46 @ 32.8281245169061&quot;
## [1] &quot;2010-07-15 00:00:00 AAPL_adj 13 @ 32.4658384412953&quot;
## [1] &quot;2010-07-16 00:00:00 AAPL_adj 15 @ 33.1132447519415&quot;
## [1] &quot;2010-07-21 00:00:00 AAPL_adj 67 @ 34.6709448593864&quot;
## [1] &quot;2010-07-23 00:00:00 AAPL_adj -3147 @ 33.6246305427903&quot;
## [1] &quot;2010-09-21 00:00:00 AAPL_adj 2769 @ 37.1258627071335&quot;
## [1] &quot;2011-04-01 00:00:00 AAPL_adj -2769 @ 45.9214462525203&quot;
## [1] &quot;2011-05-13 00:00:00 AAPL_adj 2209 @ 45.2086437030919&quot;
## [1] &quot;2011-05-16 00:00:00 AAPL_adj 2 @ 44.3637427445526&quot;
## [1] &quot;2011-05-17 00:00:00 AAPL_adj 43 @ 43.4220589833276&quot;
## [1] &quot;2011-05-18 00:00:00 AAPL_adj 48 @ 44.006688373276&quot;
## [1] &quot;2011-05-24 00:00:00 AAPL_adj 15 @ 43.8798216684994&quot;
## [1] &quot;2011-05-31 00:00:00 AAPL_adj -2317 @ 44.6122447113503&quot;
## [1] &quot;2011-07-15 00:00:00 AAPL_adj 2117 @ 47.2371856911493&quot;
## [1] &quot;2011-08-24 00:00:00 AAPL_adj 5 @ 48.8458929867096&quot;
## [1] &quot;2011-11-18 00:00:00 AAPL_adj -2122 @ 49.5586954053487&quot;
## [1] &quot;2011-12-29 00:00:00 AAPL_adj 1879 @ 52.7604184147789&quot;
## [1] &quot;2011-12-30 00:00:00 AAPL_adj 16 @ 52.7748073346566&quot;
## [1] &quot;2012-05-10 00:00:00 AAPL_adj -1895 @ 75.1489364843129&quot;
## [1] &quot;2012-06-26 00:00:00 AAPL_adj 1324 @ 74.7238700874815&quot;
## [1] &quot;2012-06-27 00:00:00 AAPL_adj 14 @ 75.2038727149497&quot;
## [1] &quot;2012-10-18 00:00:00 AAPL_adj -1338 @ 84.0107133628424&quot;
## [1] &quot;2013-05-20 00:00:00 AAPL_adj 1704 @ 57.702437434572&quot;
## [1] &quot;2013-05-21 00:00:00 AAPL_adj 29 @ 58.5360921156059&quot;
## [1] &quot;2013-06-18 00:00:00 AAPL_adj 1 @ 57.6556788337951&quot;
## [1] &quot;2013-06-20 00:00:00 AAPL_adj 1 @ 56.0177655048158&quot;
## [1] &quot;2013-06-21 00:00:00 AAPL_adj 50 @ 55.9095500863203&quot;
## [1] &quot;2013-06-24 00:00:00 AAPL_adj 3 @ 54.4279450227602&quot;
## [1] &quot;2013-06-25 00:00:00 AAPL_adj 49 @ 54.2008263223713&quot;
## [1] &quot;2013-06-26 00:00:00 AAPL_adj 7 @ 53.9603509990938&quot;
## [1] &quot;2013-06-27 00:00:00 AAPL_adj -1844 @ 53.3391172023021&quot;
## [1] &quot;2013-08-01 00:00:00 AAPL_adj 1645 @ 60.8874187232282&quot;
## [1] &quot;2013-09-18 00:00:00 AAPL_adj 14 @ 62.288630508577&quot;
## [1] &quot;2013-10-07 00:00:00 AAPL_adj -1659 @ 65.4327788398408&quot;
## [1] &quot;2013-10-17 00:00:00 AAPL_adj 1484 @ 67.2375075223774&quot;
## [1] &quot;2013-10-18 00:00:00 AAPL_adj 3 @ 68.0457369974481&quot;
## [1] &quot;2014-01-29 00:00:00 AAPL_adj -1487 @ 68.1670731190817&quot;
## [1] &quot;2014-03-12 00:00:00 AAPL_adj 1372 @ 72.733561996219&quot;
## [1] &quot;2014-03-13 00:00:00 AAPL_adj 2 @ 73.1322624931313&quot;
## [1] &quot;2014-03-17 00:00:00 AAPL_adj 15 @ 71.8068850638596&quot;
## [1] &quot;2014-03-18 00:00:00 AAPL_adj -1389 @ 71.5619513215039&quot;
## [1] &quot;2014-03-25 00:00:00 AAPL_adj 1364 @ 73.6847222604636&quot;
## [1] &quot;2014-03-31 00:00:00 AAPL_adj 1 @ 73.37583725238&quot;
## [1] &quot;2014-04-02 00:00:00 AAPL_adj 1 @ 73.8044711654273&quot;
## [1] &quot;2014-04-08 00:00:00 AAPL_adj 25 @ 71.4653411892903&quot;
## [1] &quot;2014-04-09 00:00:00 AAPL_adj 8 @ 71.1183468222455&quot;
## [1] &quot;2014-04-10 00:00:00 AAPL_adj 7 @ 72.2123947642041&quot;
## [1] &quot;2014-04-14 00:00:00 AAPL_adj 9 @ 71.0176525287248&quot;
## [1] &quot;2014-04-17 00:00:00 AAPL_adj 3 @ 70.7591073193403&quot;
## [1] &quot;2014-04-23 00:00:00 AAPL_adj -1418 @ 71.9919515657196&quot;
## [1] &quot;2014-04-28 00:00:00 AAPL_adj 1301 @ 77.943882953473&quot;
## [1] &quot;2014-10-20 00:00:00 AAPL_adj -1301 @ 94.6439193845976&quot;
## [1] &quot;2014-10-29 00:00:00 AAPL_adj 985 @ 102.662471436688&quot;
## [1] &quot;2015-01-06 00:00:00 AAPL_adj -985 @ 103.001288430376&quot;
## [1] &quot;2015-02-06 00:00:00 AAPL_adj 858 @ 116.491485509158&quot;
## [1] &quot;2015-02-10 00:00:00 AAPL_adj 11 @ 116.637076575269&quot;
## [1] &quot;2015-04-17 00:00:00 AAPL_adj -869 @ 121.858912853907&quot;
## [1] &quot;2015-04-29 00:00:00 AAPL_adj 766 @ 126.333382759857&quot;
## [1] &quot;2015-04-30 00:00:00 AAPL_adj 25 @ 124.858064939017&quot;
## [1] &quot;2015-05-01 00:00:00 AAPL_adj 9 @ 122.392738351109&quot;
## [1] &quot;2015-05-04 00:00:00 AAPL_adj 17 @ 125.69278245721&quot;
## [1] &quot;2015-05-08 00:00:00 AAPL_adj 5 @ 123.469279769953&quot;
## [1] &quot;2015-06-29 00:00:00 AAPL_adj -822 @ 122.280199845824&quot;
## [1] &quot;2015-10-28 00:00:00 AAPL_adj 885 @ 114.482259314017&quot;
## [1] &quot;2015-11-17 00:00:00 AAPL_adj 28 @ 112.995955565041&quot;
## [1] &quot;2015-12-17 00:00:00 AAPL_adj 2 @ 110.144507689672&quot;
## [1] &quot;2015-12-21 00:00:00 AAPL_adj -915 @ 105.483868873907&quot;
## [1] &quot;2016-03-11 00:00:00 AAPL_adj 997 @ 101.073743853996&quot;
## [1] &quot;2016-04-28 00:00:00 AAPL_adj 56 @ 96.496561342483&quot;
## [1] &quot;2016-05-02 00:00:00 AAPL_adj 23 @ 92.8980829110911&quot;
## [1] &quot;2016-05-06 00:00:00 AAPL_adj -1076 @ 92.8669223571517&quot;
## [1] &quot;2016-06-24 00:00:00 AAPL_adj 1047 @ 92.4094018468721&quot;
## [1] &quot;2016-06-27 00:00:00 AAPL_adj 35 @ 92.4989129454683&quot;
## [1] &quot;2016-06-28 00:00:00 AAPL_adj -1082 @ 92.3994537379766&quot;
## [1] &quot;2016-07-01 00:00:00 AAPL_adj 1064 @ 94.9754947544617&quot;
## [1] &quot;2016-07-12 00:00:00 AAPL_adj -1064 @ 96.6464428592831&quot;
## [1] &quot;2016-07-26 00:00:00 AAPL_adj 1023 @ 96.2983306600025&quot;
## [1] &quot;2016-07-27 00:00:00 AAPL_adj 15 @ 103.708186831476&quot;
</pre>
<p>Here we effectvely conclude the study so we can perform some analytics.</p>
<pre><code>updatePortf(portfolio_st)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>dateRange &lt;- time(getPortfolio(portfolio_st)$summary)[-1]
updateAcct(portfolio_st, dateRange)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<pre><code>updateEndEq(account_st)
</code></pre>
<pre>## [1] &quot;SMAC-20-50&quot;
</pre>
<p>Here’s our first set of useful summary statistics.</p>
<pre><code>tStats &lt;- tradeStats(Portfolios = portfolio_st, use=&quot;trades&quot;,
                     inclZeroDays = FALSE)
tStats[, 4:ncol(tStats)] &lt;- round(tStats[, 4:ncol(tStats)], 2)
print(data.frame(t(tStats[, -c(1,2)])))
</code></pre>
<pre>##                     AAPL_adj
## Num.Txns               90.00
## Num.Trades             21.00
## Net.Trading.PL     112580.62
## Avg.Trade.PL         5360.98
## Med.Trade.PL         1379.84
## Largest.Winner      42426.01
## Largest.Loser       -8386.18
## Gross.Profits      152876.04
## Gross.Losses       -40295.42
## Std.Dev.Trade.PL    12835.99
## Percent.Positive       57.14
## Percent.Negative       42.86
## Profit.Factor           3.79
## Avg.Win.Trade       12739.67
## Med.Win.Trade        9970.11
## Avg.Losing.Trade    -4477.27
## Med.Losing.Trade    -3234.16
## Avg.Daily.PL         4765.18
## Med.Daily.PL          856.79
## Std.Dev.Daily.PL    12868.07
## Ann.Sharpe              5.88
## Max.Drawdown       -27945.73
## Profit.To.Max.Draw      4.03
## Avg.WinLoss.Ratio       2.85
## Med.WinLoss.Ratio       3.08
## Max.Equity         120545.86
## Min.Equity          -1182.21
## End.Equity         112580.62
</pre>
<pre><code>final_acct &lt;- getAccount(account_st)
plot(final_acct$summary$End.Eq[&quot;2010/2016&quot;], main = &quot;Portfolio Equity&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-21-1.png?w=456" data-recalc-dims="1"></p>
<p>Our portfolio’s value grew by 10% in about six years. Considering that only 10% of the portfolio was ever involved in any single trade, this is not bad performance.</p>
<p>A more realistic portfolio would not be betting 10% of its value on only one stock. A more realistic one would consider investing in multiple stocks. Multiple trades may be ongoing at any given time involving multiple companies, and most of the portfolio will be in stocks, not cash.</p>
<p>Fortunately, <strong>quantmod</strong> allows us to do this.</p>
<pre><code># Now for analytics
updatePortf(portfolio_st_2)
</code></pre>
<pre>## [1] &quot;SMAC-20-50_v2&quot;
</pre>
<pre><code>dateRange &lt;- time(getPortfolio(portfolio_st_2)$summary)[-1]
updateAcct(account_st_2, dateRange)
</code></pre>
<pre>## [1] &quot;SMAC-20-50_v2&quot;
</pre>
<pre><code>updateEndEq(account_st_2)
</code></pre>
<pre>## [1] &quot;SMAC-20-50_v2&quot;
</pre>
<pre><code>tStats2 &lt;- tradeStats(Portfolios = portfolio_st_2, use=&quot;trades&quot;,
                     inclZeroDays = FALSE)
tStats2[, 4:ncol(tStats2)] &lt;- round(tStats2[, 4:ncol(tStats2)], 2)
print(data.frame(t(tStats2[, -c(1,2)])))
</code></pre>
<pre>##                     AAPL_adj  AMZN_adj    FB_adj  GOOG_adj   HPQ_adj
## Num.Txns               90.00    100.00     52.00     82.00    101.00
## Num.Trades             21.00     18.00     12.00     18.00     16.00
## Net.Trading.PL     112580.62 131136.44 123027.65  34936.13  47817.45
## Avg.Trade.PL         5360.98   7285.36  10252.30   1940.90   2988.59
## Med.Trade.PL         1379.84   2734.07   4946.80  -1110.80  -2937.26
## Largest.Winner      42426.01  39293.00  78430.09  31629.27  55409.83
## Largest.Loser       -8386.18 -17567.20 -18374.18 -10477.41 -16000.11
## Gross.Profits      152876.04 190370.65 148742.50  78661.57 129111.69
## Gross.Losses       -40295.42 -59234.21 -25714.85 -43725.44 -81294.24
## Std.Dev.Trade.PL    12835.99  18910.41  23637.58   9807.28  20596.60
## Percent.Positive       57.14     61.11     75.00     38.89     37.50
## Percent.Negative       42.86     38.89     25.00     61.11     62.50
## Profit.Factor           3.79      3.21      5.78      1.80      1.59
## Avg.Win.Trade       12739.67  17306.42  16526.94  11237.37  21518.62
## Med.Win.Trade        9970.11  10904.54   9600.62  10363.86  12287.22
## Avg.Losing.Trade    -4477.27  -8462.03  -8571.62  -3975.04  -8129.42
## Med.Losing.Trade    -3234.16  -7586.30  -3885.64  -4277.02  -8975.12
## Avg.Daily.PL         4765.18   4733.02  10715.22   1745.28   1887.01
## Med.Daily.PL          856.79   2358.72   4733.39  -1206.41  -4857.62
## Std.Dev.Daily.PL    12868.07  15980.17  24734.19  10072.85  20825.92
## Ann.Sharpe              5.88      4.70      6.88      2.75      1.44
## Max.Drawdown       -27945.73 -50062.96 -44728.34 -31255.17 -67890.79
## Profit.To.Max.Draw      4.03      2.62      2.75      1.12      0.70
## Avg.WinLoss.Ratio       2.85      2.05      1.93      2.83      2.65
## Med.WinLoss.Ratio       3.08      1.44      2.47      2.42      1.37
## Max.Equity         120545.86 131136.44 127540.04  48651.04  47817.45
## Min.Equity          -1182.21 -11314.07 -15750.32 -23388.32 -63845.70
## End.Equity         112580.62 131136.44 123027.65  34936.13  47817.45
##                      IBM_adj  MSFT_adj  NFLX_adj NTDOY_adj   SNY_adj
## Num.Txns              115.00    112.00     96.00    109.00    121.00
## Num.Trades             21.00     20.00     18.00     20.00     22.00
## Net.Trading.PL       9126.76  29513.06 306152.14  17217.18 -26278.53
## Avg.Trade.PL          434.61   1475.65  17008.45    860.86  -1194.48
## Med.Trade.PL         -722.94  -2408.39   3954.98  -1467.95    474.75
## Largest.Winner      22193.38  18573.05 177643.74  45165.04  15195.77
## Largest.Loser       -8188.45 -11537.97 -30183.02  -9052.12 -17715.75
## Gross.Profits       50691.47  91471.86 372517.68  85502.47  64843.56
## Gross.Losses       -41564.71 -61958.80 -66365.54 -68285.29 -91122.09
## Std.Dev.Trade.PL     6571.91   8946.29  44740.88  12590.03   8855.56
## Percent.Positive       38.10     45.00     66.67     40.00     54.55
## Percent.Negative       61.90     55.00     33.33     60.00     45.45
## Profit.Factor           1.22      1.48      5.61      1.25      0.71
## Avg.Win.Trade        6336.43  10163.54  31043.14  10687.81   5403.63
## Med.Win.Trade        3593.06  10044.61  14941.58   2718.22   3385.61
## Avg.Losing.Trade    -3197.29  -5632.62 -11060.92  -5690.44  -9112.21
## Med.Losing.Trade    -2368.90  -5111.92  -7694.86  -6391.76  -8601.77
## Avg.Daily.PL          434.61   1160.50  17854.27    -77.22  -1194.48
## Med.Daily.PL         -722.94  -2870.77   5280.37  -1587.30    474.75
## Std.Dev.Daily.PL     6571.91   9076.67  45969.27  12195.79   8855.56
## Ann.Sharpe              1.05      2.03      6.17     -0.10     -2.14
## Max.Drawdown       -42354.92 -32484.71 -57707.47 -55006.73 -38592.68
## Profit.To.Max.Draw      0.22      0.91      5.31      0.31     -0.68
## Avg.WinLoss.Ratio       1.98      1.80      2.81      1.88      0.59
## Med.WinLoss.Ratio       1.52      1.96      1.94      0.43      0.39
## Max.Equity          31766.86  44009.02 340579.29  35295.68  12237.77
## Min.Equity         -10588.07 -18649.24   -646.98 -39765.29 -34367.59
## End.Equity           9126.76  29513.06 306152.14  17217.18 -26278.53
##                     TWTR_adj  YHOO_adj
## Num.Txns               31.00    108.00
## Num.Trades              6.00     22.00
## Net.Trading.PL      17321.55 110711.07
## Avg.Trade.PL         2886.93   5032.32
## Med.Trade.PL        -3051.02  -1332.22
## Largest.Winner       3058.35  55850.27
## Largest.Loser      -11305.68  -8359.86
## Gross.Profits       45892.12 161301.36
## Gross.Losses       -28570.57 -50590.28
## Std.Dev.Trade.PL    20412.79  15701.97
## Percent.Positive       33.33     31.82
## Percent.Negative       66.67     68.18
## Profit.Factor           1.61      3.19
## Avg.Win.Trade       22946.06  23043.05
## Med.Win.Trade       22946.06  14477.80
## Avg.Losing.Trade    -7142.64  -3372.69
## Med.Losing.Trade    -8618.81  -3245.27
## Avg.Daily.PL        -5102.44   4582.54
## Med.Daily.PL        -6074.75  -1445.65
## Std.Dev.Daily.PL     6490.57  15943.85
## Ann.Sharpe            -12.48      4.56
## Max.Drawdown       -61686.41 -34526.52
## Profit.To.Max.Draw      0.28      3.21
## Avg.WinLoss.Ratio       3.21      6.83
## Med.WinLoss.Ratio       2.66      4.46
## Max.Equity          33279.24 115011.39
## Min.Equity         -28407.16 -11616.95
## End.Equity          17321.55 110711.07
</pre>
<pre><code>final_acct2 &lt;- getAccount(account_st_2)
plot(final_acct2$summary$End.Eq[&quot;2010/2016&quot;], main = &quot;Portfolio Equity&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-24-1.png?w=456" data-recalc-dims="1"></p>
<p>A more realistic portfolio that can invest in any in a list of twelve (tech) stocks has a final growth of about 100%. How good is this? While on the surface not bad, we will see we could have done better.</p>
<h2>Benchmarking</h2>
<p>Backtesting is only part of evaluating the efficacy of a trading strategy. We would like to <strong>benchmark</strong> the strategy, or compare it to other available (usually well-known) strategies in order to determine how well we have done.</p>
<p>Whenever you evaluate a trading system, there is one strategy that you should always check, one that beats all but a handful of managed mutual funds and investment managers: buy and hold <a target="_blank" rel="nofollow" href="https://finance.yahoo.com/quote/SPY">SPY</a>. The <strong>efficient market hypothesis</strong> claims that it is all but impossible for anyone to beat the market. Thus, one should always buy an index fund that merely reflects the composition of the market. SPY is an <strong>exchange-traded fund</strong> (a mutual fund that is traded on the market like a stock) whose value effectively represents the value of the stocks in the S&amp;P 500 stock index. By buying and holding SPY, we are effectively trying to match our returns with the market rather than beat it.</p>
<p>I obtain data on SPY below, and look at the profits for simply buying and holding SPY.</p>
<pre><code>getSymbols(&quot;SPY&quot;, from = start, to = end)
</code></pre>
<pre>## [1] &quot;SPY&quot;
</pre>
<pre><code># A crude estimate of end portfolio value from buying and holding SPY
1000000 * (SPY$SPY.Adjusted[&quot;2016-09-30&quot;][[1]] / SPY$SPY.Adjusted[[1]])
</code></pre>
<pre>## [1] 2189421
</pre>
<pre><code>plot(final_acct2$summary$End.Eq[&quot;2010/2016&quot;] / 1000000,
     main = &quot;Portfolio Equity&quot;, ylim = c(0.8, 2.5))
lines(SPY$SPY.Adjusted / SPY$SPY.Adjusted[[1]], col = &quot;blue&quot;)
</code></pre>
<p><img src="https://ntguardian.files.wordpress.com/2017/04/unnamed-chunk-26-1.png?w=456" data-recalc-dims="1"></p>
<p>Buying and holding SPY beats our trading system, at least how we currently set it up, and we haven’t even accounted for how expensive our more complex strategy is in terms of fees. Given both the opportunity cost and the expense associated with the active strategy, we should not use it.</p>
<p>What could we do to improve the performance of our system? For starters, we could try diversifying. All the stocks we considered were tech companies, which means that if the tech industry is doing poorly, our portfolio will reflect that. We could try developing a system that can also short stocks or bet bearishly, so we can take advantage of movement in any direction. We could seek means for forecasting how high we expect a stock to move. Whatever we do, though, must beat this benchmark; otherwise there is an opportunity cost associated with our trading system.</p>
<p>Other benchmark strategies exist, and if our trading system beat the “buy and hold SPY” strategy, we may check against them. Some such strategies include:</p>
<ul>
<li>Buy SPY when its closing monthly price is aboves its ten-month moving average.</li>
<li>Buy SPY when its ten-month momentum is positive. (<strong>Momentum</strong> is the first difference of a moving average process, or <img title="MO^q_t = MA^q_t - MA^q_{t - 1}" alt="MO^q_t = MA^q_t - MA^q_{t - 1}" class="latex" src="https://s0.wp.com/latex.php?latex=MO%5Eq_t+%3D+MA%5Eq_t+-+MA%5Eq_%7Bt+-+1%7D&bg=ffffff&%23038;fg=444444&%23038;s=0">.)</li>
</ul>
<p>(I first read of these strategies <a target="_blank" rel="nofollow" href="https://www.r-bloggers.com/are-r2s-useful-in-finance-hypothesis-driven-development-in-reverse/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+RBloggers+%28R+bloggers%29">here</a>.) The general lesson still holds: <em>don’t use a complex trading system with lots of active trading when a simple strategy involving an index fund without frequent trading beats it.</em> <a target="_blank" rel="nofollow" href="http://www.nytimes.com/2015/03/15/your-money/how-many-mutual-funds-routinely-rout-the-market-zero.html?_r=0">This is actually a very difficult requirement to meet.</a></p>
<p>As a final note, suppose that your trading system <em>did</em> manage to beat any baseline strategy thrown at it in backtesting. Does backtesting predict future performance? Not at all. <a target="_blank" rel="nofollow" href="http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2745220">Backtesting has a propensity for overfitting</a>, so just because backtesting predicts high growth doesn’t mean that growth will hold in the future.</p>
<h2>Conclusion</h2>
<p>While this lecture ends on a depressing note, keep in mind that <a target="_blank" rel="nofollow" href="http://www.nytimes.com/2009/06/06/business/06nocera.html">the efficient market hypothesis has many critics.</a> My own opinion is that as trading becomes more algorithmic, beating the market will become more difficult. That said, it may be possible to beat the market, even though mutual funds seem incapable of doing so (bear in mind, though, that part of the reason mutual funds perform so poorly is because of fees, which is not a concern for index funds).</p>
<p>This lecture is very brief, covering only one type of strategy: strategies based on moving averages. Many other trading signals exist and employed. Additionally, we never discussed in depth shorting stocks, currency trading, or stock options. Stock options, in particular, are a rich subject that offer many different ways to bet on the direction of a stock.</p>
<p>Some resources I referred to when researching this subject include:</p>
<ul>
<li>The documentation of all used packages</li>
<li>The <a target="_blank" rel="nofollow" href="http://www.quantmod.com/"><strong>quantmod</strong> dedicated website</a></li>
<li>A <a target="_blank" rel="nofollow" href="https://www.youtube.com/watch?v=61_F2fcvrsw">webinar by Ilya Kipnis</a> (author of <strong>IKTrading</strong>); you can follow him on his blog, at <a target="_blank" rel="nofollow" href="https://quantstrattrader.wordpress.com/">https://quantstrattrader.wordpress.com/</a></li>
</ul>
<p>A resource I have not thoroughly checked out but looks promising is the ebook <a target="_blank" rel="nofollow" href="https://timtrice.github.io/backtesting-strategies/index.html"><em>Backtesting Strategies with R</em></a>, by Tim Trice, also discussing <strong>quantstrat</strong>.</p>
<p>Remember that it is possible (if not common) to lose money in the stock market. It’s also true, though, that it’s difficult to find returns like those found in stocks, and any investment strategy should take investing in it seriously. This lecture is intended to provide a starting point for evaluating stock trading and investments, and, more generally, analyzing temporal data, and I hope you continue to explore these ideas.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ntguardian.wordpress.com/2511/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/ntguardian.wordpress.com/2511/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ntguardian.wordpress.com/2017/04/03/introduction-stock-market-data-r-2/"> R – Curtis Miller's Personal Website</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/totLnKFz7Vs"></a></div>