Difference between revisions of "MC2-Project-1"

From Quantitative Analysis Software Courses
Jump to navigation Jump to search
Line 4: Line 4:
 
==To Do==
 
==To Do==
  
Create a file for your code called <tt>marketsim.py</tt>.  In <tt>marketsim.py</tt> create a function, your market simulator, <tt>compute_portvals()</tt> that returns a dataframe with one column.  It should adhere to the following API
+
Create a file for your code called <tt>marketsim.py</tt>.  Your job is to create a function, your market simulator, <tt>compute_portvals()</tt> that returns a dataframe with one column.  It should adhere to the following API
  
 
  def compute_portvals(start_date, end_date, ordersfile, startval)
 
  def compute_portvals(start_date, end_date, ordersfile, startval)
Line 10: Line 10:
 
     return df_portvals
 
     return df_portvals
  
where <tt>start_date</tt> and <tt>end_date</tt> represent the first date and last date to track, respectively, <tt>ordersfile</tt> is the name of a file from which to read orders, and <tt>startval</tt> is the initial value of the portfolio. The returned result <tt>df_portvals</tt> is a data frame containing the value of the portfolio for each trading day from <tt>start_date</tt> to <tt>end_date</tt> inclusive.
+
where <tt>start_date</tt> and <tt>end_date</tt> are the first date and last date to track, respectively, <tt>ordersfile</tt> is the name of a file from which to read orders, and <tt>startval</tt> is the initial value of the portfolio. The returned result <tt>df_portvals</tt> is a data frame containing the value of the portfolio for each trading day from <tt>start_date</tt> to <tt>end_date</tt> inclusive.
  
 
The files containing orders are CSV files organized like this:
 
The files containing orders are CSV files organized like this:
Line 27: Line 27:
 
  2008, 12, 5, IBM, BUY, 50
 
  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 value of equities. Return the result in a dataframe that would contain values like this:
+
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. Return the result in a dataframe that would contain values like this:
  
 
  2008-12-3 1000000
 
  2008-12-3 1000000
Line 34: Line 34:
 
  ...
 
  ...
  
<B>Part 2:</B> Create a portfolio analysis tool, analyze.py, that takes a command line like this:
+
We will evaluate your code by calling <tt>compute_portvals()</tt> with multiple test cases.
  
python analyze.py values.csv \$SPX
+
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
 
 
The tool should read in the daily values (cumulative portfolio value) from values.csv and plot them. It should use the symbol on the command line as a benchmark for comparison (in this case $SPX). Using this information, analyze.py should:
 
  
 
* Plot the price history over the trading period.
 
* Plot the price history over the trading period.
* Your program should also output:
+
* Standard deviation of daily returns of the total portfolio
** Standard deviation of daily returns of the total portfolio
+
* Average daily return of the total portfolio
** Average daily return of the total portfolio
+
* Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio
** Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio
+
* Cumulative return of the total portfolio
** Cumulative return of the total portfolio
+
* Ending value of the portfolio
  
 
==Orders files to run your code on==
 
==Orders files to run your code on==

Revision as of 13:24, 18 September 2015

Overview

In this project you will create a market simulator that accepts trading orders and keeps track of a portfolio's value over time and then assesses the performance of that portfolio.

To Do

Create a file for your code called marketsim.py. Your job is to create a function, your market simulator, compute_portvals() that returns a dataframe with one column. It should adhere to the following API

def compute_portvals(start_date, end_date, ordersfile, startval)
    # do stuff
    return df_portvals

where start_date and end_date are the first date and last date to track, respectively, ordersfile is the name of a file from which to read orders, and startval is the initial value of the portfolio. The returned result df_portvals is a data frame containing the value of the portfolio for each trading day from start_date to end_date inclusive.

The files containing orders are CSV files organized like this:

  • Year
  • Month
  • Day
  • Symbol
  • BUY or SELL
  • Number of Shares

For example:

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 adjusted closing prices. The value for each day is cash plus the current value of equities. Return the result in a dataframe that would contain values like this:

2008-12-3 1000000
2008-12-4 1000010
2008-12-5 1000250
...

We will evaluate your code by calling compute_portvals() with multiple test cases.

