0

:)

Struggle with the last check. Get this error message:

expected to find "56.00" in page, but it wasn't found

Tried the same approach like I used for the buy-function. Create an extra template to display the amount of shares, the value and the new cash, but it doesn't work.

What I changed was that every time I sell or buy shares of the same stock the price will be updated. Maybe check50 want to sell the share for the price I bought them? In reality it makes sense.

Python-Code

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""

if request.method == "POST":

    stock = lookup(request.form.get("symbol"))

    if stock == None:
        return apology("invalid ticker symbol",400)

    # Check if user entered a valid value for share
    try:
        share = int(request.form.get("shares"))
    except:
        return apology("shares must be a posative integer", 400)

    if share < 0:
        return apology("shares must be a posative integer", 400)

    check_stock = db.execute("SELECT share AS total_shares FROM buy WHERE user_id = :user_id AND stock = :stock", user_id = session["user_id"], stock = request.form.get("symbol"))

    if check_stock[0]["total_shares"] < 1 or check_stock[0]["total_shares"] <= 0 or check_stock[0]["total_shares"] < share:
        return apology("You have not enough stocks of this company",400)

    actual_price = stock["price"]

    db.execute("UPDATE buy SET share = share - :share WHERE user_id = :user_id AND stock = :stock", share = share, user_id = session["user_id"], stock = request.form.get("symbol"))
    db.execute("UPDATE users SET cash = cash + :new_value WHERE id = :user_id", new_value = actual_price * share, user_id = session["user_id"])

    # Create entry in transaction table
    db.execute("INSERT INTO 'transaction' ('user_id','share','price','symbol','date','type') VALUES (:user_id,:share,:price,:symbol,:date,:type)", user_id = session["user_id"], share = share, price = actual_price, symbol = request.form.get("symbol"), date = datetime.datetime.now(), type = "sell")

    new_cash = db.execute("SELECT cash from users WHERE id = :user_id", user_id = session["user_id"])
    new_cash = int(new_cash[0]["cash"])
    new_share = db.execute("SELECT share AS share FROM buy WHERE user_id = :user_id AND stock = :stock", user_id = session["user_id"], stock = request.form.get("symbol"))
    new_share = int(new_share[0]["share"])
    old_price = db.execute("SELECT price AS price FROM buy WHERE user_id = :user_id AND stock = :stock", user_id = session["user_id"], stock = request.form.get("symbol"))
    old_price = int(old_price[0]["price"])
    new_share_value = new_share * old_price

    return render_template("sold.html", new_share_value = usd(new_share_value), new_cash = new_cash, new_total = usd(new_cash + new_share_value))


else:
    stocks = db.execute("SELECT stock FROM buy WHERE user_id = :user_id", user_id = session["user_id"])

    return render_template ("sell.html", stocks = stocks)

HTML-Template

{% extends "layout.html" %}

{% block title %}
Log In
{% endblock %}

{% block main %}
<p>You know have {{ new_share }} shares worth {{ new_share_value }}.
Know you have {{ new_cash }} cash and in total {{ new_total }}.</p>
{% endblock %}

Thanks for your help! :)

1

check50 is looking to see that if you bought, say, 5 shares of ABC and then sold 3 shares, your index page will show the remaining 2 shares with a value using the current quote. (since that is what your index page should always be showing ... current holdings).

So if ABC was worth $28/share and you had 5 shares (worth $140.00) and sold 3 shares, then your index page would show 2 shares worth $56.00.

It's that's $56.00 that check50 is looking to find.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .