This Python trading algorithm, named "Ratio-Return Credit Spread Exit-Early", aims to generate trading signals for credit spread strategies on NIFTY options. It leverages a combination of technical indicators derived from historical options data, including open interest (OI), implied volatility (IV), volume, and proprietary factors like Hamiltonian energy and entropy. The core methodology involves calculating a composite alpha signal based on three factors: resource depletion (entropy drop), niche specialization (volume/OI concentration), and directional bias (Hamiltonian energy/IV imbalance). The algorithm reads pre-calculated option data, computes a rolling time-series rank of the alpha signal and a secondary alpha based on IV kurtosis and eigenvalue, and then assesses these alpha values to identify potential credit spread opportunities. The algorithm specifically looks for entry signals for credit put spreads and credit call spreads based on the levels of 'alpha' and 'alpha2', with different thresholds for long and short positions.
The algorithm generates trading signals based on the calculated alpha values, initiating a credit spread strategy when specific conditions are met. It looks for "long" opportunities when 'alpha' exceeds 0.85 and 'alpha2' exceeds 0.8, triggering a credit put spread. Conversely, it looks for "short" opportunities when 'alpha' is below 0.15 and 'alpha2' is below 0.2, triggering a credit call spread. The risk management component includes setting a stop-loss based on a percentage of the margin required for the trade, as well as a target profit level. The algorithm determines the specific option contracts to trade based on the at-the-money (ATM) strike price and predefined offsets (e.g., 400 points). The trading logic then constructs the necessary buy and sell orders, factoring in the lot size of the NIFTY options. To encourage early profit taking, the algorithm sets a "premature_target_ttl," and will attempt to book the trade if the profit target is reached prior to expiry. Finally, the algorithm checks for active trades before executing a new trade, pushing a message to Slack, and using Stratzy's API to send the trades for execution, only after calculating the maximum potential profit for the credit spread.