Respiratory motion during radiotherapy can distort the highly conformal delivery of dose to lung tumours, compromising treatment outcomes and elevating dose to healthy tissues. A new optimization and delivery framework is proposed to track and adapt for the tumour motion during arc delivery in real time. An aperture library, consisting of a radiation field aperture for each combination of beam angle and tumour position, is optimized before delivery. The tracked tumour position would then be used to select the appropriate aperture from the library, ensuring that the tumour is under continuous irradiation throughout delivery. The instantaneous delivered dose therefore depends on the respiratory motion trajectory realized by the patient. Robust optimization is applied to optimize the aperture library, such that the expectation value of the dose, which considers all possible respiratory trajectories, gives the desired dose to the tumour with minimal dose to healthy tissues. A Markov chain is used to model the respiratory motion, assigning each trajectory a probability. This model is trained using 5 min of patient motion data acquired in another study, and tested on the subsequent 5 min of data. Statistical tests confirm that the model is capable of adequately estimating the distribution of the patient's respiratory motion from fractions of a second to the order of a minute into delivery. The optimization framework is developed in matRad, an open-source treatment planning software. A novel dose calculation and optimization method is implemented, which accurately accounts for the continuous aperture motion undergone during arc delivery and tumour motion adaptation. Plans optimized under this framework are compared to treatments planned with other respiratory motion compensation methods, with focus on the quality of plans and their robustness with respect to variations in the motion trajectory. Robustness is evaluated by simulating trajectories and the corresponding dose from the probabilistic model. The proposed framework is found to generate plans which have more or slightly less robustness to variations depending on the method being compared, with an increase in robustness resulting in more consistent treatment outcomes. Compared to those methods which are more robust, the proposed framework delivers a lower dose to healthy organs.