1
0
Fork 0

Parse orders and extract information (start/end date and symbols)

master
Felix Martin 2020-10-08 21:28:12 -04:00
parent 0429c497c7
commit 117d97b8e9
1 changed files with 126 additions and 91 deletions

View File

@ -21,8 +21,8 @@ GT honor code violation.
-----do not edit anything above this line---
Student Name: Tucker Balch (replace with your name)
GT User ID: tb34 (replace with your User ID)
GT ID: 900897987 (replace with your GT ID)
GT User ID: felixm (replace with your User ID)
GT ID: 1337 (replace with your GT ID)
"""
import pandas as pd
@ -30,47 +30,81 @@ import numpy as np
import datetime as dt
import os
from util import get_data, plot_data
from optimize_something.optimization import calculate_stats
def compute_portvals(orders_file = "./orders/orders.csv", start_val = 1000000, commission=9.95, impact=0.005):
# this is the function the autograder will call to test your code
# NOTE: orders_file may be a string, or it may be a file object. Your
# code should work correctly with either input
# TODO: Your code here
def read_orders(orders_file):
"""
Parser orders into the form:
Date datetime64[ns]
Symbol object
Order object
Shares int32
This is how the order book looks like:
Date,Symbol,Order,Shares
2011-01-10,AAPL,BUY,1500
2011-01-10,AAPL,SELL,1500
"""
orders = pd.read_csv(orders_file,
dtype='|str, str, str, i4',
parse_dates=["Date"])
orders.sort_values(by="Date", inplace=True)
return orders
def get_order_book_info(orders):
"""Return start_date, end_date, and symbols (as a list)."""
start_date = orders.iloc[0].Date
end_date = orders.iloc[0].Date
symbols = sorted(list((set(orders.Symbol.tolist()))))
return start_date, end_date, symbols
def compute_portvals(orders_file="./orders/orders-01.csv", start_val=1000000, commission=9.95, impact=0.005):
orders = read_orders(orders_file)
start_date, end_date, symbols = get_order_book_info(orders)
# In the template, instead of computing the value of the portfolio, we just
# read in the value of IBM over 6 months
start_date = dt.datetime(2008,1,1)
end_date = dt.datetime(2008,6,1)
start_date = dt.datetime(2008, 1, 1)
end_date = dt.datetime(2008, 6, 1)
portvals = get_data(['IBM'], pd.date_range(start_date, end_date))
portvals = portvals[['IBM']] # remove SPY
rv = pd.DataFrame(index=portvals.index, data=portvals.values)
return rv
return portvals
# Don't know why this was in template. Keep for now.
# rv = pd.DataFrame(index=portvals.index, data=portvals.values)
# return rv
def test_code():
# this is a helper function you can use to test your code
# note that during autograding his function will not be called.
# Define input parameters
of = "./orders/orders2.csv"
of = "./orders/orders-02.csv"
sv = 1000000
# Process orders
portvals = compute_portvals(orders_file = of, start_val = sv)
portvals = compute_portvals(orders_file=of, start_val=sv)
if isinstance(portvals, pd.DataFrame):
portvals = portvals[portvals.columns[0]] # just get the first column
portvals = portvals[portvals.columns[0]] # just get the first column
else:
"warning, code did not return a DataFrame"
raise Exception("warning, code did not return a DataFrame")
# Get portfolio stats
# Here we just fake the data. you should use your code from previous assignments.
start_date = dt.datetime(2008,1,1)
end_date = dt.datetime(2008,6,1)
cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = [0.2,0.01,0.02,1.5]
cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [0.2,0.01,0.02,1.5]
# One way of getting the portfolio dates
# print(portvals.index[0])
# Compare portfolio against $SPX
# Get portfolio stats.
start_date = dt.datetime(2008, 1, 1)
end_date = dt.datetime(2008, 6, 1)
cum_ret, avg_daily_ret, \
std_daily_ret, sharpe_ratio = calculate_stats(portvals.to_frame(), [1])
spy = get_data(['SPY'], pd.date_range(start_date, end_date))
cum_ret_SPY, avg_daily_ret_SPY, \
std_daily_ret_SPY, sharpe_ratio_SPY = calculate_stats(spy, [1])
# Compare portfolio against $SPY
print(f"Date Range: {start_date} to {end_date}")
print()
print(f"Sharpe Ratio of Fund: {sharpe_ratio}")
@ -87,5 +121,6 @@ def test_code():
print()
print(f"Final Portfolio Value: {portvals[-1]}")
if __name__ == "__main__":
test_code()