kirkthaker / investopedia-trading-api

An api, written in Python, for Investopedia's paper trading stock simulator.
Other
215 stars 61 forks source link

AttributeError: 'str' object has no attribute 'value' #16

Open glotchimo opened 7 years ago

glotchimo commented 7 years ago

Update details (from StackOverflow)

This is a question involving Python, Beautiful Soup, and HTML, as well as the Investopedia API.

The first part involved is this block of Python that takes an input and executes a function based off of the given input (action is raw_input):

if action == 'trade':
    symbol = raw_input("Symbol: ")
    order_type = raw_input("Order Type: ")
    quantity = raw_input("Quantity: ")

    if order_type == 'buy':
        client.trade(str(symbol), ita.Action.buy, int(quantity))
    elif order_type == 'sell':
        client.trade(str(symbol), ita.Action.sell, int(quantity))
    elif order_type == 'short':
        client.trade(str(symbol), ita.Action.short, int(quantity))

The second parameter (ita.Action.[order_type]), written as directed in the API documentation, and as indicated in the code itself, refers to this block:

# input transaction type
[option.attrs.pop("selected", "") for option in trade_form.select("select#transactionTypeDropDown")[0]("option")]
trade_form.select("select#transactionTypeDropDown")[0].find("option", {"value": str(orderType.value)})["selected"] = True

That block uses .find from Beautiful Soup to target a drop-down menu that is formatted as such:

<select name="transactionTypeDropDown" id="transactionTypeDropDown" style="width: auto;">
    <option value="1">Buy</option>
    <option value="2">Sell</option>
    <option value="3">Sell Short</option>
    <option value="4">Buy to Cover</option>
</select>

In the API's documentation, the examples are listed to operate as follows:

client.trade("GOOG", ita.Action.buy, 10)

However, AttributeError is reporting that 'int' object has no attribute 'value', in that given line, even when I've attempted to run the line as it is written in the documentation on its own.

File "/fakepath/MMIAS/ita/ita.py", line 233, in trade
trade_form.select("select#transactionTypeDropDown")[0].find("option", {"value": str(orderType.value)})["selected"] = True
AttributeError: 'int' object has no attribute 'value'

I am assuming that the 'int' object in question is the option value from the drop-down menu, and with that, I am assuming that that an int, perhaps 1 - 4, is occupying the orderType parameter. So, why would it be suggested that the parameter be ita.Action.[buy/sell/short/etc], rather than just an integer value? And why does it return the same error if an integer value fulfills that parameter? Help me understand.

kirkthaker commented 7 years ago

Thanks for bringing this to my attention - I'll spend this weekend combing through it

glotchimo commented 7 years ago

It has something to do with version because I ran it through 3.6 and it worked, but not in 2.7.

kirkthaker commented 7 years ago

The library was rewritten recently so that it would only be Python 3+ compatible, so that's probably what happened for you.

However, sometimes when I run the function on my own machine (w/ Python 3.6) it fails, but I think it might be a different problem. I might have to do with market opening times (i.e. there are no options to choose from when the market's closed), but I can only test that when the market's open. Leaving this issue open for now until I fix that.