<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/labeling-opportunities-in-price-series/"></div></div><div><a data-url="https://www.r-bloggers.com/labeling-opportunities-in-price-series/" data-counturl="https://www.r-bloggers.com/labeling-opportunities-in-price-series/" data-count="horizontal" data-text="Labeling Opportunities in Price Series" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.quintuitive.com/2016/08/13/labeling-opportunities-price-series/"> R – Quintuitive</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>One approach to trading which has been puzzling me lately, is to sit and wait for opportunities. <img alt="🙂" class="wp-smiley" src="https://i2.wp.com/s.w.org/images/core/emoji/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"> Sounds simplistic, but it is indeed different than, for instance, the asset allocation strategies. In order to be able to even attempt taking advantage of these opportunities, however, we must be able to identify them. Once the opportunities are identified – we can try to explain (forecast) them using historical data.<br>
<span></span><br>
The first step is to define what an <em>opportunity</em> is. It could be anything, for instance we can start with <em>a strong directional move</em>, but then we need to define what a strong directional move is. Let’s give it a try.</p>
<p>We can define a strong directional move as a 5% move over 3 days for instance. However, that’s not likely to work for different markets (think futures). 5% move is a lot in treasuries, but not so much in oil. The solution is to normalize the returns using volatility. I will use exponential moving average for that.</p>
<p>Here is a short code snippet:</p>
<pre>
adjust.returns = function(returns, vola.len=35) {
    vola = sqrt(EMA(returns * returns, n = vola.len))
    return(returns/vola)
}

good.entries = function(ohlc, days.out=15, vola.len=35, days.pos=0.6) {
    stopifnot(days.out > 3)
    
    hi = Hi(ohlc)
    lo = Lo(ohlc)
    cl = Cl(ohlc)
    rets = ROC(cl, type='discrete')
    arets = abs(adjust.returns(rets, vola.len=vola.len))
    res = rep(0, NROW(ohlc))
    days = rep(0, NROW(ohlc))
    for(ii in 3:days.out) {
        hh = lag.xts(runMax(hi, n=ii), -ii)
        ll = lag.xts(runMin(lo, n=ii), -ii)
        hi.ratio = (hh/cl - 1)/arets
        lo.ratio = (ll/cl - 1)/arets
        
        # days required
        dd = ceiling(days.pos*ii)
        
        longs = hi.ratio > dd &amp; -lo.ratio &lt; dd
        longs = ifelse(longs, 1, 0)
        
        shorts = -lo.ratio > dd &amp; hi.ratio &lt; dd
        shorts = ifelse(shorts, -1, 0)
        
        both = ifelse(longs == 1, 1, shorts)
        new.days = ii*as.numeric(res == 0 &amp; both != 0)
        res = ifelse(res != 0, res, both)
        days = ifelse(days != 0, days, new.days)
    }
    return(data.frame(entry=res, days=days))
}
</pre>
<p>The code basically defines an opportunity as a directional move over X days (X is between 3 and 15). The minimum move we would accept is computed by taking a percentage of the days (60% by default), rounding that number up, and multiplying it by the standard deviation of the normalized returns. We also require that the move in the opposite direction is smaller. Hard to explain, but hopefully you get the idea.</p>
<p>The function returns a label for each day: 0, 1, or -1. The opportunities are the 1 and -1 days of course.</p>
<p>Is this trade-able? Absolutely. When an opportunity arises, we enter in the suggested direction. The exit is a profit target, we can use a stop loss of the same magnitude.</p>
<p>Why all this effort again? Simple – these labels can be used as <a target="_blank" href="http://www.quintuitive.com/2012/11/30/trading-with-support-vector-machines-svm/">the response</a>, or the dependent variable in an attempt to explain (forecast) the opportunities using historical data. At this point what’s left is to generate what we think might constitute a good set of predictors, align the two sets appropriately (no data snooping) and the rest is standard machine learning.</p>
<p>That’s for another post though.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quintuitive.com/2016/08/13/labeling-opportunities-price-series/">Labeling Opportunities in Price Series</a> appeared first on <a target="_blank" rel="nofollow" href="http://www.quintuitive.com/">Quintuitive</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/labeling-opportunities-in-price-series/"></div></div><div><a data-url="https://www.r-bloggers.com/labeling-opportunities-in-price-series/" data-counturl="https://www.r-bloggers.com/labeling-opportunities-in-price-series/" data-count="vertical" data-text="Labeling Opportunities in Price Series" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.quintuitive.com/2016/08/13/labeling-opportunities-price-series/"> R – Quintuitive</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/TMU49z5TC4Q"></a></div>