I have read in other comments that API may return error sometimes due to volume and others. Is there a way around it? Can we temporary bypass the issue, or is there any way to continue checking other parts of the code while the API get solved by itself?
I may have other issue in the code that I am unable to catch. BUY or QUOTE do not return the 'NoneType' error message! Please any help would be much appreciated.
Error message:
File "/home/ubuntu/pset8/finance/application.py", line 223, in sell price = quote["price"] TypeError: 'NoneType' object is not subscriptable
This is my SELL chunk:
@login_required
def sell():
"""Sell shares of stock"""
user_id = session["user_id"]
portfolio = db.execute("SELECT symbol, SUM(shares) AS max_shares FROM history WHERE id=:user_id GROUP BY symbol HAVING max_shares > 0", user_id=user_id)
if request.method == "POST":
symbol = request.form.get("symbol")
quote = lookup(symbol)
price = quote["price"]
name = quote["name"]
share_amount = request.form.get("shares")
shares_to_sell = 0
if share_amount:
shares_to_sell = int(request.form.get("shares"))
if symbol == None:
return apology("Symbol is needed")
if shares_to_sell < 1:
return apology("Insert amount of shares to sell")
max_shares = portfolio[0]["max_shares"]
if max_shares < shares_to_sell:
return apology("Not enough shares to sell")
users_rows = db.execute("SELECT cash FROM users WHERE id=:username")
cash = users_rows[0]["cash"]
balance = cash + (shares_to_sell * price)
db.execute("UPDATE users SET cash = :balance WHERE id = :username", balance=balance, username=user_id)
db.execute("INSERT INTO history (id, type, symbol, shares, price, name) VALUES (:username, 'sell', :symbol, :share_amount, :price, :name)", username=user_id, symbol=symbol, share_amount=-shares_to_sell, price=price, name=name)
return redirect("/")
else:
return render_template("sell.html", portfolio=portfolio)
sell.html as follows:
{% block title %}
Sell
{% endblock %}
{% block main %}
<form action="/sell" method="post">
<div class="form-group">
<select class="form-control" name="symbol">
<option disabled selected value=>Symbol</option>
{% for row in portfolio %}
<option value="symbol" id="symbol">{{ row["symbol"] }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<input autocomplete="off" class="form-control" name="shares" placeholder="Number of shares" type="number" min="0">
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}
Symbol I am trying to sell is V, which BUY allowed me to purchase with no errors.