Qstk Homework 3-1

Computational Investing, Part I

keywords: algorithmic, algorithms, trading, finance, equities, markets, quantitative finance, georgia tech, gt, Tucker Balch

Course Goals

At the end of the course you will have created a working market simulator that you can use to test your own investing strategies. You will understand the basic principles of Modern Portfolio Theory and Active Portfolio Management.

  • Understand electronic markets.
  • Understand market data.
  • Write software to visualize market data.
  • Write software to discover market data.
  • Create a market simulator.

Prerequisite Knowledge

Who this course is for The course is intended for people with strong software programming experience and introductory level knowledge of investment practice. A primary prerequisite is an interest and excitement about the stock market. You should have strong programming skills to succeed in this course. Here's a quiz you can take to see if you have strong programming skills compinvesti-prog-quiz

Who this course is not for If you already use advanced software tools such as Mean Variance Optimizers in your regular investing practice, you will probably find that you are "overqualified" for this course.

Software we'll use In order to complete the programming assignments you will also need to become familiar with basic Unix commands, and you must download and install a set of Python modules to your computer (including NumPy, SciPy, Pandas, and QSTK).For more information about installing the software you will need, visit QSToolKit_Installation_Guide. The software works well on windows and Ubuntu unix. Some people have success with MacOS, but that is not as strongly supported.


Course Textbook The textbook for this course is What Hedge Funds Really Do by Philip Romero and Tucker Balch. You can learn about purchase information here: [1]

Grading Policy The first week will have a multiple choice quiz and in the following weeks you'll have a programming assignment each week. A total score of 70% or higher will guarantee a passing grade in the course.

Expectations Participants are expected to:

  • Watch all lecture videos every week
  • Complete assigned readings in the textbook
  • Complete all weekly assignments if any
  • Complete all quizzes
  • Abide by the standards of academic honesty – plagiarism and any form of cheating will not be tolerated and will result in the removal of the participant from the course
  • Seek help if needed

Communication & Etiquette Please communicate with the instructor and staff via the online forums. Because there are so many students, it is not possible for us to respond individually to every question. We will be using Piazza for the discussion forums in this class. The link can be either found the left navigation bar or [2]. Written language will be primary means of communication. As such, there can be miscommunication as there is no intonation in these written communications. Please be positive, supportive and constructive in your comments and forum postings. Please avoid any rude comments about the fellow students and posting anything that might hurt the sentiments of others.

Enrolling This course is being run through coursera.org. Please visit the course site for details and to sign up: https://www.coursera.org/course/compinvesting1

See the wiki page for the last offering Computational_Investing_I_2012_Fall

See the wiki page for the OMS / Udacity version OMS_ML4T_I


Plagiarism is considered a serious offense. Please make sure that all materials that you submit in any assignments, projects or quizzes are your own. If you copy and paste or copy word for word from the Internet or any other source, that is considered plagiarism. You will be immediately removed from the class if you plagiarize.

Useful Resources



Due dates for Fall 2014 session

Challenge Puzzles


Week 1

Readings for Week 1 Chapters 1, 2, 3, 4

  • Module 1: Course Overview
    • Video 11*: Learning objectives of for the course [*need to reshoot to emphasize programming difficulty]
      • Who is this course for?
      • Logistics
      • Instructor background
  • Module 2: So you want to be a fund manager?
    • Video 21: Module learning objectives
      • Viewpoint of course
      • Incentives for portfolio managers
      • Two main types of hedge fund
    • Video 22: Common metrics for assessing fund performance
      • Annual return
      • Risk
      • Reward/Risk
    • Video 23: Common metrics for assessing fund performance
    • Video 24: Demo
      • Download historical data
      • Manipulate historical data in Excel
  • Module 3: Market Mechanics
    • Video 31: Module objectives
      • Major order types
      • The order book
      • How market orders drive prices up and down
      • Live example
    • Video 32: Order book recap
      • How orders flow from trader to execution
      • Colocated computing
      • Mechanics of short selling
    • Video 33: How hedge funds exploit market mechanics
      • Order book-based trading
      • Arbitrage
    • Video 34: The computing inside a hedge fund
      • Trading algos
      • Optimizers
      • Forecasters
  • Module 4: Interview with Paul Jiganti
    • Video 310: How your order gets to the market Part 1
    • Video 320: How your order gets to the market Part 2
    • Video 330: What happened with Knight Capital
  • QUIZ: Market Mechanics

Week 2

Readings for Week 2 Chapters 5, 6, 7

  • Module 1: What is a Company Worth?
    • Video 41: Intrinsic value: Value of future dividends
    • Video 42: How and why news affects prices (Event Study)
    • Video 43: Fundamental analysis of company value
  • Module 2: Capital Assets Pricing Model
    • Video 71: Capital Assets Pricing Model
    • Video 72: CAPM: What is Beta
    • Video 73: How Hedge Funds use CAPM
  • Module 3: QSTK Software Overview
    • Video 61*: QSTK software overview
    • Video 63: Installing QSTK on a Mac
    • Video 81: Installing QSTK on Windows and testing QSTK on Windows
  • Module 4: Working with Historical Data* [need to add this module. Daily returns, cumulative returns, etc.]
  • Homework 0: Install QSTK

