Difference between revisions of "MC2-Project-2"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
 
(78 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Updates==
+
==Updates/FAQs==
 +
 
 +
'''2016/2/29''':
 +
 
 +
* FAQ and project description overhauled.  If you've already started on the project be sure to reread this, some important items have changed.
 +
 
 +
* Q: Do we show all entry and exit signals or just the ones that are actually relevant for our current positions? For example, if there is an exit signal but we don't hold a position that would be exited yet do we show it on the chart?  A: Only show the ones relevant to your current positions.
 +
 
 +
* Q: Is the IBM price between Dec 2007 and Dec 2009 the only information I have for my strategy?  A: You should use only the data available to you in the "/data" directory provided, which is open, high, low, close, adjusted close, and volume.
 +
 
 +
* Q: Does this mean that we will have a single asset portfolio? and with only 100 (long/short) shares at any given time? A: Yes
 +
 +
* Q: What should we do if there is a buy signal, followed again by a buy signal? A: You should hold one position until exit. So you should ignore the second buy signal.
 +
 
 +
* Q: Do I have to use the provided data for this project? A: Yes, you must use the provided data.
 +
 
 +
* Q: Can I use data before Dec 31, 2007? A: No, you can only use data from Dec 31, 2007 to Dec 31, 2009.
 +
 
 +
* Q: Should we use $SPX or SPY as the benchmark? A: $SPX.
 +
 
 +
* Q: Regarding part 2 of assignment MC2-P2, do we need to come up with an original idea or can we use a public domain strategy discussed on the internet or published in a book? A: You can use one in the public domain, but be sure to cite the source in your report.
 +
 
 +
'''2016/3/18''':
 +
 
 +
* Q: Can we only use IBM price and volume information?  A: You can use data available to you in the "/data" directory provided, which also includes other stocks and some ETFs.
 +
 
 +
* Q: What is the "baseline?" A: That is the performance of the basic Bollinger Band strategy for IBM.
 +
 
 +
* Q: Are we allowed to implement leverage for this project for part 2 (our strategy)?  A: You should only be in a single long or short position of 100 shares of IBM at any one time.  If it requires leverage to enter that position, it is OK.
 +
 
 +
'''2016/3/21''':
 +
 
 +
* Q: Can we use util.py?  A: Yes, you can assume that util.py is available in the directory from which you run your code.  If you modify util.py or if you create additional supporting code files, submit them with the rest of your code.  We should be able to run your code by placing all of your .py files in a directory and running them.
 +
 
 +
'''2016/3/22''':
 +
 
 +
* Q: If I'm determining whether to go long or short on day t, can I use price or volume information from day t+1?  A: No, that is "peeking into the future."  You can only use information up to the date you are considering to trade.
 +
 
 +
'''2016/3/25''':
 +
* Q: Should I assess performance from Dec 31, 2007 to Dec 31, 2009? A: Feed the orders into your market simulator and assess it over the period from first trade to last trade only.  The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009.  It will probably start later and end earlier.
 +
 
 +
* Q: How long should our report be? A: Use 11pt font and single spaced lines.  We expect that a complete report addressing all the criteria would be at least 3 pages.  It should be no longer than 6 pages including charts, tables and text.  To encourage conciseness we will deduct 2% for each page over 6 pages.
 +
 
 +
* Q: Can you clarify which code should create which output? A:
 +
** bollinger_strategy.py and my_strategy.py should both output
 +
*** A chart including IBM prices, supporting lines illustrating your indicators, and vertical lines indicating entry and exit points.
 +
*** An orders.csv file following the protocol defined in the last project, enumerating entries and exits.
 +
** Feed the orders file into your marketsim.py code.  It should output:
 +
*** A chart illustrating the performance of your strategy versus a benchmark.
 +
*** Statistical analysis of the strategy (e.g., cumulative return, final value, Sharpe ratio, etc.)
  
 
==Overview==
 
==Overview==
 
In this project you will develop trading strategies using Technical Analysis, then test them using your market simulator.
 
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.
+
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, not your codeHowever, you will also submit your code that will be checked visually to ensure it appropriately matches the report you submit.
  
 
==Template==
 
==Template==
  
Create your code within a directory named <tt>ml4t/mc2_p2</tt> . Within that directory you should create/write four python files as follows:
+
Create your code within a directory named <tt>ml4t/mc2_p2/</tt>. Within that directory you should create two python programs as follows:
  
* <tt>bollinger-chart.py</tt>: This code should generate a .png chart that illustrates the bollinger bands, and entry and exit points for a bollinger-based strategy.
+
* <tt>bollinger_strategy.py</tt>: This code should generate a .png chart that illustrates the bollinger bands, and entry and exit points for a bollinger-based strategy.
* <tt>bollinger-strategy.py</tt>: This code should generate an orders.txt file that you feed into your market simulator to backtest the strategy.
+
It should also generate an <tt>orders.csv</tt> file that you feed into your market simulator to backtest the strategy.
* <tt>my-chart.py</tt>: This code should generate a .pnt chart (or charts) that illustrate the clever strategy you develop, along with indications of entry and exit points.
+
* <tt>my_strategy.py</tt>: This code should generate a .png chart (or charts) that illustrate the clever strategy you develop, along with indications of entry and exit points. It should also generate an <tt>orders.csv</tt> file that you feed into your market simulator to backtest the strategy.
* <tt>my-strategy.py</tt>: This code should 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.
 
You will submit all of the above code files along with your PDF report.
  
==Part 1A: Bollinger Band Strategy Chart (25%)==
+
==Time Period and Stock for Testing==
 +
 
 +
We will use IBM and trade it from December 31, 2007 until December 31, 2009.  Note that the first trade cannot occur until 20 days after December 31, 2007 because the first value for Bollinger bands is not available until then.
 +
 
 +
You should evaluate the performance of your strategy from first trade to last trade.  The time period of performance will therefor not exactly match Dec 31, 2007 to Dec 31 2009.  It will probably start later and end earlier.
 +
 
 +
==Part 1A: Bollinger Band Strategy Chart (15%)==
  
 
If you don't know about Bollinger Bands already, go learn about them.  Here are some references:
 
If you don't know about Bollinger Bands already, go learn about them.  Here are some references:
Line 24: Line 78:
 
* https://en.wikipedia.org/wiki/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:
+
Here are the essential things you need to know and to implement them.  In addition to the price of a stock over time, Bollinger Bands include:
  
 
* 20 day simple moving average (SMA) line.
 
* 20 day simple moving average (SMA) line.
Line 42: Line 96:
 
* Long entries as a vertical green line at the time of entry.
 
* Long entries as a vertical green line at the time of entry.
 
* Long exits as a vertical black line at the time of exit.
 
* 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 entries as a vertical RED line at the time of entry.
 
* Short exits as a vertical black line at the time of exit.
 
* Short exits as a vertical black line at the time of exit.
  
==Part 1B: Bollinger Band Strategy Backtest (25%)==
+
==Part 1B: Bollinger Band Strategy Backtest (15%)==
  
Reuse your charting code to generate trades
+
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%)==
+
Feed the orders into your market simulator and assess it over the period from first trade to last trade only.  The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009.  It will probably start later and end earlier.
  
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:
+
==Example Results==
  
