I am trying to iterate over a month of Alpaca data at “1Min” intervals to build a parameter search tool and backtest. However, I cannot figure out why I am not getting the right data. I am putting in start and end dates to get get a bracket of time. However, it spits out the completely wrong dates.
Any ideas on what I may be doing wrong?
Hey @Cloblak!
If you modify the date parameters to check only for 6-22 to 6-23, just as an example, do you get additional data or no?
Yea! I had to build a loop that changed the end date to the last date on the current data pull, going back until it matched the data I need.
def days_of_data_single_stock(ticker, interval, days_to_collect):
ticker_data_dict = None
ticker_data_dict = {}
monthly_data_dict = None
monthly_data_dict = {}
interval_loop_data = None
interval_loop_data = pd.DataFrame()
stock_data = None
TZ = 'America/New_York'
start_str = (datetime.now() - timedelta(days=days_to_collect)).strftime("%m-%d-%Y %H:%M")
start=pd.Timestamp(start_str, tz=TZ).isoformat()
end_str = datetime.now().strftime("%m-%d-%Y %H:%M")
end = pd.Timestamp(end_str, tz=TZ).isoformat()
stock_data = api_paper.get_barset(ticker, interval, end = end, limit=1000).df
stock_data = stock_data.reset_index()
interval_loop_data = interval_loop_data.append(stock_data)
df_start_ref = interval_loop_data["time"][0]
start_str_ref = pd.Timestamp(start)
#print(start_str_ref.value < df_start_ref.value)
while start_str_ref.value < (df_start_ref - pd.Timedelta(days=1)).value:
end_new = pd.Timestamp(interval_loop_data["time"].iloc[0].strftime("%Y-%m-%d"), tz=TZ).isoformat()
stock_data_new = None
stock_data_new = api_paper.get_barset(ticker, interval, start = start, end = end_new, limit=1000).df
stock_data_new = stock_data_new.reset_index()
interval_loop_data = interval_loop_data.append(stock_data_new).sort_values(by=['time'], ascending=True)
#print(interval_loop_data)
df_start_ref = interval_loop_data["time"].iloc[0]
#print(start_str_ref.value < df_start_ref.value, start_str_ref, df_start_ref, (df_start_ref - pd.Timedelta(days=1)))
return interval_loop_data.reset_index()