So I have had some working code for awhile but when trying to get back and do some refactoring, I am running into a ‘list indices must be integers or slices, not Bar’ and cannot seem to figure out how to resolve it.
I am doing a simple iteration through tickers based on certain conditions to generate a list but now sure if its the dates I am passing through or have I am formulating the dataframe.
def get_tickers(api: tradeapi.REST):
assets = api.list_assets(status="active", asset_class="us_equity")
symbols = [asset.symbol for asset in assets if asset.tradable]
'''Getting the list of symbols that can be traded'''
start_time = (pd.to_datetime(datetime.now()) - pd.Timedelta("500000T"))
start_time = start_time.replace(second=0, microsecond=0).tz_localize('GMT').tz_convert('America/New_York')
end_time = (pd.to_datetime(datetime.now()))
end_time = end_time.replace(second=0, microsecond=0).tz_localize('GMT').tz_convert('America/New_York')
limit = 250
j = 0
symbol_list = []
price_list = []
for i in tqdm(range(200, len(symbols)+1, 200)):
get_symbols = symbols[j:i]
daily_bars = api.get_bars(symbol=get_symbols, timeframe=TimeFrame.Day, start=start_time.isoformat(), end=end_time.isoformat(), limit=limit, adjustment='raw')
for bar in daily_bars:
try:
if daily_bars[bar] != []:
df = daily_bars[bar].df
# macd_line = macd(df["close"], window_slow=26, window_fast=12, fillna=True)
# macd_signal_line = macd_signal(df["close"], window_slow=26, window_fast=12, window_sign=9, fillna=True)
sma200_line = sma_indicator(df["close"], window=200, fillna=True)
cond_1 = df.iloc[-1]["close"] >= signal_min_share_price
cond_2 = df.iloc[-1]["close"] <= signal_max_share_price
cond_3 = df.iloc[-1]["close"] < round(sma200_line[-1],2)
if cond_1 and cond_2 and cond_3:
symbol_list.append(bar)
price_list.append(df.iloc[-1]["close"])
except Exception as e:
logging.warning(e)
pass
j = i
symbols_to_trade_df = pd.DataFrame({
"Stocks": symbol_list,
"Price": price_list,
})
symbols_to_trade = symbols_to_trade_df.sort_values(['Price'], ascending=[False])
symbols_to_trade = symbols_to_trade.reset_index(drop=True)
print(symbols_to_trade[:20])
print(start_time)
print(end_time)
return symbols_to_trade.loc[:, "Stocks"].to_list()