def getstockprice(): h1 = 'Get Stock Price' title = 'Terminal Trader' symbol = request.form['symbol'] payload_from_wrapper = wrapper.get_stock_price(symbol) name, price = payload_from_wrapper username = '******' cash = orm.get_balance(username) stocks_in_portfolio = orm.sell_get_list_of_positions(username) if stocks_in_portfolio is not False: current_prices = {} # [ (ticker, price, quantity ) ] for stocks in stocks_in_portfolio: name, price = wrapper.get_stock_price(stocks[0]) current_prices[stocks[0]] = price NPV = 0 for stocks in stocks_in_portfolio: stock_q_times_p = float(stocks[2]) * float(current_prices[stocks[0]]) NPV += stock_q_times_p stock_payload = [[x[0].upper(), x[2]] for x in stocks_in_portfolio] NPV += float(cash) NPVreturn = (NPV - 1000000.0) / 1000000.0 if NPVreturn > 0.999999: NPVmessage = 'up' else: NPVmessage = 'down' # clean up data cash = '{:.2f}'.format(cash) if len(cash) > 5: cash = cash[:-6] + ',' + cash[-6:] NPV = '{:.2f}'.format(NPV) if len(NPV) > 5: NPV = NPV[:-6] + ',' + NPV[-6:] NPVreturn = '{:.6f}'.format(NPVreturn) return render_template('getstockprice.html',h1=h1,title=title,name=name,price=price)
def get_cash_stocks_prices_snapshot(username): cash = orm.get_balance(username) stocks_in_portfolio = orm.sell_get_list_of_positions(username) if stocks_in_portfolio is not False: current_prices = {} # [ (ticker, price, quantity ) ] for stocks in stocks_in_portfolio: name, price = wrapper.get_stock_price(stocks[0]) current_prices[stocks[0]] = price return cash, stocks_in_portfolio, current_prices else: return cash, stocks_in_portfolio, False
def index(): h1 = 'Flask Trader' title = 'Terminal Trader' username = '******' cash = orm.get_balance(username) stocks_in_portfolio = orm.sell_get_list_of_positions(username) if stocks_in_portfolio is not False: current_prices = {} # [ (ticker, price, quantity ) ] for stocks in stocks_in_portfolio: name, price = wrapper.get_stock_price(stocks[0]) current_prices[stocks[0]] = price NPV = 0 for stocks in stocks_in_portfolio: stock_q_times_p = float(stocks[2]) * float(current_prices[stocks[0]]) NPV += stock_q_times_p stock_payload = [[x[0].upper(), x[2]] for x in stocks_in_portfolio] NPV += float(cash) NPVreturn = (NPV - 1000000.0) / 1000000.0 if NPVreturn > 0.999999: NPVmessage = 'up' else: NPVmessage = 'down' # clean up data cash = '{:.2f}'.format(cash) if len(cash) > 5: cash = cash[:-6] + ',' + cash[-6:] NPV = '{:.2f}'.format(NPV) if len(NPV) > 5: NPV = NPV[:-6] + ',' + NPV[-6:] NPVreturn = '{:.6f}'.format(NPVreturn) return render_template('index.html',h1=h1,title=title,cash=cash,NPV=NPV,NPVreturn=NPVreturn, up_or_down=NPVmessage, stocks=stock_payload) else: return render_template('index.html',h1=h1,title=title,cash=cash,NPV='0',NPVreturn='0', up_or_down='0', stocks=[])
def buy_stocks(username,shares,password,stock_exchange, stock_symbol,volume,stock_price): orm.check_balance(username) for cash in balance: if cash[balance'] == '0': print("insufficient balance") if balance > (shares * stock_price): orm.buying_stocks(username,password,stock_exchange, stock_symbol,volume,stock_price) def buying_stocks(check_balance, stock_exchange, stock_symbol, stock_price, volume, username,password): #Check the balance print("\n\nTo access any function below, enter the corresponding key") print ('''To: want to buy stocks,press A. check Balance, press B. deposit cash, press D. exit service, press E\n :''') status = input("") if status == 'A': passcheck(get_password,username) check_balance(balance) if status=='B': check_balance = orm.get_balance(username) elif status=='D': passcheck(get_password,username) depositcash(stock_price*stock_symbol) elif status=='E': print ("Thank you for using our Services") # if enough balance, buy stocks, else return insufficient funds def buy(check_balance,shares,stock_price): number_of_shares = input("Enter number of shares: ") object = Markit() price = object.get_quote(stock_symbol) enter_price = input("Enter the price to purchase shares: ") if get_stock(stock) == None: stock_to_buy = {} stock_to_buy['shares'] = shares stock_to_buy['value'] = "{0:.2f}".format(float(stock_price)) else: stock_to_buy = get_stock(stock) stock_to_buy['value'] = "{0:.2f}".format(float(enter_price) * int(number_of_shares)) stock_to_buy['shares'] = str(int(stock_to_buy['shares']) + int(shares)) #return "Your balance is " + ({0:.2f}".format(float(enter_price) * int(number_of_shares) - balance) def check_balance(username): balance = ORM.check_balance(username) return balance ''' pass def sell_stocks(shares, price): sale_price = input("Enter sale price: ") for stock in portfolio: if stock['portfolio'] portfolio.remove(stock) pass ''' if __name__ == '__main__': buy_stocks(username,shares,password,stock_exchange, stock_symbol,volume,stock_price)
def buy(): h1 = 'Buy' title = 'Terminal Trader' username = '******' symbol = request.form['symbol'] quantity = request.form['quantity'] # get price payload_from_wrapper = wrapper.get_stock_price(symbol) price = payload_from_wrapper[1] if price is not None: # check if person can buy quantity cost = float(price) * float(quantity) username = '******' cash_balance = orm.get_balance(username) print(cash_balance,cost) if cash_balance > cost: #if they can buy quantity, subtract cost from balance payload_message1 = orm.buy_stocks_users_table(username,cost, cash_balance) if payload_message1 is True: # add to positions table and transactions table orm.buy_stocks_positions_table(quantity,symbol,price,username) payload_message2=orm.buy_stocks_transactions_table(quantity,symbol,price,username) if payload_message2 is True: # get updated portfolio cash = orm.get_balance(username) stocks_in_portfolio = orm.sell_get_list_of_positions(username) if stocks_in_portfolio is not False: current_prices = {} # [ (ticker, price, quantity ) ] for stocks in stocks_in_portfolio: name, price = wrapper.get_stock_price(stocks[0]) current_prices[stocks[0]] = price NPV = 0 for stocks in stocks_in_portfolio: stock_q_times_p = float(stocks[2]) * float(current_prices[stocks[0]]) NPV += stock_q_times_p stock_payload = [[x[0].upper(), x[2]] for x in stocks_in_portfolio] NPV += float(cash) NPVreturn = (NPV - 1000000.0) / 1000000.0 if NPVreturn > 0.999999: NPVmessage = 'up' else: NPVmessage = 'down' # clean up data cash = '{:.2f}'.format(cash) if len(cash) > 5: cash = cash[:-6] + ',' + cash[-6:] NPV = '{:.2f}'.format(NPV) if len(NPV) > 5: NPV = NPV[:-6] + ',' + NPV[-6:] NPVreturn = '{:.6f}'.format(NPVreturn) # return success confirmation return render_template('buy.html', h1=h1, title=title, quantity=quantity,symbol=symbol,price=price, cash=cash,NPV=NPV,NPVreturn=NPVreturn,up_or_down=NPVmessage, stocks=stock_payload) else: return render_template('error.html', h1=h1, title=title, error_message="Error at positions or transactions table") else: return render_template('error.html', h1=h1, title=title, error_message="Error at users table") else: # else return to error.html with error message return render_template('error.html', h1=h1, title=title, error_message="You don't have enough cash to make this transaction") else: return render_template('error.html', h1=h1, title=title, error_message="Stock does not exist")
def sell(): h1 = 'Sell' title = 'Terminal Trader' symbol = request.form['symbol'] quantity = request.form['quantity'] username = '******' # get price payload_from_wrapper = wrapper.get_stock_price(symbol) price = payload_from_wrapper[1] if price is not None: # check if person has shares to sell... list_of_positions = [ (ticker, price, quantity ) ] list_of_positions = orm.sell_get_list_of_positions(username) for _ in list_of_positions: if _[0] == symbol: list_of_positions= _ if len(list_of_positions) != 0: if int(list_of_positions[2]) >= int(quantity): # edit users table income = float(price) * float(quantity) payload_message1, new_balance, income = orm.sell_stocks_user_table(username,income) if payload_message1 is not False: # edit transactions table payload_message2 = orm.sell_stocks_transactions_table(username,symbol,quantity,price) if payload_message2 is not False: # edit positions table orm.sell_stocks_positions_table(username,symbol,float(quantity),float(price)) # get updated positions username = '******' cash = orm.get_balance(username) stocks_in_portfolio = orm.sell_get_list_of_positions(username) if stocks_in_portfolio is not False: current_prices = {} # [ (ticker, price, quantity ) ] for stocks in stocks_in_portfolio: name, price = wrapper.get_stock_price(stocks[0]) current_prices[stocks[0]] = price NPV = 0 for stocks in stocks_in_portfolio: stock_q_times_p = float(stocks[2]) * float(current_prices[stocks[0]]) NPV += stock_q_times_p stock_payload = [[x[0].upper(), x[2]] for x in stocks_in_portfolio] NPV += float(cash) NPVreturn = (NPV - 1000000.0) / 1000000.0 if NPVreturn > 0.999999: NPVmessage = 'up' else: NPVmessage = 'down' # clean up data cash = '{:.2f}'.format(cash) if len(cash) > 5: cash = cash[:-6] + ',' + cash[-6:] NPV = '{:.2f}'.format(NPV) if len(NPV) > 5: NPV = NPV[:-6] + ',' + NPV[-6:] NPVreturn = '{:.6f}'.format(NPVreturn) return render_template('sell.html',h1=h1,title=title,symbol=symbol, quantity=quantity,price=price,income=income, new_balance=new_balance, cash=cash, NPV=NPV, NPVreturn=NPVreturn, up_or_down=NPVmessage, stocks=stock_payload ) else: return render_template('error.html', h1=h1, title=title, error_message='Error at transactions table') else: return render_template('error.html', h1=h1, title=title, error_message='Error at users table') else: return render_template('error.html', h1=h1, title=title, error_message='You do not own enough shares') else: return render_template('error.html', h1=h1, title=title, error_message='You do not own this stock') else: return render_template('error.html', h1=h1,title=title,error_message='Stock symbol does not exist')
def get_balance(username): balance_message = orm.get_balance(username) return balance_message