EOD Historical Data
Macroeconomics Indicators
Introduction
The Macroeconomic Indicators dataset, provided by EODHD, provides data on 39 major macroeconomic indicators of 249 countries and regions. The data starts in January 1998, and is delivered on indicators update.
For more information about the Macroeconomics Indicators dataset, including CLI commands and pricing, see the dataset listing.
Getting Started
The following snippet demonstrates how to request data from the Macroeconomic Indicators dataset:
self.add_data(EODHDMacroIndicators, Country.UNITED_STATES) self.add_data(EODHDMacroIndicators, EODHD.MacroIndicators.UnitedStates.GDP_CURRENT_USD)
AddData<EODHDMacroIndicators>(Country.UnitedStates); AddData<EODHDMacroIndicators>(EODHD.MacroIndicators.UnitedStates.GdpCurrentUsd);
Requesting Data
To add Macroeconomic Indicators data to your algorithm, call the AddData<EODHDMacroIndicators>
add_data
method.
class EODHDMacroIndicatorsAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2019, 1, 1) self.set_end_date(2020, 6, 1) self.set_cash(100000) self.dataset_symbol = self.add_data( EODHDMacroIndicators, EODHD.MacroIndicators.UnitedStates.GDP_CURRENT_USD # Country.UNITED_STATES ).symbol
namespace QuantConnect.Algorithm.CSharp.AltData { public class EODHDMacroIndicatorsAlgorithm : QCAlgorithm { private Symbol _datasetSymbol; public override void Initialize() { SetStartDate(2019, 1, 1); SetEndDate(2020, 6, 1); SetCash(100000); _datasetSymbol = AddData<EODHDMacroIndicators>( EODHD.MacroIndicators.UnitedStates.GdpCurrentUsd ).Symbol; // _datasetSymbol = AddData<EODHDMacroIndicators>(Country.UnitedStates).Symbol; } } }
Accessing Data
To get the current Macroeconomic Indicators 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): data_point = slice[self.dataset_symbol] self.log(f"{data_point.frequency} {data_point.indicator} of {data_point.country} at {data_point.time} with value {data_point.value}")
public override void OnData(Slice slice) { if (slice.ContainsKey(_datasetSymbol)) { var dataPoint = slice[_datasetSymbol]; Log($"{dataPoint.Frequency} {dataPoint.Indicator} of {dataPoint.Country} at {dataPoint.Time} with value {dataPoint.Value}"); } }
To iterate through all of the dataset objects in the current Slice
, call the Get
get
method.
def on_data(self, slice: Slice) -> None: for _, data_point in slice.get(EODHDMacroIndicators).items(): self.log(f"{data_point.frequency} {data_point.indicator} of {data_point.country} at {data_point.time} with value {data_point.value}")
public override void OnData(Slice slice) { foreach (var kvp in slice.Get<EODHDMacroIndicators>()) { var eventDataPoint = kvp.Value; Log($"{dataPoint.Frequency} {dataPoint.Indicator} of {dataPoint.Country} at {dataPoint.Time} with value {dataPoint.Value}"); } }
Historical Data
To get historical Macroeconomic Indicators data, call the History
history
method with the type EODHDMacroIndicators
cast and the underlying Symbol
. If there is no data in the period you request, the history result is empty.
history_bars = self.history[EODHDMacroIndicators](self.dataset_symbol, 100)
var history = History<EODHDMacroIndicators>(_datasetSymbol, 100);
For more information about historical data, see History Requests.
Example Applications
The Macroeconomic Indicators dataset provides data on global macro indicators, allowing traders to react to market sentiment. Examples include the following strategies:
- Long straddle to trade the market shock brought by the macro indicators.
- Hold long or short position of the market representative ETF based on the expected market direction.
- Statistical arbitrage on 2 or more correlated global markets based on market strength indicated by macro indicators.
Classic Algorithm Example
The following example algorithm holds long position of SPY if the US GDP is in positive growth, while holds short position if it is the opposite case.
class EODHDMacroIndicatorsAlgorithm(QCAlgorithm): def Initialize(self): self.set_start_date(2020, 10, 7) self.equity_symbol = self.add_equity("SPY", Resolution.Daily).symbol ticker = EODHD.MacroIndicators.UnitedStates.GDP_GROWTH_ANNUAL self.dataset_symbol = self.add_data(EODHDMacroIndicators, ticker).Symbol def OnData(self, slice): indicators = slice.get(EODHDMacroIndicators).get(self.dataset_symbol) if indicators: gdp = indicators.data[0].value self.SetHoldings(self.equity_symbol, 1 if gdp > 0 else -1)
namespace QuantConnect.DataLibrary.Tests { public class EODHDMacroIndicatorsAlgorithm : QCAlgorithm { private Symbol _equitySymbol, _datasetSymbol; public override void Initialize() { SetStartDate(2020, 10, 07); _equitySymbol = AddEquity("SPY", Resolution.Daily).Symbol; var ticker = EODHD.MacroIndicators.UnitedStates.GdpGrowthAnnual; _datasetSymbol = AddData<EODHDMacroIndicators>(ticker).Symbol; } public override void OnData(Slice slice) { if (slice.Get<EODHDMacroIndicators>().TryGetValue(_datasetSymbol, out var indicators)) { var gdp = indicators.FirstOrDefault() as EODHDMacroIndicator; SetHoldings(_equitySymbol, gdp.Value > 0m ? 1 : -1); } } } }
Framework Algorithm Example
The following example algorithm holds the long position of SPY if the US GDP is in positive growth while holding a short position if it is the opposite case. It is implemented in the framework algorithm.
class EODHDMacroIndicatorsAlgorithm(QCAlgorithm): def initialize(self) -> None: # Use market ETF as a vehicle to trade. symbol = Symbol.create("SPY", SecurityType.EQUITY, Market.USA) self.add_universe_selection(ManualUniverseSelectionModel(symbol)) # Custom alpha model to emit insights according to macroeconomic indicators. self.add_alpha(MacroIndicatorAlphaModel(self)) # Equal weighting position sizing to dissipate capital risk evenly. self.set_portfolio_construction(EqualWeightingPortfolioConstructionModel(Expiry.END_OF_MONTH)) class MacroIndicatorAlphaModel(AlphaModel): def __init__(self, algorithm: QCAlgorithm) -> None: # Request US GDP growth indicator data to generate trade signal. ticker = EODHD.MacroIndicators.UnitedStates.GDP_GROWTH_ANNUAL self.dataset_symbol = algorithm.add_data(EODHDMacroIndicators, ticker).symbol self._symbol = None def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]: # Trade based on the updated macroeconomic indicator. indicators = slice.get(EODHDMacroIndicators).get(self.dataset_symbol) if not indicators: return [] gdp = indicators.data[0].value # If the GDP gap is positive, the market ETF is expected to rise. # Otherwise, it is expected the market ETF will drop. direction = InsightDirection.UP if gdp > 0 else InsightDirection.DOWN return [Insight.price(self._symbol, timedelta(5), direction)] def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None: for added in changes.added_securities: self._symbol = added.symbol
namespace QuantConnect.DataLibrary.Tests { public class EODHDMacroIndicatorsAlgorithm : QCAlgorithm { public override void Initialize() { // Use market ETF as a vehicle to trade. var symbol = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA); AddUniverseSelection(new ManualUniverseSelectionModel(symbol)); // Custom alpha model to emit insights according to macroeconomic indicators. AddAlpha(new MacroIndicatorAlphaModel(this)); // Equal weighting position sizing to dissipate capital risk evenly. SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel(Expiry.EndOfMonth)); } } public class MacroIndicatorAlphaModel : AlphaModel { private Symbol _symbol, _datasetSymbol; public MacroIndicatorAlphaModel(QCAlgorithm algorithm) { // Request US GDP growth indicator data to generate trade signals. var ticker = EODHD.MacroIndicators.UnitedStates.GdpGrowthAnnual; _datasetSymbol = algorithm.AddData<EODHDMacroIndicators>(ticker).Symbol; } public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice slice) { // Trade based on the updated macroeconomic indicator. if (slice.Get<EODHDMacroIndicators>().TryGetValue(_datasetSymbol, out var indicators)) { var gdp = indicators.FirstOrDefault() as EODHDMacroIndicator; // If the GDP growth is positive, the market ETF is expected to rise. // Otherwise, it is expected the market ETF will drop. var direction = gdp.Value > 0m ? InsightDirection.Up : InsightDirection.Down; yield return Insight.Price(_symbol, TimeSpan.FromDays(5), direction); } } public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { foreach (var added in changes.AddedSecurities) { _symbol = added.Symbol; } } } }
Data Point Attributes
The EODHD Macro Indicators dataset provides EODHDMacroIndicators
objects, which have the following attributes:
MacroIndicators Enumeration
To filter the specific indicator, you can make use of the MacroIndicators
enumeration. The MacroIndicators
enumeration has the following members:
Country Enumeration
To filter the country of the macro indicators, you can make use of the Country
enumeration. The Country
enumeration has the following members:
Frequency Enumeration
To filter the indicator frequency of the macro indicators, you can make use of the Frequency
enumeration. The Frequency
enumeration has the following members: