Skip to content

Commit

Permalink
Updated Computational Investing Part I samples
Browse files Browse the repository at this point in the history
  • Loading branch information
gbeced committed Jul 24, 2017
1 parent 1399f1c commit d0af234
Show file tree
Hide file tree
Showing 14 changed files with 1,042 additions and 1,217 deletions.
62 changes: 6 additions & 56 deletions doc/compinvpart1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ Computational Investing Part I
==============================

As I was taking the `Computational Investing Part I <https://class.coursera.org/compinvesting1-2012-001/class>`_ course in 2012
I had to work on a set of assignments and for some of them I used PyAlgoTrade.
I had to work on a set of assignments and for one of them I used PyAlgoTrade.

Homework 1
----------

For this assignment I had to pick 4 stocks, invest a total of $100000 during 2011, and calculate:
For this assignment I had to pick 4 stocks, invest a total of $1000000 during 2011, and calculate:

* Final portfolio value
* Anual return
Expand All @@ -17,10 +17,10 @@ For this assignment I had to pick 4 stocks, invest a total of $100000 during 201

Download the data with the following commands: ::

python -c "from pyalgotrade.tools import yahoofinance; yahoofinance.download_daily_bars('aeti', 2011, 'aeti-2011-yahoofinance.csv')"
python -c "from pyalgotrade.tools import yahoofinance; yahoofinance.download_daily_bars('egan', 2011, 'egan-2011-yahoofinance.csv')"
python -c "from pyalgotrade.tools import yahoofinance; yahoofinance.download_daily_bars('glng', 2011, 'glng-2011-yahoofinance.csv')"
python -c "from pyalgotrade.tools import yahoofinance; yahoofinance.download_daily_bars('simo', 2011, 'simo-2011-yahoofinance.csv')"
python -m "pyalgotrade.tools.quandl" --source-code="WIKI" --table-code="IBM" --from-year=2011 --to-year=2011 --storage=. --force-download --frequency=daily
python -m "pyalgotrade.tools.quandl" --source-code="WIKI" --table-code="AES" --from-year=2011 --to-year=2011 --storage=. --force-download --frequency=daily
python -m "pyalgotrade.tools.quandl" --source-code="WIKI" --table-code="AIG" --from-year=2011 --to-year=2011 --storage=. --force-download --frequency=daily
python -m "pyalgotrade.tools.quandl" --source-code="WIKI" --table-code="ORCL" --from-year=2011 --to-year=2011 --storage=. --force-download --frequency=daily

Although the deliverable was an Excel spreadsheet, I validated the results using this piece of code:

Expand All @@ -29,53 +29,3 @@ Although the deliverable was an Excel spreadsheet, I validated the results using
The results were:

.. literalinclude:: ../samples/compinv-1.output

Homework 3 and 4
----------------

For these assignments I had to build a market simulation tool that loads orders from a file, executes those,
and prints the results for each day.

The orders file for homework 3 look like this: ::

2011,1,10,AAPL,Buy,1500,
2011,1,13,AAPL,Sell,1500,
2011,1,13,IBM,Buy,4000,
2011,1,26,GOOG,Buy,1000,
2011,2,2,XOM,Sell,4000,
2011,2,10,XOM,Buy,4000,
2011,3,3,GOOG,Sell,1000,
2011,3,3,IBM,Sell,2200,
2011,6,3,IBM,Sell,3300,
2011,5,3,IBM,Buy,1500,
2011,6,10,AAPL,Buy,1200,
2011,8,1,GOOG,Buy,55,
2011,8,1,GOOG,Sell,55,
2011,12,20,AAPL,Sell,1200,

This is the market simulation tool that I built:

.. literalinclude:: ../samples/compinv-3.py

The output for homework 3 looks like this: ::

First date 2011-01-10 00:00:00
Last date 2011-12-20 00:00:00
Symbols ['AAPL', 'IBM', 'GOOG', 'XOM']
2011-01-10 00:00:00: Portfolio value: $1000000.00
2011-01-11 00:00:00: Portfolio value: $998785.00
2011-01-12 00:00:00: Portfolio value: $1002940.00
2011-01-13 00:00:00: Portfolio value: $1004815.00
.
.
.
2011-12-15 00:00:00: Portfolio value: $1113532.00
2011-12-16 00:00:00: Portfolio value: $1116016.00
2011-12-19 00:00:00: Portfolio value: $1117444.00
2011-12-20 00:00:00: Portfolio value: $1133860.00
Final portfolio value: $1133860.00
Anual return: 13.39 %
Average daily return: 0.05 %
Std. dev. daily return: 0.0072
Sharpe ratio: 1.21

10 changes: 5 additions & 5 deletions samples/compinv-1.output
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Final portfolio value: $1604979.11
Anual return: 60.50 %
Average daily return: 0.20 %
Std. dev. daily return: 0.0136
Sharpe ratio: 2.30
Final portfolio value: $876350.83
Anual return: -12.36 %
Average daily return: -0.04 %
Std. dev. daily return: 0.0176
Sharpe ratio: -0.33
22 changes: 11 additions & 11 deletions samples/compinv-1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pyalgotrade import strategy
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.barfeed import quandlfeed
from pyalgotrade.stratanalyzer import returns
from pyalgotrade.stratanalyzer import sharpe
from pyalgotrade.utils import stats
Expand All @@ -14,23 +14,23 @@ def __init__(self, feed):

# Place the orders to get them processed on the first bar.
orders = {
"aeti": 297810,
"egan": 81266,
"glng": 11095,
"simo": 17293,
"ibm": 1996,
"aes": 22565,
"aig": 5445,
"orcl": 8582,
}
for instrument, quantity in orders.items():
self.marketOrder(instrument, quantity, onClose=True, allOrNone=True)

def onBars(self, bars):
pass

# Load the yahoo feed from CSV files.
feed = yahoofeed.Feed()
feed.addBarsFromCSV("aeti", "aeti-2011-yahoofinance.csv")
feed.addBarsFromCSV("egan", "egan-2011-yahoofinance.csv")
feed.addBarsFromCSV("glng", "glng-2011-yahoofinance.csv")
feed.addBarsFromCSV("simo", "simo-2011-yahoofinance.csv")
# Load the bar feed from the CSV file
feed = quandlfeed.Feed()
feed.addBarsFromCSV("ibm", "WIKI-IBM-2011-quandl.csv")
feed.addBarsFromCSV("aes", "WIKI-AES-2011-quandl.csv")
feed.addBarsFromCSV("aig", "WIKI-AIG-2011-quandl.csv")
feed.addBarsFromCSV("orcl", "WIKI-ORCL-2011-quandl.csv")

# Evaluate the strategy with the feed's bars.
myStrategy = MyStrategy(feed)
Expand Down
117 changes: 0 additions & 117 deletions samples/compinv-3.py

This file was deleted.

Loading

0 comments on commit d0af234

Please sign in to comment.