This Python algorithm implements a credit spread strategy on NIFTY options, specifically designed for overnight positions. It leverages intraday option chain data to identify opportunities where selling a credit spread (either call or put) can generate a profit. The core idea is to capitalize on potential range-bound movement or directional bias in the NIFTY index. It involves calculating a proprietary "alpha" signal derived from option chain data, incorporating factors like implied volatility (IV), open interest (OI), volume ratios, and Hamiltonian energy from option prices, which suggests the direction of price movement, and also incorporates kurtosis of IV and other factors for better signal generation. This alpha is designed to capture market sentiment and identify situations where option premiums are likely to decline overnight, leading to profitable credit spread positions.
The algorithm generates trading signals based on the calculated "alpha" values. High alpha values (> 0.85 and > 0.8) suggest a potential credit put spread, where a put option is sold and a further out-of-the-money put option is bought for hedge. Conversely, low alpha values (< 0.15 and < 0.2) indicate a potential credit call spread. The algorithm retrieves option chain data, calculates the alpha, and if conditions are met, it places orders to sell the higher strike option and buy the lower strike option for the respective spread. Risk management is implemented through a stop-loss mechanism, calculated as a percentage of the required margin, and a target profit is set with multiple options including full target, and premature target and premature target expiry. The system also incorporates checks for market open status, trading hours (10:15 - 14:15), expiry date, and active trade status to prevent trading under unfavorable conditions, thus prioritizing capital preservation and consistent strategy execution.