Background: Currently, the commercial treatment planning systems for magnetic-resonance guided linear accelerators (MR-linacs) only support step-and-shoot intensity-modulated radiation therapy (IMRT). However, recent studies have shown the feasibility of delivering arc therapy on MR-linacs, which is expected to improve dose distributions and delivery speed. By accurately accounting for the electron return effect in the presence of a magnetic field, a Monte Carlo (MC) algorithm is ideally suited for the inverse treatment planning of this technique. Purpose: We propose a novel MC-based continuous aperture optimization (MCCAO) algorithm for volumetric modulated arc therapy (VMAT), including applications to VMAT on MR-linacs and trajectory-based VMAT. A unique feature of MCCAO is that the continuous character of gantry rotation and multileaf collimator (MLC) motion is accounted for at every stage of the optimization. Methods: The optimization process uses a multistage simulation of 4D dose distribution. A phase space is scored at the top surface of the MLC and the energy deposition of each particle history is mapped to its position in this phase space. A progressive sampling method is used, where both MLC leaf positions and monitor unit (MU) weights are randomly changed, while respecting the linac mechanical limits. Due to the continuous nature of the leaf motion, such changes affect not only a single control point,but propagate to the adjacent ones as well, and the corresponding dose distribution changes are accounted for. A dose-volume cost function is used,which includes the MC statistical uncertainty. Results: We applied our optimization technique to various treatment sites,using standard and flattening-filter-free (FFF) 6 MV beam models, with and without a 1.5 T magnetic field. MCCAO generates deliverable plans, whose dose distributions are in good agreement with measurements on ArcCHECK and stereotactic radiosurgery End-To-End Phantom. Conclusions: We show that the novel MCCAO method generates VMAT plans that meet clinical objectives for both conventional and MR-linacs.