The dynamics of the coupled electron-nuclear spin system is studied in an ensemble of singlycharged (In,Ga)As/GaAs quantum dots (QDs) using periodic optical excitation at 1 GHz repetition rate. In combination with the electron-nuclei interaction, this allows us to lock the electron spins into magnetic resonance in a transverse external magnetic field. Sweeping the field to higher values, the locking leads to an effective "diamagnetic" response of significant strength due to dynamic nuclear polarization, which shields the QD electrons at least partly from the external field and can even keep the internal magnetic field constant up to 1.3 T field variation. We model the effect through a magnetic field-dependent polarization rate of the nuclei, from which we suggest a strategy for adjusting the nuclear polarization through the detuning between optical excitation and electronic transition, in addition to the magnetic field.