def yahoo_prices(symbols, start_date, verbose = True): ticker_df_list = [] start_dates = {}; for index, row in symbols.iterrows(): try: data = DataReader(row.Ticker, 'yahoo', start_date) data['Ref'] = row.Ticker data = data.loc[:, ['Ref', 'Adj Close']] data.rename(columns={'Adj Close': 'Price'}, inplace=True) if verbose: print("{}: Historical Perf: {}".format(row.Ticker, data.tail(1).iloc[0]['Price']/data.head(1).iloc[0]['Price']-1)) ticker_df_list.append(data) start_dates[row.Ticker] = data.head(1).index[0] except Exception as e: if verbose: print("No data for ticker %s\n%s" % (row.Ticker, str(e))) df = pd.concat(ticker_df_list) cell= df[['Ref','Price']] return cell.pivot(columns='Ref'), start_dates
@author: taranpreet singh """ import matplotlib.pyplot as plt from pandas_datareader.data import DataReader # date time to use date objects from datetime import date # Time period of import, start and end dates start = date(2017,10,01) end = date(2017,11,06) stockApl = DataReader('AAPL', 'yahoo', start, end)['Close'] stockApl.head() #plotting stockApl.plot(title='APPLE') plt.show() #calculating daily returns of the stock dr_apl = stockApl.pct_change(1) #encoding for comparison dr_apl[ dr_apl <0 ] = 0 dr_apl[ dr_apl >0 ] = 4
# date time to use date objects from datetime import date # Time period of import, start and end dates start = date(2017, 10, 01) end = date(2017, 11, 06) # DataReader is a function to import, there are different sources available to import data # such as ggogle fin, yahoo fin,fred, Oanda(for exchange rates) # for eg Importing FB data from goolge stockFb = DataReader('fb', 'google', start, end) type(stockFb) # DataReader returns a pandas data frame object stockFb.head() stockFb.info() # from yahoo stockApl = DataReader('AAPL', 'yahoo', start, end) stockApl.head() stockApl.info() #plotting stockApl['Close'].plot(title='APPLE') plt.show() #sp500 from fred up to now sp500 = DataReader('SP500', 'fred', start) #note sys date is deafult for end argument sp500.tail()
# In[31]: AAPL['Daily Return'].hist(bins=100) # Let's go ahead and build a DataFrame with all the ['Close'] columns for each of the stocks dataframes # In[32]: closing_df = DataReader(['AAPL', 'GOOG', 'MSFT', 'AMZN'], 'yahoo', start, end)['Adj Close'] # In[33]: # Let's take a quick look closing_df.head() # Now that we have all the closing prices, let's go ahead and get the daily return for all the stocks, like we did for the Apple stock. # In[34]: # Make a new tech returns DataFrame tech_rets = closing_df.pct_change() # In[36]: tech_rets.head() # Now we can compare the daily percentage return of two stocks to check how correlated. First let's see a sotck compared to itself. # So now we can see that if two stocks are perfectly (and positivley) correlated with each other a linear relationship bewteen its daily return values should occur. So let's go ahead and compare Google and Microsoft the same way.
import pandas as pd import matplotlib.pyplot as plt from pandas_datareader.data import DataReader from datetime import date start = date(1900,1,1) # default Jan 1, 2010 series_code = 'DGS10' # 10-year Treasury Rate data_source = 'fred' # FED Economic Data Service data = DataReader(series_code, data_source, start) data.info() pd.concat([data.head(3), data.tail(3)]) series_name = '10-year Treasury' data = data.rename(columns={series_code: series_name}) data.plot(title=series_name) plt.show()
resid = arma_mod30.resid stats.normaltest(resid) fig = plt.figure(figsize=(12,8)) ax = fig.add_subplot(111) fig = qqplot(resid, line='q', ax=ax, fit=True) predict_sunspots = arma_mod30.predict('1990', '2012', dynamic=True) print(predict_sunspots) fig, ax = plt.subplots(figsize=(12, 8)) ax = dta.ix['1950':].plot(ax=ax) fig = arma_mod30.plot_predict('1990', '2012', dynamic=True, ax=ax, plot_insample=False) ##作业:美国unemployment预测+做图 ##以下代码由于IDLE安装包问题,在spyder中实现的 from pandas_datareader.data import DataReader endog = DataReader('UNRATE', 'fred', start='1954-01-01') print(endog.head()) arma_mod30 = sm.tsa.ARMA(dta, (3,0)).fit(disp=False) resid = arma_mod30.resid stats.normaltest(resid) fig = plt.figure(figsize=(12,8)) ax = fig.add_subplot(111) fig = qqplot(resid, line='q', ax=ax, fit=True) predict_sunspots = arma_mod30.predict('1990', '2020', dynamic=True) print(predict_sunspots) fig, ax = plt.subplots(figsize=(12, 8)) ax = endog.loc['1950':].plot(ax=ax) fig = arma_mod30.plot_predict('1990', '2020', dynamic=True, ax=ax, plot_insample=False)
stock_data = DataReader(ticker, data_source, start, end) In the first chapter, you learned that a stock ticker is the unique symbol needed to get stock information for a certain company. In this exercise, you will practice importing the 2016 data for Apple, with ticker 'AAPL'. ''' # Import DataReader from pandas_datareader.data import DataReader # Import date from datetime import date # Set start and end dates start = date(2016, 1, 1) end = date(2016, 12, 31) # Set the ticker ticker = 'AAPL' # Set the data source data_source = 'google' # Import the stock prices stock_prices = DataReader(ticker, data_source, start, end) # Display and inspect the result print(stock_prices.head()) stock_prices.info()
#stock price START_DATE = date(2018, 1, 1) END_DATE = date(2018, 12, 31) TICKER = 'AMZN' DATA_SOURCE = 'yahoo' stock_data = DataReader(TICKER, DATA_SOURCE, START_DATE, END_DATE) stock_data['High'].plot(title=TICKER) plt.show() #interest rates RATE = 'DGS-10' DATA_SOURCE = 'fred' START_DATE = date(2018, 1, 1) rates_data = DataReader(RATE, DATA_SOURCE, START_DATE) rates_data.head() stock_data['High'].plot(title=RATE) plt.show() #bonds and stocks START_DATE = date(2008, 1, 1) SERIES = ['BAMLHYH0A0HYM2TRIV', 'SP500'] data = DataReader(SERIES, 'fred', START_DATE) data.plot(title=" & ".join(SERIES) + " comparison") plt.show()
#use the drop NAN and draw daily return of the stock # %% plt.figure(figsize=(12, 12)) for i, company in enumerate(company_list, 1): plt.subplot(2, 2, i) sns.distplot(company['Daily Return'].dropna(), bins=100, color='purple') plt.ylabel('Daily Return') plt.title(f'{company_name[i-1]}') # %% #4. what is the correlation between differnet stocks closing prices closing_df = DataReader(tech_list, 'yahoo', start, end)['Adj Close'] #take a quick look at the closing df closing_df.head(10) # %% #make a new tech returns DataFrame tech_rets = closing_df.pct_change() tech_rets.head() # %% #compare return of Google and Microsoft. Obviously, two stocks are perfectly correlated with each other a linear relationship between its daily return values should occur. sns.jointplot('GOOG', 'MSFT', tech_rets, kind='scatter') # # %% #use pairplot on our DataFrame for an automatic visual analysis sns.pairplot(tech_rets, kind='reg')
) # secondary_y: column on tight axis with different scale plt.tight_layout() # improving layout by reducing white spaces plt.show() #################### import pandas as pd from pandas_datareader.data import DataReader from datetime import date start = date(2015, 1, 1) # default Jan 1, 2010 end = date(2016, 12, 31) # default: today ticker = 'GOOG' data_source = 'google' stock_data = DataReader(ticker, data_source, start, end) stock_data.info() pd.concat([stock_data.head(3), stock_data.tail(3)]) stock_data.tail(3) import matplotlib.pyplot as plt stock_data['Close'].plot(title=ticker) plt.show() # ------------------------ FRED import pandas as pd from pandas_datareader.data import DataReader from datetime import date start = date(1962, 1, 1) # default Jan 1, 2010 series_code = 'DGS10' # 10-year Treasury Rate data_source = 'fred' # FED Economic Data Service
import gym import custom_anytrading from stable_baselines.common.policies import MlpPolicy from stable_baselines.common import make_vec_env from stable_baselines import PPO2 from sklearn.preprocessing import MinMaxScaler print('loading data') data = DataReader('AAPL', 'yahoo', start='2000-01-01', end='2019-01-01') print(data.head) test_data = data.tail(200) train_data = data.head(-500) env = gym.make('custom_stocks-v0', stock_df=train_data, pred_df=train_data, window_size=14, initial_balance=5000, min_percent_loss=.25, with_pred=False) test_env = gym.make('custom_stocks-v0', stock_df=test_data, pred_df=test_data, window_size=14, initial_balance=5000, min_percent_loss=.25,
import matplotlib.pyplot as plt from numpy import loadtxt, where end = dt.datetime.now() start = end - dt.timedelta(days=5 * 356) df = DataReader("MU", "iex", start, end) # df.reset_index(inplace = True) df.index = pd.to_datetime(df.index, format='%Y-%m-%d') df['year'] = df.index.year.values df['month'] = df.index.month.values df['day'] = df.index.day.values df.head() # print(year) df['date'] = df['year'].astype(str) + '-' + df['month'].astype( str) + '-' + df['day'].astype(str) # df['date'] = pd.to_datetime(df[['year', 'month','day']]) df.date.values df.tail() df.close.plot(figsize=(12, 8), title='MU') # for i, (index, row) in enumerate(df.iterrows()): # print (row) # print( df.loc[df.index[ i - 4 ], 'close'])