investfly.models.strategy.DataService

Strategy data service interface.

This module defines the DataService abstract base class that provides the interface for accessing market data, indicators, and financial information from within trading strategies.

class DataService(abc.ABC):

Interface for accessing market data and indicators from strategies.

DataService is an abstract base class that defines the interface for accessing various types of market data and computing technical indicators from within trading strategies.

This service provides access to:

  • Technical indicators (SMA, RSI, MACD, etc.)
  • Market data (quotes, bars, financials)
  • News and fundamental data

The service is implemented by the execution engine and injected into strategy instances via TradingStrategy.setDataService().

Attributes: ALL_BARS: Constant value (-1) used with getBars() to retrieve all available historical bars for a security.

Example: Strategies access the data service via self.dataService:

# Compute indicators
sma = self.dataService.computeIndicatorSeries(
    "SMA", security, {"period": 20, "barInterval": BarInterval.ONE_DAY}
)

# Get current quote
quote = self.dataService.getQuote(security)
if quote:
    current_price = quote.lastPrice

# Get historical bars
bars = self.dataService.getBars(
    security, BarInterval.ONE_DAY, numBars=50
)
ALL_BARS: int = -1

Constant for retrieving all available bars in getBars().

@abstractmethod
def computeIndicatorSeries( self, indicatorId: str, security: investfly.models.marketdata.Security, params: Dict[str, Any]) -> investfly.models.indicator.IndicatorSeries:

Compute a technical indicator series for a given security.

This method computes a technical indicator (e.g., SMA, RSI, MACD) and returns a series of indicator values over time. The series can be used for analysis, signal generation, and crossover detection.

The indicatorId parameter is declared as a string (not an enum) to support both standard indicators provided by Investfly and custom indicators defined by users. For standard indicators, the string value must match one of the values from the StandardIndicatorId enum.

Args: indicatorId: The identifier of the indicator to compute. Must be a string value. For standard indicators supported by Investfly, use one of the values from StandardIndicatorId enum:

    **Moving Averages:**
    - "SMA": Simple Moving Average
    - "EMA": Exponential Moving Average

    **Momentum Indicators:**
    - "RSI": Relative Strength Index
    - "ROC": Rate of Change
    - "CMO": Chande Momentum Oscillator
    - "CMO_SMOOTHED": Smoothed CMO
    - "PPO": Percentage Price Oscillator
    - "ULTIMATE_OSC": Ultimate Oscillator

    **Trend Indicators:**
    - "MACD": Moving Average Convergence Divergence
    - "MACDS": MACD Signal Line
    - "ADX": Average Directional Index
    - "PLUS_DI": Plus Directional Indicator
    - "MINUS_DI": Minus Directional Indicator
    - "PSAR": Parabolic SAR

    **Volatility Indicators:**
    - "ATR": Average True Range
    - "STD_DEV": Standard Deviation
    - "UPPER_BBAND": Upper Bollinger Band
    - "LOWER_BBAND": Lower Bollinger Band
    - "BBAND": Bollinger Bands (composite, for charts only)

    **Oscillators:**
    - "CCI": Commodity Channel Index
    - "WILLIAM_R": Williams' %R
    - "FAST_STOCHASTIC_OSC": Fast Stochastic Oscillator
    - "SLOW_STOCHASTIC_OSC": Slow Stochastic Oscillator
    - "STOCHASTICS": Stochastic (composite, for charts only)

    **Price Indicators:**
    - "MEDIAN_PRICE": Median Price
    - "TYPICAL_PRICE": Typical Price
    - "MAX": Maximum value over period
    - "MIN": Minimum value over period

    **Candlestick Patterns:**
    - "DOJI": Doji pattern
    - "HAMMER": Hammer pattern
    - "INVERTED_HAMMER": Inverted Hammer pattern
    - "DRAGONFLY_DOJI": Dragonfly Doji pattern
    - "GRAVESTONE_DOJI": Gravestone Doji pattern
    - "HANGING_MAN": Hanging Man pattern
    - "BULLISH": Bullish pattern
    - "BEARISH": Bearish pattern

    **Support/Resistance:**
    - "SUPPORT": Support level
    - "RESISTANCE": Resistance level

    **Other:**
    - "AVGVOLUME": Average Volume
    - "HIGH52WEEK": 52-Week High
    - "LOW52WEEK": 52-Week Low
    - "DRAWDOWN": Drawdown
    - "PRICECHANGEPCT": Price Change Percentage

    For custom indicators, use the custom indicator ID string as
    defined when the indicator was created.

security: The security for which to compute the indicator.
params: Dictionary of parameters for the indicator computation.
    Common parameters include:
    - "period" (int): Period for the indicator (e.g., 20 for SMA(20)).
    - "barInterval" (BarInterval): Bar interval for the data source.
    - Indicator-specific parameters (e.g., "fast_period", "slow_period"
      for MACD).

Returns: IndicatorSeries object containing the computed indicator values. The series provides methods for: - Accessing the latest value: series.last.value - Converting to list: series.toList() - Crossover detection: series.cross_over(other), series.cross_under(other)

Note: - The indicatorId parameter is a string type (not StandardIndicatorId enum) to support both standard and custom indicators. - When using standard indicators, the string value must exactly match one of the StandardIndicatorId enum values (see investfly.models.indicator.IndicatorEnums.StandardIndicatorId). - Custom indicators can be referenced by their custom ID string.

Example:

