Difference between revisions of "MC2-Project-2"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
Line 73: Line 73:
 
* Analysis of the performance of your strategy idea over the 2008-2009 period.  Note that your strategy should outperform the Bollinger Band strategy.
 
* Analysis of the performance of your strategy idea over the 2008-2009 period.  Note that your strategy should outperform the Bollinger Band strategy.
 
* Summary of backtest performance metrics.
 
* Summary of backtest performance metrics.
 
==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 <tt>orders-short.csv</tt>, the orders are:
 
 
<pre>
 
Date,Symbol,Order,Shares
 
2011-01-05,AAPL,BUY,1500
 
2011-01-20,AAPL,SELL,1500
 
</pre>
 
 
The daily value of the portfolio (spaces added to help things line up):
 
<PRE>
 
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
 
</PRE>
 
 
For reference, here are the <b>adjusted close</b> values for AAPL on the relevant days:
 
<PRE>
 
              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
 
</PRE>
 
 
The full results:
 
 
<pre>
 
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
 
</pre>
 
  
 
==More comprehensive examples==
 
==More comprehensive examples==

Revision as of 21:51, 27 October 2015

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.

Time Period and Stock for Testing

We will use IBM and trade it from December 31, 2007 until December 31, 2009.

Part 1A: Bollinger Band Strategy Chart (15%)

If you don't know about Bollinger Bands already, go learn about them. Here are some references:

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 (15%)

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: Your Strategy (70%)

For this component of the project you are to devise your own Technical Analysis-based signal. You should write code to create a chart that shows relevant information about the indicator, including long and short entry and exit events. Your strategy should outperform the Bollinger Band strategy described above (in terms of cumulative return).

Contents of the Report

Your report (PDF -- do not even think of submitting a Word document) should contain the following elements:

Part 1:

  • Bollinger Band strategy chart.
  • Bollinger Band strategy backtest chart.
  • Summary of Bollinger Band backtest performance metrics.

Part 2:

  • Written description of your strategy idea. You will be graded on the clarity of your description and also on how compelling an idea it is.
  • Chart or charts that illustrate your strategy idea. At least one of the charts should be similar to the Bollinger Band strategy chart.
  • Backtest chart of your strategy.
  • Analysis of the performance of your strategy idea over the 2008-2009 period. Note that your strategy should outperform the Bollinger Band strategy.
  • Summary of backtest performance metrics.

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