Ejemplo n.º 1
0
def portfolio():
    if session.get('logged_in'):
        with sqlite3.connect('mainDB.db') as conn:
            userID = session['UserName']
            userID = str(userID).lower()
            data = DBTools.getOwnedStocksByID(conn, userID)
            length = len(data) + 1
            data = json.dumps(data)
            cash = DBTools.getUserCash(conn, userID=str(userID))
            cash = '${:20,.2f}'.format(cash)
            data2 = DBTools.getOwnedStocksByIDPrices(conn, userID=userID)
            length2 = len(data2) + 1
            data2 = json.dumps(data2)
            portfolioValue = DBTools.getPortfolioValue(conn, DBTools.getAllPortData(conn, userID=userID)[0]['portfolioID'])
            portfolioValue = '${:20,.2f}'.format(portfolioValue)

        return render_template("portfolio.html", ad=ad_factory.get_ad(), availableStocks=DBTools.getAvailableStocks(), data=data, length=length, UserCash=cash, length2=length2, prices=data2, portValue=portfolioValue)
    else:
        return render_template("login.html", ad=ad_factory.get_ad(), availableStocks=DBTools.getAvailableStocks())
Ejemplo n.º 2
0
def PostStock():
    with sqlite3.connect('mainDB.db') as conn:
        Action = request.form['Action']
        Quantity = request.form['Quantity']
        Ticker = request.form['Ticker']

        TickerPath = Ticker + ".html"
        data = StockPrices.getAllStockData(Ticker)
        sUrl = url_for('static', filename='./stockplots/' + TickerPath)
        User = session['UserName']

        # file = open('testfile.txt','w')
        # file.write(str(User))
        # file.close()
        User2 = str(User)
        Price = StockPrices.getPrice(Ticker)
        Price = float(Price)
        Quantity = float(Quantity)
        total = (Price * Quantity)
        total = '${:20,.2f}'.format(total)
        port = DBTools.getAllPortData(conn, User2)[0]['portfolioID']

        if(Action == 'Buy'):
            if(Trades.buyStock(conn, Ticker, port, Price, Quantity)):
                flash("Purchased " + str(Quantity) + " Stock from " + str(Ticker) + " for " + str(total))
            else:
                flash("Could not Complete Purchase")
        elif(Action == 'Sell'):
            if(Trades.sellStock(conn, Ticker, port, Price, Quantity)):
                flash("Sold " + str(Quantity) + " Stock from " + str(Ticker) + " for " + str(total))
            else:
                flash("Could not Sell Stock")
        Quant = DBTools.getPortfolioStocks(conn, str(session['UserName']).lower(), Ticker)
        cash = DBTools.getUserCash(conn, userID=str(session['UserName']).lower())
        cash = '${:20,.2f}'.format(cash)
        return render_template("stock.html", ad=ad_factory.get_ad(1), availableStocks=DBTools.getAvailableStocks(), ticker=Ticker, sUrl=sUrl, stock_data=data, ownedQuantity=Quant, UserCash=cash)