def plotData(data,x_label,y_label,title):
    plt.rcParams["figure.figsize"] = [15.0,10.0]
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.title(title)
    plt.legend('price legend')
    data.plot()
예제 #2
0
def data_stack():
    goog = data.DataReader('GOOG',
                           start='2004',
                           end='2016',
                           data_source='google')
    goog.head()
    goog = goog['Close']
    goog.plot()

    goog.plot(alpha=0.5, style='-')
    goog.resample('BA').mean().plot(style=':')
    goog.asfreq('BA').plot(style='--')
    plt.legend(['input', 'resample', 'asfreq'], loc='upper left')

    fig, ax = plt.subplots(2, sharex=True)
    data = goog.iloc[:10]
    data.asfreq('D').plot(ax=ax[0], marker='o')
    data.asfreq('D', method='bfill').plot(ax=ax[1], style='-o')
    data.asfreq('D', method='ffill').plot(ax=ax[1], style='--o')
    ax[1].legend(["back-fill", "forward-fill"])

    ## Time-shifts
    fig, ax = plt.subplots(3, sharey=True)
    # apply a frequency to the data
    goog = goog.asfreq('D', method='pad')

    goog.plot(ax=ax[0])
    goog.shift(900).plot(ax=ax[1])
    goog.tshift(900).plot(ax=ax[2])

    # legends and annotations
    local_max = pd.to_datetime('2007-11-05')
    offset = pd.Timedelta(900, 'D')

    ax[0].legend(['input'], loc=2)
    ax[0].get_xticklabels()[2].set(weight='heavy', color='red')
    ax[0].axvline(local_max, alpha=0.3, color='red')
    ax[1].legend(['shift(900)'], loc=2)
    ax[1].get_xticklabels()[2].set(weight='heavy', color='red')
    ax[1].axvline(local_max + offset, alpha=0.3, color='red')

    ax[2].legend(['tshift(900)'], loc=2)
    ax[2].get_xticklabels()[1].set(weight='heavy', color='red')
    ax[2].axvline(local_max + offset, alpha=0.3, color='red')
    ROI = 100 * (goog.tshift(-365) / goog - 1)
    ROI.plot()
    plt.ylabel('% Return on Investment')
    # Rolling windows

    rolling = goog.rolling(365, center=True)
    data = pd.DataFrame({
        'input': goog,
        'one-year rolling_mean': rolling.mean(),
        'one-year rolling_std': rolling.std()
    })
    ax = data.plot(style=['-', '--', ':'])
    ax.lines[0].set_alpha(0.3)
예제 #3
0
파일: shoes.py 프로젝트: artopping/pydata
def fix():
    yf.pdr_override()
    stocks = ['NKE', 'ADDYY', 'UAA']
    events = {'NKE': '2003-05-21', 'ADDYY': '2015-08-15', 'UAA': '2015-09-15'}
    anno = {
        'NKE': 'Lebron James- $90 million',
        'ADDYY': 'James Harden- $14 million',
        'UAA': 'Steph Curry- $12 million'
    }

    for i in stocks:
        data = yf.download(i, start="2000-01-01", end="2018-01-01")
        data['index1'] = data.index
        point = events[i]
        #print list(data.columns.values)
        #data = [go.Scatter(x=data['index1'], y=data['Close'])]
        #url= py.plot(data, filename='basic-line')
        data.plot(x='index1', y='Close')
        plt.axvline(x=point)
        plt.xlabel('Date')
        plt.ylabel('Closing Price')
        plt.title('{} shoe deal for {}'.format(i, anno[i]))
        plt.show()
예제 #4
0
def plot_regime_color(data, lambda_value=0.1, figx=9, figy=6):
    '''
    Plot a timeseries and corresponding regimes (normal or crash) according to 
    the lambda value in input. Regimes are indetified by vertical coloured rectangles.
    The input lambda_value is the lambda parameter (scalar) used by the 
    trend-filtering algorithm.
    '''
    # compute returns
    rets = erk.compute_returns(data).dropna()
    # get betas from trend-filtering
    betas = trend_filtering(rets.values, lambda_value)
    # find regimes switching points
    regimelist = regime_switch(betas)
    curr_reg = np.sign(betas[0] - 1e-5)

    fig, ax = plt.subplots(1, 1, figsize=(figx, figy))
    for i in range(len(regimelist) - 1):
        if curr_reg == 1:
            pass
            # uncomment below if we want to color the normal regimes
            #ax.axhspan(0, data.max(), xmin=regimelist[i]/regimelist[-1], xmax=regimelist[i+1]/regimelist[-1],
            #          facecolor="green", alpha=0.3)
        else:
            ax.axhspan(0,
                       data.max(),
                       xmin=regimelist[i] / regimelist[-1],
                       xmax=regimelist[i + 1] / regimelist[-1],
                       facecolor='gray',
                       alpha=0.5)
        curr_reg = -1 * curr_reg

    data.plot(ax=ax, grid=True)
    ax.set_ylabel("value")
    ax.set_title('Regime plot')
    ax.set_yscale('log')
    plt.show()
    return ax
예제 #5
0
def get_from_yahoo():
    s = requests.Session()

    #Replace B=xxxx
    cookies = dict(B='c650m5hchrhii&b=3&s=tk')

    #Replace crumb=yyyy
    crumb = 'NMhMTCv7QpM'

    begin = datetime_timestamp("2014-01-01 09:00:00")

    end = datetime_timestamp("2017-04-30 09:00:00")

    r = s.get(
        "https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=" +
        begin + "&period2=" + end + "&interval=1d&events=history&crumb=" +
        crumb,
        cookies=cookies,
        verify=False)

    f = open('IBM.csv', 'w')
    f.write(r.text)
    f.close()
    es = pd.read_csv('IBM.csv',
                     index_col=0,
                     parse_dates=True,
                     sep=",",
                     dayfirst=True)

    data = pd.DataFrame({"IBM": es["Adj Close"][:]})

    print(data.info())

    data.plot(subplots=True, grid=True, style="b", figsize=(8, 6))

    plt.show()
