from sklearn.ensemble import RandomForestRegressor from sklearn.ensemble import GradientBoostingRegressor import matplotlib.pyplot as plt from sklearn.metrics import mean_absolute_error from sklearn.model_selection import RandomizedSearchCV from sklearn.neural_network import MLPRegressor from sklearn.ensemble import BaggingRegressor from sklearn.model_selection import GridSearchCV from sklearn.ensemble import AdaBoostRegressor from sklearn.neighbors import KNeighborsRegressor ticker = 'MCD' start_date = datetime.datetime.now() - datetime.timedelta(days=365) end_date = datetime.date.today() df = DataReader(ticker, 'yahoo', start_date, end_date) print(df) for sma_period in [5, 10, 20, 50, 100, 200]: indicator_name = "SMA_%d" % (sma_period) df[indicator_name] = df['Close'].rolling(sma_period).mean() df['BollingerBand_Up_20_2'] = df['Close'].rolling( 20).mean() + 2 * df['Close'].rolling(20).std() df['BollingerBand_Down_20_2'] = df['Close'].rolling( 20).mean() - 2 * df['Close'].rolling(20).std() df['BollingerBand_Up_20_1'] = df['Close'].rolling( 20).mean() + df['Close'].rolling(20).std() df['BollingerBand_Down_20_1'] = df['Close'].rolling( 20).mean() - df['Close'].rolling(20).std()
from keras.models import Sequential from pandas.plotting import lag_plot from keras.layers import Dense, LSTM from pandas_datareader import DataReader from statsmodels.tsa.arima_model import ARIMA from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error warnings.filterwarnings('ignore') ticker = 'TSLA' start_date = datetime.datetime.now() - datetime.timedelta(days=3650) end_date = datetime.date.today() # # Fetching the historic prices df = DataReader(ticker, 'yahoo', start_date, end_date) # # Plotting the data plt.figure(figsize=(16, 8)) plt.title(f'{ticker} Close Price History') plt.plot(df['Close'], linewidth=2) plt.xlabel('Date', fontsize=18) plt.ylabel('Close Price USD ($)', fontsize=18) plt.show() last = df.tail(365) plt.figure(figsize=(16, 8)) plt.title(f"{ticker}'s Daily Returns") returns = last['Close'] / last['Close'].shift(1) - 1 returns.plot(label='returns %', linewidth=1) plt.xlabel('Date', fontsize=18)
def grafica(shortticker): from flask import Flask, request, render_template, jsonify import pandas.io.sql as sql import sqlite3 import platform import datetime import numpy as np import pandas as pd import json #import pygal import matplotlib.pyplot as plt from scipy.stats import norm from bokeh.charts import Histogram import plotly #from pandas.io.data import DataReader from pandas_datareader import wb, DataReader from sklearn.linear_model import LogisticRegression from sklearn.lda import LDA from sklearn.qda import QDA from bokeh.layouts import gridplot from bokeh.plotting import figure, show, output_file def datetime(x): return np.array(x, dtype=np.datetime64) symbol = shortticker #"GOOG" df = DataReader(symbol, "google", '01/01/2016', '08/03/2017') df['date'] = df.index p1 = figure(x_axis_type="datetime", title="Stock Closing Prices") p1.grid.grid_line_alpha = 0.3 p1.xaxis.axis_label = 'Date' p1.yaxis.axis_label = 'Price' p1.line(datetime(df['date']), df['Close'], color='#A6CEE3', legend=symbol) #p1.line(datetime(GOOG['date']), GOOG['adj_close'], color='#B2DF8A', legend='GOOG') #p1.line(datetime(IBM['date']), IBM['adj_close'], color='#33A02C', legend='IBM') #p1.line(datetime(MSFT['date']), MSFT['adj_close'], color='#FB9A99', legend='MSFT') #p1.legend.location = "top_left" df_array = np.array(df['Close']) df_dates = np.array(df['date'], dtype=np.datetime64) window_size = 30 window = np.ones(window_size) / float(window_size) aapl_avg = np.convolve(df_array, window, 'same') p2 = figure(x_axis_type="datetime", title="One-Month Average") p2.grid.grid_line_alpha = 0 p2.xaxis.axis_label = 'Date' p2.yaxis.axis_label = 'Price' p2.ygrid.band_fill_color = "olive" p2.ygrid.band_fill_alpha = 0.1 p2.circle(df_dates, df_array, size=4, legend='close', color='darkgrey', alpha=0.2) p2.line(df_dates, aapl_avg, legend='avg', color='navy') p2.legend.location = "top_left" output_file("./templates/stocks.html", title="My Own Bokeh Example") show(gridplot([[p1, p2]], plot_width=400, plot_height=400)) # open a browser return render_template('stocks.html')
import pandas as pd import numpy as np __author__ = 'Han (Aaron) Xiao' """ Collect market data for Fama-French Three Factor model. Get daily index data (S&P500 close price) from yahoo finance and daily risk free data (1-month T-bill rate) from Fred. Get data from 2015-06-30 to 2019-06-30. Save data to local SQL database. """ start = '2015-06-30' end = '2019-06-30' # Get data and index = DataReader(name='^GSPC', data_source='yahoo', start=start, end=end) index.dropna(inplace=True) sp500 = index['Close'] sp500.name = 'sp500_close' rf = DataReader(name='DGS1MO', data_source='fred', start=start, end=end) rf.rename(columns={'DGS1MO': 'risk_free'}, inplace=True) rf.dropna(inplace=True) # Note: here the unit of rf is still the percentage(%) and is annualized def format_date(df1, df2): """ concat two df into same time range (festival excluded). Fill NA with last valid observation. :return: """ data = pd.concat([df1, df2], axis=1)
from pandas_datareader import DataReader from datetime import datetime from dtwpy.distance_measure import distancefunc from dtwpy import dtwpy from dtwpy import plotTest as pltt import matplotlib.pyplot as plt import numpy as np K = DataReader("F", "yahoo", datetime(2000, 1, 1), datetime(2012, 1, 1)) K_2008 = K[K.index.year == 2008] K_2009 = K[K.index.year == 2009] query = K_2008.Volume.values template = K_2009.Volume.values dist = distancefunc(name="euclidean") figure_number = 1 C = ('r--', 'g--', 'b--', 'y--', 'k--', 'm--', 'c--') ''' fastdtw ''' CC = C[0] L = "FastDTW" distance, path = dtwpy.fastdtw(query, template, radius=1, dist=dist, pattern="symmetric1",
watch_std = [] def_watch = [] def_watch_pct = [] def_watch_mean = [] def_watch_std = [] must_watch = [] must_watch_pct = [] must_watch_mean = [] must_watch_std = [] for stock in stocks: try: time.sleep(1) df = DataReader(stock, 'yahoo' ,start, now) df = df[['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']] sma = 50 limit = 10 #calculates sma and creates a column in the dataframe df['SMA'+str(sma)] = df.iloc[:,4].rolling(window=sma).mean() df['PC'] = ((df["Adj Close"]/df['SMA'+str(sma)])-1)*100 mean = round(df["PC"].mean(), 3) stdev = round(df["PC"].std(), 3) current = round(df["PC"][-1], 3) yday = round(df["PC"][-2], 3) print (stock)
from __future__ import division import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from pandas import Series, DataFrame from pandas_datareader import DataReader from datetime import datetime sns.set_style('whitegrid') tech_list = ['AAPL', 'GOOG', 'MSFT', 'AMZN'] end = '2018-01-01' start = '2017-01-01' for stock in tech_list: globals()[stock] = DataReader(stock, data_source='yahoo', start=start, end=end) closing_df = DataReader(tech_list, data_source='yahoo', start=start, end=end)['Adj Close'] tech_rets = closing_df.pct_change() # sns.pairplot(tech_rets.dropna()) # returns_fig = sns.PairGrid(tech_rets.dropna()) returns_fig = sns.PairGrid(closing_df) returns_fig.map_upper(plt.scatter, color='purple') returns_fig.map_lower(sns.kdeplot, cmap='cool_d') returns_fig.map_diag(plt.hist, bins=30) plt.show()
config['session'] = True config['api_key'] = "b89559c7df932f3d1173a58e423f0c1634f61651" client = TiingoClient(config) ticker = "AAPL" start_date = datetime.datetime.now() - datetime.timedelta(days=365) end_date = datetime.date.today() # price price = si.get_live_price('{}'.format(ticker)) price = round(price, 2) # volatility, momentum, beta, alpha, r_squared df = DataReader(ticker, 'yahoo', start_date, end_date) dfb = DataReader('^GSPC', 'yahoo', start_date, end_date) rts = df.resample('M').last() rbts = dfb.resample('M').last() dfsm = pd.DataFrame( { 's_adjclose': rts['Adj Close'], 'b_adjclose': rbts['Adj Close'] }, index=rts.index) dfsm[['s_returns','b_returns']] = dfsm[['s_adjclose','b_adjclose']]/\ dfsm[['s_adjclose','b_adjclose']].shift(1) -1 dfsm = dfsm.dropna()
def dailyreturnplot(): df = DataReader(ticker, 'yahoo', start_date, end_date)['Close'] df.fillna(method="ffill", inplace=True) df.fillna(method="bfill", inplace=True) dailyreturn = compute_daily_return(df) plot_data(dailyreturn, title="Daily returns")
def oportunidaddesubida(ndiasmaximo, shortticker): from flask import Flask, request, render_template, jsonify import pandas.io.sql as sql import sqlite3 import platform import datetime import numpy as np import pandas as pd import json #import pygal import matplotlib.pyplot as plt from scipy.stats import norm from bokeh.charts import Histogram import plotly #from pandas.io.data import DataReader from pandas_datareader import wb, DataReader from sklearn.linear_model import LogisticRegression from sklearn.lda import LDA from sklearn.qda import QDA from bokeh.layouts import gridplot from bokeh.plotting import figure, show, output_file symbol = shortticker historical_period_in_days = 365 #*2 start_date = datetime.datetime.today() - datetime.timedelta( days=historical_period_in_days) end_date = datetime.datetime.today() #start_date = datetime.datetime(2015,01,01) #end_date = datetime.datetime(2016,01,01) lags = int(ndiasmaximo) outcome_period = 5 #def create_lagged_series(symbol, start_date, end_date, lags=50, outcome_period=5): """This creates a pandas DataFrame that stores the percentage returns of the adjusted closing value of a stock obtained from Yahoo Finance, along with a number of lagged returns from the prior trading days (lags defaults to 5 days). Trading volume, as well as the Direction from the previous day, are also included.""" # Obtain stock information from Yahoo Finance ts = DataReader( symbol, "google", start_date - datetime.timedelta(days=2 * (lags + outcome_period)), end_date) #ts = web.DataReader(symbol, 'google', start_date, end_date) ## Create the new lagged DataFrame tslag = pd.DataFrame(index=ts.index) tslag = ts[['Low', 'High']] tslag = tslag.sort_index(axis=0, ascending=False) for i in xrange(0, lags): tslag["lag%s" % str(i + 1)] = (tslag["High"] - tslag["Low"].shift(-(i + 1)) ) / tslag["Low"].shift(-(i + 1)) tslag = tslag[(tslag.index >= start_date) & (tslag.index <= end_date)] result = {} for item in (tslag.index): result[str(item).split('T')[0]] = list(tslag.ix[item, 2:].values) result2 = {} valores = [] for item in result.keys(): for j in range(lags): valores.append(result[item][j]) result2['diferencias'] = valores ## nuevo rango = {} interval = [-0.1, -0.07, -0.05, -0.02, 0.0, 0.02, 0.05, 0.07, 0.1] for i in range(len(interval) + 1): rango[str(i + 1)] = [] for i in range(len(valores)): if valores[i] <= -0.10: rango['1'].append(valores[i]) if (valores[i] > -0.10) & (valores[i] <= -0.07): rango['2'].append(valores[i]) if (valores[i] > -0.07) & (valores[i] <= -0.05): rango['3'].append(valores[i]) if (valores[i] > -0.05) & (valores[i] <= -0.02): rango['4'].append(valores[i]) if (valores[i] > -0.02) & (valores[i] <= 0.00): rango['5'].append(valores[i]) if (valores[i] > 0.00) & (valores[i] <= 0.02): rango['6'].append(valores[i]) if (valores[i] > 0.02) & (valores[i] <= 0.05): rango['7'].append(valores[i]) if (valores[i] > 0.05) & (valores[i] <= 0.07): rango['8'].append(valores[i]) if (valores[i] > 0.07) & (valores[i] <= 0.10): rango['9'].append(valores[i]) if (valores[i] > 0.10): rango['10'].append(valores[i]) rangos = [] percen = {} percen['rango'] = 'x < -10%' try: percen['percentual'] = (round((float(len(rango['1'])) / len(valores)), 4)) except: percen['percentual'] = (0.0) rangos.append(percen) for i in range(1, len(interval), 1): percen = {} percen['rango'] = str(interval[i - 1] * 10) + '% ' + '<= x < ' + str( interval[i] * 10) + '%' percen['percentual'] = (round( (float(len(rango['%s' % (i + 1)])) / len(valores)), 4)) rangos.append(percen) percen = {} percen['rango'] = 'x > 10%' try: percen['percentual'] = (round((float(len(rango['10'])) / len(valores)), 4)) except: percen['percentual'] = (0.0) rangos.append(percen) per = {} per['rangos'] = rangos return json.dumps(per)
import pandas as pd from pandas_datareader import DataReader import datetime as dt import matplotlib.pyplot as plt import seaborn as sns import matplotlib.ticker as ticker import numpy as np # Pull data for ticker, adj is split & dividend adjusted num_of_years = 8 start_date = dt.datetime.now() - dt.timedelta(int(365.25 * num_of_years)) end_date = dt.datetime.now() stock = 'SPY' data = DataReader(stock, "yahoo", start_date, end_date) def lumpsum(invest_date, principal=10000): invest_price = data.loc[invest_date]['Adj Close'] current_price = data['Adj Close'][-1] investment_return = (current_price / invest_price) - 1 return principal * (1 + investment_return) def dollar_cost_average(invest_date, periods=12, freq='30D', principal=10000): # Get DCA dates dca_dates = investment_dates_all = pd.date_range(invest_date,
# # In[ ]: # List of Tech_stocks for analytics tech_list = ['AAPL', 'GOOGL', 'MSFT', 'AMZN'] # set up Start and End time for data grab end = datetime.now() start = datetime(end.year - 1, end.month, end.day) #For-loop for grabing google finance data and setting as a dataframe # Set DataFrame as the Stock Ticker for stock in tech_list: globals()[stock] = DataReader(stock, 'yahoo', start, end) #change # In[4]: AAPL.head() # In[5]: AAPL.describe() # In[6]: AAPL.info() # Now that we've seen the DataFrame, let's go ahead and plot out the volume and closing price of the AAPL(Apple) stocks.
### Estimación del modelo estructural para la serie GNP que posee tendencia y ciclo # Datasets ####Debe instalarse pandas-datareader #import pandas as pd from pandas_datareader import DataReader import numpy as np import statsmodels.api as sm import matplotlib.pylab as plt import pandas as pd # Obtención de los datos start = '1948-01' end = '2008-01' us_gnp = DataReader('GNPC96', 'fred', start=start, end=end) plt.plot(us_gnp) log_gnp = np.log(us_gnp) dates = us_gnp.index._mpl_repr() plt.plot(log_gnp) # Especificación del modelo unrestricted_model = { 'level': 'local linear trend', 'cycle': True, 'damped_cycle': True, 'stochastic_cycle': True } output_mod = sm.tsa.UnobservedComponents(log_gnp, **unrestricted_model) output_res = output_mod.fit(method='powell', disp=False) print(output_res.summary()) ##Gráfico
risk_free_return = 0.02 pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) nasdaq_tickers = si.tickers_nasdaq() index_ticker = '^GSPC' start = datetime.datetime.now() - datetime.timedelta(days=365) end = datetime.date.today() expected_returns = [] for ticker in nasdaq_tickers: try: stock = DataReader(ticker, 'yahoo', start, end) index = DataReader(index_ticker, 'yahoo', start, end) return_s1 = stock.resample('M').last() return_s2 = index.resample('M').last() dataframe = pd.DataFrame( { 's_adjclose': return_s1['Adj Close'], 'm_adjclose': return_s2['Adj Close'] }, index=return_s1.index) dataframe[['s_returns', 'm_returns']] = np.log( dataframe[['s_adjclose', 'm_adjclose']] / dataframe[['s_adjclose', 'm_adjclose']].shift(1)) dataframe = dataframe.dropna()
def loadStock(brand='AAPL', year=1): end = datetime.now() start = datetime(end.year - year, end.month, end.day) return DataReader(brand, 'yahoo', start, end)
import pandas as pd from pandas_datareader import DataReader import datetime as dt from stockstats import StockDataFrame import matplotlib.pyplot as plt pd.set_option('display.max_columns', None) pd.set_option('display.max_rows', None) ticker = 'AAPL' num_of_years = 1 start = dt.datetime.now() - dt.timedelta(int(365.25 * num_of_years)) now = dt.datetime.now() df = DataReader(ticker, 'yahoo', start, now) df.columns = ['high', 'low', 'open', 'close', 'volume', 'adj close'] df = df[['open', 'close', 'high', 'low', 'volume']] stock = StockDataFrame.retype(df) print(stock['rsi_3']) plt.gcf() plt.plot(stock['boll']) plt.plot(stock['boll_ub']) plt.plot(stock['boll_lb'])
from sklearn.naive_bayes import GaussianNB from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix from sklearn.preprocessing import MinMaxScaler from sklearn.tree import DecisionTreeClassifier from sklearn.neural_network import MLPClassifier from sklearn.metrics import classification_report from sklearn.neighbors import KNeighborsClassifier from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split start_date = datetime.datetime(1969, 1, 1) end_date = datetime.date.today() df = DataReader('^DJI', 'yahoo', start_date, end_date) print(df.head()) print('\n') print(df.shape) print('\n') print(df.columns) print('\n') print(df.index) print('\n') #plot closing price for DJI df.Close.plot(grid=True, figsize=(10, 6)) plt.title("DJI Closing price") #Create technical indicators
eval(companies[company])['Daily Return'].plot(figsize=(12, 4), legend=True, linestyle='--', marker='o') st.pyplot(clear_figure=True) st.bar_chart(eval(companies[company])['Daily Return']) st.write('Distplot:') sns.distplot(eval(companies[company])['Daily Return'].dropna(), bins=100, color='magenta') st.pyplot(clear_figure=True) # Grab all the closing prices for the tech stock list into one DataFrame closingprice_df = DataReader(tech_list, 'yahoo', start_date, end_date)['Close'] # make a new tech returns DataFrame tech_returns = closingprice_df.pct_change() if st.checkbox('Show Comparison between two companies'): st.write('***Comparison***') if len(tech_list) == 2: st.write( get_name(tech_list[0]) + ' and ' + get_name(tech_list[1]) + ' by jointplot:') sns.jointplot(tech_list[0], tech_list[1], tech_returns, kind='hex', height=8,
import seaborn as sns pd.set_option('display.max_rows', None) pd.set_option('display.min_rows', None) start_date = datetime.datetime.now() - datetime.timedelta(days=365) end_date = datetime.date.today() tickers = pd.read_csv('nyse.csv') tickers = tickers['Symbol'] sharpe_ratios = [] invalid_tickers = [] for ticker in tickers: try: df = DataReader(ticker, 'yahoo', start_date, end_date) x = 5000 y = (x) stock_df = df stock_df['Norm return'] = stock_df['Adj Close'] / stock_df.iloc[0][ 'Adj Close'] allocation = float(x / y) stock_df['Allocation'] = stock_df['Norm return'] * allocation stock_df['Position'] = stock_df['Allocation'] * x pos = [df['Position']] val = pd.concat(pos, axis=1) val.columns = ['WMT Pos']
def get_data(start, end): data = [] for stock in tech_list: globals()[stock] = DataReader(stock, 'yahoo', start, end)
import matplotlib.pyplot as plt import pandas as pd import datetime import numpy as np import plotly.graph_objects as go from sklearn.pipeline import make_pipeline from sklearn.preprocessing import Normalizer from sklearn.cluster import KMeans from sklearn.decomposition import PCA companies = ['TGT', 'AMZN', 'NFLX', 'PG', 'NSRGY', 'MDLZ', 'MRK', 'MSFT', 'AAPL'] start_date = datetime.datetime(2017, 5, 17) end_date = datetime.date.today() df = DataReader(companies, 'yahoo', start_date, end_date) stock_open = np.array(df['Open']).transpose() stock_close = np.array(df['Close']).transpose() movements = stock_close - stock_open sum_of_movement = np.sum(movements,1) for i in range(len(companies)): print('company:{}, Change:{}'.format(df['High'].columns[i],sum_of_movement[i])) # Define, Fit, and transform a Normalizer normalizer = Normalizer() norm_movements = normalizer.fit_transform(movements)
def __init__(self, symbols, start=None, end=None, bench='^GSPC'): logger.info(f'Get optimal allocation: class Portfolio') # Make sure input is a list if type(symbols) != list: symbols = [symbols] # Create distionary to hold assets. self.asset = {} nb_of_days_considered_min = int(busday_count(start, end) * .9) # Retrieve assets from data source (IE. Yahoo) logger.info(f'Get optimal allocation: get assets historic') symbols_considered = [] for symbol in tqdm(symbols): try: historical_data = DataReader(symbol, "yahoo", start=start, end=end) if nb_of_days_considered_min < historical_data.shape[0]: logger.info( f'Get optimal allocation: Select asset with {nb_of_days_considered_min} days min - Asset: {symbol} with {historical_data.shape[0]} days') self.asset[symbol] = historical_data symbols_considered.append(symbol) else: logger.info(f'Get optimal allocation: Asset {symbol}.') logger.info(f'Error: Not enough historic') except Exception as e: logger.info(f'Get optimal allocation: Asset {symbol} not found.') logger.info(f'Error: {e}') logger.info(f'Get optimal allocation: {len(symbols)} symbols (assets)') # Keep only considered symbols self.asset = {k: self.asset[k] for k in self.asset if k in symbols_considered} # Get Benchmark asset. self.benchmark = DataReader(bench, "yahoo", start=start, end=end) self.benchmark['Return'] = self.benchmark['Adj Close'].diff() # Get returns, beta, alpha, and sharp ratio. iteration = 1 for symbol in symbols_considered: logger.info(f'---------') logger.info(f'Get optimal allocation: Iteration: {iteration}/{len(symbols_considered)} ({datetime.datetime.today()})') logger.info(f'Get optimal allocation: Symbol consider: {symbol}') self.benchmark = self.benchmark.loc[self.benchmark.index.intersection(self.asset[symbol].index)] self.asset[symbol] = self.asset[symbol].loc[self.asset[symbol].index.intersection(self.benchmark.index)] logger.info( f'Get optimal allocation: {symbol} shape: {self.asset[symbol].shape}, bench shape: {self.benchmark.shape}') # Get returns. self.asset[symbol]['Return'] = self.asset[symbol]['Adj Close'].diff() # Get Beta. A = self.asset[symbol]['Return'].fillna(0) B = self.benchmark['Return'].fillna(0) self.asset[symbol]['Beta'] = cov(A, B)[0, 1] / cov(A, B)[1, 1] # Get Alpha self.asset[symbol]['Alpha'] = self.asset[symbol]['Return'] - \ self.asset[symbol]['Beta'] * self.benchmark['Return'] # Get Sharpe Ratio tmp = self.asset[symbol]['Return'] self.asset[symbol]['Sharpe'] = \ sqrt(len(tmp)) * mean(tmp.fillna(0)) / std(tmp.fillna(0)) iteration += 1 self.dates_to_consider = self.benchmark.index
import datetime as dt import pandas as pd from pandas_datareader import DataReader import matplotlib.pyplot as plt from pylab import rcParams import yahoo_fin.stock_info as si start = dt.datetime(1980,12,1) now = dt.datetime.now() stock = input('enter a ticker: ') while stock != 'quit': price = si.get_live_price(stock) df = DataReader(stock, 'yahoo', start, now) df.drop(df[df["Volume"]<1000].index, inplace=True) dfmonth=df.groupby(pd.Grouper(freq="M"))["High"].max() glDate=0 lastGLV=0 currentDate="" curentGLV=0 for index, value in dfmonth.items(): if value > curentGLV: curentGLV=value currentDate=index counter=0 if value < curentGLV: counter=counter+1
stockHolding[ic] = capital * (1 - transFeeRate) / currPrice capital = 0 elif suggestedAction[ic] == -1: if stockHolding[ic] > 0: capital = stockHolding[ic] * currPrice * (1 - transFeeRate) stockHolding[ic] = 0 elif suggestedAction[ic] == 0: pass else: assert False total[ic] = capital + stockHolding[ic] * currPrice * (1 - transFeeRate) returnRate = (total[-1] - capitalOrig) / capitalOrig return returnRate if __name__ == '__main__': SEARCH = False df = DataReader(stock, 'yahoo', start_date, end_date) transFeeRate = float(0.01) priceVec = df["Adj Close"].values print('Optimizing over %i numbers of transactions.' % (len(priceVec))) actionVec = find_optimal_action(priceVec, transFeeRate) returnRate = profit_estimate(priceVec, transFeeRate, actionVec) print('Return rate: ', returnRate)
plt.style.use('seaborn-whitegrid') from sklearn.preprocessing import MinMaxScaler from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, LSTM, Dropout import tensorflow as tf #from keras.models import Sequential #from keras.layers import Dense, LSTM, Dropout import math from sklearn.metrics import mean_squared_error ##### Read Data company = 'GOOG' cutoffyear = 2017 dat = DataReader(company, 'yahoo', datetime(2009, 1, 1), datetime.today()) ##### Split Train and Test train, test = dat[dat.index.year <= cutoffyear], dat[ dat.index.year > cutoffyear] ##### Scale close prices based on MinMaxScaler on training set -> need to scale x and y separate to inverse transform predictions y later scaler_x = MinMaxScaler(feature_range=(0, 1)) scaler_y = MinMaxScaler(feature_range=(0, 1)) # scale train train_sc_x = pd.DataFrame(scaler_x.fit_transform( train.filter(['High', 'Low', 'Open', 'Volume', 'Close'])), index=train.index) train_sc_x.columns = train.filter(['High', 'Low', 'Open', 'Volume', 'Close' ]).columns + '_sc'
import matplotlib.pyplot as plt from tensorflow.keras import Sequential from pandas_datareader import DataReader from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error from tensorflow.keras.layers import Dense, LSTM from pandas.util.testing import assert_frame_equal warnings.filterwarnings("ignore") #get the stock quote for the past 10 years stock = input("Enter a stock ticker: ") start_date = datetime.datetime.now() - datetime.timedelta(days=3650) end_date = datetime.date.today() df = DataReader(stock, "yahoo", start_date, end_date) data = df.filter(['Close']) dataset = data.values train_data_len = math.ceil(len(dataset) * .8) #scale the data scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(dataset) #create the training dataset train_data = scaled_data[0:train_data_len, :] #split the data into x_train and y_train dataset x_train = [] y_train = []
return v['Symbol'] #Stores Stock Symbol #Sample User Input userInput = sys.argv[1] stockSymbol = findSymbol(userInput) print(stockSymbol) # set up Start and End time for data grab end = datetime.now() start = datetime(end.year - 1, end.month, end.day) # Set DataFrame as the Stock Ticker globals()[stockSymbol] = DataReader(stockSymbol, 'yahoo', start, end) def getDataFrame(stockName): return globals()[stockName] ###getDataFrame(stockSymbol).head() # Summary stats for Apple Stock ###getDataFrame(stockSymbol).describe() # General Info ###getDataFrame(stockSymbol).info() # Let's see a historical view of the closing price
import sys import os sys.path.append('../') from pandas_datareader import DataReader import pandas as pd import datetime from market_regime.market_regime_detection import Market_regime as mrd from market_regime import config long_term = True secs = ['SPY'] #short-term check if not long_term: data = DataReader(secs, "av-intraday", start=str(datetime.date.today() - datetime.timedelta(days=30)), end=str(datetime.date.today()), api_key=config.ALPHAVANTAGE_API_KEY)['close'] data = pd.DataFrame(data) MR = mrd(data, data_freq='m').directional_change_fit( dc_offset=[0.01, 0.02]).markov_switching_regression_fit( ).hidden_markov_model_fit() MR.plot_market_regime(day_interval=1, no_markov=False, plot_hmm=True) else: data = DataReader(secs, 'yahoo', '2019-12-01', str(datetime.date.today()))['Adj Close'] MR = mrd(data, data_freq='d').directional_change_fit( dc_offset=[0.1, 0.15]).markov_switching_regression_fit( ).hidden_markov_model_fit() MR.plot_market_regime(day_interval=20, no_markov=True, plot_hmm=False)
def fetch_daily_values(self, code, start, end): from pandas_datareader import DataReader data = DataReader(code, 'yahoo', start, end) return data
# In[11]: # list of texh_stocks for analtytics tech_list = ['AAPL', 'GOOGL', 'MSFT', 'AMZN'] #set up start and end time for data grab end = datetime.now() start = datetime(end.year - 1, end.month, end.day) # 1 year back date print("End time ", end, "Start time ", start) # for loop for grabbing data and setting as a dataframe #dataframe as stock ticker # pip install fix-yahoo-finance (google doesn't work try yahoo) #globals() function in Python returns the dictionary of current global symbol table. for stock in tech_list: globals()[stock] = DataReader(stock, 'yahoo', start, end) # In[18]: AAPL.head() # In[16]: AAPL.describe() # In[17]: AAPL.info() # In[28]: