Should we keep the detrended time series when predicting? I am learning about exploring traditional TS. According to this notebook, detrending is necessary for time series predicting models

When a time series is stationary, it can be easier to model. Statistical modeling methods assume or require the time series to be stationary.

Yet when I want to predict the next move with SARIMAX, do I need to keep the detrended time series or the original time series?
Here is the tail of the original time series:
>>> df_settle.tail()

Date
2020-10-01    211.905228
2020-11-01    201.941315
2020-12-01    216.210007
2021-01-01    222.419998
2021-02-01    239.649994
Freq: MS, Name: Close, dtype: float64

If I keep the detrended the prediction doesn't make much sense:
df_log = np.log(df_settle)
df_log_ma = df_log.rolling(2).mean()
df_detrend = df_log - df_log_ma
df_detrend.dropna(inplace = True)

import itertools
import warnings
from statsmodels.tsa.statespace.sarimax import SARIMAX

warnings.filterwarnings("ignore")

def arima_grid_search(dataframe, s):
  p = d = q = range(2)
  param_combinations = list(itertools.product(p, d, q))

  lowest_aic, pdq, pdqs = None, None, None

  total_iterations = 0
  for order in param_combinations:
    for (p, d, q) in param_combinations:
      seasonal_order = (p, d, q, s)
      total_iterations +=1
      try:
        model = SARIMAX(df_settle, order=order,
                        seasonal_order = seasonal_order,
                        enforce_stationarity=False,
                        enforce_invertibility=False,
                        disp=False
                      )
        model_result = model.fit(maxiter=200, disp=False)

        if not lowest_aic or model_result.aic < lowest_aic:
          lowest_aic = model_result.aic
          pdq, pdqs = order, seasonal_order

      except Exception as ex:
        continue

  return lowest_aic, pdq, pdqs

lowest_aic, order, seasonal_order = arima_grid_search(df_detrend, 12)

model = SARIMAX(
    df_detrend, 
    order=order,
    seasonal_order = seasonal_order,
    enforce_stationarity=False,
    enforce_invertibility=False,
    disp=False
)

model_results = model.fit(maxiter=200, disp=False)

n = len(df_detrend.index)
prediction = model_results.get_prediction(
    start=n-14*5, #changed from 12
    end=n+5
)

prediction_ci = prediction.conf_int()

Indeed, I get:
>>> prediction_ci.head(3)

    lower Close upper Close
2020-04-01  -0.090269   0.051201
2020-05-01  -0.071606   0.069863
2020-06-01  -0.042065   0.099405

Which doesn't make much sense compared to the last value of the original dataframe: 239.649994 for 2021-02-01
 A: 'd' in SARIMAX is coming from integrated. For example if a process is integrated of order 1 it means it becomes stationary if you take the first difference. I believe when you define a SARIMAX model in R and if you take 'd' bigger than 0 it will take the difference 'd' times before fitting the model. If you wanna make the data stationary by yourself, such as using log difference over a regular difference operation, you need to set 'd' to 0, so that R does not take the difference for you. Setting 'd' to 0 is a way to tell your model: my data is already stationary. Seasonal order refers to seasonal difference. For instance if your data is both trending and have seasonality you need to remove both before fitting the model, and this corresponds to taking difference 2 times with lags determined by the seasonal and trend order. Looking your code, I see you are grid searching (p, d, q, s), then you should feed the data as it is without any transformation since SARIMAX will do the transformation for you under the hood. Or you can take log-difference set the d to zero and grid-search for (p, q, s) only.
