-
Notifications
You must be signed in to change notification settings - Fork 2
/
NewBacktest.py
187 lines (154 loc) · 5.6 KB
/
NewBacktest.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from __future__ import annotations
import typing as tp
import numpy as np
import pandas as pd
from catboost import CatBoostRegressor
import lightgbm
from lightgbm import LGBMRegressor
from .SimpleDataset import SimpleDataset
from .IfInference import IfInference
from backtesting import Backtest
from backtesting.lib import SignalStrategy, TrailingStrategy
class NewBacktest:
@staticmethod
def to_backtest_format(tradestats: pd.DataFrame, timestamp: str) -> pd.DataFrame:
df = pd.DataFrame(
{'Open': tradestats['open'],
'High': tradestats['high'],
'Low': tradestats['low'],
'Close': tradestats['close'],
'Volume': tradestats['volume']})
# WARNING! KILL THAT WITH FIRE!
tradestats["date"] = tradestats["date"].dt.round("min")
df.index = tradestats['date']
return df
@staticmethod
def create_strategy_class(
signals: np.ndarray,
part_of_sum_for_buy: float,
start_sum: float,
perc_for_stop: float,
percent_trailing: float = 6,
):
class MyStrategy(SignalStrategy, TrailingStrategy):
def init(self):
super().init()
self.signal = signals
self.part_of_sum_for_buy = part_of_sum_for_buy
self.percent = percent_trailing
self.start_sum = start_sum
self.perc_for_stop = perc_for_stop
entry_size = self.signal * self.part_of_sum_for_buy
self.set_signal(entry_size=entry_size)
self.set_trailing_sl(self.percent)
def next(self):
if self.equity >= self.start_sum * self.perc_for_stop:
for trade in self.trades:
self.position.close()
self.buying_power = 0
else:
super().next()
return MyStrategy
@staticmethod
def get_preds_model(
ticker: str,
timestamp: str,
model_features: tp.Dict[str, tp.Any],
model_id: str,
candles: int = 1_000,
date_col: str = "date",
target_col: str = "target",
notebook: bool = False,
seed: int = 42,
) -> tp.Tuple[pd.DataFrame, np.ndarray]:
model_name = model_features["model"]
model_path = f"{model_id}_{ticker}_{timestamp}_{model_name}.bin"
test_data = SimpleDataset.create_dataset(
model_features,
ticker=ticker,
timeframe=timestamp,
candles=candles + 1_000,
notebook=notebook,
)
test_data = test_data.tail(candles)
df_return = test_data.copy()
test_data = test_data.drop(columns=[date_col, target_col])
if model_features["model"] == "catboost":
model = CatBoostRegressor(
eval_metric="RMSE",
random_seed=seed,
)
model.load_model(model_path)
preds = model.predict(test_data)
if model_features["model"] == "lightgbm":
model = lightgbm.Booster(model_file=model_path)
preds = model.predict(test_data)
signals = (preds > np.quantile(preds, 0.75)) * 1
return df_return, signals
def __init__(
self,
type: tp.Literal["if_model", "ml_model"],
model_id: str | None,
ticker: str,
timestamp: tp.Literal["1m", "10m", "60m"],
part_of_sum_for_buy: float,
percent_trailing: float,
start_sum: float,
perc_for_stop: float,
model_features: tp.Dict[str, tp.Any] | None = None,
IF_features: dict[str, tp.Any] | None = None,
notebook: bool = False,
) -> None:
self.model_id = model_id
self.ticker = ticker
self.timestamp = timestamp
self.PART_OF_SUM_FOR_BUY = part_of_sum_for_buy
self.PERCENT_TRAILING = percent_trailing
self.START_SUM = start_sum
self.PERC_FOR_STOP = perc_for_stop
self.notebook = notebook
self.type = type
self.model_features = model_features
self.IF_features = IF_features
def do_backtest(
self,
candles: int = 1_000,
my_comission: float = 0.00003,
html_save_path="graph.html",
):
if self.type == "if_model":
if_model = IfInference(
IF_features=self.IF_features,
ticker=self.ticker,
timestamp=self.timestamp,
notebook=self.notebook,
)
dataset, self.SIGNALS = if_model.predict_n_last_candles(candles=candles)
self.SIGNALS[self.SIGNALS == 0] = -1
elif self.type == "ml_model":
dataset, self.SIGNALS = NewBacktest.get_preds_model(
ticker=self.ticker,
model_id=self.model_id,
timestamp=self.timestamp,
model_features=self.model_features,
candles=candles,
notebook=self.notebook,
)
self.SIGNALS[self.SIGNALS == 0] = -1
dataset = NewBacktest.to_backtest_format(dataset, timestamp=self.timestamp)
strategy = NewBacktest.create_strategy_class(
self.SIGNALS,
self.PART_OF_SUM_FOR_BUY,
self.START_SUM,
self.PERC_FOR_STOP,
self.PERCENT_TRAILING,
)
bt = Backtest(
dataset,
strategy=strategy,
cash=self.START_SUM,
commission=my_comission,
)
stats = bt.run()
bt.plot(filename=html_save_path, open_browser=False)
return stats