Addressing Lag in 200-Day SMA Calculation

66 views Asked by At
start_date_3 = "2022-09-24"
end_date_3 = "2023-09-24"
inter_3 = "1d"

ohlc_3 = yf.download('AAPL', start=start_date_3, end=end_date_3, interval=inter_3)
ohlc_3["200-Day SMA"] = ohlc_3["Close"].rolling(200).mean()
na_count_200 = ohlc_3["200-Day SMA"].isna().sum()

if na_count_200 > 0 and 'd' in inter_3:
    start_date_3_1 = pd.to_datetime(start_date_3) - pd.offsets.BDay(na_count_200 + 1)

ohlc_3 = yf.download('AAPL', start="2021-12-20", end=end_date_3, interval=inter_3)
ohlc_3["200-Day SMA"] = ohlc_3["Close"].rolling(200).mean()
ohlc_3 = ohlc_3.dropna()

I've defined a start date (start_date_3), end date (end_date_3), and interval (inter_3) for retrieving stock data using the yfinance library. After calculating the 200-Day SMA, I noticed there might be NaN values, indicating a lag in the calculation which is correct.

To mitigate this lag, my approach involves retracing the last 200 business days to recalculate the SMA and using dropna to eliminate the lagged data. However, post this operation, the start date no longer aligns with the original start_date_3.

I'm reaching out for guidance on the efficiency and standard practices for addressing this lag in SMA calculation. Is there a more optimal method to achieve this goal while ensuring the consistency of start dates?

0

There are 0 answers