-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
76 lines (58 loc) · 5.65 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""MLT: Utility code.
Copyright 2017, Georgia Tech Research Corporation
Atlanta, Georgia 30332-0415
All Rights Reserved
"""
import os
import pandas as pd
def symbol_to_path(symbol, base_dir=None):
"""Return CSV file path given ticker symbol."""
if base_dir is None:
base_dir = os.environ.get("MARKET_DATA_DIR", "../data/")
return os.path.join(base_dir, "{}.csv".format(str(symbol)))
def get_data(symbols, dates, addSPY=True, colname="Adj Close"):
"""Read stock data (adjusted close) for given symbols from CSV files."""
df = pd.DataFrame(index=dates)
if addSPY and "SPY" not in symbols: # add SPY for reference, if absent
symbols = ["SPY"] + list(
symbols
) # handles the case where symbols is np array of 'object'
for symbol in symbols:
df_temp = pd.read_csv(
"data/" + symbol_to_path(symbol),
index_col="Date",
parse_dates=True,
usecols=["Date", colname],
na_values=["nan"],
)
df_temp = df_temp.rename(columns={colname: symbol})
df = df.join(df_temp)
if symbol == "SPY": # drop dates SPY did not trade
df = df.dropna(subset=["SPY"])
return df
def plot_data(df, title="Stock prices", xlabel="Date", ylabel="Price"):
import matplotlib.pyplot as plt
"""Plot stock prices with a custom title and meaningful axis labels."""
ax = df.plot(title=title, fontsize=12)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
plt.show()
def get_orders_data_file(basefilename):
return open(
os.path.join(
os.environ.get("ORDERS_DATA_DIR", "orders/"), basefilename
)
)
def get_learner_data_file(basefilename):
return open(
os.path.join(
os.environ.get("LEARNER_DATA_DIR", "Data/"), basefilename
),
"r",
)
def get_robot_world_file(basefilename):
return open(
os.path.join(
os.environ.get("ROBOT_WORLDS_DIR", "testworlds/"), basefilename
)
)