def sell(ticker_symbol, trade_volume, username): trade_volume = float(trade_volume) user_balance = mapper.select_balance(username) ticker_symbols = mapper.select_ticker(ticker_symbol, username) brokerage_fee = 6.95 last_price = wrapper.get_last_price(ticker_symbol) transaction_cost = (last_price * trade_volume) - brokerage_fee if ticker_symbols != None: # If the holding exists. Continue... x = mapper.select_holding(ticker_symbol, username) prev_volume = float(x[0]) vwap = float(x[1]) total_vol = prev_volume - trade_volume new_balance = user_balance + transaction_cost if prev_volume > trade_volume: mapper.update_holdings(total_vol, vwap, ticker_symbol, username) mrkt_value = market_value(username) new_mrkt_val = mrkt_value + new_balance mapper.update_balance(new_balance, new_mrkt_val, username) elif prev_volume == trade_volume: mapper.delete_holding(ticker_symbol, username) mrkt_value = market_value(username) new_mrkt_val = mrkt_value + new_balance mapper.update_balance(new_balance, new_mrkt_val, username) elif prev_volume < trade_volume: return 'You do not have that many shares!' return 'Successful!' else: return 'You do not have that stock!'
def buy(ticker_symbol, trade_volume, username): trade_volume = float(trade_volume) user_balance = mapper.select_balance(username) brokerage_fee = 6.95 last_price = wrapper.get_last_price(ticker_symbol) print(last_price) transaction_cost = (last_price * float(trade_volume)) + brokerage_fee if user_balance >= transaction_cost: ticker_symbols = mapper.select_ticker(ticker_symbol, username) if ticker_symbols == None: # If the holding exists. Continue... mapper.insert_to_holdings(ticker_symbol, trade_volume, last_price, username) else: x = mapper.select_holding(ticker_symbol, username) prev_volume = float(x[0]) prev_price = float(x[1]) # Calculating VWAP old = prev_volume * prev_price new = trade_volume * last_price total_vol = prev_volume + trade_volume new_vwap = (old + new) / (total_vol) mapper.update_holdings(total_vol, new_vwap, ticker_symbol, username) new_balance = user_balance - transaction_cost mrkt_value = market_value(username) new_mrkt_val = mrkt_value + new_balance mapper.update_balance(new_balance, new_mrkt_val, username) return 'Successful!' else: return 'Error: You do not have enough money to trade!'
def sell(ticker_symbol, trade_volume, username): last_price = wrapper.get_last_price(ticker_symbol) # Error handling: if the user enters a ticker symbol that does not exist. if last_price == "exit": print("The ticker symbol that you entered does not exist.") return "exit" brokerage_fee = 6.95 # Error handling: if the user enters a trade volume that is not a number. try: balance_to_add = last_price * float(trade_volume) - brokerage_fee except ValueError: print("The trade volume you entered is not valid.") return "exit" # Checks if the user holds any stock from the company with ticker_symbol. ticker_symbols = mapper.get_ticker_symbols(ticker_symbol, username) if len(ticker_symbols) == 0: return "Error: You do not hold any shares from that company." # Gets needed values from the user and holdings database tables. balance = mapper.get_balance(username) number_of_shares = mapper.get_number_of_shares(ticker_symbol, username) new_number_of_shares = number_of_shares - int(trade_volume) # If the user holds enough shares to complete their trade. if int(trade_volume) <= number_of_shares: # If the new number of shares would be 0 after the user sells their shares. if new_number_of_shares == 0: # Deletes the row from holdings database table for company with ticker_symbol. mapper.delete_holdings_row(ticker_symbol) else: # Updates holdings database table with the new number of shares. mapper.update_number_of_shares(new_number_of_shares, ticker_symbol, username) # Gets the last price stored in the holdings database table for a given ticker_symbol. curr_price = mapper.get_last_price(ticker_symbol, username) # Gets the number of shares from holdings database table for the company with ticker_symbol. curr_number_of_shares = mapper.get_number_of_shares(ticker_symbol, username) # Calculates the new VWAP based on old values in the database table. new_vwap = calculate_vwap(curr_price, curr_number_of_shares, last_price, trade_volume) # Updates the VWAP in the holdings database table with a new value. mapper.update_volume_weighted_average_price(new_vwap, ticker_symbol, username) # Updates users database table with the new balance after selling the stock. new_balance = balance + balance_to_add mapper.update_balance(new_balance, username) # Inserts a new row to the orders database table after selling the stock. mapper.insert_orders_row("sell", ticker_symbol, trade_volume, last_price, username) return "Stock sell was successful." else: # Returns error response. return "Error: You do not have enough shares to sell to complete that trade."
def buy(ticker_symbol, trade_volume, username): last_price = wrapper.get_last_price(ticker_symbol) # Error handling: if the user enters a ticker symbol that does not exist. if last_price == "exit": print("The ticker symbol that you entered does not exist.") return "exit" balance = mapper.get_balance(username) brokerage_fee = 6.95 # Error handling: if the user enters a trade volume that is not a number. try: transaction_cost = last_price * float(trade_volume) + brokerage_fee except ValueError: print("The trade volume you entered is not valid.") return "exit" # If the user has enough money in their account to execute the trade. if transaction_cost < balance: ticker_symbols = mapper.get_ticker_symbols(ticker_symbol, username) # If the user does not hold any stock from company with ticker_symbol. if len(ticker_symbols) == 0: new_balance = balance - transaction_cost # Updates the user's balance in the users database table. mapper.update_balance(new_balance, username) # Inserts a new row to the holdings database table after buying the stock. mapper.insert_holdings_row(ticker_symbol, trade_volume, last_price, username) # Inserts a new row to the orders database table after buying the stock. mapper.insert_orders_row("buy", ticker_symbol, trade_volume, last_price, username) return "Stock purchase was successful." # If the user holds some stock from company with ticker_symbol. else: # Gets the number of shares from holdings database table for the company with ticker_symbol. curr_number_of_shares = mapper.get_number_of_shares(ticker_symbol, username) new_number_of_shares = curr_number_of_shares + int(trade_volume) # Gets the last price stored in the holdings database table for a given ticker_symbol. curr_price = mapper.get_last_price(ticker_symbol, username) # Calculates the new VWAP based on the current values in the database table and the most recent (last) prices. new_vwap = calculate_vwap(curr_price, curr_number_of_shares, last_price, trade_volume) # Updates the VWAP in the holdings database table with a new value. mapper.update_volume_weighted_average_price(new_vwap, ticker_symbol, username) # Updates the holdings database table with the new number of shares after buying stock. mapper.update_number_of_shares(new_number_of_shares, ticker_symbol, username) # Inserts a new row to the orders database table after buying the stock. mapper.insert_orders_row("buy", ticker_symbol, trade_volume, last_price, username) return "Stock purchase was successful." else: # Returns error response. return "Error: You do not have enough money in your balance to execute that trade."
def update_balance(username, balance): return mapper.update_balance(username, balance)
def update_balance(new_balance, username): return mapper.update_balance(new_balance, username)