Parse orders and extract information (start/end date and symbols)
parent
0429c497c7
commit
117d97b8e9
|
@ -1,91 +1,126 @@
|
||||||
"""MC2-P1: Market simulator.
|
"""MC2-P1: Market simulator.
|
||||||
|
|
||||||
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
|
Copyright 2018, Georgia Institute of Technology (Georgia Tech)
|
||||||
Atlanta, Georgia 30332
|
Atlanta, Georgia 30332
|
||||||
All Rights Reserved
|
All Rights Reserved
|
||||||
|
|
||||||
Template code for CS 4646/7646
|
Template code for CS 4646/7646
|
||||||
|
|
||||||
Georgia Tech asserts copyright ownership of this template and all derivative
|
Georgia Tech asserts copyright ownership of this template and all derivative
|
||||||
works, including solutions to the projects assigned in this course. Students
|
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
|
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
|
or to make it available on publicly viewable websites including repositories
|
||||||
such as github and gitlab. This copyright statement should not be removed
|
such as github and gitlab. This copyright statement should not be removed
|
||||||
or edited.
|
or edited.
|
||||||
|
|
||||||
We do grant permission to share solutions privately with non-students such
|
We do grant permission to share solutions privately with non-students such
|
||||||
as potential employers. However, sharing with other current or future
|
as potential employers. However, sharing with other current or future
|
||||||
students of CS 7646 is prohibited and subject to being investigated as a
|
students of CS 7646 is prohibited and subject to being investigated as a
|
||||||
GT honor code violation.
|
GT honor code violation.
|
||||||
|
|
||||||
-----do not edit anything above this line---
|
-----do not edit anything above this line---
|
||||||
|
|
||||||
Student Name: Tucker Balch (replace with your name)
|
Student Name: Tucker Balch (replace with your name)
|
||||||
GT User ID: tb34 (replace with your User ID)
|
GT User ID: felixm (replace with your User ID)
|
||||||
GT ID: 900897987 (replace with your GT ID)
|
GT ID: 1337 (replace with your GT ID)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import os
|
import os
|
||||||
from util import get_data, plot_data
|
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
|
def read_orders(orders_file):
|
||||||
# NOTE: orders_file may be a string, or it may be a file object. Your
|
"""
|
||||||
# code should work correctly with either input
|
Parser orders into the form:
|
||||||
# TODO: Your code here
|
|
||||||
|
Date datetime64[ns]
|
||||||
# In the template, instead of computing the value of the portfolio, we just
|
Symbol object
|
||||||
# read in the value of IBM over 6 months
|
Order object
|
||||||
start_date = dt.datetime(2008,1,1)
|
Shares int32
|
||||||
end_date = dt.datetime(2008,6,1)
|
|
||||||
portvals = get_data(['IBM'], pd.date_range(start_date, end_date))
|
This is how the order book looks like:
|
||||||
portvals = portvals[['IBM']] # remove SPY
|
|
||||||
rv = pd.DataFrame(index=portvals.index, data=portvals.values)
|
Date,Symbol,Order,Shares
|
||||||
|
2011-01-10,AAPL,BUY,1500
|
||||||
return rv
|
2011-01-10,AAPL,SELL,1500
|
||||||
return portvals
|
"""
|
||||||
|
orders = pd.read_csv(orders_file,
|
||||||
def test_code():
|
dtype='|str, str, str, i4',
|
||||||
# this is a helper function you can use to test your code
|
parse_dates=["Date"])
|
||||||
# note that during autograding his function will not be called.
|
orders.sort_values(by="Date", inplace=True)
|
||||||
# Define input parameters
|
return orders
|
||||||
|
|
||||||
of = "./orders/orders2.csv"
|
|
||||||
sv = 1000000
|
def get_order_book_info(orders):
|
||||||
|
"""Return start_date, end_date, and symbols (as a list)."""
|
||||||
# Process orders
|
start_date = orders.iloc[0].Date
|
||||||
portvals = compute_portvals(orders_file = of, start_val = sv)
|
end_date = orders.iloc[0].Date
|
||||||
if isinstance(portvals, pd.DataFrame):
|
symbols = sorted(list((set(orders.Symbol.tolist()))))
|
||||||
portvals = portvals[portvals.columns[0]] # just get the first column
|
return start_date, end_date, symbols
|
||||||
else:
|
|
||||||
"warning, code did not return a DataFrame"
|
|
||||||
|
def compute_portvals(orders_file="./orders/orders-01.csv", start_val=1000000, commission=9.95, impact=0.005):
|
||||||
# Get portfolio stats
|
orders = read_orders(orders_file)
|
||||||
# Here we just fake the data. you should use your code from previous assignments.
|
start_date, end_date, symbols = get_order_book_info(orders)
|
||||||
start_date = dt.datetime(2008,1,1)
|
|
||||||
end_date = dt.datetime(2008,6,1)
|
# In the template, instead of computing the value of the portfolio, we just
|
||||||
cum_ret, avg_daily_ret, std_daily_ret, sharpe_ratio = [0.2,0.01,0.02,1.5]
|
# read in the value of IBM over 6 months
|
||||||
cum_ret_SPY, avg_daily_ret_SPY, std_daily_ret_SPY, sharpe_ratio_SPY = [0.2,0.01,0.02,1.5]
|
start_date = dt.datetime(2008, 1, 1)
|
||||||
|
end_date = dt.datetime(2008, 6, 1)
|
||||||
# Compare portfolio against $SPX
|
portvals = get_data(['IBM'], pd.date_range(start_date, end_date))
|
||||||
print(f"Date Range: {start_date} to {end_date}")
|
portvals = portvals[['IBM']] # remove SPY
|
||||||
print()
|
|
||||||
print(f"Sharpe Ratio of Fund: {sharpe_ratio}")
|
return portvals
|
||||||
print(f"Sharpe Ratio of SPY : {sharpe_ratio_SPY}")
|
# Don't know why this was in template. Keep for now.
|
||||||
print()
|
# rv = pd.DataFrame(index=portvals.index, data=portvals.values)
|
||||||
print(f"Cumulative Return of Fund: {cum_ret}")
|
# return rv
|
||||||
print(f"Cumulative Return of SPY : {cum_ret_SPY}")
|
|
||||||
print()
|
|
||||||
print(f"Standard Deviation of Fund: {std_daily_ret}")
|
def test_code():
|
||||||
print(f"Standard Deviation of SPY : {std_daily_ret_SPY}")
|
of = "./orders/orders-02.csv"
|
||||||
print()
|
sv = 1000000
|
||||||
print(f"Average Daily Return of Fund: {avg_daily_ret}")
|
|
||||||
print(f"Average Daily Return of SPY : {avg_daily_ret_SPY}")
|
portvals = compute_portvals(orders_file=of, start_val=sv)
|
||||||
print()
|
if isinstance(portvals, pd.DataFrame):
|
||||||
print(f"Final Portfolio Value: {portvals[-1]}")
|
portvals = portvals[portvals.columns[0]] # just get the first column
|
||||||
|
else:
|
||||||
if __name__ == "__main__":
|
raise Exception("warning, code did not return a DataFrame")
|
||||||
test_code()
|
|
||||||
|
# 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()
|
||||||
|
|
Loading…
Reference in New Issue