def perform_transaction(username, symbol, shares, transaction_type): user = Users.objects.get(username=username) #stock = Stocks.objects.get(symbol = symbol) share = Shares.objects.get(username=username) new_val = Transactions(user_id=username, stock_id=symbol, timestamp=datetime.datetime.now(), number_of_shares=shares, transaction_type=transaction_type) new_val.save() if transaction_type == "+": #subtract stock amount purchased from users wallet user.cash_amount -= stock_info.getStockPrice(symbol) * shares #add number of shares on the given stock in Shares table share.__dict__[symbol] += shares else: #add stock amount sold to users wallet user.cash_amount += stock_info.getStockPrice(symbol) * shares #subtract number of shares from the given stock in Shares table share.__dict__[symbol] -= shares user.save() share.save()
def generate_test(user_data): '''Generate test data based on user account status''' test_data = [] risk_profile = RISK_PROFILES[user_data[1]] stocks = user_data[3:] for i, stock in enumerate(stocks): symbol = STOCK_SYMBOLS[i] owned = 'not owned' # check if the user owns the current stock if stock != 0: owned = 'owned' # identify the relationship between the current stock price and vwap price = stock_info.getStockPrice(symbol) vwap = stock_info.getStockVWAP(symbol) if vwap > price: vwap_price = 'greater' elif vwap < price: vwap_price = 'less' else: vwap_price = 'equal' # add preprocess stock info to test data test_data.append([risk_profile, owned, vwap_price]) return test_data
def get_user_stocks(username): column = 1 shares = Shares.objects.filter(username=username).values_list() shares_array = shares[::1] user_stocks = [] for i in shares_array: for j in i: if column > 3 and j != 0: user_stocks.append([ stocks[column - 4], stock_info.getStockCompany(stocks[column - 4]), str(j), str(stock_info.getStockPrice(stocks[column - 4])) ]) column += 1 column = 0 return user_stocks
def dash(request): user = request.user user = get_user(user.username) U_stocks = get_user_stocks_and_price(user.values_list()[0][0]) stocks = [] names = [] for i, k in U_stocks: stocks.append(float(k) * (stock_info.getStockPrice(i))) names.append(i) plt.pie(stocks, labels=names, autopct='%.1f%%', startangle=90, shadow=False) plt.savefig("main/static/pie.jpg") ai = get_recommendations(user.values_list()[0][0]) return render(request, 'main/dash.html', { "stocks": U_stocks, "cash": user.values_list()[0][5], "reccomend": ai })
def create_all_stocks(): for x in stocks: create_stock(x, stock_info.getStockCompany(x), stock_info.getStockPrice(x), stock_info.getStockPercentChange(x), stock_info.getStockVWAP(x))