leverage = (sum(longs) + sum(abs(shorts))) / ((sum(longs) - sum(abs(shorts)) + cash)
+
[[File:bb-entries.png]]
  
Here are a few examples:  
+
[[File:bb-strat.png]]
  
* 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.
+
<pre>
* 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.
+
Data Range: 2008-02-28 00:00:00 to 2009-12-29 00:00:00
* 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.  
+
Sharpe Ratio of Fund: 1.00195922396
 +
Sharpe Ratio of $SPX: -0.116052774605
  
FAQs:
+
Cumulative Return of Fund: 0.3524
 +
Cumulative Return of $SPX: -0.176561768835
  
* Q: What if the portfolio becomes levered after the trades have been entered?
+
Standard Deviation of Fund: 0.0113472713465
* A: It is OK if the trades are entered and then later, due to stock price changes, leverage exceeds 2.0.
+
Standard Deviation of $SPX: 0.0225771006222
  
* Q: Should I allow a partial order to be filled so it gets just right up to 2.0
+
Average Daily Return of Fund: 0.000716211380412
* A: No reject the order entirely.
+
Average Daily Return of $SPX: -0.000165053001441
  
* Q: What if the portfolio is levered at 2.0 already, should I accept orders that reduce leverage?
+
Final Portfolio Value: 13524.0
* A: Yes.
+
</pre>
 
 
==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 <tt>orders-short.csv</tt>, the orders are:
+
First few trades:
  
 
<pre>
 
<pre>
Date,Symbol,Order,Shares
+
2008-02-28    IBM  SELL    100
2011-01-05,AAPL,BUY,1500
+
2008-03-27    IBM  BUY    100
2011-01-20,AAPL,SELL,1500
+
2008-04-22    IBM  SELL    100
 +
2008-05-21    IBM  BUY     100
 +
2008-07-01   IBM  BUY    100
 +
2008-07-08    IBM  SELL     100
 
</pre>
 
</pre>
  
The daily value of the portfolio (spaces added to help things line up):
+
==Part 2: Your Strategy (60%)==
<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:
+
For this component of the project you are to devise your own Technical Analysis-based signalYour overall strategy can include Bollinger Bands, but you must devise and code another, separate indicatorYou should write code to create a chart that shows relevant information about the indicator, including long and short entry and exit eventsYour strategy should outperform the Bollinger Band strategy described above (in terms of cumulative return)You should test your strategy over the same time period as the Bollinger Band example above.
<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:
+
Important: To ensure a fair comparison of strategies, you should hold only 100 share positions at a time (long or short).  Tune your algorithm for the best performance you can find for the in sample period used above.  Your report should include an analysis of:
  
<pre>
+
* In sample performance during the period December 31, 2007 to December 31, 2009.
Data Range: 2011-01-05 to 2011-01-20
+
* Out of sample performance for the two subsequent years December 31, 2009 to December 31, 2011.
  
Sharpe Ratio of Fund: -0.446948390642
+
Feed the orders into your market simulator and assess it over the period from first trade to last trade only.  The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009. It will probably start later and end earlier.
Sharpe Ratio of $SPX: 0.882168679776
 
  
Cumulative Return of Fund: -0.001965
+
==Part 3: Performance (10%)==
Cumulative Return of $SPX: 0.00289841448894
 
  
Standard Deviation of Fund: 0.00634128215394
+
0 to 10 points will be awarded depending on the performance of your new strategy. See the rubric below for details.
Standard Deviation of $SPX: 0.00544933521991
 
  
Average Daily Return of Fund: -0.000178539446839
+
==Contents of the Report==
Average Daily Return of $SPX: 0.000302827205547
 
  
Final Portfolio Value: 998035.0
+
Submit your report in PDF as <tt>report.pdf</tt>. Use 11pt font and single spaced lines.  We expect that a complete report addressing all the criteria would be at least 3 pages.  It should be no longer than 6 pages including charts, tables and text.  To encourage conciseness we will deduct 2% for each page over 6 pages.  Your report should contain the following elements:
</pre>
 
  
==More comprehensive examples==
+
Part 1:
  
===<tt>orders.csv</tt>===
+
* Bollinger Band strategy chart showing entry and exit points.
 +
* Bollinger Band strategy backtest chart.
 +
* Summary of Bollinger Band backtest performance metrics.
  
We provide an example, <tt>orders.csv</tt> that you can use to test your code, and compare with others. All of these runs assume a starting portfolio of 1000000 ($1M).
+
Part 2:
<pre>
 
Data Range: 2011-01-10 to 2011-12-20
 
  
Sharpe Ratio of Fund: 1.21540888742
+
* Written description of your strategy idea.  You will be graded on the clarity of your description.  It should be sufficiently well described that someone could reproduce your result.
Sharpe Ratio of $SPX: 0.0183389807443
+
* Chart or charts that illustrate your strategy idea.  At least one of the charts should show entry and exit points.
 +
* 2 backtest charts of your strategy: One in sample, the other out of sample.
 +
* Analysis of the performance of your strategy idea over the 2008-2009 period, and separately 2009 to 2011.  Note that your strategy should outperform the Bollinger Band strategy in the in sample period.
 +
* Summary of backtest performance metrics, be sure to clearly state the cumulative return for your strategy in sample.
 +
* Your report should address the following questions:
 +
** What do you think of refining and testing your strategy over the same 2 years?  Is that a good practice?  Why or why not?  Does the strategy continue to work as well out of sample?  Why?
  
Cumulative Return of Fund: 0.13386
+
==Hints and resources==
Cumulative Return of $SPX: -0.0224059854302
 
  
Standard Deviation of Fund: 0.00720514136323
+
Consider augmenting the Bollinger Band strategy with additional consideration of the SPY's Bollinger values. If the individual stock is substantially different, this could signal an important move for the stock.
Standard Deviation of $SPX: 0.0149716091522
 
  
Average Daily Return of Fund: 0.000551651296638
+
==What to turn in==
Average Daily Return of $SPX: 1.7295909534e-05
+
Be sure to follow these instructions precisely!
  
Final Portfolio Value: 1133860.0
+
Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):
</pre>
 
  
===<tt>orders2.csv</tt>===
+
* Your code as <tt>bollinger_strategy.py, my_strategy.py</tt>
 +
