Finish experiment 1 and start with Q trader
This commit is contained in:
@@ -10,6 +10,7 @@ from matplotlib.widgets import MultiCursor
|
||||
from BenchmarkStrategy import BenchmarkStrategy
|
||||
from ManualStrategy import ManualStrategy
|
||||
from StrategyLearner import StrategyLearner
|
||||
from QLearner import QLearner
|
||||
|
||||
|
||||
def plot_indicators(symbol, df):
|
||||
@@ -92,7 +93,42 @@ def compare_manual_strategies(symbol, sv, sd, ed):
|
||||
plt.savefig('figure_1.png', dpi=fig.dpi)
|
||||
|
||||
|
||||
def experiment1():
|
||||
def compare_all_strategies(symbol, sv, sd, ed):
|
||||
df = util.get_data([symbol], pd.date_range(sd, ed))
|
||||
df.drop(columns=["SPY"], inplace=True)
|
||||
normalize = indicators.normalize
|
||||
|
||||
bs = BenchmarkStrategy()
|
||||
orders = bs.testPolicy(symbol, sd, ed, sv)
|
||||
df["Benchmark"] = normalize(marketsim.compute_portvals(orders, sv))
|
||||
df["Orders Benchmark"] = orders["Shares"]
|
||||
|
||||
ms = ManualStrategy()
|
||||
orders = ms.testPolicy(symbol, sd, ed, sv)
|
||||
df["Manual"] = normalize(marketsim.compute_portvals(orders, sv))
|
||||
df["Orders Manual"] = orders["Shares"]
|
||||
|
||||
sl = StrategyLearner(testing=True)
|
||||
sl.addEvidence(symbol, sd, ed, sv)
|
||||
orders = sl.testPolicy(symbol, sd, ed, sv)
|
||||
df["Strategy"] = normalize(marketsim.compute_portvals(orders, sv))
|
||||
df["Orders Strategy"] = orders["Shares"]
|
||||
|
||||
fig, ax = plt.subplots(3, sharex=True)
|
||||
df[[symbol]].plot(ax=ax[0])
|
||||
df[["Benchmark", "Manual", "Strategy"]].plot(ax=ax[1])
|
||||
df[["Orders Benchmark", "Orders Manual", "Orders Strategy"]].plot(ax=ax[2])
|
||||
|
||||
for a in ax:
|
||||
a.grid()
|
||||
MultiCursor(fig.canvas, ax, color='r', lw=0.5)
|
||||
|
||||
# plt.show()
|
||||
fig.set_size_inches(10, 8, forward=True)
|
||||
plt.savefig('figure_2.png', dpi=fig.dpi)
|
||||
|
||||
|
||||
def experiment1(create_report=False):
|
||||
symbol = "JPM"
|
||||
sv = 10000
|
||||
sd = dt.datetime(2008, 1, 1) # in-sample
|
||||
@@ -103,25 +139,29 @@ def experiment1():
|
||||
df = util.get_data([symbol], pd.date_range(sd, ed_out))
|
||||
df.drop(columns=["SPY"], inplace=True)
|
||||
|
||||
if create_report:
|
||||
compare_manual_strategies(symbol, sv, sd, ed)
|
||||
compare_all_strategies(symbol, sv, sd, ed)
|
||||
return
|
||||
|
||||
# visualize_correlations(symbol, df)
|
||||
# plot_indicators(symbol, df)
|
||||
# compare_manual_strategies(symbol, sv, sd, ed)
|
||||
|
||||
bs = BenchmarkStrategy()
|
||||
orders = bs.testPolicy(symbol, sd_out, ed_out, sv)
|
||||
df["Benchmark"] = marketsim.compute_portvals(orders, sv)
|
||||
df["Orders Benchmark"] = orders["Shares"]
|
||||
|
||||
sl = StrategyLearner(testing=True)
|
||||
sl.addEvidence(symbol, sd, ed, sv)
|
||||
orders = sl.testPolicy(symbol, sd_out, ed_out, sv)
|
||||
df["SL"] = marketsim.compute_portvals(orders, sv)
|
||||
df["Orders SL"] = orders["Shares"]
|
||||
ql = QLearner(testing=True)
|
||||
ql.addEvidence(symbol, sd, ed, sv)
|
||||
orders = ql.testPolicy(symbol, sd_out, ed_out, sv)
|
||||
df["QL"] = marketsim.compute_portvals(orders, sv)
|
||||
df["Orders QL"] = orders["Shares"]
|
||||
|
||||
fig, ax = plt.subplots(3, sharex=True)
|
||||
df[[symbol]].plot(ax=ax[0])
|
||||
df[["Benchmark", "SL"]].plot(ax=ax[1])
|
||||
df[["Orders Benchmark", "Orders SL"]].plot(ax=ax[2])
|
||||
df[["Benchmark", "QL"]].plot(ax=ax[1])
|
||||
df[["Orders Benchmark", "Orders QL"]].plot(ax=ax[2])
|
||||
|
||||
for a in ax:
|
||||
a.grid()
|
||||
|
||||
Reference in New Issue
Block a user