Exemple #1
0
def get_zacks_target_price_cnt(ticker: str, start_date: datetime,
                               end_date: datetime):
    """
      retrieves the 'zacks_target_price_cnt' data point for the supplied date 
      range. see the '_get_company_historical_data()' pydoc for specific information
      or parameters, return types and exceptions.
    """
    return _aggregate_by_year_month(
        _get_company_historical_data(ticker,
                                     intrinio_util.date_to_string(start_date),
                                     intrinio_util.date_to_string(end_date),
                                     'zacks_target_price_cnt'))
Exemple #2
0
def get_sma_indicator(ticker: str, start_date: datetime, end_date: datetime,
                      period_days: int):
    '''
      Returns a dictionary of SMA (simple moving average) indicators given a 
      ticker symbol, a date range and the period.  

      Currently only returns one page of 100 results

      Parameters
      ----------
      ticker : str
        Ticker Symbol
      start_date : object
        The beginning price date as python date object
      end_date : object
        The end price date as python date object
      period_days: int
        The number of price days included in this average


      Returns
      -----------
      a dictionary of date->price like this
      {
        "2020-05-29": 282.51779999999997,
        "2020-05-28": 281.09239999999994,
        "2020-05-27": 279.7845999999999,
        "2020-05-26": 278.26659999999987,
        "2020-05-22": 277.4913999999999,
        "2020-05-21": 276.07819999999987,
        "2020-05-20": 275.2497999999999
      }

    '''

    start_date_str = intrinio_util.date_to_string(start_date).replace(
        '-', '').replace('-', '')
    end_date_str = intrinio_util.date_to_string(end_date).replace('-',
                                                                  '').replace(
                                                                      '-', '')

    sma_dict = {}

    cache_key = "%s-%s-%s-%s-%d-%s" % (INTRINIO_CACHE_PREFIX, ticker,
                                       start_date_str, end_date_str,
                                       period_days, "tech-sma")
    api_response = cache.read(cache_key)

    if api_response is None:
        try:
            api_response = SECURITY_API.get_security_price_technicals_sma(
                ticker,
                period=period_days,
                price_key='close',
                start_date=start_date,
                end_date=end_date,
                page_size=100)

            cache.write(cache_key, api_response)
        except ApiException as ae:
            raise DataError(
                "API Error while reading SMA indicator from Intrinio Security API: ('%s', %s - %s (%d))"
                % (ticker, start_date_str, end_date_str, period_days), ae)
        except Exception as e:
            raise ValidationError(
                "Unknown Error while reading SMA indicator from Intrinio Security API: ('%s', %s - %s (%d))"
                % (ticker, start_date_str, end_date_str, period_days), e)

    sma_list = api_response.technicals

    if len(sma_list) == 0:
        raise DataError(
            "No SMA indicators returned from Intrinio Security API: ('%s', %s - %s (%d))"
            % (ticker, start_date_str, end_date_str, period_days), None)

    for sma in sma_list:
        sma_dict[intrinio_util.date_to_string(sma.date_time)] = sma.sma

    return sma_dict
