After a week or so of idle i returned to finance and tried to chek my buy function. I bought some stocks of apple(AAPL) but got invalid symbol error, when i tried to use quote(this function always worked) and got same error. I reinstalled helpers file, this didnt help. Here is my code of buy function, but i dont think its me, cos a week ago i could use buy and quote function.

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

if request.method == "POST":

    if lookup(request.form.get("symbol")) == None:
        return apology("invalid symbol", 400)

    cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])[0]["cash"]

    price = lookup(request.form.get("symbol"))['price']

    shares = float(request.form.get("shares"))

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

    if price*shares > cash:
        return apology("can`t afford", 400)

    db.execute("UPDATE users SET cash = :cash WHERE id = :user_id", cash = cash - price*shares, user_id = session["user_id"])

    db.execute("INSERT INTO stocks  (user_id, symbol, shares) VALUES (:user_id, :symbol, :shares)", user_id = session["user_id"], symbol = symbol, shares = shares)

    db.execute("INSERT INTO history (user_id, action, symbol, price, shares, datetime) VALUES (:user_id, :action, :symbol, :price, :shares, :datetime",
    user_id = session["user_id"], action = "buy", symbol = symbol, price = price, shares = shares, datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

    return apology("ITSALIVE")

    return render_template("buy.html")

1 Answer 1


OK, i am just dumbo, i used my API token without last character

You must log in to answer this question.

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