Exemple #1
0
#companies = pdconn.read_table('companies')
#cursor = pdconn.execute('select ticker from company c left join industry i on c.industry = i.industry where sector = "Healthcare"')
cursor = pdconn.execute('select ticker from company')
companies = cursor.fetchall()

start_date = '1985-01-01'
#start_date = '2016-01-01'
end_date = '2016-05-10'

#companies is a list of tuples
for co in companies:
    co = co[0]
    if co in done: continue
    done.append(co)
    print("Fetching data for " + co)
    #fetch data from yahoo as pandas dataframe
    try:
        ts = DataReader(co, 'yahoo', start=start_date, end=end_date)
    except:
        print('Could not read data for ' + co)
        continue
    ts.rename(columns={'Adj Close': 'AdjClose'}, inplace=True)
    # AdjClose values sometimes get extremely large, causing out of bounds errors.  Limit those values here.
    if max(ts['AdjClose']) > 9.99e5:
        ts.loc[ts.AdjClose > 9.99e5, 'AdjClose'] = 9.99e5
    ts.insert(0, 'ticker', co)  #add ticker to dataframe
    ts = ts.round(decimals=2)
    pdconn.to_sql(ts, 'histPrice', if_exists='append')
    time.sleep(random.randint(1, 5))
Exemple #2
0
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='google-closing-price')

### To get better overview of price variations, we plot MOVING AVERAGES
# Moving Average is a constantly updated average price for a stock over a specified period
# Ex. 10-day MA presents its first data point as the avg prices from Day1-10
# Next data point is avg of prices from Day2-Day11 and so on

# What was the moving average of the 4stocks?
moving_avg_days = [10, 20, 50]
GOOG['10days MA'] = GOOG['Adj Close'].rolling(10).mean()
GOOG['20days MA'] = GOOG['Adj Close'].rolling(20).mean()
GOOG['50days MA'] = GOOG['Adj Close'].rolling(50).mean()

table = plotly.offline.plot(
    ff.create_table(GOOG.round(2).head(20), index=True, index_title="Date"))
table['layout'].update(width=950)
plotly.offline.plot(table, filename='moving-avg-google')

### 2. Analyzing the Great Recession in Dec 2007 and Google price change
df = DataReader("goog", 'yahoo', datetime(2007, 10, 1), datetime(2010, 10, 1))

trace = go.Candlestick(x=df.index,
                       open=df.Open,
                       high=df.High,
                       low=df.Low,
                       close=df.Close)
data = [trace]
layout = {
    'title':
    'Google Stock behavior during the 2007 Recession',