MC2-Project-2
Contents
- 1 Updates
- 2 Overview
- 3 Template
- 4 Part 1A: Bollinger Band Strategy Chart (20%)
- 5 Part 1B: Bollinger Band Strategy Backtest (25%)
- 6 Part 2: Leverage (5%)
- 7 Orders files to run your code on
- 8 Short example to check your code
- 9 More comprehensive examples
- 10 Implementation suggestions & assumptions
- 11 What to turn in
- 12 Rubric
Updates
Overview
In this project you will develop trading strategies using Technical Analysis, then test them using your market simulator.
In this project we shift from an auto graded code format to a report format. For this project your grade will be based on the PDF report you submit. You will also submit your code that will be checked visually to ensure it appropriately matches the report you submit.
Template
Create your code within a directory named ml4t/mc2_p2 . Within that directory you should create/write four python files as follows:
- bollinger-strategy.py: This code should generate a .png chart that illustrates the bollinger bands, and entry and exit points for a bollinger-based strategy.
It should also generate an orders.txt file that you feed into your market simulator to backtest the strategy.
- my-strategy.py: This code should generate a .pnt chart (or charts) that illustrate the clever strategy you develop, along with indications of entry and exit points. It should also generate an orders.txt file that you feed into your market simulator to backtest the strategy.
You will submit all of the above code files along with your PDF report.
Part 1A: Bollinger Band Strategy Chart (20%)
If you don't know about Bollinger Bands already, go learn about them. Here are some references:
- http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:bollinger_bands
- https://en.wikipedia.org/wiki/Bollinger_Bands
Here are the essential things you need to know and to implement. In addition to the price of a stock over time, Bollinger Bands include:
- 20 day simple moving average (SMA) line.
- Upper Band = SMA + 2 * 20 day standard deviation.
- Lower Band = SMA - 2 * 20 day standard deviation.
A basic Bollinger Band trading strategy works as follows: There are two potential entries, long and short. The long entry is made when the price transitions from below the lower band to above the lower band. This indicates that the stock price has moved substantially away from the moving average, but is now moving back towards the moving average. When this entry signal criteria is met, buy the stock and hold it until the exit. The exit signal occurs when the price moves from below the SMA to above it.
The short entry and exit are mirrors of the long entry and exit: The short entry is made when the price transitions from above the upper band to below the upper band. This indicates that the stock price has moved substantially away from the moving average, but is now moving back towards the moving average. When this entry signal criteria is met, short the stock and hold it until the exit. The exit signal occurs when the price moves from above the SMA to below it.
You should create a chart that shows:
- Stock price (adjusted close)
- SMA
- Upper Band
- Lower Band
- Long entries as a vertical green line at the time of entry.
- Long exits as a vertical black line at the time of exit.
- Short entries as a vertical green line at the time of entry.
- Short exits as a vertical black line at the time of exit.
Part 1B: Bollinger Band Strategy Backtest (25%)
Reuse your charting code to generate trades. Start with $10,000. For a long entry, BUY 100 shares and hold until the exit. For a short entry, SELL 100 shares and hold until the exit. Chart the performance of your fund over the time period described in "what to turn in" below.
Part 2: Leverage (5%)
Many brokers allow "leverage" which is to say that you can borrow money from them in order to buy (or sell) more assets. As an example, suppose you deposit $100,000 with your broker; You might then buy $100,000 worth of stocks. At that point you would have a cash position of $0 and a sum of long positions of $100,000. This situation is 1.0 leverage. However, many brokers allow up to 2.0 leverage. So, you could borrow $100,000, to buy more stocks. If you did that, you'd have long positions of $200,000 and a cash position of -$100,000 due to the loan. Here's how to calculate leverage:
leverage = (sum(longs) + sum(abs(shorts))) / ((sum(longs) - sum(abs(shorts)) + cash)
Here are a few examples:
- You deposit $100,000, then short $50K worth of stock and buy $50K worth of stock. You would then have $100K of cash, $50K of longs, -$50K of shorts, so your leverage would be 1.0.
- You deposit $100,000 then short $200K worth of stock. You have $300K of cash and -$200K in shorts. So your leverage is 2.0.
- You deposit $100,000 then buy $50K of stock. Your leverage is 0.5.
Your simulator should prohibit trades that would cause portfolio leverage to exceed 2.0.
FAQs:
- Q: What if the portfolio becomes levered after the trades have been entered?
- A: It is OK if the trades are entered and then later, due to stock price changes, leverage exceeds 2.0.
- Q: Should I allow a partial order to be filled so it gets just right up to 2.0
- A: No reject the order entirely.
- Q: What if the portfolio is levered at 2.0 already, should I accept orders that reduce leverage?
- A: Yes.
Orders files to run your code on
Example orders files are available in the orders subdirectory.
Short example to check your code
Here is a very very short example that you can use to check your code. Starting conditions:
start_date = '2011-1-05' end_date = '2011-1-20' start_val = 1000000
For the orders file orders-short.csv, the orders are:
Date,Symbol,Order,Shares 2011-01-05,AAPL,BUY,1500 2011-01-20,AAPL,SELL,1500
The daily value of the portfolio (spaces added to help things line up):
2011-01-05 1000000 2011-01-06 999595 2011-01-07 1003165 2011-01-10 1012630 2011-01-11 1011415 2011-01-12 1015570 2011-01-13 1017445 2011-01-14 1021630 2011-01-18 1009930 2011-01-19 1007230 2011-01-20 998035
For reference, here are the adjusted close values for AAPL on the relevant days:
AAPL 2011-01-05 332.57 2011-01-06 332.30 2011-01-07 334.68 2011-01-10 340.99 2011-01-11 340.18 2011-01-12 342.95 2011-01-13 344.20 2011-01-14 346.99 2011-01-18 339.19 2011-01-19 337.39 2011-01-20 331.26
The full results:
Data Range: 2011-01-05 to 2011-01-20 Sharpe Ratio of Fund: -0.446948390642 Sharpe Ratio of $SPX: 0.882168679776 Cumulative Return of Fund: -0.001965 Cumulative Return of $SPX: 0.00289841448894 Standard Deviation of Fund: 0.00634128215394 Standard Deviation of $SPX: 0.00544933521991 Average Daily Return of Fund: -0.000178539446839 Average Daily Return of $SPX: 0.000302827205547 Final Portfolio Value: 998035.0
More comprehensive examples
orders.csv
We provide an example, orders.csv that you can use to test your code, and compare with others. All of these runs assume a starting portfolio of 1000000 ($1M).
Data Range: 2011-01-10 to 2011-12-20 Sharpe Ratio of Fund: 1.21540888742 Sharpe Ratio of $SPX: 0.0183389807443 Cumulative Return of Fund: 0.13386 Cumulative Return of $SPX: -0.0224059854302 Standard Deviation of Fund: 0.00720514136323 Standard Deviation of $SPX: 0.0149716091522 Average Daily Return of Fund: 0.000551651296638 Average Daily Return of $SPX: 1.7295909534e-05 Final Portfolio Value: 1133860.0
orders2.csv
The other sample file is orders2.csv that you can use to test your code, and compare with others.
Data Range: 2011-01-14 to 2011-12-14 Sharpe Ratio of Fund: 0.788982285751 Sharpe Ratio of $SPX: -0.177203019906 Cumulative Return of Fund: 0.0787526 Cumulative Return of $SPX: -0.0629581516192 Standard Deviation of Fund: 0.00711102080156 Standard Deviation of $SPX: 0.0150564855724 Average Daily Return of Fund: 0.000353426354584 Average Daily Return of $SPX: -0.000168071648902 Final Portfolio Value: 1078752.6
Implementation suggestions & assumptions
In terms of execution prices, you should assume you get the adjusted close price for the day of the trade.
Here is a video outlining an approach to solving this problem [youtube video].
What to turn in
Be sure to follow these instructions diligently!
Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):
- Your code as marketsim.py (only the function compute_portvals() will be tested)
Unlimited resubmissions are allowed up to the deadline for the project.
Rubric
- Basic simulator: 10 test cases: We will test your code against 10 cases (9.5% per case). Each case will be deemed "correct" if:
- For each day, abs(reference portval - your portval) < $0.01
- Leverage: 2 test cases (2.5% per case). Each case will be deemed "correct" if:
- For each day, abs(reference portval - your portval) < $0.01