<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/forecasting-opportunities/"></div></div><div><a data-url="https://www.r-bloggers.com/forecasting-opportunities/" data-counturl="https://www.r-bloggers.com/forecasting-opportunities/" data-count="horizontal" data-text="Forecasting Opportunities" 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/09/13/forecasting-opportunities/"> R – Quintuitive</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a target="_blank" href="http://www.quintuitive.com/2016/08/13/labeling-opportunities-price-series/">previous post</a> in this series, showed a way to identify trading opportunities. The approach I implemented used time series daily data to identify good entry points in terms of risk-reward. The natural next step is to try to make use of these opportunities using machine learning.<br>
<span></span><br>
To refresh: the output of the previous post was a time series which had three possible states: short, none, long. Each state identified whether an opportunity for size-able return exists over the next few days. This was generated using data from the future. That’s the dependent variable.</p>
<p>What data shall we use for the independent variables, for the predictors? No idea. <img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s.w.org/images/core/emoji/2/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"> To get something going, I decided to try a few features which might have some influence:</p>
<ul>
<li>The returns of each of the last five days.</li>
<li>The returns over the last 252 (a year), 63 (a quarter), 21 (a month) and 5 (a week) days. This is the momentum, or the rate of change.</li>
<li>The channel position (as a fraction) over the last 252 (a year), 63 (a quarter), 21 (a month) and 5 (a week) days. In other words, take the min and the max over the period and compute where within the channel the last close is. For instance, if the last close is a new high, the value of this column is 1. A new low is 0.</li>
</ul>
<p>All returns were normalized to daily, using an exponential moving average over the returns. For the people who understand code easier – he is pretty much the final version to build the features:</p>
<pre>
build.features = function(xx, days = 1:5, vola.len=35)
{
    stopifnot(has.Cl(xx))

    close = Cl(xx)

    rets = ROC(close, n = days[1], type = &quot;discrete&quot;)
    arets = rets / sqrt(EMA(rets * rets, n = vola.len))
    # Subtract one because we want to align at the end of the day
    res = na.trim(lag.xts(arets, k=days[1]-1))

    colNames = c(paste(sep = &quot;&quot;, days[1], &quot;D_LAG&quot;))
    for (nn in tail(days, -1))
    {
        res = merge(res, na.trim(lag.xts(arets, k=nn-1)), all = F)
        colNames = append(colNames, paste(sep = &quot;&quot;, nn, &quot;D_LAG&quot;))
    }

    # Annual, quarterly, monthly and weekly:
    #   1. returns (normalized to daily)
    #   2. min/max channel position
    cc = c(252, 63, 21, 5)
    for (nn in cc)
    {
        # Compute the return
        rr = ROC(close, n = nn, type = &quot;discrete&quot;)
        # Normalize to daily return
        rr = (1 + rr)^(1/nn) - 1
        # Normalize by volatility
        rr = rr / sqrt(EMA(rets * rets, n = vola.len))
        res = merge(res, na.trim(rr), all = F)
        colNames = append(colNames, paste0(nn, &quot;D_RET&quot;))

        res = merge(res, na.trim((close - runMin(close, n=nn))/(runMax(close, n=nn) - runMin(close, n=nn))), all=F)
        colNames = append(colNames, paste0(nn, &quot;D_CHANNEL&quot;))
    }

    res = reclass(res, xx)
    colnames(res) = colNames
    return(res)
}
</pre>
<p>Next, using my parallel framework based on the the <a target="_blank" href="http://topepo.github.io/caret/index.html">caret package</a>, I run a few machine learning methods: <em>lda</em>, <em>qda</em>, <em>svm</em>, <em>xgb trees</em> and a few others. Despite the parallelism, pretty much all algorithms took more than a day to finish for about six years of data on a single time series (I used heating oil back-adjusted futures).</p>
<p>Unfortunately, time spent didn’t translate into meaningful results. The only algorithm which gave somewhat interesting forecasts was the <em>qda</em>. Everything else had pretty much flat, out of the market, forecast for all points. The reason could be that the opportunity series was generated using a conservative approach – only about 9% of the days were identified as opportunities.</p>
<p>The <em>qda</em> generated about 121 non-flat forecasts, a bit less than the actual. Out of these 121, 108 were wrong and 13 were spot-on. So why do I say that the method looks promising? The opportunities are generated in a way to guarantee that a certain profit target is achieved without hitting a smaller stop loss. For more details refer to the previous post in this series. If neither of these goals is achieved, we will exit the market after a specific number of days. Thus, we may still end up making money. That’s why it looks promising to me.</p>
<p>So what’s next? Not sure yet, but there are a few directions I can think of:</p>
<ul>
<li>Improving the predictors</li>
<li>More sophisticated machine learning approach</li>
<li>Using history from multiple financial instruments for learning</li>
</ul>
<p>The post <a target="_blank" rel="nofollow" href="http://www.quintuitive.com/2016/09/13/forecasting-opportunities/">Forecasting Opportunities</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/forecasting-opportunities/"></div></div><div><a data-url="https://www.r-bloggers.com/forecasting-opportunities/" data-counturl="https://www.r-bloggers.com/forecasting-opportunities/" data-count="vertical" data-text="Forecasting Opportunities" 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/09/13/forecasting-opportunities/"> 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/Ie9P3NqCgzI"></a></div>