<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/nuts-and-bolts-of-quantstrat-part-v/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://quantstrattrader.wordpress.com/2017/04/13/nuts-and-bolts-of-quantstrat-part-v/"> R – QuantStrat TradeR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This post will be about pre-processing custom indicators in quantstrat–that is, how to add values to your market data that do not arise from the market data itself.</p>
<p>The <a target="_blank" rel="nofollow" href="https://quantstrattrader.wordpress.com/2014/09/09/nuts-and-bolts-of-quantstrat-part-i/">first</a> <a target="_blank" rel="nofollow" href="https://quantstrattrader.wordpress.com/2014/09/16/nuts-and-bolts-of-quantstrat-">four</a> <a target="_blank" rel="nofollow" href="https://quantstrattrader.wordpress.com/2014/09/20/nuts-and-bolts-of-quantstrat-part-iii/">parts</a> of my <a target="_blank" rel="nofollow" href="https://quantstrattrader.wordpress.com/2014/09/24/nuts-and-bolts-of-quantstrat-part-iv/">nuts and bolts of quantstrat</a> were well received. They are even available as a <a target="_blank" rel="nofollow" href="https://www.datacamp.com/courses/financial-trading-in-r">datacamp course</a>. For those that want to catch up to today’s post, I highly recommend the datacamp course.</p>
<p>To motivate this post, the idea is that say you’re using alternative data that isn’t simply derived from a transformation of the market data itself. I.E. you have a proprietary alternative data stream that may predict an asset’s price, you want to employ a cross-sectional ranking system, or any number of things. How do you do this within the context of quantstrat?</p>
<p>The answer is that it’s as simple as binding a new xts to your asset data, as this demonstration will show.</p>
<p>First, let’s get the setup out of the way.</p>
<pre>
require(quantstrat)
require(PerformanceAnalytics)

initDate=&quot;1990-01-01&quot;
from=&quot;2003-01-01&quot;
to=&quot;2012-12-31&quot;
options(width=70)

options(&quot;getSymbols.warning4.0&quot;=FALSE)

currency('USD')
Sys.setenv(TZ=&quot;UTC&quot;)

symbols &lt;- 'SPY'
suppressMessages(getSymbols(symbols, from=from, to=to, src=&quot;yahoo&quot;, adjust=TRUE))  

stock(symbols, currency=&quot;USD&quot;, multiplier=1)
</pre>
<p>Now, we have our non-derived indicator. In this case, it’s a toy example–the value is 1 if the year is odd (I.E. 2003, 2005, 2007, 2009), and 0 if it’s even. We compute that and simply column-bind (cbind) it to the asset data.</p>
<pre>
nonDerivedIndicator &lt;- as.numeric(as.character(substr(index(SPY), 1, 4)))%%2 == 1
nonDerivedIndicator &lt;- xts(nonDerivedIndicator, order.by=index(SPY))

SPY &lt;- cbind(SPY, nonDerivedIndicator)
colnames(SPY)[7] = &quot;nonDerivedIndicator&quot;
</pre>
<p>Next, we just have a very simple strategy–buy a share of SPY on odd years, sell on even years. That is, buy when the nonDerivedIndicator column crosses above 0.5 (from 0 to 1), and sell when the opposite occurs.</p>
<pre>
strategy.st &lt;- portfolio.st &lt;- account.st &lt;- &quot;nonDerivedData&quot;
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD')
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

add.signal(strategy.st, name = sigThreshold, 
           arguments = list(column = &quot;nonDerivedIndicator&quot;, threshold = 0.5, relationship = &quot;gte&quot;, cross = TRUE),
           label = &quot;longEntry&quot;)

add.signal(strategy.st, name = sigThreshold, 
           arguments = list(column = &quot;nonDerivedIndicator&quot;, threshold = 0.5, relationship = &quot;lte&quot;, cross = TRUE),
           label = &quot;longExit&quot;)


tmp &lt;- applySignals(strategy = strategy.st, mktdata=SPY)


add.rule(strategy.st, name=&quot;ruleSignal&quot;, 
         arguments=list(sigcol=&quot;longEntry&quot;, sigval=TRUE, ordertype=&quot;market&quot;, 
                        orderside=&quot;long&quot;, replace=FALSE, prefer=&quot;Open&quot;, orderqty = 1), 
         type=&quot;enter&quot;, path.dep=TRUE)

add.rule(strategy.st, name=&quot;ruleSignal&quot;, 
         arguments=list(sigcol=&quot;longExit&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)

#apply strategy
t1 &lt;- Sys.time()
out &lt;- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 &lt;- Sys.time()
print(t2-t1)

#set up analytics
updatePortf(portfolio.st)
dateRange &lt;- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)
</pre>
<p>And the result:</p>
<pre>
chart.Posn(portfolio.st, 'SPY')
</pre>
<p><img src="https://i2.wp.com/imgur.com/SvMGxQb.png"></p>
<p>In conclusion, you can create signals based off of any data in quantstrat. Whether that means volatility ratios, fundamental data, cross-sectional ranking, or whatever proprietary alternative data source you may have access to, this very simple process is how you can use quantstrat to add all of those things to your systematic trading backtest research.</p>
<p>Thanks for reading.</p>
<p>Note: I am always interested in full-time opportunities which may benefit from my skills. I have experience in data analytics, asset management, and systematic trading research. If you know of any such opportunities, do not hesitate to contact me on my LinkedIn, found <a target="_blank" rel="nofollow" href="https://www.linkedin.com/in/ilyakipnis/">here</a>.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quantstrattrader.wordpress.com/585/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/quantstrattrader.wordpress.com/585/" 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://quantstrattrader.wordpress.com/2017/04/13/nuts-and-bolts-of-quantstrat-part-v/"> R – QuantStrat TradeR</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/1ZwnKjXLbrE"></a></div>