AR Forecasting one day ahead I am doing a project where I want to forecast prices for the next day based on some lagged values of the price. When I read about how to implement this (in Python) what I find it that they use a training data set, fit the model, and then use these parameters to forecast a test data set.
For example:
#first until n-5 observation
df_train = df['Temp'].iloc[:-5]
#n-5 until n observation
df_test = df['Temp'].iloc[-5:]

from statsmodels.tsa.ar_model import AutoReg
model = AutoReg(df_train, lags=22).fit()

forecasts = model.forecast(5).tolist()
test_values = df_test.tolist()
for index in range(len(forecasts)):
  difference = forecasts[index] - test_values[index]
  print(forecasts[index], test_values[index], difference)

Source
First, I see that long training sets are generally used, but then the model uses for example 22 lags to predict the test set, like here. In this case, wouldn't it be sufficient to use this training set:
df_train = df['Temp'].iloc[-27:-5]

Which would give 22 observations?
Secondly, in my case, I don't want to use a fixed training set to forecast a set number of days in the future, but I want to use my lagged data to predict the next 24 hours.
Say if I use a 7 days lag and the first forecasting day is n. Then, to predict n, I want to use observations n-8 until n-1. For the next day, n+1, I want to use n-7 until n to predict.
So:

*

*Do I even need a training and test split for this forecast?

*Are you aware of some sources where I can read about this procedure?

All the best,
Simon

+1 to Richard's answer. A few additions:
First off, as Richard notes, you are using the full training set to estimate the autoregression coefficients, which is what AutoReg does. In forecasting, you would only use the last 22 (or whatever your order is) observations together with the estimated coefficients.
This is completely standard autoregressive modeling, which is a subset of the more general autoregressive integrated moving average (ARIMA) class of models. You can read all about this in this excellent online textbook.
Importantly, if your goal is actual forecasting, it makes a lot more sense to use an automated ARIMA modeler. This will decide on the optimal AR order, but also on the number of differences and the MA order, and estimate the coefficients.
Any decent auto-ARIMA modeler will likely end up with far fewer than 22 lags. (Seasonality may come in, depending on what prices you are modeling. If this makes sense to you, be sure to allow seasonality.) See this thread and this one.