예제 #6
0
ax[2].legend(['tshift(900)'], loc=2)
ax[2].get_xticklabels()[1].set(weight='heavy', color='red')
ax[2].axvline(local_max + offset, alpha=0.3, color='red');
#shit() shifts the data, tshift() shifts the index values

#computing differences over time
ROI = 100 * (goog.tshift(-365) / goog - 1)
ROI.plot()
plt.ylabel('% Return on Investment');

#rolling windows
rolling = goog.rolling(365, center=True)
data = pd.DataFrame({'input': goog,
'one-year rolling_mean': rolling.mean(),
'one-year rolling_std': rolling.std()})
ax = data.plot(style=['-', '--', ':'])
ax.lines[0].set_alpha(0.3)


#query and evals - can save memory and time for large arrays / data sets
import numexpr
nrows, ncols = 100000, 100
rng = np.random.RandomState(42)
df1, df2, df3, df4 = (pd.DataFrame(rng.rand(nrows, ncols)) for i in range(4))
%timeit df1 + df2 + df3 + df4
%timeit pd.eval('df1 + df2 + df3 + df4')
df1, df2, df3, df4, df5 = (pd.DataFrame(rng.randint(0, 1000, (100, 3))) for i in range(5))
result1 = -df1 * df2 / (df3 + df4) - df5
result2 = pd.eval('-df1 * df2 / (df3 + df4) - df5')
np.allclose(result1, result2)
import urllib.request
import pandas as pd
from pandas_datareader import data, wb
import matplotlib.pyplot as plt
import datetime as dt  # Standard Python date / time library

url = 'http://research.stlouisfed.org/fred2/series/UNRATE/downloaddata/UNRATE.csv'
source = urllib.request.urlopen(url).read().decode('utf-8').split("\n")

print(source[0])
print(source[1])
print(source[2])

data = pd.read_csv(source, index_col=0, parse_dates=True, header=None)
print(type(data))
print(data.head())
print(data.describe())

# NEW

start, end = dt.datetime(2006, 1, 1), dt.datetime(2012, 12, 31)

data = data.DataReader('UNRATE', 'fred', start, end)

print(type(data))

data.plot()

plt.show()
예제 #8
0
raw.tail()
SP500.columns = ['GSPC.O']
raw = SP500

symbol = ['GSPC.O']

data = (pd.DataFrame(raw[symbol]).dropna())

SMA1 = 42
SMA2 = 252

data['SMA1'] = data[symbol].rolling(SMA1).mean()
data['SMA2'] = data[symbol].rolling(SMA2).mean()

data.plot(figsize=(10, 6))

data.dropna(inplace=True)

data['Position'] = np.where(data['SMA1'] > data['SMA2'], 1, -1)

data.tail()

ax = data.plot(secondary_y='Position', figsize=(10, 6))
ax.get_legend().set_bbox_to_anchor((0.25, 0.85))

data['Returns'] = np.log(data[symbol] / data[symbol].shift(1))
data['Strategy'] = data['Position'].shift(1) * data['Returns']

data.round(4).head()
예제 #9
0
ax[2].legend(['tshift(900)'], loc=2)
ax[2].get_xticklabels()[1].set(weight='heavy', color='red')
ax[2].axvline(local_max + offset, alpha=.3, color='red')

ROI=100*(goog.tshift(-365)/goog - 1)
ROI.plot()
plt.ylabel('% Return on Investment');

# rollling window
rolling = goog.rolling(365, center=True)
data = pd.DataFrame({'input' : goog, 
                     'one-year rolling_mean' : rolling.mean(),
                     'one-year rolling_std' : rolling.std()})

ax = data.plot(style=['-', '--', ':'])
ax.lines[0].set_alpha(0.3)


data = pd.read_csv('./data/FremontBridge.csv', index_col = 'Date', parse_dates=True)
data.head()    
data.columns= ['West', 'East']    
data.head()    
data.tail()
data['Total']= data.eval('West+East')    
data.dropna().describe()    
    
import seaborn; seaborn.set()    
data.plot(figsize = (15, 5))    
plt.ylabel('Hourly Bicyle Count')
# Define the ticker list
import pandas as pd
tickers_list = ['AAPL', 'IBM', 'MSFT', 'WMT']
# Import pandas
data = pd.DataFrame(columns=tickers_list)
# Feth the data
for ticker in tickers_list:
    data[ticker] = quandl.get('WIKI/' + ticker,
                              start_date=start_date,
                              end_date=end_date,
                              api_key=QUANDL_API_KEY)['Adj. Close']
# Print first 5 rows of the data
data.head()

# Plot all the close prices
data.plot(figsize=(10, 7))
# Show the legend
plt.legend()
# Define the label for the title of the figure
plt.title("Adjusted Close Price", fontsize=16)
# Define the labels for x-axis and y-axis
plt.ylabel('Price', fontsize=14)
plt.xlabel('Year', fontsize=14)
# Plot the grid lines
plt.grid(which="major", color='k', linestyle='-.', linewidth=0.5)
plt.show()

# Import TimeSeries class
from alpha_vantage.timeseries import TimeSeries
ALPHA_VANTAGE_API_KEY = 'REF5GTSBYWRUSV9KP'
# This is to prompt you to change the ALPHA_VANTAGE Key