0

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.

enter image description here

1 Answer 1

2

Two Three possible points of failure.

  • symbol = request.form.get("symbol"). Remember, python get() returns None if the requested key is not found. Is there an element in the html with attribute name="symbol"? If not symbol will be None.
  • quote = lookup(symbol). Lookup will return None if symbol is not found by the API. If symbol is not a valid ticker symbol, None will be the result If the value of symbol is None (as from the previous line), None will be the result. lookup also returns none if it cannot access the API because the API_KEY is not set or not valid.
  • request.form.get("symbol") returns the word "symbol", literally. That is because the value sent from the server is set here: <option value="symbol" id="symbol">{{ row["symbol"] }}</option>. Regardless of which option is selected, the server always hears "symbol".
3
  • Thanks Dino. I understand what you say. I have udated my question with the HTML code and the screenshot of the the index page. As BUY and QUOTE returned date from quote, still cannot figure out why SELL can't.
    – meh362
    Jul 1, 2020 at 15:39
  • Aha! Answer amended. Jul 1, 2020 at 16:35
  • That 3rd hit solved it. Thx Dino
    – meh362
    Jul 1, 2020 at 18:31

You must log in to answer this question.

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