Week 3

  • Module 1: Manipulating Data in Python with Numpy
    • Video 51: Numpy Part 1
    • Video 52: Numpy Part 2
    • Video 53: Numpy Part 3
  • Module 2: Manipulating Data in QSTK
    • Video 171: QSTK Part 1
    • Video 172: QSTK Part 2
    • Video 173*: QSTK Part 3 [*show how to do major steps for HW1, discuss cached data]
  • Module 3: Homework 1: Analyze and Optimize a Portfolio
    • Video 181: Homework 1 Overview
    • Video 182: Homework 1 Excel example
  • Module 4: Interview with Tom Sosnoff
    • Video 340: Sosnoff Part 1
    • Video 350: Sosnoff Part 2
    • Video 360: Sosnoff Part 3
  • Homework 1: Create and analyze a portfolio

Week 4

Readings for Week 4: Chapter 8, 10, 11

  • Module 1: Efficient Markets Hypothesis and Event Studies
    • Video 91: Where does information come from? Arbitrage: Difference between real value and market price
    • Video 92: 3 Versions of Efficient Markets Hypothesis. Is EMH True?
    • Video 93: Event Studies
    • Video 94*: Event Studies Code Demo. Homework 2 Defined. (uses old code)
  • Module 2: Portfolio Optimization and the Efficient Frontier
    • Video 111: Module Objectives and Overview
    • Video 112: The Inputs and Outputs of a Portfolio Optimizer
    • Video 113: The Importance of Correlation and Covariance (in daily returns)
    • Video 114: The Efficient Frontier
    • Video 115: How Optimizers Work (In general, not just for portfolios)

Homework 2: Event Studies

Week 5

Readings for Week 5: Chapters 12, 13, 14

  • Module 1: Digging Into Data
    • Video 121: Module Objectives and Overview (Review of the "Correct Answers" to the $5 Event Studies, Survivor Bias)
    • Video 122: Actual vs Adjusted Prices (Dividends & Splits)
    • Video 123: Data Scrubbing (Checking for Sanity)
  • Module 2: Overview of Homework 3
    • Video 131: How Next Two Homeworks Fit Together
    • Video 132: Specification for Homework 3
    • Video 133: Suggestions on Implementation of Homework 3
  • Homework 3*: Build a Market Simulator [clean up example data CSVs]

Week 6

Readings for Week 6: Chapters 7, 9

  • Module 1: Overview of Homework 4
    • Video 161: Review of How to Assess Event Study
    • Video 162: Overview of Homework 4
  • Module 2: The Fundamental Law
    • Video 151: Coin Flipping
    • Video 152: Fundamental Law Part 1
    • Video 153: Fundamental Law Part 2
  • Module 3: CAPM for Portfolios: Managing Market Risk
    • Video 141: CAPM recap, overview for portfolios
    • Video 142: Example use of CAPM for long/short bet removing market risk
  • Homework 4: Event Study into Simulator

Week 7

  • Module 1: Information Feeds and Technical Analysis
    • Video 191: Example Information Feeds
    • Video 192: Intro to Technical Analysis
    • Video 193: Some Example Technical Indicators
    • Video 194: Bollinger Bands
  • Homework 5: Implement Bollinger Bands

Week 8

  • Module 1: Making a Better Market Simulator
    • Commissions
    • Market Impact (Slippage)
  • Module 2: Brief Introduction to Machine Learning
    • Parameterized models
    • Instance based models
  • Module 3: Arbitrage
  • Homework 6: Event Study with Bollinger Bands
  • Homework 7: Bollinger Band-based trading

Legacy Assignments

These are assignments that appeared in previous offerings of the course.


In this project you will create a basic market simulator that accepts trading orders and keeps track of a portfolio's value and saves it to a file. You will also create another program that assesses the performance of that portfolio.

To Do

Part 1: Create a market simulation tool, marketsim.py that takes a command line like this:

python marketsim.py 1000000 orders.csv values.csv

Where the number represents starting cash and orders.csv is a file of orders 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 (cash plus value of equities) and print the result to the file values.csv. The contents of the values.csv file should look something like this:

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

Part 2: Create a portfolio analysis tool, analyze.py, that takes a command line like this:

python analyze.py values.csv \$SPX

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.
  • Your program should also output:
    • 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

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.
alt Example chart. $DJI (green) is the benchmark blue is the fund.

0 thoughts on “Qstk Homework 3-1”


Leave a Comment

Your email address will not be published. Required fields are marked *