Idiopathic pulmonary fibrosis (IPF) is an age-related interstitial lung disease of unknown cause. Oxidative stress, an imbalance between oxidants and antioxidants, is implicated in IPF pathogenesis and prognosis but needs further study. We used transcriptome sequencing data (GSE70866) and oxidative stress-related genes from GeneCards. A prognostic risk model for IPF patients was constructed using LASSO. Functional and pathway differences were analyzed between risk score groups, along with comparisons of immune cells and functions. An IPF rat model with vitamin D3 (VD3) intervention was also established. Finally, we used IL-4 to induce M2 macrophages to explore the mechanism of action of CCL2. We identified 483 DEGs and 50 oxidative stress-related DEGs (OSDEGs). Single-factor COX regression identified 34 prognostic OSDEGs, and LASSO identified an 8-gene signature for the risk model. The high-risk group had more CD8 + T cells, macrophages, APC costimulation, and cytokine-cytokine receptor activity. CCL2 was significantly correlated with macrophages in IPF. VD3 inhibited the TGF-β signaling pathway and reduced macrophage M2 infiltration in the rat model. In the IL-4 induced M2 macrophage model, we found that M2 macrophages produced more CCL2, and the production of CCL2 was significantly reduced after VD3 intervention. We established prognostic markers of eight oxidative stress-related genes. The risk score effectively predicts adverse outcomes in IPF. VD3 may alleviate IPF by reducing macrophage infiltration and inhibiting the TGF-β signaling pathway.