Difference between revisions of "MC2-Project-2"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
 
(79 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Updates==
+
==Updates/FAQs==
  
==Overview==
+
'''2016/2/29''':
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 formatFor 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.
+
* FAQ and project description overhauledIf you've already started on the project be sure to reread this, some important items have changed.
  
==Template==
+
* 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.
  
Create your code within a directory named <tt>ml4t/mc2_p2</tt> . Within that directory you should create/write four python files as follows:
+
* 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.
  
* <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.
+
* 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
* <tt>bollinger-strategy.py</tt>: This code should generate an orders.txt 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.
+
* 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.
* <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.
+
* Q: Do I have to use the provided data for this project? A: Yes, you must use the provided data.
  
==Part 1: Basic simulator (95%)==
+
* Q: Can I use data before Dec 31, 2007? A: No, you can only use data from Dec 31, 2007 to Dec 31, 2009.
  
Open the provided template file: <tt>marketsim.py</tt>. Your job is to implement a function, your market simulator, <tt>compute_portvals()</tt> that returns a dataframe with one column.
+
* Q: Should we use $SPX or SPY as the benchmark? A: $SPX.
  
It should adhere to the following API:
+
* 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.
  
def compute_portvals(start_date, end_date, orders_file, start_val):
+
'''2016/3/18''':
    # TODO: Your code here
 
    return portvals
 
  
where <tt>start_date</tt> and <tt>end_date</tt> are the first date and last date to track, respectively (specified as 'yyyy-mm-dd' strings*), <tt>orders_file</tt> is the name of a file from which to read orders, and <tt>start_val</tt> is the starting value of the portfolio (initial cash available). Return the result (<tt>portvals</tt>) as a <tt>pandas.Series</tt> or a single-column <tt>pandas.DataFrame</tt> (column name does not matter), containing the value of the portfolio for each trading day from <tt>start_date</tt> to <tt>end_date</tt>, inclusive.
+
* 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.
  
*<b>Note</b>: If you have already implemented this project assuming <tt>start_date</tt> and <tt>end_date</tt> are <tt>datetime</tt> objects (as specified in a previous version of the requirements), that is fine - your submission will be graded appropriately if you inform us (instructions on this coming soon!).
+
* Q: What is the "baseline?" A: That is the performance of the basic Bollinger Band strategy for IBM.
  
The files containing orders are CSV files with the following columns:
+
* 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.
  
* Date (yyyy-mm-dd)
+
'''2016/3/21''':
* Symbol (e.g. AAPL, GOOG)
 
* Order (BUY or SELL)
 
* Shares (no. of shares to trade)
 
  
For example:
+
* 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.
  Date,Symbol,Order,Shares
 
2008-12-3,AAPL,BUY,130
 
  2008-12-8,AAPL,SELL,130
 
  2008-12-5,IBM,BUY,50
 
  
Your simulator should calculate the total value of the portfolio for each day using <b>adjusted closing prices</b>. The value for each day is cash plus the current value of equities. The result should contain values like this:
+
'''2016/3/22''':
  
  2008-12-3 1000000
+
* 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.
  2008-12-4 1000010
 
2008-12-5 1000250
 
...
 
  
We will evaluate your code by calling <tt>compute_portvals()</tt> with multiple test cases.
+
'''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.
  
For debugging purposes, you should write your own additional helper function to call <tt>compute_portvals()</tt> with your own test cases. We suggest that you report the following factors:
+
* 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.
  
* Plot the price history over the trading period.
+
* Q: Can you clarify which code should create which output? A:
* Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio
+
** bollinger_strategy.py and my_strategy.py should both output
* Cumulative return of the total portfolio
+
*** A chart including IBM prices, supporting lines illustrating your indicators, and vertical lines indicating entry and exit points.
* Standard deviation of daily returns of the total portfolio
+
*** An orders.csv file following the protocol defined in the last project, enumerating entries and exits.
* Average daily return of the total portfolio
+
** Feed the orders file into your marketsim.py code.  It should output:
* Ending value of the portfolio
+
*** 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.)
  
==Part 2: Leverage (5%)==
+
==Overview==
 +
In this project you will develop trading strategies using Technical Analysis, then test them using your market simulator.
  
Many brokers allow "leverage" which is to say that you can borrow money from them in order to buy (or sell) more assetsAs 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:
+
In this project we shift from an auto graded code format to a report formatFor 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.
  
leverage = (sum(longs) + sum(abs(shorts))) / ((sum(longs) - sum(abs(shorts)) + cash)
+
==Template==
  
Here are a few examples:  
+
Create your code within a directory named <tt>ml4t/mc2_p2/</tt>. Within that directory you should create two python programs as follows:
  
* 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.
+
* <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.
* 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.
+
It should also generate an <tt>orders.csv</tt> file that you feed into your market simulator to backtest the strategy.
* You deposit $100,000 then buy $50K of stock. Your leverage is 0.5.
+
* <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.
  
Your simulator should prohibit trades that would cause portfolio leverage to exceed 2.0.  
+
You will submit all of the above code files along with your PDF report.
  
FAQs:
+
==Time Period and Stock for Testing==
  
* Q: What if the portfolio becomes levered after the trades have been entered?
+
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.
* 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
+
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.
* A: No reject the order entirely.
 
  
* Q: What if the portfolio is levered at 2.0 already, should I accept orders that reduce leverage?
+
==Part 1A: Bollinger Band Strategy Chart (15%)==
* A: Yes.
 
  
==Orders files to run your code on==
+
If you don't know about Bollinger Bands already, go learn about them.  Here are some references:
  
Example orders files are available in the orders subdirectory.
+
* http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:bollinger_bands
 +
* https://en.wikipedia.org/wiki/Bollinger_Bands
  
==Short example to check your code==
+
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:
  
Here is a very very short example that you can use to check your code. Starting conditions:
+
* 20 day simple moving average (SMA) line.
 +
* Upper Band = SMA + 2 * 20 day standard deviation.
 +
* Lower Band = SMA - 2 * 20 day standard deviation.
  
  start_date = '2011-1-05'
+
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.
  end_date = '2011-1-20'
 
  start_val = 1000000
 
  
For the orders file <tt>orders-short.csv</tt>, the orders are:
+
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.
  
<pre>
+
You should create a chart that shows:
Date,Symbol,Order,Shares
+
 
2011-01-05,AAPL,BUY,1500
+
* Stock price (adjusted close)
2011-01-20,AAPL,SELL,1500
+
* SMA
</pre>
+
* 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.
  
The daily value of the portfolio (spaces added to help things line up):
+
==Example Results==
<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:
+
[[File:bb-entries.png]]
<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:
+
[[File:bb-strat.png]]
  
 
<pre>
 
<pre>
Data Range: 2011-01-05 to 2011-01-20
+
Data Range: 2008-02-28 00:00:00 to 2009-12-29 00:00:00
  
Sharpe Ratio of Fund: -0.446948390642
+
Sharpe Ratio of Fund: 1.00195922396
Sharpe Ratio of $SPX: 0.882168679776
+
Sharpe Ratio of $SPX: -0.116052774605
  
Cumulative Return of Fund: -0.001965
+
Cumulative Return of Fund: 0.3524
Cumulative Return of $SPX: 0.00289841448894
+
Cumulative Return of $SPX: -0.176561768835
  
Standard Deviation of Fund: 0.00634128215394
+
Standard Deviation of Fund: 0.0113472713465
Standard Deviation of $SPX: 0.00544933521991
+
Standard Deviation of $SPX: 0.0225771006222
  
Average Daily Return of Fund: -0.000178539446839
+
Average Daily Return of Fund: 0.000716211380412
Average Daily Return of $SPX: 0.000302827205547
+
Average Daily Return of $SPX: -0.000165053001441
  
Final Portfolio Value: 998035.0
+
Final Portfolio Value: 13524.0
 
</pre>
 
</pre>
  
==More comprehensive examples==
+
First few trades:
  
===<tt>orders.csv</tt>===
 
 
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).
 
 
<pre>
 
<pre>
Data Range: 2011-01-10 to 2011-12-20
+
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
 +
</pre>
  
Sharpe Ratio of Fund: 1.21540888742
+
==Part 2: Your Strategy (60%)==
Sharpe Ratio of $SPX: 0.0183389807443
 
  
Cumulative Return of Fund: 0.13386
+
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.
Cumulative Return of $SPX: -0.0224059854302
 
  
Standard Deviation of Fund: 0.00720514136323
+
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:
Standard Deviation of $SPX: 0.0149716091522
 
  
Average Daily Return of Fund: 0.000551651296638
+
* In sample performance during the period December 31, 2007 to December 31, 2009.
Average Daily Return of $SPX: 1.7295909534e-05
+
* Out of sample performance for the two subsequent years December 31, 2009 to December 31, 2011.
  
Final Portfolio Value: 1133860.0
+
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.
</pre>
 
  
===<tt>orders2.csv</tt>===
+
==Part 3: Performance (10%)==
  
The other sample file is <tt>orders2.csv</tt> that you can use to test your code, and compare with others.  
+
0 to 10 points will be awarded depending on the performance of your new strategy.  See the rubric below for details.
  
<pre>
+
==Contents of the Report==
Data Range: 2011-01-14 to 2011-12-14
 
  
Sharpe Ratio of Fund: 0.788982285751
+
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:
Sharpe Ratio of $SPX: -0.177203019906
 
  
Cumulative Return of Fund: 0.0787526
+
Part 1:
Cumulative Return of $SPX: -0.0629581516192
 
  
Standard Deviation of Fund: 0.00711102080156
+
* Bollinger Band strategy chart showing entry and exit points.
Standard Deviation of $SPX: 0.0150564855724
+
* Bollinger Band strategy backtest chart.
 +
* Summary of Bollinger Band backtest performance metrics.
  
Average Daily Return of Fund: 0.000353426354584
+
Part 2:
Average Daily Return of $SPX: -0.000168071648902
 
  
Final Portfolio Value: 1078752.6
+
* 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.
</pre>
+
* 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?
  
==Implementation suggestions & assumptions==
+
==Hints and resources==
  
In terms of execution prices, you should assume you get the <B>adjusted close</B> price for the day of the trade.
+
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.
 
 
Here is a video outlining an approach to solving this problem [[https://www.youtube.com/watch?v=E1GTOSUoDpE youtube video]].
 
  
 
==What to turn in==
 
==What to turn in==
Be sure to follow these instructions diligently!
+
Be sure to follow these instructions precisely!
  
 
Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):
 
Via T-Square, submit as attachment (no zip files; refer to schedule for deadline):
  
* Your code as <tt>marketsim.py</tt> (only the function <tt>compute_portvals()</tt> will be tested)
+
* Your code as <tt>bollinger_strategy.py, my_strategy.py</tt>
 +
* Your report as <tt>report.pdf</tt>
  
 
Unlimited resubmissions are allowed up to the deadline for the project.
 
Unlimited resubmissions are allowed up to the deadline for the project.
Line 220: Line 191:
 
==Rubric==
 
==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:
+
Part 1 (30%):
** For each day, abs(reference portval - your portval) < $0.01
+
* Bollinger Band entry/exit graph correct: 7.5%
* Leverage: 2 test cases (2.5% per case). Each case will be deemed "correct" if:
+
* Bollinger Band code is correct: 7.5%
** For each day, abs(reference portval - your portval) < $0.01
+
* 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).

Latest revision as of 09: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).