This algorithm implements a credit spread strategy on Nifty options, aiming to profit from the decay of option premiums. It analyzes historical and real-time data to identify potential trading opportunities based on calculated alpha values. The core idea is to sell a higher premium option (either call or put, depending on the signal) and simultaneously buy a lower premium option of the same type but further out-of-the-money, thus creating a credit spread. The algorithm caches data to improve efficiency and speed of execution. Technical indicators, volatility analysis, and option chain data are used to formulate trading decisions. The algorithm is scheduled to run between 10:15 AM and 2:15 PM, avoiding the initial market opening and final closing hours.
The algorithm generates buy/sell signals based on two alpha values, `alpha` and `alpha2`, which are derived from a combination of volatility metrics, option chain analysis, and spot price movements. A credit put spread is initiated if both alpha values exceed predefined thresholds (0.75 and 0.70 respectively), and a credit call spread is initiated if both alpha values fall below predefined thresholds (0.25 and 0.30 respectively). The algorithm defines risk management parameters, incorporating a stop-loss percentage of 5% and a target profit percentage of 10%. Additionally, it attempts to set a target exit time, potentially exiting trades early if the profit target is reached before expiry. The algorithm calculates and considers margin requirements before executing trades, ensuring sufficient capital is available. It also includes checks to avoid trading on expiry days and when the market is closed. If all conditions are met and a trade is generated, the algorithm sends the trade signals to an execution system and pushes slack messages.