I've seen people ask for this a few times, so I'm sharing a simple implementation with comments that should help explain the logic.

The sample SPY strategy in the attached backtest will sell a 30 delta put, expiring in 10 days. After expiration, it will sell another, and so on. 

The two core methods (functions) of note are

  1. InitOptionsAndGreeks: This method initializes options securities settings, chain filters, pricing models, etc
  2. SelectContractByDelta: This is a convenience method to get an option contracts using delta & expiration

 

You can reuse SelectContractByDelta anywhere you need to retrieve a contract using delta --eg: you might want to trade SPY covered calls: buying a 60 Delta 180 DTE call contract and then selling 30 Delta 5 DTE calls against it. 

The speed / performance of this example can likely be improved, but this should be a useful start for many