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

@ -1,91 +1,126 @@
"""MC2-P1: Market simulator.
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
Atlanta, Georgia 30332
All Rights Reserved
Template code for CS 4646/7646
Georgia Tech asserts copyright ownership of this template and all derivative
works, including solutions to the projects assigned in this course. Students
and other users of this template code are advised not to share it with others
or to make it available on publicly viewable websites including repositories
such as github and gitlab. This copyright statement should not be removed
or edited.
We do grant permission to share solutions privately with non-students such
as potential employers. However, sharing with other current or future
students of CS 7646 is prohibited and subject to being investigated as a
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)
"""
import pandas as pd
import numpy as np
import datetime as dt
import os
from util import get_data, plot_data
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
# 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)
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
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"
sv = 1000000
# Process orders
portvals = compute_portvals(orders_file = of, start_val = sv)
if isinstance(portvals, pd.DataFrame):
portvals = portvals[portvals.columns[0]] # just get the first column
else:
"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]
# Compare portfolio against $SPX
print(f"Date Range: {start_date} to {end_date}")
print()
print(f"Sharpe Ratio of Fund: {sharpe_ratio}")
print(f"Sharpe Ratio of SPY : {sharpe_ratio_SPY}")
print()
print(f"Cumulative Return of Fund: {cum_ret}")
print(f"Cumulative Return of SPY : {cum_ret_SPY}")
print()
print(f"Standard Deviation of Fund: {std_daily_ret}")
print(f"Standard Deviation of SPY : {std_daily_ret_SPY}")
print()
print(f"Average Daily Return of Fund: {avg_daily_ret}")
print(f"Average Daily Return of SPY : {avg_daily_ret_SPY}")
print()
print(f"Final Portfolio Value: {portvals[-1]}")
if __name__ == "__main__":
test_code()
"""MC2-P1: Market simulator.
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
Atlanta, Georgia 30332
All Rights Reserved
Template code for CS 4646/7646
Georgia Tech asserts copyright ownership of this template and all derivative
works, including solutions to the projects assigned in this course. Students
and other users of this template code are advised not to share it with others
or to make it available on publicly viewable websites including repositories
such as github and gitlab. This copyright statement should not be removed
or edited.
We do grant permission to share solutions privately with non-students such
as potential employers. However, sharing with other current or future
students of CS 7646 is prohibited and subject to being investigated as a
GT honor code violation.
-----do not edit anything above this line---
Student Name: Tucker Balch (replace with your name)
GT User ID: felixm (replace with your User ID)
GT ID: 1337 (replace with your GT ID)
"""
import pandas as pd
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 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)
portvals = get_data(['IBM'], pd.date_range(start_date, end_date))
portvals = portvals[['IBM']] # remove SPY
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():
of = "./orders/orders-02.csv"
sv = 1000000
portvals = compute_portvals(orders_file=of, start_val=sv)
if isinstance(portvals, pd.DataFrame):
portvals = portvals[portvals.columns[0]] # just get the first column
else:
raise Exception("warning, code did not return a DataFrame")
# One way of getting the portfolio dates
# print(portvals.index[0])
# 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}")
print(f"Sharpe Ratio of SPY : {sharpe_ratio_SPY}")
print()
print(f"Cumulative Return of Fund: {cum_ret}")
print(f"Cumulative Return of SPY : {cum_ret_SPY}")
print()
print(f"Standard Deviation of Fund: {std_daily_ret}")
print(f"Standard Deviation of SPY : {std_daily_ret_SPY}")
print()
print(f"Average Daily Return of Fund: {avg_daily_ret}")
print(f"Average Daily Return of SPY : {avg_daily_ret_SPY}")
print()
print(f"Final Portfolio Value: {portvals[-1]}")
if __name__ == "__main__":
test_code()