def calc_kelly_leverages(securities: Set[str], start_date: date, end_date: date, risk_free_rate: float = 0.04) -> Dict[str, float]: """Calculates the optimal leverages for the given securities and time frame. Returns a list of (security, leverage) tuple with the calculate optimal leverages. Note: risk_free_rate is annualized """ f = {} ret = {} excess_return = {} # Download the historical prices from Yahoo Finance and calculate the # excess return (return of security - risk free rate) for each security. for symbol in securities: try: hist_prices = get_historical_data( symbol, start=start_date, end=end_date, output_format='pandas') except IOError as e: raise ValueError(f'Unable to download data for {symbol}. ' f'Reason: {str(e)}') f[symbol] = hist_prices ret[symbol] = hist_prices['close'].pct_change() # risk_free_rate is annualized excess_return[symbol] = (ret[symbol] - (risk_free_rate / 252)) # Create a new DataFrame based on the Excess Returns. df = DataFrame(excess_return).dropna() # Calculate the CoVariance and Mean of the DataFrame C = 252 * df.cov() M = 252 * df.mean() # Calculate the Kelly-Optimal Leverages using Matrix Multiplication F = inv(C).dot(M) # Return a list of (security, leverage) tuple return {security: leverage for security, leverage in zip(df.columns.values.tolist(), F)}
def prediction_generator(investor, prediction_file='data/prediction.csv', horizon=5): """ Main function that will generate predictions getting data from IEXFINANCE """ start = datetime.now() - timedelta(days=duration) end = datetime.now() # get data from IEXFINANCE, import data to a panda's dataframe df = get_historical_data(ticker_list, start, end, output_format='pandas', token="sk_261e4411a4ef43fab3fea00a67631841") idx = pd.IndexSlice df = df.loc[:, idx[:, "close"]] df.columns = ticker_list # importing predictions predictions = pd.DataFrame() for i in ticker_list: model = pm.auto_arima(df[i].values[~np.isnan(df[i].values)]) preds = model.predict(n_periods=5) predictions[i] = preds # measuring growth to differentiate between risky and non-risky predictions new_df = df.iloc[df.shape[0] - 1:, ].reset_index(drop=True).append( predictions.iloc[:1, :].reset_index(drop=True)).reset_index(drop=True) growth = pd.DataFrame() for i in range(new_df.shape[1]): growth[ticker_list[i]] = [((new_df[ticker_list[i]].values[1] - new_df[ticker_list[i]].values[0]) / new_df[ticker_list[i]].values[1])] growth = growth.iloc[0].sort_values(ascending=False) # differentiate ticker lists for risky (a) and non risky investors (b) ticker_list_a = growth.index[0:3] ticker_list_b = growth.index[3:] # OUTPUT if investor == "risky": predictions = predictions.loc[:, ticker_list_a] # saving a txt with text to be printed to the client with open('data/growth_info.txt', 'w', encoding="utf-8") as f: print(*[ "Stock growth:\n" + " " + ticker_list_a[i] + ": " + str(growth[ticker_list_a[i]] * 100) + "%\n " if i == 0 else ticker_list_a[i] + ":" + str(growth[ticker_list_a[i]] * 100) + "%\n" for i in range(len(ticker_list_a)) ], file=f) # importing the txt file again to an object to be printed to the client with open("data/growth_info.txt", "r", encoding="utf-8") as f_open: growth_info = f_open.read() # generating text to print predictions to the client values_list = [] for k in range(len(ticker_list_a)): values = "" # setting prediction horizon (5 days) days = ["Day " + str(i + 1) + ": €" for i in range(horizon)] for i in range(predictions.shape[0]): if i != predictions.shape[0] - 1: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + ", " else: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + "." values_list.append(values) # saving a txt with text to be printed to the client with open('data/predition_info.txt', 'w', encoding="utf-8") as f: print( *[ ticker_list_a[i] + "\n" + values_list[i] + "\n" if i == 0 else "\n" + ticker_list_a[i] + "\n" + values_list[i] + "\n" for i in range(len(values_list)) ], file=f, ) # importing the txt file again to an object to be printed to the client with open("data/predition_info.txt", "r", encoding="utf-8") as f_open: predicion_info = f_open.read() elif investor == "non-risky": # saving a txt with text to be printed to the client with open('data/growth_info.txt', 'w', encoding="utf-8") as f: print(*[ "Stock growth:\n" + " " + ticker_list_b[i] + ": " + str(growth[ticker_list_b[i]] * 100) + "%\n" if i == 0 else ticker_list_b[i] + ":" + str(growth[ticker_list_b[i]] * 100) + "%\n" for i in range(len(ticker_list_b)) ], file=f) # importing the txt file again to an object to be printed to the client with open("data/growth_info.txt", "r", encoding="utf-8") as f_open: growth_info = f_open.read() # generating text to print predictions to the client values_list = [] predictions = predictions.loc[:, ticker_list_b] for k in range(len(ticker_list_b)): values = "" # setting prediction horizon (5 days) days = ["Day " + str(i + 1) + ": €" for i in range(horizon)] for i in range(predictions.shape[0]): if i != predictions.shape[0] - 1: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + ", " else: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + "." values_list.append(values) # saving a txt with text to be printed to the client with open('data/predition_info.txt', 'w', encoding="utf-8") as f: print( *[ ticker_list_b[i] + "\n" + values_list[i] + "\n" if i == 0 else "\n" + ticker_list_b[i] + "\n" + values_list[i] + "\n" for i in range(len(values_list)) ], file=f, ) # importing the txt file again to an object to be printed to the client with open("data/predition_info.txt", "r", encoding="utf-8") as f_open: predicion_info = f_open.read() return growth_info + predicion_info
#live file 'DailyPricesList.csv' tickerSOI = os.path.join(directory, 'DailyPricesList.csv') #datesList = os.path.join(directory, 'dateslist.csv') tickers = pd.read_csv(tickerSOI, engine='python') #days = pd.read_csv(datesList, engine='python') maintable = pd.DataFrame() #for each ticker in the file, pulls price data for specified date, and pushes to mysql db under associated table name for index,row in tickers.iterrows(): #remember to toggle exchange start = "4/30/21" symbol = row['Ticker'] tablename = row['Table'] df = get_historical_data(symbol, start, token = key, close_only=True, output_format='pandas') df.rename(columns={'close':tablename}, inplace = True) df = df.drop('volume',1) #print(df) maintable = pd.concat([df, maintable],axis = 1) print("Appended " + tablename) maintable.index.names = ['date'] maintable.to_sql('financialsectorsprices', engine, if_exists='append')
def prediction_generator(investor, prediction_file='data/prediction.csv', horizon=5): """ Main function that will generate predictions getting data from IEXFINANCE """ start = datetime.now() - timedelta(days=duration) end = datetime.now() # get data from IEXFINANCE, import data to a panda's dataframe df = get_historical_data(ticker_list, start, end, output_format='pandas') idx = pd.IndexSlice df = df.loc[:, idx[:, "close"]] df.columns = ticker_list hist_data = df.iloc[:, ].to_csv( "data/hist_data.csv") # columns must be selected robjects.r(''' library(astsa) library(forecast) library(seasonal) library(tseries) library(readr) hist_data <- read_csv("data/hist_data.csv", col_names = TRUE) data <- as.ts(hist_data[,-1],start=1,frequency = 365) cn<-colnames(data) ns <- ncol(data) h=5 fcast <- matrix(NA,nrow=h,ncol=ns) prediction <- sapply(data, function(x) forecast(auto.arima(x,seasonal = TRUE),h=h)) for (i in 1:ns) fcast[,i] <- prediction[,i]$mean %>% as.vector() write.table(x = fcast, file = "data/prediction.csv",row.names = FALSE,col.names=cn,sep=",") ''') # importing predictions predictions = pd.read_csv(prediction_file, header=0, delimiter=",") # measuring growth to differentiate between risky and non-risky predictions new_df = df.iloc[df.shape[0] - 1:, ].reset_index(drop=True).append( predictions.iloc[:1, :].reset_index(drop=True)).reset_index(drop=True) growth = pd.DataFrame() for i in range(new_df.shape[1]): growth[ticker_list[i]] = [((new_df[ticker_list[i]].values[1] - new_df[ticker_list[i]].values[0]) / new_df[ticker_list[i]].values[1])] growth = growth.iloc[0].sort_values(ascending=False) # differentiate ticker lists for risky (a) and non risky investors (b) ticker_list_a = growth.index[0:3] ticker_list_b = growth.index[3:] # OUTPUT if investor == "risky": predictions = predictions.loc[:, ticker_list_a] # saving a txt with text to be printed to the client with open('data/growth_info.txt', 'w', encoding="utf-8") as f: print(*[ "Stock growth:\n" + " " + ticker_list_a[i] + ": " + str(growth[ticker_list_a[i]] * 100) + "%\n " if i == 0 else ticker_list_a[i] + ":" + str(growth[ticker_list_a[i]] * 100) + "%\n" for i in range(len(ticker_list_a)) ], file=f) # importing the txt file again to an object to be printed to the client with open("data/growth_info.txt", "r", encoding="utf-8") as f_open: growth_info = f_open.read() # generating text to print predictions to the client values_list = [] for k in range(len(ticker_list_a)): values = "" # setting prediction horizon (5 days) days = ["Day " + str(i + 1) + ": €" for i in range(horizon)] for i in range(predictions.shape[0]): if i != predictions.shape[0] - 1: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + ", " else: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + "." values_list.append(values) # saving a txt with text to be printed to the client with open('data/predition_info.txt', 'w', encoding="utf-8") as f: print( *[ ticker_list_a[i] + "\n" + values_list[i] + "\n" if i == 0 else "\n" + ticker_list_a[i] + "\n" + values_list[i] + "\n" for i in range(len(values_list)) ], file=f, ) # importing the txt file again to an object to be printed to the client with open("data/predition_info.txt", "r", encoding="utf-8") as f_open: predicion_info = f_open.read() elif investor == "non-risky": # saving a txt with text to be printed to the client with open('data/growth_info.txt', 'w', encoding="utf-8") as f: print(*[ "Stock growth:\n" + " " + ticker_list_b[i] + ": " + str(growth[ticker_list_b[i]] * 100) + "%\n" if i == 0 else ticker_list_b[i] + ":" + str(growth[ticker_list_b[i]] * 100) + "%\n" for i in range(len(ticker_list_b)) ], file=f) # importing the txt file again to an object to be printed to the client with open("data/growth_info.txt", "r", encoding="utf-8") as f_open: growth_info = f_open.read() # generating text to print predictions to the client values_list = [] predictions = predictions.loc[:, ticker_list_b] for k in range(len(ticker_list_b)): values = "" # setting prediction horizon (5 days) days = ["Day " + str(i + 1) + ": €" for i in range(horizon)] for i in range(predictions.shape[0]): if i != predictions.shape[0] - 1: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + ", " else: values += days[i] + str(np.round(predictions.iloc[i, k], 2)) + "." values_list.append(values) # saving a txt with text to be printed to the client with open('data/predition_info.txt', 'w', encoding="utf-8") as f: print( *[ ticker_list_b[i] + "\n" + values_list[i] + "\n" if i == 0 else "\n" + ticker_list_b[i] + "\n" + values_list[i] + "\n" for i in range(len(values_list)) ], file=f, ) # importing the txt file again to an object to be printed to the client with open("data/predition_info.txt", "r", encoding="utf-8") as f_open: predicion_info = f_open.read() return growth_info + predicion_info
def MpfPlotWave(ticker): # assert ticker is not empty if len(ticker) == 0: return start = datetime(2019, 2, 9) end = datetime(2020, 2, 11) # values that can be parameterized intEmaPeriod = 34 intBars = 90 row = 0 for i in ticker: ohlc = get_historical_data(i, start, end, output_format='pandas', token=jsnIEX['iextoken']) ohlc.columns = ["Open", "High", "Low", "Close", "Volume"] if (intBars + intEmaPeriod > len(ohlc)): print("Error: Bars + EmaPeriod exceeds ohlc " + str(len(ohlc) - intBars - intEmaPeriod)) return hEma = ohlc['High'].ewm(intEmaPeriod).mean() cEma = ohlc['Close'].ewm(intEmaPeriod).mean() lEma = ohlc['Low'].ewm(intEmaPeriod).mean() # extract OHLC into a list of lists lohlc = ohlc[['Open', 'High', 'Low', 'Close']].values.tolist() # convert dates in datetime format to mathplotlib dates mdates = dates.date2num(ohlc.index) # prepare ohlc in mathplotlib format mohlc = [[mdates[i]] + lohlc[i] for i in range(len(mdates))] # set default font sizes params = {'axes.labelsize': 20, 'axes.titlesize': 24} pyplot.rcParams.update(params) fig, ax = pyplot.subplots(figsize=(24, 24)) # set default tick sizes ax.tick_params(axis='both', which='major', labelsize=20) ax.tick_params(axis='both', which='minor', labelsize=18) # mpfold.plot_day_summary_ohlc(ax, mohlc[-50:], ticksize=5, colorup='#77d879', colordown='#db3f3f') # alternatively, use a barchart mpfold.candlestick_ohlc(ax, mohlc[-intBars:], width=0.4, colorup='#77d879', colordown='#db3f3f') ax.plot(hEma[-intBars:], color='red', linewidth=2, label='high, ' + str(intEmaPeriod) + '-Day EMA') ax.plot(cEma[-intBars:], color='green', linewidth=2, label='close, ' + str(intEmaPeriod) + '-Day EMA') ax.plot(lEma[-intBars:], color='blue', linewidth=2, label='low, ' + str(intEmaPeriod) + '-Day EMA') ax.set_xlabel('Date') ax.set_ylabel('Price') ax.set_title(i + ' Chart with ' + str(intEmaPeriod) + '-Day EMA Wave') ax.legend(fontsize=20) ax.xaxis.set_major_formatter(dates.DateFormatter('%b %d')) fig.autofmt_xdate()
def test_market_closed_pandas(self): data = get_historical_data("AAPL", "20190101") assert isinstance(data, pd.DataFrame)
def test_invalid_dates_batch(self): start = datetime(2010, 5, 9) end = datetime(2017, 5, 9) with pytest.raises(ValueError): get_historical_data(["AAPL", "TSLA"], start, end)
def historical_data(self): self.data_series = get_historical_data(self.ticker, output_format="pandas", token=self.api_key, start=self.date_value) return self.data_series
import numpy as np import pandas as pd from iexfinance.stocks import get_historical_data import FinanceDataReader as fdr from datetime import datetime import matplotlib.pyplot as plt from sklearn.svm import SVC from sklearn.metrics import scorer, accuracy_score start = datetime(2013, 1, 1) end = datetime(2018, 1, 1) Df = get_historical_data('TSLA', start=start, end=end, output_format='pandas') plt.figure(figsize=(20, 10)) plt.title("TSLA") Df['close'].plot() plt.show() krx = fdr.StockListing('KRX')
import matplotlib.pyplot as plt import keras import math import numpy as np import pandas as pd from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error import datetime import iexfinance from iexfinance.stocks import get_historical_data daysAhead = 0 start = datetime.date(2019, 3, 30) end = datetime.date(2014, 12, 30) df = get_historical_data("HMSY", end, start, output_format='pandas') #print (df) #df.plot() #plt.show() #df.to_csv("dataFrame.csv") print(df) plt.figure(figsize=(21, 10)) #Splt.plot(range(df.shape[0]),(df['low']+df['high'])/2.0) #plt.show() highs = df['high'].tolist() lows = df['low'].tolist() averages = list() for i in range(len(highs)): averages.append((highs[i] + lows[i]) / 2) print(len(averages)) scaler = MinMaxScaler(feature_range=(0, 1)) averages = np.array(averages).reshape(-1, 1)
=========================== 7) Evaluate the Model =========================== ''' # Evaluate the performance of our model. The metrics you use is up to you. #Accuracy and Mean Squared Error are shown below. # scores = cross_val_score(regressor, X_test, y_test, cv=10) # print ("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() / 2)) # mse = mean_squared_error(y_test, regressor.predict(X_test)) # print("MSE: %.4f" % mse) ''' =========================== 7) Predict =========================== ''' # X=f_cleanData[-1:] # print(regressor.predict(X)) from iexfinance.stocks import get_historical_data from datetime import datetime start = datetime(2017, 1, 1) # starting date: year-month-date end = datetime(2018, 1, 1) # ending date: year-month-date data = get_historical_data('ERIC', start=start, end=end, output_format='pandas') print(data.head())
def getHistoricalDataByYear(ticker, year): start = datetime(year, 1, 1) end = datetime((year+1), 1, 1) return get_historical_data(ticker, start, end)
def getHistoricalDataByRange(ticker, start, end): return get_historical_data(ticker, start, end)
def optimize(request): if request.method == "POST": try: tickers = request.POST['Tickers'] tickers = tickers.upper() print(tickers) print(type(tickers)) if type(tickers) != list: if "'" in tickers: tickers = tickers.replace("'", '') tickers = tickers.replace(', ', ', ') tickers = str(tickers) tickers = tickers.split(', ') tickers = list(tickers) amount = request.POST['Amount'] if ',' in amount: amount = amount.replace(',', '') # start = datetime.strptime(request.POST['start'],'%Y-%m-%d') start = pd.to_datetime(request.POST['start']) # if len(tickers) > 7: # start = pd.to_datetime('2013-06-01') if len(tickers) > 30: tickers = tickers[:30] if start < (datetime.now() - dt.timedelta(weeks=(52 * 5))): start = datetime.now() - dt.timedelta(weeks=(52 * 4)) #start = pd.to_datetime('2015-06-01') tickers = tickers end = pd.to_datetime(request.POST['end']) if end > datetime.now(): end = datetime.now() datatype = 'stock_data' # database = pd.DataFrame() # """NON OPTIMAL PORTFOLIO""" # # for ticker, allocation in zip(tickers, [1/len(tickers) for ticker in tickers]): # data = data_views.get_stock_data(datatype, ticker, start, end) # data = data.loc[:, ['AdjAdjClose']] # # data.rename(columns = {'AdjAdjClose':ticker}, inplace = True) # data[ticker + ' Normed_Returns'] = data[ticker]/data[ticker].iloc[0] # data[ticker + ' Allocation'] = data[ticker + ' Normed_Returns']*allocation # data[ticker + ' Position_values'] = data[ticker + ' Allocation']*int(amount) # database = database.join(data, how = 'outer') # database.dropna(inplace = True) data = pd.DataFrame() dataframe = pd.DataFrame() for stock in tickers: stock_data = get_historical_data( stock, start=start, end=end, token='sk_6d1c2037a984473895a42a17710cf794', output_format='pandas', close_only=True) stock_data = stock_data[['close']] stock_data.rename(columns={'close': stock}, inplace=True) dataframe = pd.concat([dataframe, stock_data], axis=1) database = pd.DataFrame() for ticker, allocation in zip( tickers, [1 / len(tickers) for ticker in tickers]): data = pd.DataFrame(dataframe[ticker]) #data = data.loc[:, ['AdjClose']] #data.rename(columns = {'AdjClose':ticker}, inplace = True) data[ticker + ' Normed_Returns'] = data[ticker] / data[ticker].iloc[0] data[ticker + ' Allocation'] = data[ticker + ' Normed_Returns'] * allocation data[ticker + ' Position_values'] = data[ticker + ' Allocation'] * int(amount) database = database.join(data, how='outer') database.dropna(inplace=True) non_optimal_position_val = pd.DataFrame() for ticker in tickers: non_optimal_position_val = pd.concat([ non_optimal_position_val, database[ticker + ' Position_values'] ], axis=1) non_optimal_position_val[ 'Total_position'] = non_optimal_position_val.sum(axis=1) """CALCULATE LOG RETURNS""" log_df = pd.DataFrame() for ticker in tickers: log_df = pd.concat([log_df, database[ticker]], axis=1) log_ret = np.log(log_df / log_df.shift(1)) """OPTIMIZATION""" num_ports = 8000 if len(tickers) > 10: num_ports = 3000 all_weights = np.zeros((num_ports, len(tickers))) ret_arr = np.zeros(num_ports) vol_arr = np.zeros(num_ports) sharpe_arr = np.zeros(num_ports) for ind in range(num_ports): weights = np.array(np.random.random(len(tickers))) weights = weights / np.sum(weights) all_weights[ind, :] = weights ret_arr[ind] = np.sum((log_ret.mean() * weights) * 252) vol_arr[ind] = np.sqrt( np.dot(weights.T, np.dot(log_ret.cov() * 252, weights))) sharpe_arr[ind] = ret_arr[ind] / vol_arr[ind] max_sharpe = sharpe_arr.argmax() optimized_weights = all_weights[max_sharpe, :] sharpe_ratio = sharpe_arr[max_sharpe].round(3) max_ret = ret_arr[max_sharpe] max_vol = vol_arr[max_sharpe] bullet = plots.market_bullet(vol_arr, ret_arr, sharpe_arr, max_vol, max_ret) weights_ticks_df = pd.DataFrame() opt_weights_series = pd.Series(optimized_weights, name='Weights') tickers_series = pd.Series(tickers, name='Tickers') weights_ticks_df = pd.concat( [weights_ticks_df, tickers_series, opt_weights_series], axis=1) """OPTIMAL PLOT""" datatype = 'stock_data' data = pd.DataFrame() dataframe = pd.DataFrame() for stock in tickers: stock_data = get_historical_data( stock, start=start, end=end, token='sk_6d1c2037a984473895a42a17710cf794', output_format='pandas', close_only=True) stock_data = stock_data[['close']] stock_data.rename(columns={'close': stock}, inplace=True) dataframe = pd.concat([dataframe, stock_data], axis=1) database = pd.DataFrame() for ticker, allocation in zip(tickers, optimized_weights): data = pd.DataFrame(dataframe[ticker]) #data = data.loc[:, ['AdjClose']] #data.rename(columns = {'AdjClose':ticker}, inplace = True) data[ticker + ' Normed_Returns'] = data[ticker] / data[ticker].iloc[0] data[ticker + ' Allocation'] = data[ticker + ' Normed_Returns'] * allocation data[ticker + ' Position_values'] = data[ticker + ' Allocation'] * int(amount) database = database.join(data, how='outer') database.dropna(inplace=True) # for ticker, allocation in zip(tickers, optimized_weights): # data = data_views.get_stock_data(datatype, ticker, start, end) # data = data.loc[:, ['AdjAdjClose']] # data.rename(columns = {'AdjAdjClose':ticker}, inplace = True) # data[ticker + ' Normed_Returns'] = data[ticker]/data[ticker].iloc[0] # data[ticker + ' Allocation'] = data[ticker + ' Normed_Returns']*allocation # data[ticker + ' Position_values'] = data[ticker + ' Allocation']*int(amount) # database = database.join(data, how = 'outer') optimal_position_val = pd.DataFrame() for ticker in tickers: optimal_position_val = pd.concat([ optimal_position_val, database[ticker + ' Position_values'] ], axis=1) optimal_position_val['Total_position'] = optimal_position_val.sum( axis=1) portfolio_plot = plots.optimal_plot( non_optimal_position_val['Total_position'], optimal_position_val['Total_position'], start, end) portfolio_table = pd.concat([ optimal_position_val['Total_position'].round(2), non_optimal_position_val['Total_position'].round(2) ], axis=1) portfolio_table.columns = [ 'Optimal Position Value', 'Non Optimal Position Value' ] # global global_portfolio_optimal_non_optimal global_portfolio_optimal_non_optimal = portfolio_table.to_json() request.session[ 'global_portfolio_optimal_non_optimal'] = global_portfolio_optimal_non_optimal # placeholder = optimal_portfolio_global_holder() portfolio_table['Difference'] = portfolio_table[ 'Optimal Position Value'] - portfolio_table[ 'Non Optimal Position Value'] table = tables.make_stock_table(portfolio_table.round(2)) weights_table = tables.weights(weights_ticks_df.round(2)) return render( request, 'port_optimization/optimized.html', { 'global_portfolio_optimal_non_optimal': global_portfolio_optimal_non_optimal, 'sharpe_ratio': sharpe_ratio, 'weights_table': weights_table, 'table': table, 'tickers': tickers, 'portfolio_plot': portfolio_plot, 'bullet': bullet }) except Exception as e: print(e) print(e.args) error_message = e error = 'One or more of your inputs was not accepted: ' return render(request, 'port_optimization/optimization_form.html', { 'error': error, 'error_message': error_message }) return render(request, 'port_optimization/optimization_form.html')
def get_iex_data(schema, subschema, symbol, intraday_data, data_fractal, from_date, to_date, lookback_period): r"""Get data from IEX. Parameters ---------- schema : str The schema (including any subschema) for this data feed. subschema : str Any subschema for this data feed. symbol : str A valid stock symbol. intraday_data : bool If True, then get intraday data. data_fractal : str Pandas offset alias. from_date : str Starting date for symbol retrieval. to_date : str Ending date for symbol retrieval. lookback_period : int The number of periods of data to retrieve. Returns ------- df : pandas.DataFrame The dataframe containing the market data. """ symbol = symbol.upper() df = pd.DataFrame() if intraday_data: # use iexfinance function to get intraday data for each date df = pd.DataFrame() for d in pd.date_range(from_date, to_date): dstr = d.strftime('%Y-%m-%d') logger.info("%s Data for %s", symbol, dstr) try: df1 = get_historical_intraday(symbol, d, output_format="pandas") df1_len = len(df1) if df1_len > 0: logger.info("%s: %d rows", symbol, df1_len) df = df.append(df1) else: logger.info("%s: No Trading Data for %s", symbol, dstr) except: iex_error = "*** IEX Intraday Data Error (check Quota) ***" logger.error(iex_error) sys.exit(iex_error) else: # use iexfinance function for historical daily data try: df = get_historical_data(symbol, from_date, to_date, output_format="pandas") except: iex_error = "*** IEX Daily Data Error (check Quota) ***" logger.error(iex_error) sys.exit(iex_error) return df
# Getting IEX data iex_price = None iex_dividends = None iexq = None if input('Get new data from IEX? (y/n)\n') == 'y': iex_sk = input('What is your IEX token?') start = datetime.datetime(2020, 10, 30) # the range of the csv I'm using end = datetime.datetime(2020, 12, 8) iex_price = get_historical_data('AAPL', start, end, output_format='pandas', token=iex_sk) iex_price.to_csv('iexprice.csv') iex_dividends = Stock('AAPL', token=iex_sk).get_dividends( ) # the most recent dividend works for my date range iex_dividends.to_csv('iexdividends.csv') print('Data retrieved.') else: print('Using existing IEX data...') iex_price = pd.read_csv('iexprice.csv') iex_dividends = pd.read_csv('iexdividends.csv') print('Files opened.') iexq = iex_dividends['amount'][0]
def getHistoricalPrices(stock): return get_historical_data(stock, start, end, output_format='pandas', token='pk_xxxxxxxxx')
async def todaymonies(stock): stock1 = str(stock) today = datetime.today().strftime('%Y %m %d') ticker = get_historical_data(stock1, today, today) await client.say(ticker)
def test_invalid_symbol_single(self): with pytest.raises(IEXSymbolError): get_historical_data("ZNWAA")
def test_invalid_symbol_batch(self): start = datetime(2017, 2, 9) end = datetime(2017, 5, 24) with pytest.raises(IEXSymbolError): get_historical_data(["BADSYMBOL", "TSLA"], start, end)
use the anaconda default python installed on my ubuntu machine. https://scikit-learn.org/stable/install.html https://pypi.org/project/iexfinance/ ''' import numpy as np import os import pandas as pd from iexfinance.stocks import Stock from datetime import datetime import matplotlib.pyplot as plt from iexfinance.stocks import get_historical_data from sklearn.linear_model import LinearRegression from sklearn.svm import SVR from sklearn.model_selection import train_test_split from iexconfig import * start = datetime(2017, 1, 1) end = datetime(2020, 4, 26) print("iexconfig=", iexconfig) delta = get_historical_data('DAL', start, end, output_format='pandas', token=iexconfig) delta.shape type(delta) df = delta.drop(['volume'], axis=1) df.plot() plt.savefig('delta.png')
import dash import dash_html_components as html import dash_core_components as dcc from iexfinance.stocks import get_historical_data import datetime from dateutil.relativedelta import relativedelta import plotly.graph_objs as go start = datetime.datetime.today() - relativedelta(years=5) end = datetime.datetime.today() inputStock = "V2" df = get_historical_data("TSLA", start=start, end=end, output_format="pandas", token="sk_99b480a1de7547f38575b920c9a03b7a") trace_close = go.Scatter(x=list(df.index), y=list(df.close), name="Close", line=dict(color="#f44242")) layout = dict(title="Stock Chart", showlegend=False) app = dash.Dash(__name__) app.layout = html.Div([ html.Div([html.H1("Stock App"), html.Img(src="assets/stock-icon.png")],
def get_current_stock_price(ticker): print(ticker) df = get_historical_data(ticker, historical_date) return float(df[f'{historical_date.year}-{historical_date.month:02}-{historical_date.day:02}']['close'])
def GetGrowth(name, delta): end = datetime.now() start = monthdelta(end, int(delta) * -1) data = get_historical_data(name, start, end, output_format='pandas') result = CalculateGrowth(data) return str(result) + " %"
from datetime import datetime from iexfinance.stocks import get_historical_data start = datetime(2019, 7, 1) end = datetime(2019, 7, day=3) df = get_historical_data("TSLA", start, end, output_format='pandas') print(df)
# Tutorial: https://www.youtube.com/watch?v=NM8Ue4znLP8&list=PLCDERj-IUIFCaELQ2i7AwgD2M6Xvc4Slf&index=2 # pip3 install iexfinance # get data from iex exchange import dash import dash_core_components as dcc # has a component for every HTML tag (html.H1() puts the string in a h1 html tag for ex) import dash_html_components as html # html elements in dash app from iexfinance.stocks import get_historical_data # iex data import datetime # to get start and end dates from dateutil.relativedelta import relativedelta # creating fix time frame ranges because iex finance only support 5 years from todays date or their api will give error start = datetime.datetime.today() - relativedelta( years=5) # start date = 5 days from todays date end = datetime.datetime.today() dataFrame = get_historical_data("GE", start=start, end=end, output_format="pandas") print(dataFrame.head()) # app = dash.Dash() # initializes app # app.layout = html.Div( # Div element encloses/contains all the dash elements,graphs, etc # html.H1(children = "Hello World") # ) # if __name__ == "__main__": # The python interperter needs to know where which file to start from. Since this is th eonly file, its the "main" file # app.run_server(debug=True)
def __init__(self, code=None): #用于创建对象 self.aapl = Stock(code) # 成交量 self.data = get_historical_data(code, output_format='pandas').iloc[-1] self.aapl2 = Stock(code, output_format='pandas')
def button(bot, update): """ Main function controlling bot-client interactions """ query = update.callback_query if str(query.data) == "1": # collecting forecast data = "These are my forecasts for a risky investor\n" + prediction_generator( investor="risky") # plotting the best asset predictions = pd.read_csv(prediction_file, header=0, delimiter=",") # measuring growth to differentiate between risky and non-risky predictions start = datetime.now() - timedelta(days=duration) end = datetime.now() #.strftime('%Y-%m-%d') df = get_historical_data(ticker_list, start, end, output_format='pandas', token="sk_261e4411a4ef43fab3fea00a67631841") idx = pd.IndexSlice df = df.loc[:, idx[:, "close"]] df.columns = ticker_list new_df = df.iloc[df.shape[0] - 1:, ].reset_index(drop=True).append( predictions.iloc[:1, :].reset_index(drop=True)).reset_index( drop=True) growth = pd.DataFrame() for i in range(new_df.shape[1]): growth[ticker_list[i]] = [((new_df[ticker_list[i]].values[1] - new_df[ticker_list[i]].values[0]) / new_df[ticker_list[i]].values[1])] growth = growth.iloc[0].sort_values(ascending=False) # taking the best asset to provide an image with forecasts ticker_list_a = growth.index[0] #taking the riskiest predicion new_df = df.iloc[df.shape[0] - 30:, ].reset_index() new_df = new_df.loc[:, ["date", ticker_list_a]] new_dates = [ new_df.date.values[len(new_df.date.values) - 1].astype('M8[D]').astype('O') + timedelta(days=i + 1) for i in range(5) ] # new_df["date"] = new_df["date"].dt.strftime("%Y-%m-%d") predictions = predictions.loc[:, [ticker_list_a]] predictions["date"] = new_dates # ploting the image sns.set_style("darkgrid") fig = plt.figure(figsize=(8, 8)) plt.plot(new_df["date"].values, new_df[ticker_list_a].values, linestyle='--', marker='o') plt.plot(predictions["date"].values, predictions[ticker_list_a].values, linestyle='--', marker='o', color='r') plt.suptitle("Evolution of the recommended asset", fontsize=16) plt.title("Ticker name: " + ticker_list_a + "\n 30 days + 5 days forcasts (in red)", fontsize=14) plt.xlabel("Time") plt.ylabel("Stock price (€)") # plt.axis('equal') fig.savefig('data/forecast_image_risky.jpeg') plt.close(fig) bio = BytesIO() bio.name = 'data/forecast_image_risky.jpeg' im = Image.open("data/forecast_image_risky.jpeg") im.save(bio, 'JPEG') bio.seek(0) # sending messages to client bot.send_photo(chat_id=query.message.chat_id, photo=bio) bot.edit_message_text(text=data, chat_id=query.message.chat_id, message_id=query.message.message_id) if str(query.data) == "2": data = "These are my predictions for a risk-averse stupid investor\n" + prediction_generator( investor="non-risky") # plotting the best asset predictions = pd.read_csv(prediction_file, header=0, delimiter=",") # measuring growth to differentiate between risky and non-risky predictions start = datetime.now() - timedelta(days=duration) end = datetime.now() df = get_historical_data(ticker_list, start, end, output_format='pandas', token="sk_261e4411a4ef43fab3fea00a67631841") idx = pd.IndexSlice df = df.loc[:, idx[:, "close"]] df.columns = ticker_list new_df = df.iloc[df.shape[0] - 1:, ].reset_index(drop=True).append( predictions.iloc[:1, :].reset_index(drop=True)).reset_index( drop=True) growth = pd.DataFrame() for i in range(new_df.shape[1]): growth[ticker_list[i]] = [((new_df[ticker_list[i]].values[1] - new_df[ticker_list[i]].values[0]) / new_df[ticker_list[i]].values[1])] growth = growth.iloc[0].sort_values(ascending=False) # taking the best asset to provide an image with forecasts ticker_list_b = growth.index[4] #taking the best risk-averse asset?? new_df = df.iloc[df.shape[0] - 30:, ].reset_index() new_df = new_df.loc[:, ["date", ticker_list_b]] new_dates = [ new_df.date.values[len(new_df.date.values) - 1].astype('M8[D]').astype('O') + timedelta(days=i + 1) for i in range(5) ] # new_df["date"] = new_df["date"].dt.strftime("%Y-%m-%d") predictions = predictions.loc[:, [ticker_list_b]] predictions["date"] = new_dates # ploting the image sns.set_style("darkgrid") fig = plt.figure(figsize=(8, 8)) plt.plot(new_df["date"].values, new_df[ticker_list_b].values, linestyle='--', marker='o') plt.plot(predictions["date"].values, predictions[ticker_list_b].values, linestyle='--', marker='o', color='r') plt.suptitle("Evolution of the recommended asset", fontsize=16) plt.title("Ticker name: " + ticker_list_b + "\n 30 days + 5 days forcasts (in red)", fontsize=14) plt.xlabel("Time") plt.ylabel("Stock price (€)") # plt.axis('equal') fig.savefig('data/forecast_image_risky.jpeg') plt.close(fig) bio = BytesIO() bio.name = 'data/forecast_image_risky.jpeg' im = Image.open("data/forecast_image_risky.jpeg") im.save(bio, 'JPEG') bio.seek(0) # sending messages to client bot.send_photo(chat_id=query.message.chat_id, photo=bio) bot.edit_message_text(text=data, chat_id=query.message.chat_id, message_id=query.message.message_id) # stckstatus images for k in range(len(ticker_list)): # when query.data == ticker_list the query comes from stockstatus if str(query.data) == ticker_list[k]: start = datetime.now() - timedelta(days=30) end = datetime.now() #.strftime('%Y-%m-%d') # creating dictionary of data df = get_historical_data( str(query.data), start, end, output_format='pandas', token="sk_261e4411a4ef43fab3fea00a67631841") df = df.loc[:, ["close"]] sns.set_style("darkgrid") fig = plt.figure(figsize=(8, 8)) plt.plot(df.index, df.close.values, linestyle='--', marker='o') plt.title("Evolution of " + str(query.data) + "\n 30 days", fontsize=14) plt.xlabel("Time") plt.ylabel("Stock price (€)") plt.xticks(rotation=45) plt.axis('equal') fig.savefig('aux_image.jpeg') plt.close(fig) bio = BytesIO() bio.name = 'aux_image.jpeg' im = Image.open("aux_image.jpeg") im.save(bio, 'JPEG') bio.seek(0) bot.send_photo(chat_id=query.message.chat_id, photo=bio) # update.message.reply_photo(query.message.chat_id, 'https://ih1.redbubble.net/image.59574941.7749/fc,550x550,grass_green.u7.jpg') bot.edit_message_text(text="Selected ticker: {}".format( query.data), chat_id=query.message.chat_id, message_id=query.message.message_id)
def get_stock_data(ticker, startDate, endDate): # Managing the date format has been a challenge due to sending Javascript # and Python date objects via JSON I've chosen to send dates as MM-DD-YYY # string and then create date objects in python on the backend and JS # on the frontend startDateForAPItemp = startDate.split('-') startDateForAPI = datetime( int(startDateForAPItemp[0]), int(startDateForAPItemp[1]), int(startDateForAPItemp[2])) - timedelta(days=90) # the API call prefers to choose a date that aligns with a stock market # trading period (i.e. M-F), therefore Saturday and Sundays are changed # to the previous Friday if startDateForAPI.weekday() == 5: startDateForAPI += timedelta(days=2) elif startDateForAPI.weekday() == 6: startDateForAPI += timedelta(days=1) # The same logic applies to the end date. endDateForAPItemp = endDate.split('-') endDateForAPI = datetime(int(endDateForAPItemp[0]), int(endDateForAPItemp[1]), int(endDateForAPItemp[2])) currentTime = datetime.utcnow() if str(currentTime.date()) == str(endDateForAPI.date()): endDateForAPI -= timedelta(days=1) if endDateForAPI.weekday() == 5: endDateForAPI -= timedelta(days=1) elif endDateForAPI.weekday() == 6: endDateForAPI -= timedelta(days=2) # the data returned by IEXcloud api is nested where the outer dictionary has nested dictionary based on date # This flatten_json function creates a signal dictionary where date is a key/value pair along with the corresponding stock data # This flattened format is better for providing a flattened array to D3.js def flatten_json(stockData): out = [] for i in stockData: out2 = {} out2['date'] = i for j in stockData[i]: out2[j] = stockData[i][j] out.append(out2) return out # make the API to IEXcloud, using Addison Lynch's IEXfinance python package historicalData = get_historical_data(ticker, start=startDateForAPI.date(), end=endDateForAPI.date(), token=IEX_api_key) Historical_Data = flatten_json(historicalData) # using pandas for clean data manipulation using dataframes df = pd.DataFrame(Historical_Data) df = ta.utils.dropna(df) # we add the RSI indicator, regardless, to show on the initial charts indicator_RSI = RSIIndicator(close=df["close"], n=10) df['rsi'] = indicator_RSI.rsi() df = ta.utils.dropna(df) return (json.dumps(df.to_dict('records')))
def test_invalid_dates(self): start = datetime(2000, 5, 9) end = datetime(2017, 5, 9) with pytest.raises(ValueError): get_historical_data("AAPL", start, end)
print "</html>" token = API_KEY if request.method == 'POST': tickerSymbol = request.form['ticker'] start = request.form['start'] end = request.form['end'] #tickerSymbol = ARGS.ticker companyInfo = Stock(tickerSymbol, token = API_KEY) stockPrice = companyInfo.get_price() #start = datetime.strptime(ARGS.start, '%Y,%m,%d') #end = datetime.strptime(ARGS.end, '%Y,%m,%d') historicalPrices = get_historical_data(tickerSymbol, start, end, token = API_KEY) stockHistoricals = pd.DataFrame(historicalPrices).T # Formating Dataframe selected_columns = stockHistoricals[["open", "high", "low", "close", "close", "volume"]] Stock_DF = selected_columns.copy() Stock_DF.columns = ["Open", "High", "Low", "Close", "Adj Close", "Volume"] Stock_DF.index.name = 'Date' Stock_DF.to_csv("Data_FC1738-KPN.csv") if __name__ == "__main__": #PARS = argparse.ArgumentParser() #PARS.add_argument("ticker", help="Ticker Name", type=str) #PARS.add_argument("start", help="Start Date", type=str) #PARS.add_argument("end", help="End Date", type=str) #ARGS = PARS.parse_args()