thejeffreystone/alpha_vantage_to_mqtt

UnboundLocalError: local variable 'data' referenced before assignment

Closed this issue · 6 comments

I was trying to follow your script but get errors in the log. Do you have any suggestion please?

Traceback (most recent call last):
File "/alpha_vantage_to_mqtt/alpha_vantage_to_mqtt.py", line 139, in
main(interval)
File "/alpha_vantage_to_mqtt/alpha_vantage_to_mqtt.py", line 113, in main
data = getCurrentStockPrice(api_key, symbols)
File "/alpha_vantage_to_mqtt/alpha_vantage_to_mqtt.py", line 56, in getCurrentStockPrice
return data
UnboundLocalError: local variable 'data' referenced before assignment

Yeah, I haven't figured this one out yet. Best I can tell is something in the api changed, but I haven't tracked it down.

The get_batch_stock_quotes method had been depreciated for a while was finally removed from Alpha Vantage's API and was removed from the wrapper in Feb.
RomelTorres/alpha_vantage#184
RomelTorres/alpha_vantage#189

Awesome!. Thanks man for the heads up.

Looks like I need to rework this one.

sketching up a workaround, this is working for me currently but I haven't tested it extensively

from concurrent.futures.thread import ThreadPoolExecutor
...
def getCurrentStockPrice(api_key, symbols):
    """Get Current Stock Price for a list of symbols."""
    if app_mode == 'debug':
        print("Starting Alpha Vantage Call...\n")
    timeseries = TimeSeries(key=api_key)

    try:
        with ThreadPoolExecutor(max_workers=10) as executer:
            data = executer.map(lambda stock:timeseries.get_daily(symbol=stock), symbols)
    except ValueError:
        if app_mode == 'debug':
            print("API Key is not valid or symbols not known")
    if app_mode == 'debug':
        print("Alpha Vantage Call Complete...\n")
    return data

Ha nice. I had started playing around with exactly that solution this morning then all hell broke loose at work. I will continue with that and see what I can get going!

Ok, I think I may have it "fixed" Although, to be honest I think we are going to still hit the 5 calls a minute. More testing to do. My use case is current 5 stocks so it may not be an issue.

But the script is back to publishing to MQTT!

Oh, I think this is going to require 2.2.0 so I think you have to build alpha_vantage from source.

Thanks everyone!