* Your report as <tt>report.pdf</tt>
  
The other sample file is <tt>orders2.csv</tt> that you can use to test your code, and compare with others.  
+
Unlimited resubmissions are allowed up to the deadline for the project.
  
<pre>
+
==Rubric==
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
+
Part 1 (30%):
</pre>
+
* Bollinger Band entry/exit graph correct: 7.5%
 +
* Bollinger Band code is correct: 7.5%
 +
* Bollinger Band strategy performance graph correct: 7.5%
 +
* Bollinger Band strategy results correct: 7.5%
  
==Implementation suggestions & assumptions==
+
Part 2 (60%):
 +
* Written description of student's strategy is not clear -10%
 +
* Student's strategy code does not match report -40%
 +
* Chart(s) do not illustrate how the student's strategy works -10%
 +
* Backtest charts absent -10% per missing chart.
 +
* Results are not plausible -10%
 +
* Report does not address required questions -10%
  
In terms of execution prices, you should assume you get the <B>adjusted close</B> price for the day of the trade.
+
Part 3 (10%):
  
Here is a video outlining an approach to solving this problem [[https://www.youtube.com/watch?v=E1GTOSUoDpE youtube video]].
+
Compare the student's strategy against the baseline of IBM traded according to the basic Bollinger Band strategy implemented in Part 1.
 +
If (cumulative return of student's strategy / cumulative return of baseline) is:
 +
* 2.0 or above: 10 points
 +
* Between 1.8 and 2.0: 8 points
 +
* Between 1.6 and 1.8: 6 points
 +
* Between 1.4 and 1.6: 4 points
 +
* Between 1.2 and 1.4: 2 points
 +
* Below 1.2: 0 points
  
==What to turn in==
+
Other potential deductions:
Be sure to follow these instructions diligently!
 
  
Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):
+
* -2% for each page over 6 pages.
 +
* -2% for font smaller than 10pt.
  
* Your code as <tt>marketsim.py</tt> (only the function <tt>compute_portvals()</tt> will be tested)
+
==Required, Allowed & Prohibited==
  
Unlimited resubmissions are allowed up to the deadline for the project.
+
Required:
 +
* Your project must be coded in Python 2.7.x.
 +
* Code must run (without additional libraries) on one of the university provided computers.
  
==Rubric==
+
Allowed:
 +
* You can develop your code on your personal machine, but it must also run successfully on one of the university provided machines or virtual images.
 +
* Your code may use standard Python libraries.
 +
* You may use the NumPy, SciPy, matplotlib and Pandas libraries.  Be sure you are using the correct versions.
 +
* You may reuse sections of code (up to 5 lines) that you collected from other students or the internet.
 +
* Code provided by the instructor, or allowed by the instructor to be shared.
 +
* You may copy and use util.py in the local directory.  If you modify util.py, be sure to turn it in with your code.
  
* Basic simulator: 10 test cases: We will test your code against 10 cases (9.5% per case).  Each case will be deemed "correct" if:
+
Prohibited:
** For each day, abs(reference portval - your portval) < $0.01
+
* Any libraries not listed in the "allowed" section above.
* Leverage: 2 test cases (2.5% per case). Each case will be deemed "correct" if:
+
* Dead parrots.
** For each day, abs(reference portval - your portval) < $0.01
+
* Any code you did not write yourself (except for the 5 line rule in the "allowed" section).

Latest revision as of 10:35, 25 March 2016

Updates/FAQs

2016/2/29:

  • FAQ and project description overhauled. If you've already started on the project be sure to reread this, some important items have changed.
  • Q: Do we show all entry and exit signals or just the ones that are actually relevant for our current positions? For example, if there is an exit signal but we don't hold a position that would be exited yet do we show it on the chart? A: Only show the ones relevant to your current positions.
  • Q: Is the IBM price between Dec 2007 and Dec 2009 the only information I have for my strategy? A: You should use only the data available to you in the "/data" directory provided, which is open, high, low, close, adjusted close, and volume.
  • Q: Does this mean that we will have a single asset portfolio? and with only 100 (long/short) shares at any given time? A: Yes
  • Q: What should we do if there is a buy signal, followed again by a buy signal? A: You should hold one position until exit. So you should ignore the second buy signal.
  • Q: Do I have to use the provided data for this project? A: Yes, you must use the provided data.
  • Q: Can I use data before Dec 31, 2007? A: No, you can only use data from Dec 31, 2007 to Dec 31, 2009.
  • Q: Should we use $SPX or SPY as the benchmark? A: $SPX.
  • Q: Regarding part 2 of assignment MC2-P2, do we need to come up with an original idea or can we use a public domain strategy discussed on the internet or published in a book? A: You can use one in the public domain, but be sure to cite the source in your report.

2016/3/18:

  • Q: Can we only use IBM price and volume information? A: You can use data available to you in the "/data" directory provided, which also includes other stocks and some ETFs.
  • Q: What is the "baseline?" A: That is the performance of the basic Bollinger Band strategy for IBM.
  • Q: Are we allowed to implement leverage for this project for part 2 (our strategy)? A: You should only be in a single long or short position of 100 shares of IBM at any one time. If it requires leverage to enter that position, it is OK.

2016/3/21:

  • Q: Can we use util.py? A: Yes, you can assume that util.py is available in the directory from which you run your code. If you modify util.py or if you create additional supporting code files, submit them with the rest of your code. We should be able to run your code by placing all of your .py files in a directory and running them.

2016/3/22:

  • Q: If I'm determining whether to go long or short on day t, can I use price or volume information from day t+1? A: No, that is "peeking into the future." You can only use information up to the date you are considering to trade.

2016/3/25:

  • Q: Should I assess performance from Dec 31, 2007 to Dec 31, 2009? A: Feed the orders into your market simulator and assess it over the period from first trade to last trade only. The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009. It will probably start later and end earlier.
  • Q: How long should our report be? A: Use 11pt font and single spaced lines. We expect that a complete report addressing all the criteria would be at least 3 pages. It should be no longer than 6 pages including charts, tables and text. To encourage conciseness we will deduct 2% for each page over 6 pages.
  • Q: Can you clarify which code should create which output? A:
    • bollinger_strategy.py and my_strategy.py should both output
      • A chart including IBM prices, supporting lines illustrating your indicators, and vertical lines indicating entry and exit points.
      • An orders.csv file following the protocol defined in the last project, enumerating entries and exits.
    • Feed the orders file into your marketsim.py code. It should output:
      • A chart illustrating the performance of your strategy versus a benchmark.
      • Statistical analysis of the strategy (e.g., cumulative return, final value, Sharpe ratio, etc.)

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, not your code. However, 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 two python programs 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.csv file that you feed into your market simulator to backtest the strategy.

  • my_strategy.py: This code should generate a .png chart (or charts) that illustrate the clever strategy you develop, along with indications of entry and exit points. It should also generate an orders.csv 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. Note that the first trade cannot occur until 20 days after December 31, 2007 because the first value for Bollinger bands is not available until then.

You should evaluate the performance of your strategy from first trade to last trade. The time period of performance will therefor not exactly match Dec 31, 2007 to Dec 31 2009. It will probably start later and end earlier.

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 them. 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 RED 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.

Feed the orders into your market simulator and assess it over the period from first trade to last trade only. The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009. It will probably start later and end earlier.

Example Results

Bb-entries.png

Bb-strat.png

Data Range: 2008-02-28 00:00:00 to 2009-12-29 00:00:00

Sharpe Ratio of Fund: 1.00195922396
Sharpe Ratio of $SPX: -0.116052774605

Cumulative Return of Fund: 0.3524
Cumulative Return of $SPX: -0.176561768835

Standard Deviation of Fund: 0.0113472713465
Standard Deviation of $SPX: 0.0225771006222

Average Daily Return of Fund: 0.000716211380412
Average Daily Return of $SPX: -0.000165053001441

Final Portfolio Value: 13524.0

First few trades:

2008-02-28    IBM  SELL     100
2008-03-27    IBM   BUY     100 
2008-04-22    IBM  SELL     100
2008-05-21    IBM   BUY     100
2008-07-01    IBM   BUY     100
2008-07-08    IBM  SELL     100

Part 2: Your Strategy (60%)

For this component of the project you are to devise your own Technical Analysis-based signal. Your overall strategy can include Bollinger Bands, but you must devise and code another, separate indicator. 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). You should test your strategy over the same time period as the Bollinger Band example above.

Important: To ensure a fair comparison of strategies, you should hold only 100 share positions at a time (long or short). Tune your algorithm for the best performance you can find for the in sample period used above. Your report should include an analysis of:

  • In sample performance during the period December 31, 2007 to December 31, 2009.
  • Out of sample performance for the two subsequent years December 31, 2009 to December 31, 2011.

Feed the orders into your market simulator and assess it over the period from first trade to last trade only. The time period of performance will therefore not exactly match Dec 31, 2007 to Dec 31 2009. It will probably start later and end earlier.

Part 3: Performance (10%)

0 to 10 points will be awarded depending on the performance of your new strategy. See the rubric below for details.

Contents of the Report

Submit your report in PDF as report.pdf. Use 11pt font and single spaced lines. We expect that a complete report addressing all the criteria would be at least 3 pages. It should be no longer than 6 pages including charts, tables and text. To encourage conciseness we will deduct 2% for each page over 6 pages. Your report should contain the following elements:

Part 1:

  • Bollinger Band strategy chart showing entry and exit points.
  • 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. It should be sufficiently well described that someone could reproduce your result.
  • Chart or charts that illustrate your strategy idea. At least one of the charts should show entry and exit points.
  • 2 backtest charts of your strategy: One in sample, the other out of sample.
  • Analysis of the performance of your strategy idea over the 2008-2009 period, and separately 2009 to 2011. Note that your strategy should outperform the Bollinger Band strategy in the in sample period.
  • Summary of backtest performance metrics, be sure to clearly state the cumulative return for your strategy in sample.
  • Your report should address the following questions:
    • What do you think of refining and testing your strategy over the same 2 years? Is that a good practice? Why or why not? Does the strategy continue to work as well out of sample? Why?

Hints and resources

Consider augmenting the Bollinger Band strategy with additional consideration of the SPY's Bollinger values. If the individual stock is substantially different, this could signal an important move for the stock.

What to turn in

Be sure to follow these instructions precisely!

Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):

  • Your code as bollinger_strategy.py, my_strategy.py
  • Your report as report.pdf