For debugging purposes, you should write your own additional helper function to call compute_portvals() with your own test cases. We suggest that you report the following factors

  • Plot the price history over the trading period.
  • Standard deviation of daily returns of the total portfolio
  • Average daily return of the total portfolio
  • Sharpe ratio (Always assume you have 252 trading days in an year. And risk free rate = 0) of the total portfolio
  • Cumulative return of the total portfolio
  • Ending value of the portfolio

Orders files to run your code on

Grab this zip file to get the input files to run your code against: media:orders-files.zip

Short example to check your code

Here is a very very short example that you can use to check your code. Assuming a 1,000,000 starting cash and the orders file orders-short.csv:

The orders file:

2011,1,05,AAPL,Buy,1500,
2011,1,20,AAPL,Sell,1500,

The daily value of the portfolio (spaces added to help things line up):

2011, 1,  5, 1000000
2011, 1,  6,  999595
2011, 1,  7, 1003165
2011, 1, 10, 1012630
2011, 1, 11, 1011415
2011, 1, 12, 1015570
2011, 1, 13, 1017445
2011, 1, 14, 1021630
2011, 1, 18, 1009930
2011, 1, 19, 1007230
2011, 1, 20,  998035

For reference, here are the adjusted close values for AAPL on the relevant days:

2011-01-05 16:00:00    332.57
2011-01-06 16:00:00    332.30
2011-01-07 16:00:00    334.68
2011-01-10 16:00:00    340.99
2011-01-11 16:00:00    340.18
2011-01-12 16:00:00    342.95
2011-01-13 16:00:00    344.20
2011-01-14 16:00:00    346.99
2011-01-18 16:00:00    339.19
2011-01-19 16:00:00    337.39
2011-01-20 16:00:00    331.26

The full results:

Details of the Performance of the portfolio :

Data Range :  2011-01-05 16:00:00  to  2011-01-20 16:00:00

Sharpe Ratio of Fund : -0.449182051041
Sharpe Ratio of $SPX : 0.88647463107

Total Return of Fund :  0.998035
Total Return of $SPX : 1.00289841449

Standard Deviation of Fund :  0.00573613516299
Standard Deviation of $SPX : 0.00492987789459

Average Daily Return of Fund :  -0.000162308588036
Average Daily Return of $SPX : 0.000275297459588

More comprehensive examples

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).

The final value of the portfolio using the sample file is -- 2011,12,20,1133860

Details of the Performance of the portfolio :

Data Range :  2011-01-10 16:00:00  to  2011-12-20 16:00:00

Sharpe Ratio of Fund : 1.21540462111
Sharpe Ratio of $SPX : 0.0183391412227

Total Return of Fund :  1.13386
Total Return of $SPX : 0.97759401457

Standard Deviation of Fund :  0.00717514512699
Standard Deviation of $SPX : 0.0149090969828

Average Daily Return of Fund :  0.000549352749569
Average Daily Return of $SPX : 1.72238432443e-05


The other sample file is orders2.csv that you can use to test your code, and compare with others.


The final value of the portfolio using the sample file is -- 2011,12,14, 1078753

Details of the Performance of the portfolio

Data Range :  2011-01-14 16:00:00  to  2011-12-14 16:00:00

Sharpe Ratio of Fund : 0.788985460132
Sharpe Ratio of $SPX : -0.177204632551

Total Return of Fund :  1.0787526
Total Return of $SPX : 0.937041848381

Standard Deviation of Fund :  0.00708034136287
Standard Deviation of $SPX : 0.0149914504972

Average Daily Return of Fund :  0.000351902965125
Average Daily Return of $SPX : -0.000167347202139

Implementation suggestions & assumptions

In terms of execution prices, you should assume you get the adjusted close price for the day of the trade.

Here are some hints on how to build it: media:marketsim-guidelines.pdf

What to expect when you turn in your assignment (Coursera)

Once you create the tools described above, you will be asked to run specific orders files through your code and then to run the results through your analyze tool to report on various measures such as Sharpe Ratio and Cumulative Return.

Deliverables for on campus GT students

To do: Run your code for the two files orders.csv and orders2.csv. Generate charts for the two runs.

To turn in:

  • The code for your two programs: marketsim.py, analyze.py
  • A report, report.pdf that includes:
    • The 2 charts for the two orders files.
    • Text output of your analysis code.
File:Example-fund-chart.png
alt Example chart. $DJI (green) is the benchmark blue is the fund.