# Moving Average Trading Simulation and Optimization

**Goal:**

To have an app that simulates moving average trading strategy and optimizes the moving average pair during a specific time frame.

**Background Knowledge:**

There are various types of moving average(MA) trading strategy. One of the most famous ones is the crossover of two MAs: two MAs are selected(eg, 60 days MA and 120 days MA), when 60 days MA is **over** 120 days MA, we **short** the stock; when 60 days MA is** under** 120 days MA, we **long** the stock. This strategy is based on the assumption that the stock price is mean reverting, meaning that prices and returns eventually move back toward the **mean**

**The Shiny App:**

First of all, I need to design the trading strategy. I provide the signal as the price of moving average and when 60 days MA > 120 days MA, signal = -1, when 60 days MA < 120 days MA, signal = 1. The parameters of MA trading are the time frame, upper bound MA, lower bound MA. So I designed an user input area that allows the user to specify those parameters. After we train the data, we want to eventually backtest it, thus the testing date input.

After choosing the parameters, we need to determine which stocks are we trading. I provide the user with 3 benchmark options: S&P500, Dow Jones Index and Nasdaq. For the stock ticker field, the user can choose one or multiple stocks to do the analysis.

Now we have all the inputs we need, it's time to see the output. The following is the output window of the app.

The app will generate a table and gave you the profit return, the Sharpe Ratio(return/standard deviation) and Maximum Drawdown of the portfolio at given time frame. Also, there is a time series plot to visually compare the return. The most computationally expensive part of the app is probably the optimization. The backend ran through all the possible MA pairs given the constraints, then output the best performing MA pairs for selected stocks, which will also give the user a table of top 3 pairs.