Moving Average Trading Simulation and Optimization

Posted on Feb 5, 2018

The skills the authors demonstrated here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.

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

Trading Blogs - Karsli January 23, 2020
[…] Download ImageMore @ nycdatascience.edu […]

View Posts by Categories


Our Recent Popular Posts


View Posts by Tags

#python #trainwithnycdsa 2019 2020 Revenue 3-points agriculture air quality airbnb airline alcohol Alex Baransky algorithm alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus ames dataset ames housing dataset apartment rent API Application artist aws bank loans beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep boston safety Bundles cake recipe California Cancer Research capstone car price Career Career Day citibike classic cars classpass clustering Coding Course Demo Course Report covid 19 credit credit card crime frequency crops D3.js data data analysis Data Analyst data analytics data for tripadvisor reviews data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization database Deep Learning Demo Day Discount disney dplyr drug data e-commerce economy employee employee burnout employer networking environment feature engineering Finance Financial Data Science fitness studio Flask flight delay gbm Get Hired ggplot2 googleVis H20 Hadoop hallmark holiday movie happiness healthcare frauds higgs boson Hiring hiring partner events Hiring Partners hotels housing housing data housing predictions housing price hy-vee Income Industry Experts Injuries Instructor Blog Instructor Interview insurance italki Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter las vegas airport lasso regression Lead Data Scienctist Lead Data Scientist leaflet league linear regression Logistic Regression machine learning Maps market matplotlib Medical Research Meet the team meetup methal health miami beach movie music Napoli NBA netflix Networking neural network Neural networks New Courses NHL nlp NYC NYC Data Science nyc data science academy NYC Open Data nyc property NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time performance phoenix pollutants Portfolio Development precision measurement prediction Prework Programming public safety PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn seafood type Selenium sentiment analysis sentiment classification Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau teachers team team performance TensorFlow Testimonial tf-idf Top Data Science Bootcamp Top manufacturing companies Transfers tweets twitter videos visualization wallstreet wallstreetbets web scraping Weekend Course What to expect whiskey whiskeyadvocate wildfire word cloud word2vec XGBoost yelp youtube trending ZORI