Skip to content

Commit

Permalink
retain training in live mode, hyperopt
Browse files Browse the repository at this point in the history
  • Loading branch information
nateemma committed Jan 17, 2024
1 parent f550421 commit 2dd23a5
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 38 deletions.
52 changes: 28 additions & 24 deletions TSPredict/TSPredict.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,39 +210,39 @@ class TSPredict(IStrategy):

# Buy hyperspace params:
buy_params = {
"cexit_min_profit_th": 0.5,
"cexit_profit_nstd": 0.1,
"entry_bb_factor": 1.13,
"entry_bb_width": 0.023,
"entry_guard_metric": -0.2,
"enable_entry_guards": True, # value loaded from strategy
"entry_enable_squeeze": True, # value loaded from strategy
"cexit_min_profit_th": 0.8,
"cexit_profit_nstd": 1.0,
"enable_entry_guards": False,
"entry_bb_factor": 0.93,
"entry_bb_width": 0.08,
"entry_enable_squeeze": False,
"entry_guard_metric": 0.0,
}

# Sell hyperspace params:
sell_params = {
"cexit_loss_nstd": 0.4,
"cexit_metric_overbought": 0.91,
"cexit_metric_take_profit": 0.88,
"cexit_min_loss_th": -0.4,
"exit_bb_factor": 0.8,
"cexit_loss_nstd": 1.2,
"cexit_metric_overbought": 0.8,
"cexit_metric_take_profit": 0.71,
"cexit_min_loss_th": 0.0,
"enable_exit_guards": True,
"enable_exit_signal": False,
"exit_bb_factor": 1.07,
"exit_enable_squeeze": True,
"exit_guard_metric": 0.4,
"enable_exit_guards": True, # value loaded from strategy
"enable_exit_signal": True, # value loaded from strategy
"exit_guard_metric": 0.2,
}

# Entry
enable_entry_guards = CategoricalParameter(
[True, False], default=True, space="buy", load=True, optimize=False
[True, False], default=True, space="buy", load=True, optimize=True
)

entry_guard_metric = DecimalParameter(
-0.8, 0.0, default=-0.6, decimals=1, space="buy", load=True, optimize=True
)

entry_enable_squeeze= CategoricalParameter(
[True, False], default=True, space="buy", load=True, optimize=False
[True, False], default=True, space="buy", load=True, optimize=True
)