Exemple #3
0
def get_macd_indicator(ticker: str, start_date: datetime, end_date: datetime,
                       fast_period: int, slow_period: int, signal_period: int):
    '''
      Returns a dictionary of MACD indicators given a ticker symbol,
      a date range and necessary MACD parameters.  
      Currently only returns one page of 100 results

      Parameters
      ----------
      ticker : str
        Ticker Symbol
      start_date : object
        The beginning price date as python date object
      end_date : object
        The end price date as python date object
      fast_period: int
        the MACD fast period parameter
      slow_perdiod: int
        the MACD slow period parameter
      signal_period:
        the MACD signal period parameter

      Returns
      -----------
      a dictionary of date->price like this
      {
          "2020-05-29": {
              "macd_histogram": -0.5565262759342229,
              "macd_line": 9.361568685377279,
              "signal_line": 9.918094961311501
          },
          "2020-05-28": {
              "macd_histogram": -0.3259226480613542,
              "macd_line": 9.731303882233703,
              "signal_line": 10.057226530295058
          }
      }
    '''

    start_date_str = intrinio_util.date_to_string(start_date).replace(
        '-', '').replace('-', '')
    end_date_str = intrinio_util.date_to_string(end_date).replace('-',
                                                                  '').replace(
                                                                      '-', '')

    macd_dict = {}

    cache_key = "%s-%s-%s-%s-%d.%d.%d-%s" % (
        INTRINIO_CACHE_PREFIX, ticker, start_date_str, end_date_str,
        fast_period, slow_period, signal_period, "tech-macd")
    api_response = cache.read(cache_key)

    if api_response is None:
        try:
            api_response = SECURITY_API.get_security_price_technicals_macd(
                ticker,
                fast_period=fast_period,
                slow_period=slow_period,
                signal_period=signal_period,
                price_key='close',
                start_date=start_date,
                end_date=end_date,
                page_size=100)

            cache.write(cache_key, api_response)
        except ApiException as ae:
            raise DataError(
                "API Error while reading MACD indicator from Intrinio Security API: ('%s', %s - %s (%d, %d, %d))"
                % (ticker, start_date_str, end_date_str, fast_period,
                   slow_period, signal_period), ae)
        except Exception as e:
            raise ValidationError(
                "Unknown Error while reading MACD indicator from Intrinio Security API: ('%s', %s - %s (%d, %d, %d))"
                % (ticker, start_date_str, end_date_str, fast_period,
                   slow_period, signal_period), e)

    macd_list = api_response.technicals

    if len(macd_list) == 0:
        raise DataError(
            "No MACD indicators returned from Intrinio Security API: ('%s', %s - %s (%d, %d, %d))"
            % (ticker, start_date_str, end_date_str, fast_period, slow_period,
               signal_period), None)

    for macd in macd_list:
        macd_dict[intrinio_util.date_to_string(macd.date_time)] = {
            "macd_histogram": macd.macd_histogram,
            "macd_line": macd.macd_line,
            "signal_line": macd.signal_line
        }

    return macd_dict
Exemple #4
0
def get_daily_stock_close_prices(ticker: str, start_date: datetime,
                                 end_date: datetime):
    '''
      Returns a list of historical daily stock prices given a ticker symbol and
      a range of dates.  Currently only returns one page of 100 results

      Parameters
      ----------
      ticker : str
        Ticker Symbol
      start_date : object
        The beginning price date as python date object
      end_date : object
        The end price date as python date object

      Returns
      -----------
      a dictionary of date->price like this
      {
        '2019-10-01': 100,
        '2019-10-02': 101,
        '2019-10-03': 102,
        '2019-10-04': 103,
      }
    '''

    start_date_str = intrinio_util.date_to_string(start_date).replace('-', '')
    end_date_str = intrinio_util.date_to_string(end_date).replace('-', '')

    price_dict = {}

    cache_key = "%s-%s-%s-%s-%s" % (INTRINIO_CACHE_PREFIX, ticker,
                                    start_date_str, end_date_str,
                                    "closing-prices")
    api_response = cache.read(cache_key)

    if api_response is None:
        try:
            api_response = SECURITY_API.get_security_stock_prices(
                ticker,
                start_date=start_date_str,
                end_date=end_date_str,
                frequency='daily',
                page_size=100)
            cache.write(cache_key, api_response)
        except ApiException as ae:
            raise DataError(
                "API Error while reading price data from Intrinio Security API: ('%s', %s - %s)"
                % (ticker, start_date_str, end_date_str), ae)
        except Exception as e:
            raise ValidationError(
                "Unknown Error while reading price data from Intrinio Security API: ('%s', %s - %s)"
                % (ticker, start_date_str, end_date_str), e)

    price_list = api_response.stock_prices

    if len(price_list) == 0:
        raise DataError(
            "No prices returned from Intrinio Security API: ('%s', %s - %s)" %
            (ticker, start_date_str, end_date_str), None)

    for price in price_list:
        price_dict[intrinio_util.date_to_string(price.date)] = price.close

    return price_dict