def sell(self, ticker_symbol, trade_volume): # TODO connect to the model, un-hardcode the username, un-hardcode the price, un-hardcode the trade_volume buy = 0 # zero value indicates a sell with Database() as db: db.cursor.execute(f"""SELECT current_holdings FROM positions WHERE ticker_symbol = '{ticker_symbol}';""" ) #TODO add back username my_position = db.cursor.fetchone() last_price = w.quote(ticker_symbol) execution_price = last_price order_quantity = trade_volume * -1 # makes a sell neg. qty time_stamp = time.time() market_value = User.calc_market_value( self, trade_volume, last_price) * -1 # makes a sell pos. cash if ((my_position[0]) >= abs(order_quantity)): User.update_balance(self, self.username, market_value) User.record_transaction(self, self.username, buy, execution_price, ticker_symbol, order_quantity, time_stamp) print("Filled! You sold {} {} @ {}.".format( trade_volume, ticker_symbol, last_price)) else: print( f"Rejected! You don't have enough {ticker_symbol} available to sell." )
def buy(self, ticker_symbol, trade_volume): # TODO connect to the model, un-hardcode the username, un-hardcode the price, un-hardcode the trade_volume buy = 1 with Database() as db: db.cursor.execute( f"""SELECT balance from users where username='******';""" ) my_cash = db.cursor.fetchone() last_price = w.quote(ticker_symbol) execution_price = last_price market_value = User.calc_market_value(self, trade_volume, last_price) order_quantity = trade_volume time_stamp = time.time() if (float(my_cash[0]) >= market_value): User.update_balance(self, self.username, market_value) User.record_transaction(self, self.username, buy, execution_price, ticker_symbol, order_quantity, time_stamp) print("Filled! You paid {} for {} {}.".format( last_price, trade_volume, ticker_symbol)) return "Filled! You paid {} for {} {}.".format( last_price, trade_volume, ticker_symbol) else: print("Rejected! You don't have enough funds available.") return "Rejected! You don't have enough funds available."
def mtm_pnl(self, ticker_symbol): ## pseudo: (current price - my avg price ) * my_position last_price = w.quote(ticker_symbol) with Database() as db: db.cursor.execute( f"""SELECT average_price FROM positions WHERE ticker_symbol = '{ticker_symbol}';""" ) my_avg_price = db.cursor.fetchone() my_avg_price = my_avg_price[0] db.cursor.execute( f"""SELECT current_holdings FROM positions WHERE ticker_symbol = '{ticker_symbol}';""" ) my_current_qty = db.cursor.fetchone() my_current_qty = my_current_qty[0] current_pnl = (last_price - my_avg_price) * my_current_qty return current_pnl
def quote(): if request.method == 'GET': return render_template('quote.html') else: user_submission = request.form.get('ticker_symbol') return render_template('quote_got.html', msg= w.quote(user_submission), ticker = user_submission)