Moving Average Trading Simulation and Optimization

Nan(Lainey) Liu
Posted on Feb 5, 2018

  • 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.

About Author

Nan(Lainey) Liu

Nan(Lainey) Liu

Nan(Lainey) is a master student at New York University studying Financial Engineering. She is passionate in the applications of machine learning technique in financial industry eg. High-Frequency Trading, Option Pricing. Nan developed a shiny app to research on...
View all posts by Nan(Lainey) Liu >

Related Articles

Leave a Comment

No comments found.

View Posts by Categories


Our Recent Popular Posts


View Posts by Tags

2019 airbnb alumni Alumni Interview Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Big Data Book Launch Book-Signing bootcamp Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Industry Experts Job Jon Krohn JP Morgan Chase Kaggle lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python python machine learning python scrapy python web scraping python webscraping Python Workshop R R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest recommendation recommendation system regression Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Tableau TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp