Certification of quantum nonlocality plays a central role in practical applications like device-independent quantum cryptography and random number generation protocols. These applications entail the challenging problem of certifying quantum nonlocality, something that is hard to achieve when the target quantum state is only weakly entangled, or when the source of errors is high, e.g. when photons propagate through the atmosphere or a long optical fiber. Here we introduce a technique to find a Bell inequality with the largest possible gap between the quantum prediction and the classical local hidden variable limit for a given set of measurement frequencies. Our method represent an efficient strategy to certify quantum nonlocal correlations from experimental data without requiring extra measurements, in the sense that there is no Bell inequality with a larger gap than the one provided. Furthermore, we also reduce the photodetector efficiency required to close the detection loophole. We illustrate our technique by improving the detection of quantum nonlocality from experimental data obtained with weakly entangled photons.