Back to Community
Long/Short trading strategy with WSJ, Forbes, and CNBC News sentiment

Sentdex provides stock sentiment scores by analyzing a number of major news sources like the WSJ, Forbes, and CNBC. Stock sentiment simply refers to the general public's attitude towards a given security and can often be used as a "directional signal to figure out whether to long or short stocks in your portfolio."

A number of researchers have shown that daily stock news sentiment, even when lagged, can serve as important predictors of future stock price movement. One study in particular successfully used news sentiment to create a low volatility, high return trading strategy.

Inspired, I attempted to do the same by creating a long/short algorithm using Sentdex's news sentiment dataset.

This strategy calculates a three-day moving average of Sentdex's sentiment_signal factor (-3 to 6) to determine both long and short positions. On a daily basis, long positions are created in the 25 securities with the highest sentiment and short positions are created in the 25 securities with the lowest sentiment. A three-day look-back window is used because I wanted a time period that would both smooth out one-day sentiment spikes while providing a recent enough signal to (hopefully) predict stock-price movement in the following day.

The result is a low volatility, low beta strategy with returns doubling the S&P 500 from 2014 – 2015 and also avoiding the large downturns of 2014.

The full Sentdex dataset is available for $10/month and includes availability in live trading. The dataset covers almost all the securities in the S&P 500 along with a few others.

Take a look and let me know what you think. For questions on accessing this data, please email [email protected]

The purpose of this algorithm is to show a simple example of Sentdex's data in an algorithm. Further work is required to reduce the impact of slippage and commission and gear it for live trading.

Clone Algorithm
586
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56cc7356913e110ded8fa773
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

19 responses

Very impressive!

Here's a full tear sheet of the backtest. Exposure is pretty constant and turnover is high (expected because it's a daily algorithm). I would try different rebalance periods for the sake of commission costs.

Loading notebook preview...
Notebook previews are currently unavailable.

I've heard some anecdotal evidence that analyst sentiment indicators behave like lagged indicators on price action. In other words, things like momentum and mean reversion offer similar signals with less signal decay, and therefore have a higher Sharpe ratio. How would one go about testing this hypothesis in research?

I would look at a few examples like this one by John Loeber (https://www.quantopian.com/posts/does-federal-reserve-sentiment-predict-prices-of-macro-tracking-etfs). I'm not exactly the expert on this topic, but would probably guess you'd need to create a signal with momentum/mean reversion using sentiment, test it with some in-sample data, and then see if it holds up out-of-sample.

Here's a tearsheet of the sentiment factor used for this algorithm run over the same time period.

Interestingly, after running a few iterations - it looks like the information coefficient for the factor was stronger for more liquid securities (i.e. top 200 is used for the notebook) which would make sense as this dataset covers a majority of the S&P 500.

Check out the notebook yourself for the full details and I'll be releasing a post shortly on how to use this tearsheet BEFORE backtesting in order to develop your factors.

Loading notebook preview...
Notebook previews are currently unavailable.

Seong, I think there is some problem in the code, especially the class AvgSentiment(), could you please post the fixed version of the code?

Hi Ujae,

Not sure what you mean. Do you mind clarifying? The algo runs well from my testing

Seong,
When you look at

class AvgSentiment(CustomFactor):

def compute(self, today, assets, out, impact):  
    np.mean(impact, axis=0, out=out)

So, how can this work?
Also I changed the date from 01/01/2010. But not sure which of the two is causing the problem.
Now, I am leaning more toward the date change that is causing the problem.

Hi Ujae,

The Free Sentdex dataset sample is available from October 15th, 2012 till February 17th, 2016 so that is probably causing your issue. You can find full details here (https://www.quantopian.com/data/sentdex/sentiment)

As for the factor itself. It takes the mean of the sentdex sentiment score with the window_length assigned in initialize and uses out to assign the results. For more information, I'd recommend reading this tutorial on using Custom Factors in Pipeline (https://www.quantopian.com/help#custom-factors)

Hope that helps,
Seong

Here's the same algorithm but run over 2015

Clone Algorithm
586
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56eb2321436ab80de7fa3cd2
There was a runtime error.

Here is with default commission slippage

Clone Algorithm
8
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56f0f18ea40c670f617e588c
There was a runtime error.

@Seong - disappointing results..

The purpose of this algorithm is to show a simple example of Sentdex's data in an algorithm. Further work is required to reduce the impact of slippage and commission and gear it for trading.

A good starting point is probably altering the lookback windows and trading frequencies.

The choppiness worries me, but I see what you are saying.

I'm really puzzled why are the example algos always without slippage or commission. Even silly algorithms that have almost zero real world edge like the one I automated here https://www.quantopian.com/posts/worlds-best-algorithm-2#56d7fd083e72f18f62000397 can look extremely nice (look at my example! Sharpe over 4! Beta almost zero! 600% Profit!) without commission or slippage. That however tells absolutely nothing about how effective the strategy actually is.

That's a good point. The purpose of my algorithm was to show a simple example of Sentdex's data in an algorithm. By that I mean, showcasing the alpha potential of the factor, rather than the trading strategy itself (which is entirely different animal compared to factor evaluation).

In hindsight, I should rather have used the Factor Tearsheet (https://www.quantopian.com/posts/factor-tear-sheet) to evaluate Sentdex.

Seong - I have to agree with the comments above by Mikko. The contest rules state certain criteria, but I have yet to fully see an example of what type of algorithm is 'appropriate' for the contest. The algo you created is no doubt very impressive. However, there does not seem to be some clear cut visual of how to utilize the 'example' algos to fit Q's criteria. Especially with the news feed being built into the algo, it is very tough to get any idea as to what to do when example codes and data feeds are missing key parts. Is there anyway to build this algo to meet your specific requirements? Perhaps you have an example that shows the exact nature of what you are trying to attempt.

Daniel,

These are great comments and I appreciate your feedback and where your frustration is coming from.

We provide the Quantopian Lecture Series on different subjects like low beta, long/short for you to address contest requirements like high beta.

For example, here is the long-short algorithm for the long/short Quantopian lecture series algorithm: https://www.quantopian.com/posts/quantopian-lecture-series-long-short-equity-algorithm

That being said, I appreciate your feedback and would recommend following this series: https://www.quantopian.com/posts/the-psychsignal-trader-mood-series-introduction as I attempt to highlight the differences between factor evaluation (which I'm doing here) versus trading strategy creation.

I know this is totally wrong thread to talk about this but the commission is also calculated quite bluntly as taking liquidity is the same commission as providing liquidity. Simulations would be more accurate if commission model would be more realistic (for example with IB that you connect to has separate commissions for taking/providing liquidity) and people here would see how different execution models would really effect your performance. It's not very interesting to try to do "Further work [..] to reduce the impact of slippage and commission and gear it for trading." if commission model is not even nearly accurate.