def getquote(): try: if request.method == 'POST': symbol = request.form['symbol'] data = controller.get_quote(symbol) c_price = data['LastPrice'] return render_template("getquote.html",c_price=c_price) except: if request.method == 'POST': symbol = request.form['symbol'] data= controller.get_quote(symbol) if data['Message']: flash('No data available for mentioned quote', 'danger') return render_template('getquote.html')
def getquote(): if request.method == 'POST': symbol = request.form['symbol'] data = controller.get_quote(symbol) c_price = data['LastPrice'] return render_template("getquote.html",c_price=c_price) else: return render_template("getquote.html")
def sell(): old_balance = User.query.filter_by(username=session['username']).first() old_balance.balance if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) name = data['Name'] symbol = data['Symbol'] price = data['LastPrice'] quantity = (request.form['quantity']) total = (price*int(quantity)) id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first() if int(quantity) > transaction.c_quantity: flash('Transaction not allowed.','danger') return redirect(url_for('sell')) elif Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first(): transaction.c_quantity = transaction.c_quantity - int(quantity) transaction.c_total = transaction.c_total - total new_balance = (old_balance.balance + total) new_balance = round(new_balance, 2) old_balance.balance = new_balance db.session.commit() flash('Stock Sold.','success') u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template('sell.html',transaction=transaction,u_balance=u_balance) else: u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template("sell.html", transaction=transaction,u_balance=u_balance)
def sell_stock(username): # var initiation t = PrettyTable() print() symbol = str(input("Enter the ticker symbol for the company: ")).upper() qty = int(input("Enter the quantity to sell: ")) # print table details = controller.get_details(symbol) if details != 0: t.field_names = details.keys() t.add_row(details.values()) print('\n' + t.get_string(fields=[ "Name", "Symbol", "LastPrice", "Change", "ChangePercent", "ChangeYTD", "ChangePercentYTD", "Volume" ]) + '\n') print('Your total purchase value is ${0:.2f}'.format( controller.get_quote(symbol) * qty)) confirmation = str( input( 'Are you sure you want to go ahead with this transaction? (Y/N) ' )) # validate confirmation and do actual selling of stock if confirmation != 'Y' and confirmation != 'y': print(Fore.RED + Style.BRIGHT + '\nINVALID OPTION, TRY AGAIN!\n') else: last_price, total_price, balance, response = controller.sell_stock( symbol, qty, username) if response == 400: print( 'You have insufficient funds to proceed with this transaction! Please try again.' ) elif response == 500: print('There is a connectivity issue! Please try again.') elif response == 200: print('-' * 30) print( 'You have successfully sold {} shares of {} at ${} per share.' .format(qty, symbol, last_price)) print('For a total of ${0:.2f}'.format(total_price)) print('Your balance is now ${0:.2f}.'.format(balance)) else: print(Fore.RED + Style.BRIGHT + '\nNetwork error, please try again!')
def get_quote(): sym = input("Enter the symbol of the company to view quote : ") data = controller.get_quote(sym) for k,v in data.items(): print(k, " : ", v) print("\n\n\n\n")
def sell(): try: old_balance = User.query.filter_by(username=session['username']).first() old_balance.balance if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) name = data['Name'] symbol = data['Symbol'] price = data['LastPrice'] price = round(price, 2) quantity = (request.form['quantity']) total = (price*int(quantity)) total = round(total, 2) id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first() if int(quantity) > transaction.c_quantity_total: flash('Transaction not allowed.','danger') return redirect(url_for('sell')) elif Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first(): transaction.c_quantity_sold = quantity transaction.c_quantity_total = transaction.c_quantity_total - int(quantity) transaction.c_current_purchase = total new_balance = (old_balance.balance + total) new_balance = round(new_balance, 2) old_balance.balance = new_balance transaction.buy_sell = 'sell' db.session.commit() flash('Stock Sold.','success') u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template('sell.html',transaction=transaction,u_balance=u_balance) else: u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template("sell.html", transaction=transaction,u_balance=u_balance) except: if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) try: if data == 'Connection Error': flash('You are not connected to Internet, please check your internet connection and try again.','danger') return redirect(url_for('sell')) elif data['Message']: flash('No data available for mentioned quote','danger') return redirect(url_for('sell')) except: id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=id).all() for i in transaction: if i.c_symbol != symbol: flash('You do not own stocks for mentioned quote','danger') return redirect(url_for('sell'))
def buy(): try: old_balance = User.query.filter_by(username=session['username']).first() old_balance.balance if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) name = data['Name'] symbol = data['Symbol'] price = data['LastPrice'] price = round(price, 2) quantity = (request.form['quantity']) total = (price*int(quantity)) total = round(total, 2) id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first() balance = User.query.filter_by(username=session['username']).first().balance if balance < total: flash("Insufficient funds.",'danger') return redirect(url_for('buy')) elif Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first(): transaction.c_quantity_purchased = int(quantity) transaction.c_quantity_total = transaction.c_quantity_total + int(quantity) transaction.c_current_purchase = total new_balance = (old_balance.balance - total) new_balance = round(new_balance, 2) old_balance.balance = new_balance db.session.commit() flash('Stocks updated for existing company.','success') else: transaction = Transaction(c_name=name,c_symbol=symbol,c_lastprice=price,c_quantity_purchased=quantity,c_quantity_total=quantity,c_current_purchase=total,buy_sell='buy',api_name='Markit',t=user) new_balance = (old_balance.balance - total) new_balance = round(new_balance, 2) old_balance.balance = new_balance db.session.add(transaction) db.session.commit() flash('Stocks purchased for new company.', 'success') u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template('buy.html', u_balance = u_balance, transaction=transaction) else: u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() #for i in transaction: #c_name = i.c_name #c_symbol = i.c_symbol #c_lastprice = i.c_lastprice #c_quantity = i.c_quantity return render_template("buy.html",transaction=transaction,u_balance=u_balance) except: if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) if data == 'Connection Error': flash('You are not connected to Internet, please check your internet connection and try again.','danger') return redirect(url_for('buy')) elif data['Message']: flash('No data available for mentioned quote', 'danger') return redirect(url_for('buy')) return render_template('buy.html')
def buy(): old_balance = User.query.filter_by(username=session['username']).first() old_balance.balance if request.method == 'POST': user = User.query.filter_by(username=session['username']).first() symbol = request.form['symbol'] data = controller.get_quote(symbol) name = data['Name'] symbol = data['Symbol'] price = data['LastPrice'] quantity = (request.form['quantity']) total = (price*int(quantity)) id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first() balance = User.query.filter_by(username=session['username']).first().balance if balance < total: flash("Insufficient funds.",'danger') return redirect(url_for('buy')) elif Transaction.query.filter_by(t_id=id, c_symbol=request.form['symbol']).first(): transaction.c_quantity = transaction.c_quantity + int(quantity) transaction.c_total = transaction.c_total + total new_balance = (old_balance.balance - total) new_balance = round(new_balance, 2) old_balance.balance = new_balance db.session.commit() flash('Stocks Updated','success') else: transaction = Transaction(c_name=name,c_symbol=symbol,c_lastprice=price,c_quantity=quantity,c_total=total,t=user) new_balance = (old_balance.balance - total) new_balance = round(new_balance, 2) old_balance.balance = new_balance db.session.add(transaction) db.session.commit() flash('Stocks purchased for new company.', 'success') u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() return render_template('buy.html', u_balance = u_balance, transaction=transaction) else: u_balance = User.query.filter_by(username=session['username']).first().balance u_id = User.query.filter_by(username=session['username']).first().id transaction = Transaction.query.filter_by(t_id=u_id).all() #for i in transaction: #c_name = i.c_name #c_symbol = i.c_symbol #c_lastprice = i.c_lastprice #c_quantity = i.c_quantity return render_template("buy.html",transaction=transaction,u_balance=u_balance)