def train_arima(request, stock_id): threading.Thread(target=train_arima_helper, args=(stock_id, 'arima')).start() context = {'stock_id': stock_id, 'longName': get_quote_data(stock_id)['longName']} return render(request, 'aitrader/train_arima.html', context)
def train_stock(request): # ticker_list = ['600519.SS', # '601398.SS', # '600036.SS', # '601288.SS', # '601318.SS', # '601857.SS', # '601988.SS', # '601628.SS', # '601888.SS', # '603288.SS' # ] path = "./aitrader/myfolder/lstm" files = os.listdir(path) ticker_set_lstm = set() for file in files: if file[6:] == ".SS.csv" or file[6:] == ".SZ.csv": if os.path.exists( path + "/" + file[:-7] + "/solution/prediction_result.txt"): # SVM may fail to generate models ticker_set_lstm.add(file[:9]) path = "./aitrader/myfolder/svm" files = os.listdir(path) ticker_set_svm = set() for file in files: if file[6:] == ".SS.csv" or file[6:] == ".SZ.csv": if os.path.exists( path + "/" + file[:-7] + "/solution/prediction_result.txt"): # SVM may fail to generate models ticker_set_svm.add(file[:9]) path = "./aitrader/myfolder/arima" files = os.listdir(path) ticker_set_arima = set() for file in files: if file[6:] == ".SS.csv" or file[6:] == ".SZ.csv": if os.path.exists( path + "/" + file[:-7] + "/solution/prediction_result.txt"): # SVM may fail to generate models ticker_set_arima.add(file[:9]) ticker_list = ticker_set_lstm | ticker_set_svm | ticker_set_arima sticker_obj_list = [] for ticker in ticker_list: tmp = {'ticker': ticker, 'longName': get_quote_data(ticker)['longName']} path = "./aitrader/myfolder/svm" f = open(path + "/" + ticker + "_latest_date.txt", "r") latest_date = f.read() f.close() tmp['latest_date'] = latest_date sticker_obj_list.append(tmp) context = {'sticker_obj_list': sticker_obj_list} return render(request, 'aitrader/train_stock.html', context)
def trade_stock_integration(request, stock_id): if request.method == 'GET': context = {'stock_id': stock_id, 'longName': get_quote_data(stock_id)['longName']} return render(request, 'aitrader/trade_stock_integration.html', context) elif request.method == 'POST': url = '/aitrader/trade_run_integration/' + stock_id + '/' return HttpResponseRedirect(url)
def trade_stock_arima(request, stock_id): if request.method == 'GET': context = {'stock_id': stock_id, 'longName': get_quote_data(stock_id)['longName']} return render(request, 'aitrader/trade_stock_arima.html', context) elif request.method == 'POST': initial_balance = request.POST['initial_balance'] url = '/aitrader/trade_run_arima/' + stock_id + '/' + initial_balance + '/' return HttpResponseRedirect(url)
async def outstanding_close_name(self) -> Dict[str, float]: """ get the price and outstandings from yahoo API the unit of outstanding is 'M' """ out = si.get_quote_data(self.symbol)['sharesOutstanding'] out = out / 1000000 close = round(si.get_quote_table(self.symbol)['Quote Price'], 2) name = self.symbol return {'out': round(out), 'close': close, 'name': name}
def trade_run_lstm(request, stock_id, initial_balance): stock_id_number_only = stock_id[:-3] tmp = maxProfit_lstm(stock_id_number_only, float(initial_balance)) operations, week_profit, balance = tmp path = "./aitrader/myfolder/lstm/" + stock_id_number_only + "/figure" files = os.listdir(path) figure_list = [] for file in files: if file[:3] == "day": figure_list.append(file) figure_list.sort(key=lambda x: int(x[3:-4])) n = len(operations) path = "./aitrader/myfolder/lstm/" + stock_id + "_latest_date.txt" f = open(path, "r") latest_date = f.read() latest_date = datetime.datetime.strptime(latest_date, '%Y-%m-%d') f.close() date_list = findComingTradingDay(latest_date, n) date_operations_dict = {date_list[i]: operations[i] for i in range(len(operations))} path = "./aitrader/myfolder/lstm/" + stock_id_number_only + "/datapoint" files = os.listdir(path) datapoint_dict = {} for file in files: if file[:3] == "day": with open(path + '/' + file) as f: data = json.load(f) datapoint_dict[file[:-5]] = data future5days = [] with open(path + '/' + 'future5days.json') as f: data = json.load(f) future5days = data context = {'stock_id': stock_id, 'stock_id_number_only': stock_id_number_only, 'longName': get_quote_data(stock_id)['longName'], 'initial_balance': initial_balance, 'week_profit': round(week_profit), 'balance': round(balance), 'figure_list': figure_list, 'date_operations_dict': date_operations_dict, 'datapoint_dict': datapoint_dict, 'future5days': future5days } return render(request, 'aitrader/trade_run_lstm.html', context)
def update_data(self): self.load_market_data() for i, symbol in enumerate(self.df['Symbol']): # Variable for display status of Progressive Bar self.p_var.set(100/len(self.df.index)*(i+1)) self.progressbar.update() if symbol in self.tickers['Symbol'].values: # Loading live market data from yahoo finance data_from_yahoo = get_quote_data(symbol) # print(data_from_yahoo.keys()) temp = pd.Series( {'Symbol': symbol, 'Description': self.tickers.loc[self.tickers['Symbol'] == symbol]['Security'].item().strip(), 'Sector': self.tickers.loc[self.tickers['Symbol'] == symbol]['GICS Sector'].item().strip(), 'Cost Basis Per Share': self.df.loc[self.df['Symbol'] == symbol]['Cost Basis Per Share'].item().strip(), 'Quantity': self.df.loc[self.df['Symbol'] == symbol]['Quantity'].item(), 'Market Cap': data_from_yahoo['marketCap'], 'Current Price': data_from_yahoo['regularMarketPrice'], 'P/E Ratio(PER)': data_from_yahoo['trailingPE'] if 'trailingPE' in data_from_yahoo.keys() else None, 'P/B Ratio(PBR)': data_from_yahoo['priceToBook'] if 'priceToBook' in data_from_yahoo.keys() else None} ) self.new_df = self.new_df.append(temp, ignore_index=True) # print("Loading is completed") # Reorder Columns self.new_df = self.new_df[['Symbol', 'Description', 'Sector', 'Quantity', 'Cost Basis Per Share', 'Market Cap', 'Current Price', 'P/E Ratio(PER)', 'P/B Ratio(PBR)']] # first_column = self.new_df.pop('Symbol') # self.new_df.insert(0, 'Symbol', first_column) # self.pt.destroy() # self.pt.clearData() self.invested_temp_sum = 0 for i in range(len(self.new_df)): self.invested_temp_sum += self.new_df['Quantity'][i] * self.new_df['Current Price'][i] self.invested_temp_sum = round(self.invested_temp_sum, 3) self.invested_value.set(str(self.invested_temp_sum)) self.pt = Table(self.display, dataframe=self.new_df, showtoolbar=False, showstatusbar=False, editable=False, enable_menus=False) options = config.load_options() options = {'rowselectedcolor':None} config.apply_options(options, self.pt) self.pt.show() self.pt.autoResizeColumns() # self.pt.redraw() # Update menu status self.menu.entryconfig("Strategies", state="normal") # self.menu_file.entryconfig("Save to Excel", state="normal") self.button_rebalancing.config(state="normal")
def trade_run_svm(request, stock_id): stock_id_number_only = stock_id[:-3] tmp = maxProfit_svm(stock_id_number_only) operations = tmp path = "./aitrader/myfolder/svm/" + stock_id_number_only + "/figure" files = os.listdir(path) figure_list = [] for file in files: if file[:11] == "future5days": figure_list.append(file) n = len(operations) path = "./aitrader/myfolder/svm/" + stock_id + "_latest_date.txt" f = open(path, "r") latest_date = f.read() latest_date = datetime.datetime.strptime(latest_date, '%Y-%m-%d') f.close() date_list = findComingTradingDay(latest_date, n) date_operations_dict = {date_list[i]: operations[i] for i in range(len(operations))} path = "./aitrader/myfolder/svm/" + stock_id_number_only + "/datapoint" future5days = [] with open(path + '/' + 'future5days.json') as f: data = json.load(f) future5days = data context = {'stock_id': stock_id, 'stock_id_number_only': stock_id_number_only, 'longName': get_quote_data(stock_id)['longName'], 'figure_list': figure_list, 'date_operations_dict': date_operations_dict, 'future5days': future5days } return render(request, 'aitrader/trade_run_svm.html', context)
def get_quote_data(tick): print("in get_quote_data", tick) data = yf.get_quote_data(tick) return data
def portfolio_holdings(): cur = con.cursor() parsed_token = request.headers.get('Authorization') parsed_pid = int(request.args.get('portfolio_id')) if parsed_token is None: raise InvalidUsage('Invalid Auth Token', status_code=403) cur.execute( f"select token from portfolio where portfolio_id = {parsed_pid}") x = cur.fetchone() if x is None: raise InvalidUsage('Invalid Token', status_code=403) cur.execute( f"select portfolio_id from portfolio where token = '{parsed_token}'") portfolio_found = 0 x = cur.fetchall() for pid in x: if parsed_pid == pid[0]: portfolio_found = 1 break if portfolio_found == 0: raise InvalidUsage('Portfolio not found', status_code=404) cur.execute( f"select ticker, company, avg_price, units from stock where portfolio_id = {parsed_pid}" ) x = cur.fetchall() stock_list = [] assets = 0 for stock in x: ticker, company, avg_price, units = stock live_price = get_live_price(f'{ticker}') value = units * live_price assets += value for holding in x: ticker, company, avg_price, units = holding temp = get_quote_data(f'{ticker}') live_price = get_live_price(f'{ticker}') if live_price > 0: live_price = "{:.2f}".format(live_price) else: live_price = "{:.4f}".format(live_price) change_p = temp['regularMarketChangePercent'] change_d = temp['regularMarketChange'] if float(change_d) > 0: change_d = "{:.2f}".format(float(change_d)) else: change_d = "{:.2g}".format(float(change_d)) value = float(live_price) * int(units) value = "{:.2f}".format(value) profit_loss_d = float(value) - (units * avg_price) profit_loss_p = (profit_loss_d / (units * avg_price)) * 100 # else: # profit_loss_p = -1 * (100 - profit_loss_d/value) profit_loss_p = "{:.2f}".format(profit_loss_p) profit_loss_d = "{:.2f}".format(profit_loss_d) if float(profit_loss_d) < 0: profit_loss = f'-${-1*float(profit_loss_d)} ({profit_loss_p}%)' else: profit_loss = f'${float(profit_loss_d)} ({profit_loss_p}%)' change_value = float(change_d) * int(units) if float(change_value) > 0: change_value = "{:.2f}".format(float(change_value)) else: change_value = "{:.2g}".format(float(change_value)) weight = float(value) / assets * 100 weight = "{:.2f}".format(weight) change_p = temp['regularMarketChangePercent'] change_d = temp['regularMarketChange'] change_p = "{:.2f}".format(change_p) change_d = "{:.5f}".format(change_d) if float(change_d) < 0: change = f'-${-1*float(change_d)} ({change_p}%)' else: change = f'${float(change_d)} ({change_p}%)' live_price_str = f'${live_price}' avg_price_str = f'${avg_price}' value_str = f'${float(value)}' if float(change_value) < 0: change_value_str = f'-${-1*float(change_value)}' else: change_value_str = f'${float(change_value)}' stock = { 'ticker': ticker, 'company': company, 'live_price': live_price_str, 'change': change, 'profit_loss': profit_loss, 'units': units, 'avg_price': avg_price_str, 'value': value_str, 'weight': weight, 'change_value': change_value_str } stock_list.append(stock) return {'holdings': stock_list}
def portfolio_sellholding(): cur = con.cursor() parsed_token = request.headers.get('Authorization') if parsed_token is None: raise InvalidUsage('Invalid Auth Token', status_code=403) data = request.get_json() portfolio_id = int(data['portfolio_id']) ticker = str(data['ticker'].upper()) avg_price = float(data['avg_price']) quantity = int(data['quantity']) query = f"select token from portfolio where portfolio_id = {portfolio_id};" cur.execute(query) x = cur.fetchone() if x is None: raise InvalidUsage('Invalid Token', status_code=403) if isinstance(portfolio_id, int) is False and isinstance( quantity, int) is False and isinstance(avg_price, float) is False: raise InvalidUsage('Malformed Request', status_code=400) if ticker.isalpha() is False: raise InvalidUsage('Malformed Request', status_code=400) cur.execute( f"select portfolio_id from portfolio where token = '{parsed_token}'") portfolio_found = 0 x = cur.fetchall() for pid in x: if portfolio_id == pid[0]: portfolio_found = 1 break if portfolio_found == 0: raise InvalidUsage('Portfolio not found', status_code=404) # Error trapping ^ # Check is stock owned cur.execute( f'select ticker from stock where portfolio_id = {portfolio_id}') x = cur.fetchall() ticker_found = 0 for stock in x: if ticker == stock[0]: ticker_found = 1 break if ticker_found == 0: raise InvalidUsage('Insufficient shares', status_code=404) if quantity <= 0: raise InvalidUsage('Invalid quantity', status_code=404) if avg_price <= 0: raise InvalidUsage('Invalid price', status_code=404) cur.execute( f"select units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'" ) x = cur.fetchone() units = x[0] # if not owned add to portfolio if quantity == units: company = get_quote_data(f'{ticker}')['longName'] cur.execute('BEGIN TRANSACTION;') query = f"""delete from stock where ticker = '{ticker}' and portfolio_id = {portfolio_id};""" cur.execute(query) cur.execute('COMMIT;') elif quantity < units: # if owned, update units and avg price cur.execute( f"select units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'" ) x = cur.fetchone() old_units = x cur.execute('BEGIN TRANSACTION;') query = f"""UPDATE stock SET units = {old_units[0] - quantity} WHERE portfolio_id = {portfolio_id} and ticker = '{ticker}';""" cur.execute(query) cur.execute('COMMIT;') else: raise InvalidUsage('Insufficient shares', status_code=404) # add to balance cur.execute( f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}" ) balance = cur.fetchone() cash_amt = avg_price * quantity cash_amt = round(cash_amt, 2) if int(cash_amt > balance[0]): raise InvalidUsage(f'Not enough cash in balance', status_code=404) cur.execute('BEGIN TRANSACTION;') query = f"""UPDATE portfolio SET balance = {balance[0] + cash_amt} WHERE portfolio_id = {portfolio_id};""" cur.execute(query) cur.execute('COMMIT;') return {}
def portfolio_buyholding(): cur = con.cursor() parsed_token = request.headers.get('Authorization') if parsed_token is None: raise InvalidUsage('Invalid Auth Token', status_code=403) data = request.get_json() portfolio_id = int(data['portfolio_id']) ticker = str(data['ticker'].upper()) avg_price = float(data['avg_price']) quantity = int(data['quantity']) query = f"select token from portfolio where portfolio_id = {portfolio_id};" cur.execute(query) x = cur.fetchone() if x is None: raise InvalidUsage('Invalid Token', status_code=403) if isinstance(portfolio_id, int) is False and isinstance( quantity, int) is False and isinstance(avg_price, float) is False: raise InvalidUsage('Malformed Request', status_code=400) try: data = get_live_price(f'{ticker}') except: raise InvalidUsage('Invalid Ticker', status_code=404) cur.execute( f"select portfolio_id from portfolio where token = '{parsed_token}'") portfolio_found = 0 x = cur.fetchall() for pid in x: if portfolio_id == pid[0]: portfolio_found = 1 break if portfolio_found == 0: raise InvalidUsage('Portfolio not found', status_code=404) # Error trapping ^ # Check is stock owned cur.execute( f'select ticker from stock where portfolio_id = {portfolio_id}') x = cur.fetchall() ticker_found = 0 for stock in x: if ticker == stock[0]: ticker_found = 1 break if quantity <= 0: raise InvalidUsage('Invalid quantity', status_code=404) if avg_price <= 0: raise InvalidUsage('Invalid price', status_code=404) # deduct from balance cur.execute( f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}" ) balance = cur.fetchone() cash_amt = avg_price * quantity cash_amt = round(cash_amt, 2) if int(cash_amt > balance[0]): raise InvalidUsage(f'Not enough cash in balance', status_code=404) cur.execute('BEGIN TRANSACTION;') query = f"""UPDATE portfolio SET balance = {balance[0] - cash_amt} WHERE portfolio_id = {portfolio_id};""" cur.execute(query) cur.execute('COMMIT;') # if not owned add to portfolio if ticker_found == 0: company = get_quote_data(f'{ticker}')['longName'] cur.execute('BEGIN TRANSACTION;') query = f"""INSERT INTO stock (portfolio_id, ticker, company, avg_price, units) VALUES ({portfolio_id}, '{ticker.upper()}', '{company}', {avg_price}, {quantity});""" cur.execute(query) cur.execute('COMMIT;') else: # if owned, update units and avg price cur.execute( f"select avg_price, units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'" ) x = cur.fetchone() old_avg_price, old_units = x new_avg_price = ((old_avg_price * old_units) + (avg_price * quantity)) / (quantity + old_units) new_avg_price = "{:.2f}".format(new_avg_price) cur.execute('BEGIN TRANSACTION;') query = f"""UPDATE stock SET avg_price = {new_avg_price}, units = {old_units + quantity} WHERE portfolio_id = {portfolio_id} and ticker = '{ticker}';""" cur.execute(query) cur.execute('COMMIT;') return {}
def trade_run_integration(request, stock_id): stock_id_number_only = stock_id[:-3] tmp = maxProfit_integration(stock_id_number_only) operations = tmp[4] tmp = maxProfit_lstm(stock_id_number_only, float('inf')) operations_lstm = tmp[0] tmp = maxProfit_svm(stock_id_number_only) operations_svm = tmp tmp = maxProfit_arima(stock_id_number_only, float('inf')) operations_arima = tmp[0] n = len(operations) path = "./aitrader/myfolder/svm/" + stock_id + "_latest_date.txt" f = open(path, "r") latest_date = f.read() latest_date = datetime.datetime.strptime(latest_date, '%Y-%m-%d') f.close() date_list = findComingTradingDay(latest_date, n) date_operations_dict = {date_list[i]: operations[i] for i in range(len(operations))} date_operations_dict_lstm = {date_list[i]: operations_lstm[i] for i in range(len(operations_lstm))} date_operations_dict_svm = {date_list[i]: operations_svm[i] for i in range(len(operations_svm))} date_operations_dict_arima = {date_list[i]: operations_arima[i] for i in range(len(operations_arima))} future5days_lstm = [] path = "./aitrader/myfolder/lstm/" + stock_id_number_only + "/datapoint" with open(path + '/' + 'future5days.json') as f: data = json.load(f) future5days_lstm = data future5days_svm = [] path = "./aitrader/myfolder/svm/" + stock_id_number_only + "/datapoint" with open(path + '/' + 'future5days.json') as f: data = json.load(f) future5days_svm = data future5days_arima = [] path = "./aitrader/myfolder/arima/" + stock_id_number_only + "/datapoint" with open(path + '/' + 'future5days.json') as f: data = json.load(f) future5days_arima = data context = {'stock_id': stock_id, 'stock_id_number_only': stock_id_number_only, 'longName': get_quote_data(stock_id)['longName'], 'date_operations_dict': date_operations_dict, 'date_operations_dict_lstm': date_operations_dict_lstm, 'date_operations_dict_svm': date_operations_dict_svm, 'date_operations_dict_arima': date_operations_dict_arima, 'future5days_lstm': future5days_lstm, 'future5days_svm': future5days_svm, 'future5days_arima': future5days_arima } return render(request, 'aitrader/trade_run_integration.html', context)
def get_fundamental_data(self): """ all fundamental data """ try: self.fundamental = si.get_quote_data(self.ticker) except: pass try: self.quote = si.get_quote_table(self.ticker) except: pass try: self.stats = si.get_stats(self.ticker) except: pass try: self.earnings = si.get_earnings(self.ticker) except: pass try: self.financials = si.get_financials(self.ticker) except: pass # Individual fundamental factors try: self.earnings_up_count = sum( (self.earnings['quarterly_revenue_earnings'].earnings.diff() > 0).iloc[1:]) except: self.errorCount += 1 try: self.earnings_up_latest = int( (self.earnings['quarterly_revenue_earnings'].earnings.diff() > 0).iloc[-1]) except: self.errorCount += 1 try: self.trailingPE = self.quote['PE Ratio (TTM)'] except: self.errorCount += 1 try: self.EPS = self.quote['EPS (TTM)'] except: self.errorCount += 1 try: self.marketCap = self.fundamental['marketCap'] except: self.errorCount += 1 try: self.TotalDebtEquity = self.stats[ self.stats.Attribute == 'Total Debt/Equity (mrq)'].iloc[0, 1] except: self.errorCount += 1 try: self.shortRatio = float(self.stats[ self.stats.Attribute.str.contains('Short Ratio')].iloc[0, 1]) except: self.errorCount += 1 try: self.institutionPct = float( self.stats[self.stats.Attribute.str.contains( 'Held by Institutions')].iloc[0, 1][:-1]) except: self.errorCount += 1
def getQuoteData(ticker): return si.get_quote_data(ticker)
def getShortName(): t = request.args.get('t', default="") return si.get_quote_data(t)["shortName"]