forked from AI4Finance-Foundation/FinRL-Meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
121 lines (101 loc) · 4 KB
/
plot.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from copy import deepcopy
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pyfolio
from finrl.config import TRAIN_START_DATE
from finrl.meta.preprocessor.yahoodownloader import YahooDownloader
from pyfolio import timeseries
from meta import config
def get_daily_return(df, value_col_name="account_value"):
df = deepcopy(df)
df["daily_return"] = df[value_col_name].pct_change(1)
df["date"] = pd.to_datetime(df["date"])
df.set_index("date", inplace=True, drop=True)
df.index = df.index.tz_localize("UTC")
return pd.Series(df["daily_return"], index=df.index)
def convert_daily_return_to_pyfolio_ts(df):
strategy_ret = df.copy()
strategy_ret["date"] = pd.to_datetime(strategy_ret["date"])
strategy_ret.set_index("date", drop=False, inplace=True)
strategy_ret.index = strategy_ret.index.tz_localize("UTC")
del strategy_ret["date"]
return pd.Series(strategy_ret["daily_return"].values, index=strategy_ret.index)
def backtest_stats(account_value, value_col_name="account_value"):
dr_test = get_daily_return(account_value, value_col_name=value_col_name)
perf_stats_all = timeseries.perf_stats(
returns=dr_test,
positions=None,
transactions=None,
turnover_denom="AGB",
)
print(perf_stats_all)
return perf_stats_all
def backtest_plot(
account_value,
baseline_start=config.TRADE_START_DATE,
baseline_end=config.TRADE_END_DATE,
baseline_ticker="^DJI",
value_col_name="account_value",
):
df = deepcopy(account_value)
df["date"] = pd.to_datetime(df["date"])
test_returns = get_daily_return(df, value_col_name=value_col_name)
baseline_df = get_baseline(
ticker=baseline_ticker, start=baseline_start, end=baseline_end
)
baseline_df["date"] = pd.to_datetime(baseline_df["date"], format="%Y-%m-%d")
baseline_df = pd.merge(df[["date"]], baseline_df, how="left", on="date")
baseline_df = baseline_df.fillna(method="ffill").fillna(method="bfill")
baseline_returns = get_daily_return(baseline_df, value_col_name="close")
with pyfolio.plotting.plotting_context(font_scale=1.1):
pyfolio.create_full_tear_sheet(
returns=test_returns,
benchmark_rets=baseline_returns,
set_context=False,
)
def get_baseline(ticker, start, end):
return YahooDownloader(
start_date=start, end_date=end, ticker_list=[ticker]
).fetch_data()
def trx_plot(df_trade, df_actions, ticker_list):
df_trx = pd.DataFrame(np.array(df_actions["transactions"].to_list()))
df_trx.columns = ticker_list
df_trx.index = df_actions["date"]
df_trx.index.name = ""
for i in range(df_trx.shape[1]):
df_trx_temp = df_trx.iloc[:, i]
df_trx_temp_sign = np.sign(df_trx_temp)
buying_signal = df_trx_temp_sign.apply(lambda x: x > 0)
selling_signal = df_trx_temp_sign.apply(lambda x: x < 0)
tic_plot = df_trade[
(df_trade["tic"] == df_trx_temp.name)
& (df_trade["date"].isin(df_trx.index))
]["close"]
tic_plot.index = df_trx_temp.index
plt.figure(figsize=(10, 8))
plt.plot(tic_plot, color="g", lw=2.0)
plt.plot(
tic_plot,
"^",
markersize=10,
color="m",
label="buying signal",
markevery=buying_signal,
)
plt.plot(
tic_plot,
"v",
markersize=10,
color="k",
label="selling signal",
markevery=selling_signal,
)
plt.title(
f"{df_trx_temp.name} Num Transactions: {len(buying_signal[buying_signal == True]) + len(selling_signal[selling_signal == True])}"
)
plt.legend()
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=25))
plt.xticks(rotation=45, ha="right")
plt.show()