entry_bb_width = DecimalParameter(
Expand All @@ -256,7 +256,7 @@ class TSPredict(IStrategy):

# Exit
enable_exit_guards = CategoricalParameter(
[True, False], default=True, space="sell", load=True, optimize=False
[True, False], default=True, space="sell", load=True, optimize=True
)

exit_guard_metric = DecimalParameter(
Expand All @@ -269,7 +269,7 @@ class TSPredict(IStrategy):

# use exit signal? If disabled, just rely on the custom exit checks (or stoploss) to get out
enable_exit_signal = CategoricalParameter(
[True, False], default=True, space="sell", load=True, optimize=False
[True, False], default=True, space="sell", load=True, optimize=True
)

exit_enable_squeeze= CategoricalParameter(
Expand Down Expand Up @@ -387,6 +387,7 @@ def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame
/ dataframe["close"].shift(self.lookahead)
)
dataframe["gain"].fillna(0.0, inplace=True)
dataframe["gain"] = dataframe["gain"].round(4)

# dataframe["gain"] = self.smooth(dataframe["gain"], 8)

Expand Down Expand Up @@ -505,6 +506,9 @@ def smooth(self, y, window):
def get_future_gain(self, dataframe):
df = self.convert_dataframe(dataframe)
future_gain = df["gain"].shift(-self.lookahead).to_numpy()
future_gain[-self.lookahead:] = 0.0
future_gain = np.round(future_gain, decimals=3)
future_gain = np.nan_to_num(future_gain)

# future_gain = dataframe['gain'].shift(-self.lookahead).to_numpy()
# return self.smooth(future_gain, 8)
Expand Down Expand Up @@ -716,7 +720,7 @@ def predict(self, gain, dataframe) -> float:
return 0.0

# train on previous data
train_end = start_row - self.lookahead
train_end = start_row - self.lookahead - 1
train_start = max(0, train_end-self.train_len)
scale_start = max(0, end-self.scale_len)

Expand Down Expand Up @@ -751,7 +755,7 @@ def rolling_predict(self, gain, window_size):
scale_start = max(0, end-self.scale_len)

# train_end = max(0, start - 1)
train_end = min(end - 1, nrows - self.lookahead - 1)
train_end = min(end - 1, nrows - self.lookahead - 2)
train_start = max(0, train_end-self.train_len)


Expand Down Expand Up @@ -789,7 +793,7 @@ def rolling_predict(self, gain, window_size):
end = end + 1
start = start + 1
# train_end = start - 1
train_end = min(end - 1, nrows - self.lookahead - 1)
train_end = min(end - 1, nrows - self.lookahead - 2)
train_start = max(0, train_end-self.train_len)

# save the updated/trained forecaster
Expand Down Expand Up @@ -941,7 +945,7 @@ def add_latest_prediction(self, dataframe: DataFrame) -> DataFrame:

# train on previous data
# train_end = clen - self.model_window - self.lookahead
train_end = np.shape(self.training_data)[0] - self.lookahead - 1
train_end = np.shape(self.training_data)[0] - self.lookahead - 2
train_start = max(0, train_end-self.train_len)

# cannot use last portion because we are looking ahead
Expand Down Expand Up @@ -1012,7 +1016,7 @@ def add_predictions(self, dataframe: DataFrame) -> DataFrame:

if self.curr_pair not in self.custom_trade_info:
self.custom_trade_info[self.curr_pair] = {
'forecaster': copy.deepcopy(self.forecaster),
'forecaster': None,
"initialised": False,
"predictions": None,
"curr_prediction": 0.0,
Expand Down
44 changes: 44 additions & 0 deletions TSPredict/TS_Coeff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"strategy_name": "TS_Coeff",
"params": {
"roi": {
"0": 0.04,
"100": 0.02
},
"stoploss": {
"stoploss": -0.1
},
"trailing": {
"trailing_stop": false,
"trailing_stop_positive": null,
"trailing_stop_positive_offset": 0.0,
"trailing_only_offset_is_reached": false
},
"max_open_trades": {
"max_open_trades": 5
},
"buy": {
"cexit_min_profit_th": 0.8,
"cexit_profit_nstd": 1.0,
"enable_entry_guards": false,
"entry_bb_factor": 0.93,
"entry_bb_width": 0.08,
"entry_enable_squeeze": false,
"entry_guard_metric": 0.0
},
"sell": {
"cexit_loss_nstd": 1.2,
"cexit_metric_overbought": 0.8,
"cexit_metric_take_profit": 0.71,
"cexit_min_loss_th": 0.0,
"enable_exit_guards": true,
"enable_exit_signal": false,
"exit_bb_factor": 1.07,
"exit_enable_squeeze": true,
"exit_guard_metric": 0.2
},
"protection": {}
},
"ft_stratparam_v": 1,
"export_time": "2024-01-17 06:37:43.042079+00:00"
}
15 changes: 1 addition & 14 deletions TSPredict/TS_Wavelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ class TS_Wavelet(TSPredict):
# curr_dataframe = None

norm_data = False # must be false for these strategies
detrend_data = False
scale_results = False
single_col_prediction = False
merge_indicators = False
training_required = True
Expand Down Expand Up @@ -246,7 +244,6 @@ def build_coefficient_table(self, start, end):

if self.forecaster is None:
self.forecaster = Forecasters.make_forecaster(self.forecaster_type)
self.forecaster.set_detrend(self.detrend_data)

# if forecaster does not require pre-training, then just set training length to 0
if not self.forecaster.requires_pretraining():
Expand Down Expand Up @@ -351,7 +348,7 @@ def predict_data(self, predict_start, predict_end):

# train on previous data
# train_end = max(self.train_min_len, predict_start-1)
train_end = min(predict_end - 1, nrows - self.lookahead - 1)
train_end = min(predict_end - 1, nrows - self.lookahead - 2)
train_start = max(0, train_end-self.train_max_len)
results_start = train_start + self.lookahead
results_end = train_end + self.lookahead
Expand Down Expand Up @@ -413,11 +410,6 @@ def predict_data(self, predict_start, predict_end):
coeffs = self.wavelet.array_to_coeff(c_array)
preds = self.wavelet.get_values(coeffs)

# rescale if necessary
if self.scale_results:
preds = self.denorm_array(preds)


# print(f'preds[{start}:{end}] len:{len(preds)}: {preds}')
# print(f'preds[{end}]: {preds[-1]}')
# print('===========================')
Expand Down Expand Up @@ -483,11 +475,6 @@ def rolling_predict(self, data):
self.update_scaler(np.array(data)[scale_start:scale_end])

forecast = self.predict_data(start, end)
# if start < window_size:
# flen = len(forecast)
# preds[end-flen:end] = forecast
# else:
# preds[end] = forecast[-1]
preds[end] = forecast[-1]

# print(f'forecast: {forecast}')
Expand Down
Binary file added TSPredict/models/TS_Coeff/TS_Coeff.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_DWTA/TS_Coeff_DWTA.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_FFT/TS_Coeff_FFT.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_FHT/TS_Coeff_FHT.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_MODWT/TS_Coeff_MODWT.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_SWT/TS_Coeff_SWT.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Coeff_WPT/TS_Coeff_WPT.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Gain/TS_Gain.sav
Binary file not shown.
Binary file modified TSPredict/models/TS_Simple/TS_Simple.sav
Binary file not shown.

0 comments on commit 2dd23a5

Please sign in to comment.