For Calendar spread, there is arbitrage opportunity because the decay of time value of nearby month contract is different from that of deferred month contract.
We can calculate the implied volatility of near month average options and far month average options in the last 20 trading days, and then calculate the mean and std according to the difference.We need to calculate the difference between the implied volatility of the far month average option and that of the near month average option on the trading day, and then compare it with mean+2std and mean-2std.
If iv_diff > mean+2std, which means that the implied volatility of far month contract is at a high level. According to the knowledge we have learned, the higher implied volatility means the higher potential value, so we should short the deferred month option and long the nearby month option. if iv_diff < mean-2std, do the opposite. Above mentioned is the opening signal, below is the closing signal. If we open a position with iv_diff > mean+2std, then the signal of closing a pisition is iv_diff < mean. If we open a position with iv_diff < mean+2std, then the signal of closing a pisition is iv_diff > mean.That is to say, the requirements of opening the position will be more strict than closing the position.
We take July 1, 2016 to May 1, 2018 as the back test interval, and calculate all iv_diff. Then for each trading day, we use the IV of the 20 trading days before the trading day to calculate the mean and variance as the basis for the judgment of opening a position or closing a positions.The results are shown in the following figure.
According to the data obtained in the step 1, we can get all the opening and closing signals. If you sell the deferred month contract and buy the nearby month contract, it is marked as state=1. If you sell the nearby month contract and buy the deferred month contract, it is marked as state=-1. The state = 0 means no transcation. The states is shown in the figure below.
We regard one opening a position and one closing a position as a complete transaction, and calculate the return of each transaction. The return is shown in the figure below.
By summing up the returns each time, we can get the change of cumulative returns over time. The cumulative returns is shown in the figure below.
We can store the information of each transaction in the following CSV document.