Exemple #1
0
def _get_share_filename(share, extension = None):
    _check_type(share, bb.Share, raise_err = True, expected_type_name = 'bulbea.Share')

    if extension is not None:
        _check_str(extension, raise_err = True)

    source    = share.source
    ticker    = share.ticker

    start     = _get_datetime_str(share.data.index.min(), format_ = '%Y%m%d')
    end       = _get_datetime_str(share.data.index.max(), format_ = '%Y%m%d')

    filename = '{source}_{ticker}_{start}_{end}'.format(
        source = source,
        ticker = ticker,
        start  = start,
        end    = end
    )

    if extension:
        filename = '{filename}.{extension}'.format(
            filename  = filename,
            extension = extension
        )

    return filename
Exemple #2
0
    def __init__(self, source, ticker, start = None, end = None, latest = None, cache = False):
        _check_str(source, raise_err = True)
        _check_str(ticker, raise_err = True)

        envvar = AppConfig.ENVIRONMENT_VARIABLE['quandl_api_key']

        if not _check_environment_variable_set(envvar):
            message = Color.warn("Environment variable {envvar} for Quandl hasn't been set. A maximum of {max_calls} calls per day can be made. Visit {url} to get your API key.".format(envvar = envvar, max_calls = QUANDL_MAX_DAILY_CALLS, url = ABSURL_QUANDL))

            warnings.warn(message)
        else:
            quandl.ApiConfig.api_key = os.getenv(envvar)

        self.source    = source
        self.ticker    = ticker

        self.update(start = start, end = end, latest = latest, cache = cache)
Exemple #3
0
    def save(self, format_ = 'csv', filename = None):
        '''
        :param format_: type of format to save the Share object, default 'csv'.
        :type format_: :obj:`str`
        '''
        if format_ not in SHARE_ACCEPTED_SAVE_FORMATS:
            raise ValueError('Format {format_} not accepted. Accepted formats are: {accepted_formats}'.format(
                format_          = format_,
                accepted_formats = SHARE_ACCEPTED_SAVE_FORMATS
            ))

        if filename is not None:
            _check_str(filename, raise_err = True)
        else:
            filename = _get_share_filename(self, extension = format_)

        if format_ is 'csv':
            self.data.to_csv(filename)
Exemple #4
0
    def plot(self,
             attrs='Close',
             global_mean=False,
             bollinger_bands=False,
             period=50,
             bandwidth=1,
             subplots=False,
             *args,
             **kwargs):
        '''
        :param attrs: `str` or `list` of attribute names of a share to plot, defaults to *Close* attribute
        :type attrs: :obj: `str`, :obj:`list`

        :Example:

        >>> import bulbea as bb
        >>> share = bb.Share(source = 'YAHOO', ticker = 'AAPL')
        >>> share.plot()
        '''
        _check_iterable(attrs, raise_err=True)

        if _check_str(attrs):
            attrs = [attrs]

        plot_stats = global_mean or bollinger_bands
        subplots = True if len(attrs) != 1 and plot_stats else subplots
        axes = self.data[attrs].plot(subplots=subplots, *args, **kwargs)

        if plot_stats:
            if subplots:
                for i, attr in enumerate(attrs):
                    data = self.data[attr]
                    ax = axes[i]

                    if global_mean:
                        _plot_global_mean(data, ax)

                    if bollinger_bands:
                        _plot_bollinger_bands(data,
                                              ax,
                                              period=period,
                                              bandwidth=bandwidth)
            else:
                attr = attrs[0]
                data = self.data[attr]

                if global_mean:
                    _plot_global_mean(data, axes)

                if bollinger_bands:
                    _plot_bollinger_bands(data,
                                          axes,
                                          period=period,
                                          bandwidth=bandwidth)

        return axes
Exemple #5
0
    def __init__(self, source, ticker, start = None, end = None, latest = None, cache = False, data = None):
        _check_str(source, raise_err = True)
        _check_str(ticker, raise_err = True)

        envvar = AppConfig.ENVIRONMENT_VARIABLE['quandl_api_key']

        if not _check_environment_variable_set(envvar):
            message = Color.warn("Environment variable {envvar} for Quandl hasn't been set. A maximum of {max_calls} calls per day can be made. Visit {url} to get your API key.".format(envvar = envvar, max_calls = QUANDL_MAX_DAILY_CALLS, url = ABSURL_QUANDL))

            warnings.warn(message)
        else:
            quandl.ApiConfig.api_key = os.getenv(envvar)

        self.source    = source
        self.ticker    = ticker

        if data is not None:
            self.data    = data
            self.length  =  len(self.data)
            self.attrs   = list(self.data.columns)
        else:
            self.update(start = start, end = end, latest = latest, cache = cache)
Exemple #6
0
    def __init__(self,
                 source,
                 ticker,
                 start=None,
                 end=None,
                 latest=None,
                 cache=False):
        _check_str(source, raise_err=True)
        _check_str(ticker, raise_err=True)

        quandl_api_key = AppConfig.ENVIRONMENT_VARIABLE['quandl_api_key']

        if not _check_environment_variable_set(quandl_api_key):
            warnings.warn(
                "Environment variable {quandl_api_key} for Quandl hasn't been set. "
                .format(quandl_api_key=quandl_api_key),
                " A maximum of {max_free_quandl_calls} calls per day can be made."
            )

        self.source = source
        self.ticker = ticker

        self._update(start=start, end=end, latest=latest, cache=cache)
