IntroductionTrading strategies can be broadly categorized into momentum and mean reverting strategies. In momentum strategies the principle is “trend is a friend” and the gist involves buying high and selling higher whereas in mean reverting strategies the principle is “whatever goes up has to come down” and this implies buying when the asset is oversold and selling when it is overbought. Moving average crossover belongs to the former category.
There is a plethora of information on moving average crossover strategy with different names such as “Golden Cross” and “Death Cross”. The strategy involves moving average indicator of different durations. An average of the shorter look-back window is called SMA and the one with the longer look-back window is called LMA. Popularly used SMA-LMA pairs include 20-40, 20-60 and 50-200. An SMA-LMA plot along with the adjusted close price for Nifty looks as follows:-
Trading rules are simple. Buy the asset when the SMA crosses above LMA and sell the asset when LMA crosses below SMA.
How does it work?Crossover strategy is widely used to trade in the equity segment. In this post, we will explore back-testing of crossover signals to trade Nifty options using Python. Instead of buying the asset, we shall buy a call option when SMA(today) > LMA(today) and SMA(yesterday) < LMA(yesterday). Similarly sell the call option when SMA(today) < LMA(today) and SMA(yesterday) > LMA(yesterday).
Step - 1Import necessary packages.
Step - 2Download the Nifty OHLC data using Pandas-Datareader and the corresponding option data using NSEpy.
Step - 3Create a new dataframe that contains relevant information from the previous two dataframes.
Step - 4Define two moving averages, m, and n. The dataframe, df, contains new columns namely “SMA” and “LMA” along with the lagged version of these values. The first nan values are removed in this step.
Step - 5Define the buying and selling logic as explained above. We shall use “Last” as our trading price. We shall select only those values that have finite “Last” and nonzero “Number of Contracts”. One lot of nifty option is 75, thus we will multiply the “Trade Price” column by 75. To reiterate, we apply moving average indicators on the underlying index value and trade on “last” price of the option.
Step - 6Involves computing cumulative returns of the strategy
Step - 7Show the graph of cumulative PnL.
Parting thoughtsThe strategy might be tweaked for different look-back periods of SMA-LMA. Instead of selling the call, a long position input might be considered when a sell signal is generated. Also note that though we are attempting to capture delta of the option, the strategy is solely based on average value of prices.
Let us know if you come up with interesting returns on the back-test.
Next StepIf you want to learn various aspects of Algorithmic trading then check out the Executive Programme in Algorithmic Trading (EPAT™). The course covers training modules like Statistics & Econometrics, Financial Computing & Technology, and Algorithmic & Quantitative Trading. EPAT™ equips you with the required skill sets to be a successful trader. Enroll now!
Disclaimer: All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.
Download Data File