How to down historical stock options data dynamically . and also and strike wise not just untill ATM +-3 more

How to down historical stock options data dynamically . and also and strike wise not just untill ATM ±3 more
please try to provise the code .

i want to back test my strategy. not able to figure out how to select the contract.
@Rahul