Zeus_8_3_2_B_4_2 optimisation adjust_position

This commit is contained in:
Jérôme Delacotte
2025-04-28 09:08:40 +02:00
parent fef1220183
commit 22bd0d57de

View File

@@ -130,81 +130,10 @@ class Zeus_8_3_2_B_4_2(IStrategy):
trades = list() trades = list()
max_profit_pairs = {} max_profit_pairs = {}
profit_b_no_change = BooleanParameter(default=True, space="sell")
profit_b_quick_lost = BooleanParameter(default=True, space="sell")
profit_b_sma5 = BooleanParameter(default=True, space="sell")
profit_b_sma10 = BooleanParameter(default=True, space="sell")
profit_b_sma20 = BooleanParameter(default=True, space="sell")
profit_b_quick_gain = BooleanParameter(default=True, space="sell")
profit_b_quick_gain_3 = BooleanParameter(default=True, space="sell")
profit_b_old_sma10 = BooleanParameter(default=True, space="sell")
profit_b_very_old_sma10 = BooleanParameter(default=True, space="sell")
profit_b_over_rsi = BooleanParameter(default=True, space="sell")
profit_b_short_loss = BooleanParameter(default=True, space="sell")
sell_b_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_b_percent3 = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_b_candels = IntParameter(0, 48, default=12, space='sell')
sell_b_too_old_day = IntParameter(0, 10, default=300, space='sell')
sell_b_too_old_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_b_profit_no_change = DecimalParameter(0, 0.02, decimals=3, default=0.005, space='sell')
sell_b_profit_percent12 = DecimalParameter(0, 0.002, decimals=4, default=0.001, space='sell')
sell_b_RSI = IntParameter(70, 98, default=88, space='sell')
sell_b_RSI2 = IntParameter(70, 98, default=88, space='sell')
sell_b_RSI3 = IntParameter(70, 98, default=80, space='sell')
sell_b_RSI2_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
# sell_b_expected_profit = DecimalParameter(0, 0.01, decimals=3, default=0.01, space='sell')
profit_h_no_change = BooleanParameter(default=True, space="sell")
profit_h_quick_lost = BooleanParameter(default=True, space="sell")
profit_h_sma5 = BooleanParameter(default=True, space="sell")
profit_h_sma10 = BooleanParameter(default=True, space="sell")
profit_h_sma20 = BooleanParameter(default=True, space="sell")
profit_h_quick_gain = BooleanParameter(default=True, space="sell")
profit_h_quick_gain_3 = BooleanParameter(default=True, space="sell")
profit_h_old_sma10 = BooleanParameter(default=True, space="sell")
profit_h_very_old_sma10 = BooleanParameter(default=True, space="sell")
profit_h_over_rsi = BooleanParameter(default=True, space="sell")
profit_h_short_loss = BooleanParameter(default=True, space="sell")
sell_h_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_h_percent3 = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_h_candels = IntParameter(0, 48, default=12, space='sell')
sell_h_too_old_day = IntParameter(0, 10, default=300, space='sell')
sell_h_too_old_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
sell_h_profit_no_change = DecimalParameter(0, 0.02, decimals=3, default=0.005, space='sell')
sell_h_profit_percent12 = DecimalParameter(0, 0.002, decimals=4, default=0.001, space='sell')
sell_h_RSI = IntParameter(70, 98, default=88, space='sell')
sell_h_RSI2 = IntParameter(70, 98, default=88, space='sell')
sell_h_RSI3 = IntParameter(70, 98, default=80, space='sell')
sell_h_RSI2_percent = DecimalParameter(0, 0.02, decimals=3, default=0.01, space='sell')
protection_percent_buy_lost = IntParameter(1, 10, default=5, space='protection') protection_percent_buy_lost = IntParameter(1, 10, default=5, space='protection')
# protection_nb_buy_lost = IntParameter(1, 2, default=2, space='protection')
protection_fibo = IntParameter(1, 10, default=2, space='protection') protection_fibo = IntParameter(1, 10, default=2, space='protection')
# trailing stoploss hyperopt parameters
# hard stoploss profit
sell_allow_decrease = DecimalParameter(0.005, 0.02, default=0.2, decimals=2, space='sell', optimize=True, load=True) sell_allow_decrease = DecimalParameter(0.005, 0.02, default=0.2, decimals=2, space='sell', optimize=True, load=True)
# pHSL = DecimalParameter(-0.200, -0.040, default=-0.08, decimals=3, space='sell', optimize=False, load=True)
# # profit threshold 1, trigger point, SL_1 is used
# pPF_1 = DecimalParameter(0.008, 0.020, default=0.016, decimals=3, space='sell', optimize=True, load=True)
# pSL_1 = DecimalParameter(0.008, 0.020, default=0.011, decimals=3, space='sell', optimize=True, load=True)
#
# # profit threshold 2, SL_2 is used
# pPF_2 = DecimalParameter(0.040, 0.100, default=0.080, decimals=3, space='sell', optimize=True, load=True)
# pSL_2 = DecimalParameter(0.020, 0.070, default=0.040, decimals=3, space='sell', optimize=True, load=True)
def min_max_scaling(self, series: pd.Series) -> pd.Series: def min_max_scaling(self, series: pd.Series) -> pd.Series:
"""Normaliser les données en les ramenant entre 0 et 100.""" """Normaliser les données en les ramenant entre 0 et 100."""
return 100 * (series - series.min()) / (series.max() - series.min()) return 100 * (series - series.min()) / (series.max() - series.min())
@@ -269,7 +198,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
if allow_to_sell: if allow_to_sell:
self.trades = list() self.trades = list()
self.pairs[pair]['last_count_of_buys'] = self.pairs[pair]['count_of_buys'] self.pairs[pair]['last_count_of_buys'] = trade.nr_of_successful_entries #self.pairs[pair]['count_of_buys']
self.pairs[pair]['last_sell'] = rate self.pairs[pair]['last_sell'] = rate
self.pairs[pair]['last_trade'] = trade self.pairs[pair]['last_trade'] = trade
self.pairs[pair]['last_candle'] = last_candle self.pairs[pair]['last_candle'] = last_candle
@@ -329,7 +258,8 @@ class Zeus_8_3_2_B_4_2(IStrategy):
self.trades = list() self.trades = list()
return 'profit_' + str(count_of_buys) return 'profit_' + str(count_of_buys)
if (current_profit >= expected_profit) & (last_candle['percent'] < 0.0) \ if (current_profit >= expected_profit) & (last_candle['percent'] < 0.0) \
and ((last_candle['rsi'] >= 75) or before_last_candle['rsi'] >= 75): and ((last_candle['rsi'] >= 75) or before_last_candle['rsi'] >= 75)\
and (count_of_buys < 5):
self.trades = list() self.trades = list()
return 'rsi_' + str(count_of_buys) return 'rsi_' + str(count_of_buys)
@@ -350,6 +280,32 @@ class Zeus_8_3_2_B_4_2(IStrategy):
return informative_pairs return informative_pairs
from typing import List
def multi_step_interpolate(self, pct: float, thresholds: List[float], factors: List[float]) -> float:
if pct <= thresholds[0]:
return factors[0]
if pct >= thresholds[-1]:
return factors[-1]
for i in range(1, len(thresholds)):
if pct <= thresholds[i]:
# interpolation linéaire entre thresholds[i-1] et thresholds[i]
return factors[i - 1] + (pct - thresholds[i - 1]) * (factors[i] - factors[i - 1]) / (
thresholds[i] - thresholds[i - 1])
# Juste au cas où (devrait jamais arriver)
return factors[-1]
def interpolate_factor(self, pct: float, start_pct: float = 5, end_pct: float = 30,
start_factor: float = 1.0, end_factor: float = 2.0) -> float:
if pct <= start_pct:
return start_factor
if pct >= end_pct:
return end_factor
# interpolation linéaire
return start_factor + (pct - start_pct) * (end_factor - start_factor) / (end_pct - start_pct)
def log_trade(self, action, pair, date, trade_type=None, rate=None, dispo=None, profit=None, buys=None, stake=None, def log_trade(self, action, pair, date, trade_type=None, rate=None, dispo=None, profit=None, buys=None, stake=None,
last_candle=None): last_candle=None):
# Afficher les colonnes une seule fois # Afficher les colonnes une seule fois
@@ -763,12 +719,14 @@ class Zeus_8_3_2_B_4_2(IStrategy):
max_stake: float, **kwargs): max_stake: float, **kwargs):
# ne rien faire si ordre deja en cours # ne rien faire si ordre deja en cours
if trade.has_open_orders: if trade.has_open_orders:
print("skip open orders")
return None return None
if (self.wallets.get_available_stake_amount() < 50): # or trade.stake_amount >= max_stake: if (self.wallets.get_available_stake_amount() < 50): # or trade.stake_amount >= max_stake:
return 0 return 0
dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe) dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze() last_candle = dataframe.iloc[-1].squeeze()
last_candle_3 = dataframe.iloc[-4].squeeze()
# prépare les données # prépare les données
current_time = current_time.astimezone(timezone.utc) current_time = current_time.astimezone(timezone.utc)
open_date = trade.open_date.astimezone(timezone.utc) open_date = trade.open_date.astimezone(timezone.utc)
@@ -776,9 +734,12 @@ class Zeus_8_3_2_B_4_2(IStrategy):
hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0 hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0
if (len(dataframe) < 1): if (len(dataframe) < 1):
print("skip dataframe")
return None return None
pair = trade.pair pair = trade.pair
if pair not in ('BTC/USDT', 'DOGE/USDT', 'ETH/USDT'): if pair not in ('BTC/USDT', 'BTC/USDC'):
print(f"skip pair {pair}")
return None return None
count_of_buys = trade.nr_of_successful_entries count_of_buys = trade.nr_of_successful_entries
@@ -792,19 +753,37 @@ class Zeus_8_3_2_B_4_2(IStrategy):
current_time_utc = current_time.astimezone(timezone.utc) current_time_utc = current_time.astimezone(timezone.utc)
open_date = trade.open_date.astimezone(timezone.utc) open_date = trade.open_date.astimezone(timezone.utc)
days_since_open = (current_time_utc - open_date).days days_since_open = (current_time_utc - open_date).days
pct_first = 0
if self.pairs[pair]['first_buy']:
pct_first = round((last_candle['close'] - self.pairs[pair]['first_buy']) / self.pairs[pair]['first_buy'], 3) pct_first = round((last_candle['close'] - self.pairs[pair]['first_buy']) / self.pairs[pair]['first_buy'], 3)
pct_max = round((last_candle['close'] - self.pairs[trade.pair]['last_buy']) / self.pairs[trade.pair]['last_buy'], 4)
pct = 0.012 pct = 0.012
stake_amount = min(self.wallets.get_available_stake_amount(), self.adjust_stake_amount(pair, last_candle) - 20 * pct_first / pct) #min(200, self.adjust_stake_amount(pair, last_candle) * self.fibo[count_of_buys]) if count_of_buys == 1:
pct_max = current_profit
else:
if self.pairs[trade.pair]['last_buy']:
pct_max = round((last_candle['close'] - self.pairs[trade.pair]['last_buy']) / self.pairs[trade.pair]['last_buy'], 4)
else:
pct_max = - pct
lim = - pct - (count_of_buys * 0.001)
# print(f"{trade.pair} current_profit={current_profit} count_of_buys={count_of_buys} pct_max={pct_max:.3f} lim={lim:.3f} rsi_diff_1f={last_candle['rsi_diff_1h']}")
# if (days_since_open > count_of_buys) & (0 < count_of_buys <= max_buys) & (current_rate <= limit) & (last_candle['enter_long'] == 1): # if (days_since_open > count_of_buys) & (0 < count_of_buys <= max_buys) & (current_rate <= limit) & (last_candle['enter_long'] == 1):
limit_buy = 20 limit_buy = 20
if (count_of_buys < limit_buy) \ if (count_of_buys < limit_buy) \
and ((last_candle['enter_long'] == 1) or last_candle['percent48'] < - 0.03) \ and ((last_candle['enter_long'] == 1)
and (last_candle['enter_long'] == 1) \ or (last_candle['percent48'] < - 0.03)
and (pct_max < - pct - (count_of_buys * 0.001)): or ((last_candle['min50'] == last_candle_3['min50']) and (last_candle['low'] <= last_candle['min50']))
) \
and (last_candle['rsi_diff_1h'] >= -5) \
and ((pct_max < lim)):
try: try:
# print(self.adjust_stake_amount(pair, last_candle))
# print(pct_first)
# print(pct)
stake_amount = min(self.wallets.get_available_stake_amount(),
self.adjust_stake_amount(pair, last_candle) - 10 * pct_first / pct) # min(200, self.adjust_stake_amount(pair, last_candle) * self.fibo[count_of_buys])
trade_type = last_candle['enter_tag'] if last_candle['enter_long'] == 1 else 'pct48' trade_type = last_candle['enter_tag'] if last_candle['enter_long'] == 1 else 'pct48'
self.log_trade( self.log_trade(
@@ -851,15 +830,39 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# max_min = max_14_days / min_14_days # max_min = max_14_days / min_14_days
# Stack amount ajusté price=2473.47 min_max=0.15058074985054215 percent=0.8379141364642171 amount=20.0 # Stack amount ajusté price=2473.47 min_max=0.15058074985054215 percent=0.8379141364642171 amount=20.0
adjusted_stake_amount = max(base_stake_amount, min(100, base_stake_amount * percent_4)) first_price = self.pairs[pair]['first_buy']
last_max = current_price
if self.pairs[pair]['last_max'] > 0:
last_max = self.pairs[pair]['last_max']
last_count = self.pairs[pair]['last_count_of_buys']
# factor = 1
#
# if last_max > 0:
# pct = 100 * (last_max - first_price) / last_max
#
# if pct >= 20:
# factor = 2
# else:
# if pct >= 15:
# factor = 1.5
pct = 5
if last_max > 0:
pct = 100 * (last_max - first_price) / last_max
thresholds = [2, 10, 20, 30]
factors = [0.5, 1.0, 1.5, 2.0]
factor = self.multi_step_interpolate(pct, thresholds, factors)
# factor = self.interpolate_factor(pct, start_pct=5, end_pct=50, start_factor=0.8, end_factor=3.0)
adjusted_stake_amount = base_stake_amount * factor #max(base_stake_amount, min(100, base_stake_amount * percent_4))
# if pair in ('BTC/USDT', 'ETH/USDT'): # if pair in ('BTC/USDT', 'ETH/USDT'):
# if percent_4 > 0.5: # if percent_4 > 0.5:
# adjusted_stake_amount = 300 # adjusted_stake_amount = 300
# adjusted_stake_amount_2 = max(base_stake_amount / 2.5, min(75, base_stake_amount * percent)) # adjusted_stake_amount_2 = max(base_stake_amount / 2.5, min(75, base_stake_amount * percent))
# print( # print(f"Stack amount ajusté price={current_price} factor={factor} amount={adjusted_stake_amount:.4f}")
# f"Stack amount ajusté price={current_price} max_min={max_min_4:.4f} min_14={min_14_days_4:.4f} max_14={max_14_days_4:.4f} factor={factor_4:.4f} percent={percent_4:.4f} amount={adjusted_stake_amount:.4f}")
# print(f"Stack amount ajusté price={current_price} max_min={max_min:.4f} min_14={min_14_days:.4f} max_14={max_14_days:.4f} factor={factor:.4f} percent={percent:.4f} amount={adjusted_stake_amount_2:.4f}") # print(f"Stack amount ajusté price={current_price} max_min={max_min:.4f} min_14={min_14_days:.4f} max_14={max_14_days:.4f} factor={factor:.4f} percent={percent:.4f} amount={adjusted_stake_amount_2:.4f}")
return adjusted_stake_amount return adjusted_stake_amount