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 get_holdings_with_market_value(username): brokerage_fee = 6.95 mkt_holding_list = None user_holdings = get_holdings_by_username(username) if user_holdings != None: mkt_holding_list = [] # Generates an updated holding list containing market price info for item in user_holdings: # Gets updated market price market_price = wrapper.get_last_price(item['ticker_symbol']) # Creates and fills holdings dictionary with updated market info mkt_holding = {} mkt_holding['pk'] = item['pk'] mkt_holding['username'] = item['username'] mkt_holding['ticker_symbol'] = item['ticker_symbol'] mkt_holding['volume'] = item['volume'] mkt_holding['avg_buy_price'] = item['average_price'] mkt_holding['mkt_price'] = market_price ###### CALCULATES TOTALS AND DIFFERENCE ###### # Total based on Buy Prices total_buy_price = item['volume'] * item['average_price'] mkt_holding['total_buy_price'] = total_buy_price # Total based on Market Prices. Assumes one brokerage fee per holding. total_mkt_price = (item['volume'] * market_price) - brokerage_fee mkt_holding['total_mkt_price'] = total_mkt_price # Difference between market and buy price mkt_holding['difference'] = total_mkt_price - total_buy_price # Appends to the market holding list mkt_holding_list.append(mkt_holding) return mkt_holding_list
def get_earnings(username): user_ticker_symbols = mapper.get_ticker_symbols_from_user(username) earnings = 0.0 for t in user_ticker_symbols: last_price = wrapper.get_last_price(t) # Current market price user_num_shares = mapper.get_number_of_shares(t, username) if last_price == "exit": earnings += 0.0 else: earnings += float(last_price) * user_num_shares return earnings
def holdings_performance(username): df1 = mapper.pandas_connect(username) df = df1.drop(['pk', 'username'], axis=1) df.columns = ['Ticker', 'Shares', '@Price'] df['Init_Value'] = df['@Price'] * df['Shares'] last_prices = [] for tkr in df['Ticker']: price = wrapper.get_last_price(tkr) last_prices.append(float(price)) df['Mrkt_Price'] = last_prices df['Mrkt_Value'] = df['Mrkt_Price'] * df['Shares'] df['P/L'] = df['Mrkt_Value'] - df['Init_Value'] return df
def controlla(username): while True: buy_inputs = ['buy','b'] sell_inputs = ['sell','s'] lookup_inputs = ['lookup','l'] quote_inputs = ['quote','q'] performance = ['performance','p'] holdings = ['holdings','h'] exit_inputs = ['exit','e'] allowed_inputs = buy_inputs + sell_inputs\ + lookup_inputs + quote_inputs\ + performance + holdings + exit_inputs user_input = view.main_menu_UI().lower() if user_input in allowed_inputs: if user_input in buy_inputs: ticker_symbol,trade_volume = view.buy_UI() order_status = model.buy(ticker_symbol,trade_volume,username) print(order_status) input("\nPress Enter to continue...") elif user_input in sell_inputs: ticker_symbol,trade_volume = view.sell_UI() order_status = model.sell(ticker_symbol,trade_volume,username) print(order_status) input("\nPress Enter to continue...") elif user_input in lookup_inputs: company_name = view.lookup_UI() ticker_symbol = wrapper.get_ticker_symbol(company_name) print(ticker_symbol) input("\nPress Enter to continue...") elif user_input in quote_inputs: ticker_symbol = view.quote_UI() last_price = wrapper.get_last_price(ticker_symbol) print(last_price) input("\nPress Enter to continue...") elif user_input in performance: view.performance_UI() perf = model.portfolio_performance(username) print(perf) input("\nPress Enter to continue...") elif user_input in holdings: df = model.holdings_performance(username) df2 = model.portfolio_performance(username) print(df2,'\n') print(df) input("\nPress Enter to continue...") elif user_input in exit_inputs: print('\nYou are about to exit!') input("\nPress Enter to continue...") return 'exit' else: print('\nNot A Command, Try Again!')
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 get_last_price(ticker_symbol): return wrapper.get_last_price(ticker_symbol)