def addCustomColumns(df, market_upd=False): start = datetime.date( int(df.index.get_level_values('date')[0]) - 10, int(df['month'].values[0]), 1) end_date_ls = [ int(d) for d in datetime.date.today().strftime('%Y-%m-%d').split("-") ] end = datetime.date(end_date_ls[0], end_date_ls[1], end_date_ls[2]) try: url = "https://www.quandl.com/api/v1/datasets/WIKI/{0}.csv?column=4&sort_order=asc&trim_start={1}&trim_end={2}".format( df.index.get_level_values('ticker')[0], start, end) qr = pd.read_csv(url) qr['Date'] = qr['Date'].astype('datetime64[ns]') # quotes = DataReader(df.index.get_level_values('ticker')[0], 'yahoo', start, end, pause=1)['Close'] # quotes = DataReader(df.index.get_level_values('ticker')[0], 'yahoo', start, end, pause=1)['Close'] except: print("Could not read time series data for %s" % df.index.get_level_values('ticker')[0]) exc_type, exc_obj, exc_tb = sys.exc_info() app.logger.info( "Could not read time series data for {3}: {0}, {1}, {2}".format( exc_type, exc_tb.tb_lineno, exc_obj, df.index.get_level_values('ticker')[0])) raise df = addBasicCustomCols(df, qr) df = addGrowthCustomCols(df, qr) df = addTimelineCustomCols(df, qr) pdb.set_trace() if market_upd: # market = DataReader(".INX", 'google', start, end, pause=1)['Close'] market = DataReader('^GSPC', 'yahoo', start, end, pause=1)['Close'] market.to_csv( '/home/ubuntu/workspace/finance/app/static/docs/market.csv') quotes = pd.DataFrame(quotes) market.columns = ['Date', 'market'] market.set_index('Date') quotes['market'] = market else: market = pd.read_csv( '/home/ubuntu/workspace/finance/app/static/docs/market.csv') market.columns = ['Date', 'market'] market['Date'] = market['Date'].apply(pd.to_datetime) market = market.set_index('Date') qr = pd.merge(qr.set_index('Date'), market, left_index=True, right_index=True) df = calcBetas(df, qr) ''' Still need to do: 'enterpriseToEbitda' 'ebitdaMargins' 'ebitda', 'shortRatio' ''' return df
def get_data_for_multiple_stocks(tickers, start_date, end_date): ''' Obtain stocks information (Date, OHLC, Volume and Adjusted Close). Uses Pandas DataReader to make an API Call to Yahoo Finance and download the data directly. Computes other values - Log Return and Arithmetic Return. Args: tickers: List of Stock Tickers start_date: Start Date of the stock data end_date: End Date of the stock data Returns: A dictionary of dataframes for each stock ''' stocks = dict() for ticker in tickers: s = DataReader(ticker, 'yahoo', start_date, end_date) s.insert( 0, "Ticker", ticker) #insert ticker column so you can reference better later s['Date'] = pd.to_datetime(s.index) #useful for transformation later s['Prev Adj Close'] = s['Adj Close'].shift(1) s['Log Return'] = np.log(s['Adj Close'] / s['Prev Adj Close']) s['Return'] = (s['Adj Close'] / s['Prev Adj Close'] - 1) s = s.reset_index(drop=True) cols = list(s.columns.values) # re-arrange columns cols.remove("Date") s = s[["Date"] + cols] s["Date"] = pd.to_datetime(s["Date"]) s = s.set_index("Date") stocks[ticker] = s return stocks
def web_plotting_plots(): # static start = dt.datetime(2010, 1, 1) end = dt.datetime(2013, 1, 27) data = DataReader("MSFT", 'google', start, end) data = data.reset_index() fig, ax = plt.subplots() data.plot(x='Date', y='Close', grid=True, ax=ax) plt.savefig(PATH + 'MSFT.png', dpi=300) # interactive - may not work in cloud 9 warnings.simplefilter('ignore') py.sign_in('Python-Demo-Account', 'gwt101uhh0') # to interactive D3.js plot py.iplot_mpl(fig) # direct approach with Cufflinks data.set_index('Date')['Close'].iplot(world_readable=True)
'text': 'Start of the Recession' }] } fig = dict(data=data, layout=layout) plotly.offline.plot(fig, filename='google-recession-candlestick') # Grab all the closing prices for the tech stock list into one DataFrame stocks = ['AAPL','GOOG','MSFT','AMZN','GE'] closing_df = DataReader(stocks,'google',start,end)['Close'] closing_df_2 = DataReader(stocks,'google',start,end)['Close'] # change the timestamp to date closing_df["Date"] = closing_df.index.date closing_df.set_index("Date", drop=True, inplace=True) # Let's compare the closing prices(Close) of 4 tech stocks #fig1 = plot(closing_df.iplot(fill=True, asFigure=True, title="Daily Closing Price")) fig1 = plot(closing_df.iplot(fill=True,asFigure = True, subplots = True, title='Compare Closing Prices')) #plotly.offline.iplot(fig1, filename = 'compare-closing-prices') ### CORRELATION ANALYSIS ### # Correlation is a statistical measure to analyze of how stocks move in # relation to one another. # Correlation is represented by correlation coefficient - PEARSON COEFFICIENT r # ranging between -1 and +1 # When the prices of two stocks usually move in a similar direction,