Moving average trading strategy
Suppose you're given the following table showing open, high, low, and close prices for a stock on a given date (preview of the schema, full schema/table in SQL fiddle link below):
Given this data, you're asked to apply the following trading strategy:
- Calculate the 10-day moving average of the closing price
- Then, if the price of the day's open is greater than the 10-day moving average, you will "buy" the stock on open and sell on close for that day.
Write a SQL query to apply this trading strategy and return the total earnings/loss over the period of data provided.
You can view/query the table in an interactive SQL fiddle here.
Subscribe to premium account to see the solution.Get premium now