Exemple #7
0
    def plot(self,
             attrs           = 'Close',
             global_mean     = False,
             bollinger_bands = False,
             period          = 50,
             bandwidth       = 1,
             subplots        = False, *args, **kwargs):
        '''
        :param attrs: `str` or `list` of attribute names of a share to plot, defaults to *Close* attribute
        :type attrs: :obj: `str`, :obj:`list`

        :Example:

        >>> import bulbea as bb
        >>> share = bb.Share(source = 'YAHOO', ticker = 'AAPL')
        >>> share.plot()
        '''
        _check_iterable(attrs, raise_err = True)

        if _check_str(attrs):
            attrs  = [attrs]

        plot_stats = global_mean or bollinger_bands
        subplots   = True if len(attrs) != 1 and plot_stats else subplots
        axes       = self.data[attrs].plot(subplots = subplots, *args, **kwargs)

        if plot_stats:
            if subplots:
                for i, attr in enumerate(attrs):
                    data = self.data[attr]
                    ax   = axes[i]

                    if global_mean:
                        _plot_global_mean(data, ax)

                    if bollinger_bands:
                        _plot_bollinger_bands(data, ax, period = period, bandwidth = bandwidth)
            else:
                attr = attrs[0]
                data = self.data[attr]

                if global_mean:
                    _plot_global_mean(data, axes)

                if bollinger_bands:
                    _plot_bollinger_bands(data, axes, period = period, bandwidth = bandwidth)

        return axes
Exemple #8
0
    def bollinger_bands(self, attrs='Close', period=50, bandwidth=1):
        '''
        Returns the Bollinger Bands (R) for each attribute.

        :param attrs: `str` or `list` of attribute name(s) of a share, defaults to *Close*
        :type attrs: :obj:`str`, :obj:`list`

        :param period: length of the window to compute moving averages, upper and lower bands
        :type period: :obj:`int`

        :param bandwidth: multiple of the standard deviation of upper and lower bands
        :type bandwidth: :obj:`int`

        :Example:

        >>> import bulbea as bb
        >>> share     = bb.Share(source = 'YAHOO', ticker = 'AAPL')
        >>> bollinger = share.bollinger_bands()
        >>> bollinger.tail()
                    Lower (Close)  Mean (Close)  Upper (Close)
        Date
        2017-03-07     815.145883    831.694803     848.243724
        2017-03-08     816.050821    832.574004     849.097187
        2017-03-09     817.067353    833.574805     850.082257
        2017-03-10     817.996674    834.604404     851.212135
        2017-03-13     819.243360    835.804605     852.365849
        '''
        _check_iterable(attrs, raise_err=True)

        if _check_str(attrs):
            attrs = [attrs]

        frames = list()

        for attr in attrs:
            data = self.data[attr]
            lowr, mean, upper = _get_bollinger_bands(data,
                                                     period=period,
                                                     bandwidth=bandwidth)
            bollinger_bands = pd.concat([lowr, mean, upper], axis=1)
            bollinger_bands.columns = _get_bollinger_bands_columns(
                bollinger_bands)

            frames.append(bollinger_bands)

        return frames[0] if len(frames) == 1 else frames
Exemple #9
0
    def bollinger_bands(self,
                        attrs     = 'Close',
                        period    = 50,
                        bandwidth = 1):
        '''
        Returns the Bollinger Bands (R) for each attribute.

        :param attrs: `str` or `list` of attribute name(s) of a share, defaults to *Close*
        :type attrs: :obj:`str`, :obj:`list`

        :param period: length of the window to compute moving averages, upper and lower bands
        :type period: :obj:`int`

        :param bandwidth: multiple of the standard deviation of upper and lower bands
        :type bandwidth: :obj:`int`

        :Example:

        >>> import bulbea as bb
        >>> share     = bb.Share(source = 'YAHOO', ticker = 'AAPL')
        >>> bollinger = share.bollinger_bands()
        >>> bollinger.tail()
                    Lower (Close)  Mean (Close)  Upper (Close)
        Date
        2017-03-07     815.145883    831.694803     848.243724
        2017-03-08     816.050821    832.574004     849.097187
        2017-03-09     817.067353    833.574805     850.082257
        2017-03-10     817.996674    834.604404     851.212135
        2017-03-13     819.243360    835.804605     852.365849
        '''
        _check_iterable(attrs, raise_err = True)

        if _check_str(attrs):
            attrs = [attrs]

        frames = list()

        for attr in attrs:
            data                    = self.data[attr]
            lowr, mean, upper       = _get_bollinger_bands(data, period = period, bandwidth = bandwidth)
            bollinger_bands         = pd.concat([lowr, mean, upper], axis = 1)
            bollinger_bands.columns = _get_bollinger_bands_columns(bollinger_bands)

            frames.append(bollinger_bands)

        return frames[0] if len(frames) == 1 else frames