# Compute 20-period SMA on daily bars (standard indicator)
sma20 = self.dataService.computeIndicatorSeries(
    "SMA",  # Must match StandardIndicatorId.SMA.value
    security,
    {"period": 20, "barInterval": BarInterval.ONE_DAY}
)

# Compute 14-period RSI (standard indicator)
rsi = self.dataService.computeIndicatorSeries(
    "RSI",  # Must match StandardIndicatorId.RSI.value
    security,
    {"period": 14, "barInterval": BarInterval.ONE_DAY}
)

# Compute custom indicator
custom_indicator = self.dataService.computeIndicatorSeries(
    "MY_CUSTOM_INDICATOR",  # Custom indicator ID
    security,
    {"param1": 10, "barInterval": BarInterval.ONE_DAY}
)

# Check for crossover
if sma20.cross_over(rsi):
    # SMA crossed above RSI - bullish signal
    pass

# Access latest value
current_rsi = rsi.last.value
@abstractmethod
def getFinancials( self, symbol: str) -> Dict[investfly.models.marketdata.FinancialField, float]:

Retrieve fundamental financial metrics for the given symbol.

This method returns fundamental financial data such as market cap, P/E ratio, revenue, earnings, etc. for a security.

Args: symbol: The stock symbol (e.g., "AAPL", "MSFT") for which to retrieve financial data.

Returns: Dictionary mapping FinancialField enums to their corresponding float values. Returns an empty dictionary if data is unavailable.

Example:

financials = self.dataService.getFinancials("AAPL")
market_cap = financials.get(FinancialField.MARKET_CAP)
pe_ratio = financials.get(FinancialField.PE_RATIO)
@abstractmethod
def getQuote( self, security: investfly.models.marketdata.Security) -> investfly.models.marketdata.Quote:

Retrieve the latest quote for the given security.

This method returns the most recent quote data including bid/ask prices, last trade price, volume, and other market data fields.

Args: security: The Security object for which to retrieve the quote.

Returns: Quote object containing the latest market data.

Raises: NoDataException: If a quote is not available for the requested security.

@abstractmethod
def getNews( self, security: investfly.models.marketdata.Security) -> List[investfly.models.marketdata.StockNews]:

Retrieve latest news articles for the given security.

This method returns recent news articles related to the specified security. News data can be used for sentiment analysis or event-driven trading strategies.

Args: security: The Security object for which to retrieve news.

Returns: List of StockNews objects containing news articles. Returns an empty list if no news is available. Each StockNews object contains: - title: News article title - content: Article content or summary - publishedDate: Publication date - source: News source - url: Article URL

Example:

news = self.dataService.getNews(security)
for article in news:
    print(f"{article.title} - {article.publishedDate}")
@abstractmethod
def getBars( self, security: investfly.models.marketdata.Security, barInterval: investfly.models.marketdata.BarInterval, numBars: int) -> List[investfly.models.marketdata.Bar]:

Retrieve historical bars (candlestick/OHLC data) for a security.

This method returns historical price bars at the specified interval. Bars contain open, high, low, close (OHLC) prices and volume data.

Args: security: The Security object for which to fetch bars. barInterval: The interval of bars to retrieve. Options include: - BarInterval.ONE_MINUTE - BarInterval.FIVE_MINUTE - BarInterval.FIFTEEN_MINUTE - BarInterval.THIRTY_MINUTE - BarInterval.SIXTY_MINUTE - BarInterval.ONE_DAY numBars: Number of bars to return. Use DataService.ALL_BARS to retrieve all available bars. Bars are returned in chronological order (oldest first).

Returns: List of Bar objects containing OHLC data. Each Bar object contains: - open: Opening price - high: Highest price - low: Lowest price - close: Closing price - volume: Trading volume - timestamp: Bar timestamp

Example:

# Get last 50 daily bars
bars = self.dataService.getBars(
    security, BarInterval.ONE_DAY, numBars=50
)

# Get all available 1-minute bars
all_bars = self.dataService.getBars(
    security, BarInterval.ONE_MINUTE, DataService.ALL_BARS
)

# Access bar data
latest_bar = bars[-1]
close_price = latest_bar.close
@abstractmethod
def runMarketQuery( self, request: investfly.models.strategy.MarketQueryRequest) -> List[investfly.models.marketdata.Security]:

Run a market query using a market query request.

This method executes a screener query to find securities that match the given filter expression. The query is executed against the universe of securities available to this strategy.

Args: request: MarketQueryRequest containing: - securityFilterExpression: SecurityFilterExpression with filter criteria. The expression can include filters on: - Quote fields (price, volume, etc.) - Financial data (market cap, P/E ratio, etc.) - Technical indicators (SMA, RSI, etc.) - sortBy: Optional SortBySpec for sorting and limiting results.

Returns: List of Security objects that match the filter criteria. Returns an empty list if no securities match.

Example:

from investfly.models.strategy.SecurityFilterExpression import SecurityFilterExpression
from investfly.models.strategy.MarketQueryRequest import MarketQueryRequest
from investfly.models.strategy.DataParams import DataParam, DataType
from investfly.models.marketdata.QuoteField import QuoteField

# Create a filter expression: price > 100
filter_expr = SecurityFilterExpression("price > 100")
price_param = DataParam(DataType.QUOTE, quoteField=QuoteField.LAST_PRICE)
filter_expr.addDataParam("price", price_param)

# Create market query request
query_request = MarketQueryRequest(securityFilterExpression=filter_expr)

# Run the query
matching_securities = self.dataService.runMarketQuery(query_request)
for security in matching_securities:
    print(f"Found: {security.symbol}")