Unlimited resubmissions are allowed up to the deadline for the project.

Rubric

Part 1 (30%):

  • Bollinger Band entry/exit graph correct: 7.5%
  • Bollinger Band code is correct: 7.5%
  • Bollinger Band strategy performance graph correct: 7.5%
  • Bollinger Band strategy results correct: 7.5%

Part 2 (60%):

  • Written description of student's strategy is not clear -10%
  • Student's strategy code does not match report -40%
  • Chart(s) do not illustrate how the student's strategy works -10%
  • Backtest charts absent -10% per missing chart.
  • Results are not plausible -10%
  • Report does not address required questions -10%

Part 3 (10%):

Compare the student's strategy against the baseline of IBM traded according to the basic Bollinger Band strategy implemented in Part 1. If (cumulative return of student's strategy / cumulative return of baseline) is:

  • 2.0 or above: 10 points
  • Between 1.8 and 2.0: 8 points
  • Between 1.6 and 1.8: 6 points
  • Between 1.4 and 1.6: 4 points
  • Between 1.2 and 1.4: 2 points
  • Below 1.2: 0 points

Other potential deductions:

  • -2% for each page over 6 pages.
  • -2% for font smaller than 10pt.

Required, Allowed & Prohibited

Required:

  • Your project must be coded in Python 2.7.x.
  • Code must run (without additional libraries) on one of the university provided computers.

Allowed:

  • You can develop your code on your personal machine, but it must also run successfully on one of the university provided machines or virtual images.
  • Your code may use standard Python libraries.
  • You may use the NumPy, SciPy, matplotlib and Pandas libraries. Be sure you are using the correct versions.
  • You may reuse sections of code (up to 5 lines) that you collected from other students or the internet.
  • Code provided by the instructor, or allowed by the instructor to be shared.
  • You may copy and use util.py in the local directory. If you modify util.py, be sure to turn it in with your code.

Prohibited:

  • Any libraries not listed in the "allowed" section above.
  • Dead parrots.
  • Any code you did not write yourself (except for the 5 line rule in the "allowed" section).