Ошибка KeyError: 'signal'

import investpy
import numpy as np
import telebot
from datetime import date
import time

stocks = investpy.stocks.get_stocks(country='United States')['symbol']


def array_to_string(a):
    stroka = '['
    for i in range(len(a)):
        stroka += a[i]
        if i != len(a)-1:
            stroka+=' , '
    stroka+=']'
    return stroka


counter = 0
index = 1
good_stocks = []
current_date = str(date.today().day) + '/' + str(date.today().month) + '/' + str(date.today().year)
a = date.today().month
if a == 1:
    from_date = str(date.today().day) + '/' + str(12) + '/' + str(date.today().year - 1)
else:
    from_date = str(date.today().day) + '/' + str(date.today().month - 1) + '/' + str(date.today().year)
for stock in stocks:
    if counter == 10:
        time.sleep(10)
        counter = 0
    try:
        df = investpy.get_stock_historical_data(stock=stock, country='United States', from_date=from_date,
                                                to_date=current_date)
        time.sleep(5)
        technical_indicators = investpy.technical.technical_indicators(stock, 'United States', 'stock', interval='daily')
        print(technical_indicators)
        country = 'United States'
    except:
        continue
    tech_sell = len(technical_indicators[technical_indicators['signal'] == 'sell'])
    tech_buy = len(technical_indicators[technical_indicators['signal'] == 'buy'])


    time.sleep(2)
    moving_averages = investpy.technical.moving_averages(stock, country, 'stock', interval='daily')
    moving_sma_sell = len(moving_averages[moving_averages['sma_signal'] == 'sell'])
    moving_sma_buy = len(moving_averages[moving_averages['sma_signal'] == 'buy'])

    moving_ema_sell = len(moving_averages[moving_averages['ema_signal'] == 'sell'])
    moving_ema_buy = len(moving_averages[moving_averages['ema_signal'] == 'buy'])
    if tech_buy < 9 or tech_sell > 2 or moving_sma_buy < 5 or moving_ema_buy < 5:
        continue
    sma_20 = moving_averages['sma_signal'][2]
    sma_100 = moving_averages['sma_signal'][4]
    ema_20 = moving_averages['ema_signal'][2]
    ema_100 = moving_averages['ema_signal'][4]
    print(str(index) + ') ' + 'STOCK =', stock)
    print('Tech sell indicators: to buy =', tech_buy, 'of 12; ', 'to sell =', tech_sell, 'of 12')
    print('SMA moving averages: to buy =', moving_sma_buy, 'of 6; ', 'to sell =', moving_sma_sell, 'of 6')
    print('EMA moving averages: to buy =', moving_ema_buy, 'of 6; ', 'to sell =', moving_ema_sell, 'of 6')
    print('SMA_20 =', sma_20, ';', 'SMA_100 =', sma_100, ';', 'EMA_20 =', ema_20, ';', 'EMA_100 =', ema_100)
    print('Prices Last Five days of ' + stock + ' =', np.array(df['Close'][-5:][0]), ';', np.array(df['Close'][-5:][1]),
          ';', np.array(df['Close'][-5:][2]), ';', np.array(df['Close'][-5:][3]), ';', np.array(df['Close'][-5:][4]))
    p_1 = abs(1 - df['Close'][-5:][1] / df['Close'][-5:][0])
    if df['Close'][-5:][1] >= df['Close'][-5:][0]:
        pp_1 = '+' + str(round(p_1 * 100, 2)) + '%'
    else:
        pp_1 = '-' + str(round(p_1 * 100, 2)) + '%'
    p_2 = abs(1 - df['Close'][-5:][2] / df['Close'][-5:][1])
    if df['Close'][-5:][2] >= df['Close'][-5:][1]:
        pp_2 = '+' + str(round(p_2 * 100, 2)) + '%'
    else:
        pp_2 = '-' + str(round(p_2 * 100, 2)) + '%'
    p_3 = abs(1 - df['Close'][-5:][3] / df['Close'][-5:][2])
    if df['Close'][-5:][3] >= df['Close'][-5:][2]:
        pp_3 = '+' + str(round(p_3 * 100, 2)) + '%'
    else:
        pp_3 = '-' + str(round(p_3 * 100, 2)) + '%'
    p_4 = abs(1 - df['Close'][-5:][4] / df['Close'][-5:][3])
    if df['Close'][-5:][4] >= df['Close'][-5:][3]:
        pp_4 = '+' + str(round(p_4 * 100, 2)) + '%'
    else:
        pp_4 = '-' + str(round(p_4 * 100, 2)) + '%'
    print('Percentage +/- of ' + stock + ' =', pp_1, ';', pp_2, ';', pp_3, ';', pp_4, )
    print()
    index += 1
    counter += 1
    good_stocks.append(stock)
    time.sleep(2)

Здравствуйте! Не работает код. После того как заканчиваются данные в массиве, компьютер не завершает обработку, а пишет об отсутствии колонки signal. Подскажите, пожалуйста, как можно это исправить? Заранее спасибо!

Из этого кода сложно понять что происходит и как должно работать.

Видимо надо как-то проверять, что technical_indicators пуст и выходить из цикла :man_shrugging: