Indicators

The Indicators class provides methods to interact with various technical indicator endpoints of the Alphavantage API. It includes methods for fetching different types of moving averages and other technical indicators for a given stock symbol.

Methods

1. Private Method: getIndicator

This is a private method used internally to fetch data for various technical indicators.

Signature:

private function getIndicator(
    string $function, 
    string $symbol, 
    string $interval, 
    int $timePeriod, 
    string $seriesType, 
    ?string $month = null, 
    string $dataType = 'json'
): array

Parameters:

  • function (string): The technical indicator function to call (e.g., 'SMA').

  • symbol (string): The stock symbol to fetch data for.

  • interval (string): The time interval between data points. Must be one of: '1min', '5min', '15min', '30min', '60min', 'daily', 'weekly', 'monthly'.

  • timePeriod (int): The number of data points used to calculate the indicator.

  • seriesType (string): The desired price series type. Must be one of: 'close', 'open', 'high', 'low'.

  • month (string|null): Optional month parameter, valid only for intraday intervals.

  • dataType (string): The data type of the response. Must be 'json' or 'csv'.

Returns:

  • array: An array containing the indicator data.

Exceptions:

  • ApiVolumeReached

  • ConnectionException

  • InvalidArgumentException: Thrown if any of the parameters are invalid.

2. Simple Moving Average (SMA)

Fetches the Simple Moving Average (SMA) for a given stock symbol.

Signature:

public function sma(
    string $symbol, 
    string $interval, 
    int $timePeriod, 
    string $seriesType, 
    ?string $month = null, 
    string $dataType = 'json'
): array

Parameters:

  • symbol (string): The stock symbol to fetch the SMA for.

  • interval (string): The time interval between data points. Must be one of: '1min', '5min', '15min', '30min', '60min', 'daily', 'weekly', 'monthly'.

  • timePeriod (int): The number of data points used to calculate the SMA.

  • seriesType (string): The desired price series type. Must be one of: 'close', 'open', 'high', 'low'.

  • month (string|null): Optional month parameter, valid only for intraday intervals.

  • dataType (string): The data type of the response. Must be 'json' or 'csv'.

Returns:

  • array: An array containing the SMA data.

Exceptions:

  • ApiVolumeReached

  • ConnectionException

  • InvalidArgumentException: Thrown if any of the parameters are invalid.


The Indicators class is designed to provide an easy way to fetch various technical indicators using the Alphavantage API. The getIndicator method serves as a utility function to handle common logic for different indicators, while the sma method provides a specific implementation for fetching the Simple Moving Average. Be sure to handle the possible exceptions when using these methods to ensure robust error handling in your application.

Last updated