Get_bars return null[]

hey Guys!

import alpaca_trade_api as alpaca
import pandas as pd
import config

# Replace YOUR_API_KEY and YOUR_SECRET_KEY with your Alpaca API keys
api = alpaca.REST(config.ALPACA_KEY, config.ALPACA_SECRET_KEY, config.ALPACA_URL, "v2")

# Set the length and factor for the Super Trend indicator
length = 9
factor = 7

# Retrieve the latest market data for the S&P 500
bars = api.get_bars('SPY','1Day', limit=length)
sp500_data = pd.DataFrame(bars, columns=['t', 'o', 'h', 'l', 'c', 'v'])
sp500_data.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume']

# Calculate the average price over the specified length
average_price = sp500_data['close'].mean()

# Calculate the Super Trend value
sp500_data['super_trend'] = (sp500_data['high'] - sp500_data['low']) / factor + average_price

# Generate buy, sell, or hold signals based on the Super Trend value
    if sp500_data['super_trend'][0] > sp500_data['close'][0]:
        super_trend_signal = "sell"
    elif sp500_data['super_trend'][0] < sp500_data['close'][0]:
        super_trend_signal = "buy"
        super_trend_signal = "hold"   
    print(f'The current Super_trend signal for the S&P 500 index is {super_trend_signal}')
except Exception as e:
    print(f'Super_trend signal error: {e}')

Could someone explain to me why he doesn’t return anything?

> []
> Super_trend signal error: index 0 is out of bounds for axis 0 with size 0

The limit parameter limits the number of returned bars but this filter is applied only after the time range filter. If you don’t specify the start and end parameters it will mean “all bars for the current day” and in the case of a non-trading day you will not have any bars and REST API will return a null or empty array.

1 Like