def buy(user_login): view.ticker_selection_buy() ticker_buy = input().upper() ticker_check = model.apiget(ticker_buy) while ticker_check == None: print("Not a valid ticker!") view.ticker_selection_buy() ticker_buy = input().upper() ticker_check = model.apiget(ticker_buy) view.volume_amount_buy(ticker_buy, ticker_check) try: volume_amount_buy = int(input()) except ValueError: view.invalid_input() return buy(user_login) view.buy_confirm(ticker_buy, ticker_check, volume_amount_buy) buy_confirmation = input() while buy_confirmation not in ["Y", "y", "N", "n"]: view.invalid_input() buy_confirmation = input() if buy_confirmation in ["N", "n"]: return login_menu(user_login) try: user_login.buy(ticker_buy, volume_amount_buy) updated_position_value = user_login.getposition(ticker_buy) view.updated_position_value(updated_position_value) except: view.transaction_failed() view.not_enough_funds() return login_menu(user_login)
def sell(user_login): view.ticker_selection_sell() ticker_sell = input().upper() ticker_sell_price = model.apiget(ticker_sell) while ticker_sell_price == None: print("Not a valid ticker!") view.ticker_selection_sell() ticker_sell = input().upper() ticker_sell_price = model.apiget(ticker_sell) view.volume_amount_sell(ticker_sell, ticker_sell_price) try: volume_amount_sell = int(input()) except ValueError: view.invalid_input() return sell(user_login) view.sell_confirm(ticker_sell, ticker_sell_price, volume_amount_sell) sell_confirmation = input() while sell_confirmation not in ["Y", "y", "N", "n"]: view.invalid_input() sell_confirmation = input() if sell_confirmation in ["N", "n"]: return login_menu(user_login) try: user_login.sell(ticker_sell, volume_amount_sell) updated_position_value = user_login.getposition(ticker_sell) view.updated_position_value(updated_position_value) except: view.transaction_failed() view.not_enough_shares() return login_menu(user_login)
def testAPI(self): price = model.apiget("AAPL") self.assertEqual(type(price), float, "getprice returns a float") self.assertTrue(price > 0.0, "getprice returns positive value") model.CONFIG['FAKEPRICE']['stck'] = 35.55 self.assertFalse( model.apiget('stck') == 35.55, 'api fake price setter')
def sell(): if request.method == 'GET': if 'username' in session: return render_template('sell.html') else: flash('You will need to log in before you can sell your holdings') return redirect('/login') else: try: ticker_symbol = request.form['ticker_symbol'].upper() number_of_shares = int(request.form['number_of_shares']) total_price = model.apiget(ticker_symbol) * number_of_shares total_price_rounded = round(total_price, 2) user_object = model.set_user_object(session['username']) try: user_object.sell(ticker=ticker_symbol, volume=number_of_shares) except: flash('You do not own enough shares') return redirect('/sell') except: flash('Invalid Entry! Try Again..') return redirect('/sell') flash( f"You have just sold {number_of_shares} shares of {ticker_symbol} for {total_price_rounded}" ) return redirect('/buy')
def sell_stock(): if request.method == "GET": return render_template('authorized/sellstock.html') elif request.method == "POST": user = model.Account() user.set_from_pk(session["Active User"]) ticker = model.ticker_cap(request.form["ticker"]) price = model.apiget(ticker) if not price: return render_template('authorized/sellstock.html', message="Stock doesn't exist") if not user.stock_ownership_check(ticker): return render_template('authorized/sellstock.html', message="Stock not owned") volume = request.form["volume"] try: volume = int(volume) if volume <= 0: return render_template( 'authorized/sellstock.html', message="Invalid input, must be positive integer") except ValueError: return render_template( 'authorized/sellstock.html', message="Invalid input, must be positive integer") if not model.zero_check(volume): return render_template( 'authorized/sellstock.html', message="Invalid input, must be positive integer") if not user.sufficient_amount_check(ticker, volume): return render_template('authorized/sellstock.html', message="Insufficient amount owned") user.sell(ticker, volume, price=price) return redirect(url_for('trade_success'))
def view_portfolio(): user = model.Account() user.set_from_pk(session["Active User"]) allpositions = user.getpositions_html() positions = {} positions["tickers"] = [i.ticker for i in allpositions] positions["amounts"] = [i.amount for i in allpositions] positions["values"] = [ '${:,.2f}'.format(i.amount * model.apiget(i.ticker)) for i in allpositions ] positions["total value"] = sum([(i.amount * model.apiget(i.ticker)) for i in allpositions]) total_value = '${:,.2f}'.format(positions["total value"]) length = range(0, len(positions["tickers"])) return render_template('authorized/viewportfolio.html', positions=positions, length=length, total_value=total_value)
def check_stock_price_logged(): if request.method == 'GET': if 'username' in session: return render_template('check_stock_price_logged.html') else: return redirect('/check_stock_price') else: ticker_symbol = request.form['ticker_symbol'] price = model.apiget(ticker_symbol).upper() flash(f'The Price of {ticker_symbol} is currently ${price}') return redirect('/check_stock_price_logged')
def see_trades(): user = model.Account() user.set_from_pk(session["Active User"]) alltrades = user.gettrades() trades = {} trades["tickers"] = [i.ticker for i in alltrades] trades["prices"] = ['${:,.2f}'.format(i.price) for i in alltrades] trades["volumes"] = [i.volume for i in alltrades] trades["values"] = [ '${:,.2f}'.format((i.price * i.volume)) for i in alltrades ] trades["times"] = [ str(datetime.fromtimestamp(i.time, local_timezone))[0:-6] for i in alltrades ] length = range(0, len(trades["tickers"])) if request.method == "GET": if not alltrades: return render_template( 'authorized/seetrades.html', message="No trade history. Try buying some stock!") return render_template('authorized/seetrades.html', trades=trades, length=length) if request.method == "POST": ticker = request.form["ticker"] if not model.apiget(ticker): return render_template('authorized/seetrades.html', trades=trades, length=length, message="Stock doesn't exist") if not user.gettradesfor(ticker): return render_template('authorized/seetrades.html', trades=trades, length=length, message="No trades exist for that stock") alltrades = user.gettradesfor_html(ticker) trades = {} trades["tickers"] = [i.ticker for i in alltrades] trades["prices"] = ['${:,.2f}'.format(i.price) for i in alltrades] trades["volumes"] = [i.volume for i in alltrades] trades["values"] = [ '${:,.2f}'.format((i.price * i.volume)) for i in alltrades ] trades["times"] = [ str(datetime.fromtimestamp(i.time, local_timezone))[0:-6] for i in alltrades ] length = range(0, len(trades["tickers"])) return render_template("authorized/seetrades.html", trades=trades, length=length)
def sell(): if request.method == 'GET': if 'username' in session: return render_template('sell.html') else: flash('You will need to log in before you can sell your holdings') return redirect('/login') else: #TODO: add sell logic ticker_symbol = request.form['ticker_symbol'].upper() price = model.apiget(ticker_symbol) flash(f'The Price of {ticker_symbol} is currently ${price}') return redirect('/check_stock_price')
def show_dashboard(): user = model.Account() user.set_from_pk(session["Active User"]) balance = '${:,.2f}'.format(user.balance) allpositions = user.getpositions_html() positions = {} positions["tickers"] = [i.ticker for i in allpositions] positions["amounts"] = [i.amount for i in allpositions] positions["values"] = [ '${:,.2f}'.format(i.amount * model.apiget(i.ticker)) for i in allpositions ] positions["total value"] = sum([(i.amount * model.apiget(i.ticker)) for i in allpositions]) total_value = '${:,.2f}'.format(positions["total value"]) length = range(0, len(positions["tickers"])) return render_template('authorized/dashboard.html', balance=balance, length=length, positions=positions, total_value=total_value)
def get_price(): if request.method == "GET": return render_template('authorized/getprice.html') elif request.method == "POST": user = session["Active User"] ticker = request.form["ticker"] price = model.apiget(ticker) if not price: return render_template('authorized/getprice.html', message="Stock doesn't exist") return render_template('authorized/getprice.html', price='${:,.2f}'.format(price), ticker=model.ticker_cap(ticker))
def buy(): if request.method == 'GET': if 'username' in session: return render_template('buy.html') else: flash('You will need to log in before you can make purchases') return redirect('/login') else: try: ticker_symbol = request.form['ticker_symbol'].upper() number_of_shares = int(request.form['number_of_shares']) total_price = model.apiget(ticker_symbol) * number_of_shares except: flash('Invalid Entry! Try Again..') flash(total_price) return redirect('/buy')
def login_menu(user_login): if user_login.type == "ADMIN": view.login_menu_admin(user_login) login_input = input() while login_input not in [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" ]: view.invalid_input() login_input = input() if login_input == "1": view.check_balance(user_login) return login_menu(user_login) if login_input == "2": all_positions = user_login.getpositions() view.check_all_positions(user_login) model.print_gettrades(all_positions) return login_menu(user_login) if login_input == "3": view.ticker_query() ticker = input().upper() price = model.apiget(ticker) while price == None: print("Not a valid ticker!") view.ticker_query() ticker = input().upper() price = model.apiget(ticker) view.ticker_check_price(ticker, price) return login_menu(user_login) if login_input == "4": buy(user_login) if login_input == "5": sell(user_login) if login_input == "6": all_trades = user_login.gettrades() view.see_all_trades(user_login) model.print_gettrades(all_trades) return login_menu(user_login) if login_input == "7": view.goodbye() return login_terminal() if login_input == "8": view.goodbye() quit() if login_input == "9": view.set_funds_amount() amt_of_funds = input() user_login.set_balance(amt_of_funds) return login_menu(user_login) if login_input == "10": view.see_all_accounts_one() all_accounts = user_login.get_all_accounts() model.print_all_accounts(all_accounts) return login_menu(user_login) if login_input == "11": print(model.return_top_headlines_content("author")) return login_menu(user_login) else: view.login_menu_user(user_login) login_input = input() while login_input not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]: view.invalid_input() login_input = input() if login_input == "1": view.check_balance(user_login) return login_menu(user_login) if login_input == "2": all_positions = user_login.getpositions() print(all_positions) view.check_all_positions(user_login) model.print_gettrades(all_positions) return login_menu(user_login) if login_input == "3": view.ticker_query() ticker = input().upper() price = model.apiget(ticker) while price == None: print("Not a valid ticker!") view.ticker_query() ticker = input().upper() price = model.apiget(ticker) view.ticker_check_price(ticker, price) return login_menu(user_login) if login_input == "4": buy(user_login) if login_input == "5": sell(user_login) if login_input == "6": all_trades = user_login.gettrades() view.see_all_trades(user_login) model.print_gettrades(all_trades) return login_menu(user_login) if login_input == "7": view.goodbye() return login_terminal() if login_input == "8": view.goodbye() quit() if login_input == "9": view.deposit_amount() try: deposit_amount = int(input()) except ValueError: view.invalid_input() return login_menu(user_login) while deposit_amount < 0: view.invalid_input deposit_amount = input() user_login.deposit_funds(deposit_amount) view.depsosit_successful() return login_menu(user_login)
def get_ticker_price(ticker='aapl'): return model.apiget(ticker)