SummaryObstructive sleep apnea (OSA) has a heavy health‐related burden on patients and the healthcare system. Continuous positive airway pressure (CPAP) is effective in treating OSA, but adherence to it is often inadequate. A promising solution is to detect sleep apnea events in advance, and to adjust the pressure accordingly, which could improve the long‐term use of CPAP treatment. The use of CPAP titration data may reflect a similar response of patients to therapy at home. Our study aimed to develop a machine‐learning algorithm using retrospective electrocardiogram (ECG) data and CPAP titration to forecast sleep apnea events before they happen. We employed a support vector machine (SVM), k‐nearest neighbour (KNN), decision tree (DT), and linear discriminative analysis (LDA) to detect sleep apnea events 30–90 s in advance. Preprocessed 30 s segments were time–frequency transformed to spectrograms using continuous wavelet transform, followed by feature generation using the bag‐of‐features technique. Specific frequency bands of 0.5–50 Hz, 0.8–10 Hz, and 8–50 Hz were also extracted to detect the most detected band. Our results indicated that SVM outperformed KNN, LDA, and DT across frequency bands and leading time segments. The 8–50 Hz frequency band gave the best accuracy of 98.2%, and a F1‐score of 0.93. Segments 60 s before sleep events seemed to exhibit better performance than other pre‐OSA segments. Our findings demonstrate the feasibility of detecting sleep apnea events in advance using only a single‐lead ECG signal at CPAP titration, making our proposed framework a novel and promising approach to managing obstructive sleep apnea at home.