Benzinga
Benzinga News Feed
Introduction
The Benzinga News Feed dataset by Benzinga tracks US Equity news releases. The data covers about 1,250 articles per day across 8,000 Equities, starts in January 2016, and is delivered on a second frequency. This dataset is created by structuring the content produced by Benzinga's editorial team.
This dataset depends on the US Equity Security Master dataset because the US Equity Security Master dataset contains information on splits, dividends, and symbol changes.
For more information about the Benzinga News Feed dataset, including CLI commands and pricing, see the dataset listing.
Getting Started
The following snippet demonstrates how to request data from the Benzinga News Feed dataset:
self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol
self.dataset_symbol = self.add_data(BenzingaNews, self.symbol).symbol _symbol = AddEquity("AAPL", Resolution.Daily).Symbol;
_datasetSymbol = AddData<BenzingaNews>(_symbol).Symbol;
Requesting Data
To add Benzinga News Feed data to your algorithm, call the AddDataadd_data method. Save a reference to the dataset Symbol so you can access the data later in your algorithm.
class BenzingaNewsDataAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2021, 1, 1)
self.set_end_date(2021, 6, 1)
self.set_cash(100000)
self.aapl = self.add_equity("AAPL", Resolution.MINUTE).symbol
self.dataset_symbol = self.add_data(BenzingaNews, self.aapl).symbol public class BenzingaNewsDataAlgorithm : QCAlgorithm
{
private Symbol _symbol, _datasetSymbol;
public override void Initialize()
{
SetStartDate(2021, 1, 1);
SetEndDate(2021, 6, 1);
SetCash(100000);
_symbol = AddEquity("AAPL", Resolution.Minute).Symbol;
_datasetSymbol = AddData<BenzingaNews>(_symbol).Symbol;
}
}
Accessing Data
To get the current Benzinga News Feed data, index the current Slice with the dataset Symbol. Slice objects deliver unique events to your algorithm as they happen, but the Slice may not contain data for your dataset at every time step. To avoid issues, check if the Slice contains the data you want before you index it.
def on_data(self, slice: Slice) -> None:
if slice.contains_key(self.dataset_symbol):
article = slice[self.dataset_symbol]
self.log(f"{self.dataset_symbol} title at {slice.time}: {article.title}") public override void OnData(Slice slice)
{
if (slice.ContainsKey(_datasetSymbol))
{
var article = slice[_datasetSymbol];
Log($"{_datasetSymbol} title at {slice.Time}: {article.Mentions}");
}
}
To iterate through all of the dataset objects in the current Slice, call the Getget method.
def on_data(self, slice: Slice) -> None:
for dataset_symbol, article in slice.get(BenzingaNews).items():
self.log(f"{dataset_symbol} title at {slice.time}: {article.title}")
public override void OnData(Slice slice)
{
foreach (var kvp in slice.Get<BenzingaNews>())
{
var datasetSymbol = kvp.Key;
var article = kvp.Value;
Log($"{datasetSymbol} title at {slice.Time}: {article.Title}");
}
}
Historical Data
To get historical Benzinga News Feed data, call the Historyhistory method with the dataset Symbol. If there is no data in the period you request, the history result is empty.
# DataFrame history_df = self.history(self.dataset_symbol, 100, Resolution.DAILY) # Dataset objects history_bars = self.history[BenzingaNews](self.dataset_symbol, 100, Resolution.DAILY)
var history = History<BenzingaNews>(_datasetSymbol, 100, Resolution.Daily);
For more information about historical data, see History Requests.
Remove Subscriptions
To remove a subscription, call the RemoveSecurityremove_security method.
self.remove_security(self.dataset_symbol)
RemoveSecurity(_datasetSymbol);
If you subscribe to Benzinga News Feed data for assets in a dynamic universe, remove the dataset subscription when the asset leaves your universe. To view a common design pattern, see Track Security Changes.
Example Applications
The Benzinga News Feed enables you to accurately design strategies harnessing real-time news releases. Examples include the following strategies:
- Creating a dictionary of sentiment scores for various words and assigning a sentiment score to the content of each news release
- Calculating the sentiment of news releases with Natural Language Processing (NLP)
- Trading securities when their news releases that Benzinga tags with current buzzwords
Classic Algorithm Example
The following example algorithm parses the Benzinga news articles related to Apple. If the sentiment is positive, the algorithm buys Apple. Otherwise, it holds cash.
from AlgorithmImports import *
class BenzingaNewsDataAlgorithm(QCAlgorithm):
current_holdings = 0
target_holdings = 0
# A custom word-score map for calculating the total sentiment score
word_scores = {
'good': 1, 'great': 1, 'best': 1, 'growth': 1,
'bad': -1, 'terrible': -1, 'worst': -1, 'loss': -1}
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
self.set_cash(100000)
# Requesting data to obtain the updated news for sentiment score calculation
self.aapl = self.add_equity("AAPL", Resolution.MINUTE).symbol
self.benzinga_symbol = self.add_data(BenzingaNews, self.aapl).symbol
# Historical data
history = self.history(self.benzinga_symbol, 14, Resolution.DAILY)
self.debug(f"We got {len(history)} items from our history request")
def on_data(self, slice: Slice) -> None:
if slice.contains_key(self.benzinga_symbol):
# Assign a sentiment score to the news article by specific word appearance scoring
content_words = slice[self.benzinga_symbol].contents.lower()
score = 0
for word, word_score in self.word_scores.items():
score += (content_words.count(word) * word_score)
self.target_holdings = int(score > 0)
# Ensure we have AAPL data in the current Slice to avoid stale filling
if not (slice.contains_key(self.aapl) and slice[self.aapl] is not None and not slice[self.aapl].is_fill_forward):
return
# Buy or sell if the sentiment has changed from our current holdings
if self.current_holdings != self.target_holdings:
self.set_holdings(self.aapl, self.target_holdings)
self.current_holdings = self.target_holdings using System.Text.RegularExpressions;
public class BenzingaNewsDataAlgorithm : QCAlgorithm
{
private Symbol _aapl;
private Symbol _benzingaSymbol;
private int _currentHoldings = 0;
private int _targetHoldings = 0;
// A custom word-score map for calculating the total sentiment score
private Dictionary<string, int> _wordScores = new Dictionary<string, int>(){
{"good", 1}, {"great", 1}, {"best", 1}, {"growth", 1},
{"bad", -1}, {"terrible", -1}, {"worst", -1}, {"loss", -1}
};
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
SetCash(100000);
// Requesting data to obtain the updated news for sentiment score calculation
_aapl = AddEquity("AAPL", Resolution.Minute).Symbol;
_benzingaSymbol = AddData<BenzingaNews>(_aapl).Symbol;
// Historical data
var history = History<BenzingaNews>(_benzingaSymbol, 14, Resolution.Daily);
Debug($"We got {history.Count()} items from our history request");
}
public override void OnData(Slice slice)
{
if (slice.ContainsKey(_benzingaSymbol))
{
// Assign a sentiment score to the news article by specific word appearance scoring
var contentWords = slice[_benzingaSymbol].Contents.ToLower();
var score = 0;
foreach (KeyValuePair<string, int> entry in _wordScores)
{
score += (Regex.Matches(contentWords, entry.Key).Count * entry.Value);
}
_targetHoldings = Convert.ToInt32(score > 0);
}
// Ensure we have AAPL data in the current Slice to avoid stale filling
if (!(slice.ContainsKey(_aapl) && slice[_aapl] != null && !slice[_aapl].IsFillForward))
{
return;
}
// Buy or sell if the sentiment has changed from our current holdings
if (_currentHoldings != _targetHoldings)
{
SetHoldings(_aapl, _targetHoldings);
_currentHoldings = _targetHoldings;
}
}
}
Framework Algorithm Example
The following example algorithm parses the Benzinga news articles related to Apple. If the sentiment is positive, the algorithm buys Apple. Otherwise, it holds cash.
from AlgorithmImports import *
class BenzingaNewsDataAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
self.set_cash(100000)
symbols = [ Symbol.create("AAPL", SecurityType.EQUITY, Market.USA) ]
self.add_universe_selection(ManualUniverseSelectionModel(symbols))
self.add_alpha(BenzingaNewsAlphaModel())
self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel())
self.add_risk_management(NullRiskManagementModel())
self.set_execution(ImmediateExecutionModel())
class BenzingaNewsAlphaModel(AlphaModel):
symbol_data_by_symbol = {}
# A custom word-score map for calculating the total sentiment score
word_scores = {'good': 1, 'great': 1, 'best': 1, 'growth': 1,
'bad': -1, 'terrible': -1, 'worst': -1, 'loss': -1}
def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]:
insights = []
for symbol, symbol_data in self.symbol_data_by_symbol.items():
if slice.contains_key(symbol_data.benzinga_symbol):
# Assign a sentiment score to the news article by specific word appearance scoring
content_words = slice[symbol_data.benzinga_symbol].contents.lower()
score = 0
for word, word_score in self.word_scores.items():
score += (content_words.count(word) * word_score)
symbol_data.target_direction = InsightDirection.UP if score > 0 else InsightDirection.FLAT
# Ensure we have security data in the current Slice to avoid stale filling
if not (slice.contains_key(symbol) and slice[symbol] is not None and not slice[symbol].is_fill_forward):
continue
# Buy or sell if the sentiment has changed from our current holdings
if symbol_data.current_direction != symbol_data.target_direction:
symbol_data.current_direction = symbol_data.target_direction
insights.append(Insight.price(symbol, timedelta(days=14), symbol_data.target_direction))
return insights
def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None:
for security in changes.added_securities:
symbol = security.symbol
self.symbol_data_by_symbol[symbol] = SymbolData(algorithm, symbol)
for security in changes.removed_securities:
symbol_data = self.symbol_data_by_symbol.pop(security.symbol, None)
if symbol_data:
symbol_data.dispose()
class SymbolData:
current_direction = InsightDirection.FLAT
target_direction = InsightDirection.FLAT
def __init__(self, algorithm: QCAlgorithm, symbol: Symbol) -> None:
self.algorithm = algorithm
# Requesting data to obtain the updated news for sentiment score calculation
self.benzinga_symbol = algorithm.add_data(BenzingaNews, symbol).symbol
# Historical data
history = algorithm.history(self.benzinga_symbol, 14, Resolution.DAILY)
algorithm.debug(f"We got {len(history)} items from our history request")
def dispose(self) -> None:
# Unsubscribe from Benzinga news feed for this security to release computational resources
self.algorithm.remove_security(self.benzinga_symbol) using System.Text.RegularExpressions;
public class BenzingaNewsDataAlgorithm : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
SetCash(100000);
AddUniverseSelection(
new ManualUniverseSelectionModel(
QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA)
));
AddAlpha(new BenzingaAlphaModel());
SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
AddRiskManagement(new NullRiskManagementModel());
SetExecution(new ImmediateExecutionModel());
}
}
public class BenzingaAlphaModel : AlphaModel
{
private Dictionary<Symbol, SymbolData> _symbolDataBySymbol = new Dictionary<Symbol, SymbolData>();
// A custom word-score map for calculating the total sentiment score
private Dictionary<string, int> _wordScores = new Dictionary<string, int>(){
{"good", 1}, {"great", 1}, {"best", 1}, {"growth", 1},
{"bad", -1}, {"terrible", -1}, {"worst", -1}, {"loss", -1}
};
public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice slice)
{
var insights = new List<Insight>();
foreach (var kvp in _symbolDataBySymbol)
{
var symbol = kvp.Key;
var symbolData = kvp.Value;
if (slice.ContainsKey(symbolData.benzingaSymbol))
{
// Assign a sentiment score to the news article by specific word appearance scoring
var contentWords = slice[symbolData.benzingaSymbol].Contents.ToLower();
var score = 0;
foreach (var entry in _wordScores)
{
score += (Regex.Matches(contentWords, entry.Key).Count * entry.Value);
}
symbolData.targetDirection = score > 0 ? InsightDirection.Up : InsightDirection.Flat;
}
// Ensure we have security data in the current Slice to avoid stale filling
if (!(slice.ContainsKey(symbol) && slice[symbol] != null && !slice[symbol].IsFillForward))
{
continue;
}
// Buy or sell if the sentiment has changed from our current holdings
if (symbolData.currentDirection != symbolData.targetDirection)
{
insights.Add(Insight.Price(symbol, TimeSpan.FromDays(14), symbolData.targetDirection));
symbolData.currentDirection = symbolData.targetDirection;
}
}
return insights;
}
public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
{
foreach (var security in changes.AddedSecurities)
{
var symbol = security.Symbol;
_symbolDataBySymbol.Add(symbol, new SymbolData(algorithm, symbol));
}
foreach (var security in changes.RemovedSecurities)
{
var symbol = security.Symbol;
if (_symbolDataBySymbol.ContainsKey(symbol))
{
_symbolDataBySymbol[symbol].dispose();
_symbolDataBySymbol.Remove(symbol);
}
}
}
}
public class SymbolData
{
private QCAlgorithm _algorithm;
public Symbol benzingaSymbol;
public InsightDirection currentDirection = InsightDirection.Flat;
public InsightDirection targetDirection = InsightDirection.Flat;
public SymbolData(QCAlgorithm algorithm, Symbol symbol)
{
_algorithm = algorithm;
// Requesting data to obtain the updated news for sentiment score calculation
benzingaSymbol = algorithm.AddData<BenzingaNews>(symbol).Symbol;
// Historical data
var history = algorithm.History<BenzingaNews>(benzingaSymbol, 14, Resolution.Daily);
algorithm.Debug($"We got {history.Count()} items from our history request");
}
public void dispose()
{
// Unsubscribe from the Benzinga feed for this security to release computational resources
_algorithm.RemoveSecurity(benzingaSymbol);
}
}