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
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
def split(share, attrs = 'Close', window = 0.01, train = 0.60, shift = 1, normalize = False, provider = 'quandl'): ''' :param attrs: `str` or `list` of attribute names of a share, defaults to *Close* attribute :type attrs: :obj: `str`, :obj:`list` ''' _check_type(share, type_ = bb.Share, raise_err = True, expected_type_name = 'bulbea.Share') _check_iterable(attrs, raise_err = True) _check_int(shift, raise_err = True) _check_real(window, raise_err = True) _check_real(train, raise_err = True) _validate_in_range(window, 0, 1, raise_err = True) _validate_in_range(train, 0, 1, raise_err = True) if(provider == 'alphavantage'): df = pd.DataFrame(share.data) data = df.ix[3] data = pd.to_numeric(data) else: data = share.data[attrs] length = len(share) window = int(np.rint(length * window)) offset = shift - 1 splits = np.array([data[i if i is 0 else i + offset: i + window] for i in range(length - window)]) if normalize: splits = np.array([_get_cummulative_return(split) for split in splits]) size = len(splits) split = int(np.rint(train * size)) train = splits[:split,:] test = splits[split:,:] Xtrain, Xtest = train[:,:-1], test[:,:-1] ytrain, ytest = train[:, -1], test[:, -1] return (Xtrain, Xtest, ytrain, ytest)
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
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
def split(sharedata, attrs = 'Close', window = 0.01, train = 0.60, shift = 1, normalize = False): ''' :type attrs: :obj: `str`, :obj:`list` ''' _check_iterable(attrs, raise_err = True) _check_int(shift, raise_err = True) _check_real(window, raise_err = True) _check_real(train, raise_err = True) _validate_in_range(train, 0, 1, raise_err = True) data = sharedata[attrs] length = len(sharedata) if window >=0 and window <= 1: window = int(np.rint(length * window)) print('====Actual window is ' + str(window)) offset = shift - 1 splits = np.array([data[i if i is 0 else i + offset: i + window] for i in range(length - window)]) normsplit = np.array([ [split[0],split[len(split)-1]] for split in splits]) if normalize: splits = np.array([_get_cummulative_return(split) for split in splits]) size = len(splits) split = int(np.rint(train * size)) train = splits[:split,:] test = splits[split:,:] Xtrain, Xtest = train[:,:-1], test[:,:-1] XtrainNorm, XtestNorm = normsplit[:split,:],normsplit[split:,:] ytrain, ytest = train[:, -1], test[:, -1] return (Xtrain, Xtest, ytrain, ytest, XtrainNorm, XtestNorm)
def split(share, attrs = 'Close', window = 0.01, train = 0.60, shift = 1, normalize = False): ''' :param attrs: `str` or `list` of attribute names of a share, defaults to *Close* attribute :type attrs: :obj: `str`, :obj:`list` ''' _check_type(share, type_ = bb.Share, raise_err = True, expected_type_name = 'bulbea.Share') _check_iterable(attrs, raise_err = True) _check_int(shift, raise_err = True) _check_real(window, raise_err = True) _check_real(train, raise_err = True) _validate_in_range(window, 0, 1, raise_err = True) _validate_in_range(train, 0, 1, raise_err = True) data = share.data[attrs] length = len(share) window = int(np.rint(length * window)) if window <= 1: window = 2 offset = shift - 1 splits = np.array([data[i if i is 0 else i + offset: i + window] for i in range(length - window)]) print("{} {}".format(window,len(splits))) if normalize: splits = np.array([_get_cummulative_return(split) for split in splits]) size = len(splits) split = int(np.rint(train * size)) train = splits[:split,:] test = splits[split:,:] Xtrain, Xtest = train[:,:-1], test[:,:-1] ytrain, ytest = train[:, -1], test[:, -1] return (Xtrain, Xtest, ytrain, ytest)
def split(share, attrs = 'Close', window = 0.01, train = 0.60, shift = 1, normalize = False): ''' :param attrs: `str` or `list` of attribute names of a share, defaults to *Close* attribute :type attrs: :obj: `str`, :obj:`list` ''' _check_type(share, type_ = bb.Share, raise_err = True, expected_type_name = 'bulbea.Share') _check_iterable(attrs, raise_err = True) _check_int(shift, raise_err = True) _check_real(window, raise_err = True) _check_real(train, raise_err = True) _validate_in_range(window, 0, 1, raise_err = True) _validate_in_range(train, 0, 1, raise_err = True) data = share.data[attrs] length = len(share) window = int(np.rint(length * window)) offset = shift - 1 splits = np.array([data[i if i is 0 else i + offset: i + window] for i in range(length - window)]) if normalize: splits = np.array([_get_cummulative_return(split) for split in splits]) size = len(splits) split = int(np.rint(train * size)) train = splits[:split,:] test = splits[split:,:] Xtrain, Xtest = train[:,:-1], test[:,:-1] ytrain, ytest = train[:, -1], test[:, -1] return (Xtrain, Xtest, ytrain, ytest)