This code simulates an exchange trading strategy by crossing two exponential moving averages (EMA 20 and EMA 100). The strategy is based on the assumption that the crossing of the long and the short moving averages is a signal for a change of trend.
To test the performance of the strategy, the stock of the pharmaceutical company Regeneron Pharmaceuticals was selected in periods:
• From 01.01.2010 to 31.12.2019
• From 01.01.2020 to 31.12.2020
• From 01.01.2021 to 13.08.2021
A long position is opened when the short moving average crosses the long moving average (EMA 20 > EMA 100) from bottom to top. A short position is opened when the long moving average crosses above the short moving average (EMA 20 < EMA 100). When trading short, the long position is automatically closed and vice versa.
In the beginning, we upload data about the closing price of a stock using the yfinance library. Data for different time periods are assigned to the variables train, test, final. Immediately thereafter, a graph of the closing price of the stock for the specified time periods is plotted.
Then a function is created that adds a new column with the calculated values of exponential moving averages to the datasets, as well as adding moving average lines to the graph. The values of exponential moving averages were calculated using the technical analysis library 'ta'.
Then we create a function that determines the current position (1 - long position, -1 - short position). Columns with position values are added to the dataset, and a graph with the moments of buying and selling the stock is plotted.
After that we create a function that calculates the profitability of the strategy (value of the position multiplied by the change in price).
Next, we create a function that calculates the indicators for backtesting the strategy: Sharpe ratio, Drawdown ratio and Maximum drawdown. The yield of 10-year US Treasuries was taken as the risk-free rate.
Then for comparison, data on the S&P-500 stock index was uploaded, and its returns were calculated.
As you can see, the exponential moving average crossover trading strategy has outperformed the S&P-500 index only once (in 2021). And that happened only due to the fact that there was only one transaction of buying Regeneron shares during that year. Based on this, we can conclude that this trading strategy is not successful for this stock.