Example #1
0
    def history(
        self,
        period="ytd",
        interval="1d",
        start=None,
        end=None,
        adj_timezone=True,
        adj_ohlc=False,
    ):
        """
        Historical pricing data

        Pulls historical pricing data for a given symbol(s)

        Parameters
        ----------
        period: str, default ytd, optional
            Length of time
        interval: str, default 1d, optional
            Time between data points
        start: str or datetime.datetime, default None, optional
            Specify a starting point to pull data from.  Can be expressed as a
            string with the format YYYY-MM-DD or as a datetime object
        end: str of datetime.datetime, default None, optional
            Specify a ending point to pull data from.  Can be expressed as a
            string with the format YYYY-MM-DD or as a datetime object.
        adj_timezone: bool, default True, optional
            Specify whether or not to apply the GMT offset to the timestamp
            received from the API.  If True, the datetimeindex will be adjusted
            to the specified ticker's timezone.
        adj_ohlc: bool, default False, optional
            Calculates an adjusted open, high, low and close prices according
            to split and dividend information

        Returns
        -------
        pandas.DataFrame
            historical pricing data
        """
        config = self._CONFIG["chart"]
        intervals = config["query"]["interval"]["options"]
        if start or period is None or period.lower() == "max":
            start = _convert_to_timestamp(start)
            end = _convert_to_timestamp(end, start=False)
            params = {"period1": start, "period2": end}
        else:
            params = {"range": period.lower()}
        if interval not in intervals:
            raise ValueError(
                "Interval values must be one of {}".format(", ".join(intervals))
            )
        params["interval"] = interval.lower()
        if params["interval"] == "1m" and period == "1mo":
            df = self._history_1m(adj_timezone, adj_ohlc)
        else:
            data = self._get_data("chart", params)
            df = self._historical_data_to_dataframe(data, params, adj_timezone)
        if adj_ohlc and "adjclose" in df:
            df = self._adjust_ohlc(df)
        return df
Example #2
0
    def history(
            self,
            period='ytd',
            interval='1d',
            start=None,
            end=None,
            adj_timezone=True):
        """
        Historical pricing data

        Pulls historical pricing data for a given symbol(s)

        Parameters
        ----------
        period: str, default ytd, optional
            Length of time
        interval: str, default 1d, optional
            Time between data points
        start: str or datetime.datetime, default None, optional
            Specify a starting point to pull data from.  Can be expressed as a
            string with the format YYYY-MM-DD or as a datetime object
        end: str of datetime.datetime, default None, optional
            Specify a ending point to pull data from.  Can be expressed as a
            string with the format YYYY-MM-DD or as a datetime object.
        adj_timezone: bool, default True, optional
            Specify whether or not to apply the GMT offset to the timestamp
            received from the API.  If True, the datetimeindex will be adjusted
            to the specified ticker's timezone.

        Returns
        -------
        pandas.DataFrame
            historical pricing data
        """
        config = self._CONFIG['chart']
        periods = config['query']['range']['options']
        intervals = config['query']['interval']['options']
        if start or period is None or period.lower() == 'max':
            start = _convert_to_timestamp(start)
            end = _convert_to_timestamp(end, start=False)
            params = {'period1': start, 'period2': end}
        else:
            period = period.lower()
            if period not in periods:
                raise ValueError("Period values must be one of {}".format(
                    ', '.join(periods)))
            params = {'range': period}
        if interval not in intervals:
            raise ValueError("Interval values must be one of {}".format(
                ', '.join(intervals)))
        params['interval'] = interval.lower()
        data = self._get_data('chart', params)
        df = self._historical_data_to_dataframe(data, params, adj_timezone)
        return df
Example #3
0
    def news(self, count=25, start=None):
        """News articles related to given symbol(s)

        Obtain news articles related to a given symbol(s).  Data includes
        the title of the article, summary, url, author_name, publisher

        Parameters
        ----------
        count: int
            Desired number of news items to return
        start: str or datetime
            Date to begin retrieving news items.  If date is a str, utilize
            the following format: YYYY-MM-DD.

        Notes
        -----
        It's recommended to use only one symbol for this property as the data
        returned does not distinguish between what symbol the news stories
        belong to

        Returns
        -------
        dict
        """
        if start:
            start = _convert_to_timestamp(start)
        return self._get_data('news', {
            'count': count,
            'start': start
        }, **{'list_result': True})
Example #4
0
 def _history_1m(self, adj_timezone=True, adj_ohlc=False):
     params = {"interval": "1m"}
     today = datetime.today()
     dates = [
         _convert_to_timestamp((today - timedelta(7 * x)).date())
         for x in range(5)
     ]
     dataframes = []
     for i in range(len(dates) - 1):
         params["period1"] = dates[i + 1]
         params["period2"] = dates[i]
         data = self._get_data("chart", params)
         dataframes.append(
             self._historical_data_to_dataframe(data, params, adj_timezone))
     df = pd.concat(dataframes, sort=True)
     df.sort_values(by=["symbol", "date"], inplace=True)
     df.fillna(value=0, inplace=True)
     return df