FrictradeLearning.py corrections diverses

This commit is contained in:
Jérôme Delacotte
2025-12-10 18:02:52 +01:00
parent 11bb59636e
commit d7b9059d40

View File

@@ -107,19 +107,6 @@ class FrictradeLearning(IStrategy):
"minimal_roi": {"0": 0.10} "minimal_roi": {"0": 0.10}
} }
dca_levels = {
0: 0.00,
-2: 0.05,
-4: 0.07,
-6: 0.10,
-8: 0.12,
-10: 0.15,
-12: 0.18,
-14: 0.22,
-16: 0.26,
-18: 0.30,
}
allow_decrease_rate = DecimalParameter(0.1, 0.8, decimals=1, default=0.4, space='protection') allow_decrease_rate = DecimalParameter(0.1, 0.8, decimals=1, default=0.4, space='protection')
first_adjust_param = DecimalParameter(0.001, 0.01, decimals=3, default=0.003, space='protection') first_adjust_param = DecimalParameter(0.001, 0.01, decimals=3, default=0.003, space='protection')
max_steps = IntParameter(10, 50, default=40, space='protection', optimize=True, load=True) max_steps = IntParameter(10, 50, default=40, space='protection', optimize=True, load=True)
@@ -254,7 +241,7 @@ class FrictradeLearning(IStrategy):
# d = n / (p * (p - 1)) # incrément # d = n / (p * (p - 1)) # incrément
# return [round(a + i * d, 3) for i in range(p)] # return [round(a + i * d, 3) for i in range(p)]
def progressive_parts(total, n, first): def progressive_parts(total, n, first):
print('In part')
# conditions impossibles → on évite le solveur # conditions impossibles → on évite le solveur
if total <= 0 or first <= 0 or n <= 1: if total <= 0 or first <= 0 or n <= 1:
return [0] * n return [0] * n
@@ -282,7 +269,9 @@ class FrictradeLearning(IStrategy):
self.pairs[pair]['dca_thresholds'] = progressive_parts( self.pairs[pair]['dca_thresholds'] = progressive_parts(
(last_candle['mid'] - (self.pairs[pair]['last_ath'] * (1 - self.allow_decrease_rate.value))) / last_candle['mid'], (last_candle['mid'] - (self.pairs[pair]['last_ath'] * (1 - self.allow_decrease_rate.value))) / last_candle['mid'],
steps, self.first_adjust_param.value) steps, self.first_adjust_param.value)
print(f"val={last_candle['mid']} steps={steps} pct={(last_candle['mid'] - (self.pairs[pair]['last_ath'] * (1 - self.allow_decrease_rate.value))) / last_candle['mid']}") print(f"val={last_candle['mid']} lim={self.pairs[pair]['last_ath'] * (1 - self.allow_decrease_rate.value)}"
f"steps={steps} "
f"pct={(round(last_candle['mid'] - (self.pairs[pair]['last_ath'] * (1 - self.allow_decrease_rate.value))) / last_candle['mid'], 4)}")
print(self.pairs[pair]['dca_thresholds']) print(self.pairs[pair]['dca_thresholds'])
def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float, rate: float, def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float, rate: float,
@@ -1095,30 +1084,30 @@ class FrictradeLearning(IStrategy):
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()
before_last_candle = dataframe.iloc[-2].squeeze() # before_last_candle = dataframe.iloc[-2].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)
dispo = round(self.wallets.get_available_stake_amount()) dispo = round(self.wallets.get_available_stake_amount())
hours_since_first_buy = (current_time - trade.open_date_utc).seconds / 3600.0 # hours_since_first_buy = (current_time - trade.open_date_utc).seconds / 3600.0
days_since_first_buy = (current_time - trade.open_date_utc).days # days_since_first_buy = (current_time - trade.open_date_utc).days
hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0 hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0
minutes = (current_time - trade.date_last_filled_utc).total_seconds() / 60.0 # minutes = (current_time - trade.date_last_filled_utc).total_seconds() / 60.0
count_of_buys = trade.nr_of_successful_entries count_of_buys = trade.nr_of_successful_entries
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
pair = trade.pair pair = trade.pair
profit = trade.calc_profit(current_rate) #round(current_profit * trade.stake_amount, 1) profit = trade.calc_profit(current_rate) #round(current_profit * trade.stake_amount, 1)
last_lost = self.getLastLost(last_candle, pair) # last_lost = self.getLastLost(last_candle, pair)
pct_first = 0 pct_first = 0
total_counts = sum( # total_counts = sum(
pair_data['count_of_buys'] for pair_data in self.pairs.values() if not self.getShortName(pair) == 'BTC') # pair_data['count_of_buys'] for pair_data in self.pairs.values() if not self.getShortName(pair) == 'BTC')
#
if self.pairs[pair]['first_buy']: # if self.pairs[pair]['first_buy']:
pct_first = self.getPctFirstBuy(pair, last_candle) # pct_first = self.getPctFirstBuy(pair, last_candle)
if profit > - self.pairs[pair]['first_amount'] \ if profit > - self.pairs[pair]['first_amount'] \
and self.wallets.get_available_stake_amount() < self.pairs[pair]['first_amount'] \ and self.wallets.get_available_stake_amount() < self.pairs[pair]['first_amount'] \
@@ -1309,16 +1298,16 @@ class FrictradeLearning(IStrategy):
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe) dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze() last_candle = dataframe.iloc[-1].squeeze()
last_candle_1h = dataframe.iloc[-13].squeeze() # last_candle_1h = dataframe.iloc[-13].squeeze()
before_last_candle = dataframe.iloc[-2].squeeze() # before_last_candle = dataframe.iloc[-2].squeeze()
before_last_candle_2 = dataframe.iloc[-3].squeeze() # before_last_candle_2 = dataframe.iloc[-3].squeeze()
before_last_candle_12 = dataframe.iloc[-13].squeeze() # before_last_candle_12 = dataframe.iloc[-13].squeeze()
#
expected_profit = self.expectedProfit(pair, last_candle) # expected_profit = self.expectedProfit(pair, last_candle)
# print(f"current_time={current_time} current_profit={current_profit} expected_profit={expected_profit}") # # print(f"current_time={current_time} current_profit={current_profit} expected_profit={expected_profit}")
#
# ----- 1) Charger les variables de trailing pour ce trade ----- # # ----- 1) Charger les variables de trailing pour ce trade -----
max_price = self.pairs[pair]['max_touch'] # max_price = self.pairs[pair]['max_touch']
self.pairs[pair]['last_max'] = max(last_candle['close'], self.pairs[pair]['last_max']) self.pairs[pair]['last_max'] = max(last_candle['close'], self.pairs[pair]['last_max'])
self.pairs[pair]['last_min'] = min(last_candle['close'], self.pairs[pair]['last_min']) self.pairs[pair]['last_min'] = min(last_candle['close'], self.pairs[pair]['last_min'])
@@ -1337,21 +1326,20 @@ class FrictradeLearning(IStrategy):
# if current_profit > 0: # if current_profit > 0:
# print(f"profit={profit} max_profit={max_profit} current_profit={current_profit}") # print(f"profit={profit} max_profit={max_profit} current_profit={current_profit}")
if profit < 0:
return None
baisse = 0
if profit > 0: # baisse = 0
baisse = 1 - (profit / max_profit) # if profit > 0:
mx = max_profit / 5 # baisse = 1 - (profit / max_profit)
# mx = max_profit / 5
self.pairs[pair]['count_of_buys'] = count_of_buys self.pairs[pair]['count_of_buys'] = count_of_buys
self.pairs[pair]['current_profit'] = profit self.pairs[pair]['current_profit'] = profit
# dispo = round(self.wallets.get_available_stake_amount()) dispo = round(self.wallets.get_available_stake_amount())
# hours_since_first_buy = (current_time - trade.open_date_utc).seconds / 3600.0 # hours_since_first_buy = (current_time - trade.open_date_utc).seconds / 3600.0
# days_since_first_buy = (current_time - trade.open_date_utc).days # days_since_first_buy = (current_time - trade.open_date_utc).days
# hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0 # hours = (current_time - trade.date_last_filled_utc).total_seconds() / 3600.0
# minutes = (current_time - trade.date_last_filled_utc).total_seconds() / 60.0 minutes = (current_time - trade.date_last_filled_utc).total_seconds() / 60.0
# ----- 2) Mise à jour du max_price ----- # ----- 2) Mise à jour du max_price -----
self.pairs[pair]['max_touch'] = max(last_candle['close'], self.pairs[pair]['max_touch']) self.pairs[pair]['max_touch'] = max(last_candle['close'], self.pairs[pair]['max_touch'])
@@ -1363,12 +1351,12 @@ class FrictradeLearning(IStrategy):
current_trailing_only_offset_is_reached = self.trailing_only_offset_is_reached current_trailing_only_offset_is_reached = self.trailing_only_offset_is_reached
current_trailing_stop_positive_offset = self.trailing_stop_positive_offset current_trailing_stop_positive_offset = self.trailing_stop_positive_offset
max_ = last_candle['max180'] # max_ = last_candle['max180']
min_ = last_candle['min180'] # min_ = last_candle['min180']
mid = last_candle['mid'] # mid = last_candle['mid']
# éviter division par zéro # éviter division par zéro
position = (mid - min_) / (max_ - min_) # position = (mid - min_) / (max_ - min_)
zone = int(position * 3) # 0 à 2 # zone = int(position * 3) # 0 à 2
# if zone == 0: # if zone == 0:
# current_trailing_stop_positive = self.trailing_stop_positive # current_trailing_stop_positive = self.trailing_stop_positive
@@ -1382,11 +1370,11 @@ class FrictradeLearning(IStrategy):
# Exemple : offset=0.321 => stop à +24.8% # Exemple : offset=0.321 => stop à +24.8%
trailing_stop = max_profit * (1.0 - current_trailing_stop_positive) trailing_stop = max_profit * (1.0 - current_trailing_stop_positive)
baisse = 0 # baisse = 0
if max_profit: # if max_profit:
baisse = (max_profit - profit) / max_profit # baisse = (max_profit - profit) / max_profit
# if minutes % 12 == 0: # if minutes % 1 == 0:
# self.log_trade( # self.log_trade(
# last_candle=last_candle, # last_candle=last_candle,
# date=current_time, # date=current_time,
@@ -1399,9 +1387,15 @@ class FrictradeLearning(IStrategy):
# buys=count_of_buys, # buys=count_of_buys,
# stake=0 # stake=0
# ) # )
if profit < 0:
if last_candle['sma12'] > last_candle['sma24']:
return None return None
if last_candle['rsi'] > 90:
return f"rsi_{count_of_buys}_{self.pairs[pair]['has_gain']}"
if last_candle['sma12'] > last_candle['sma24']: # and last_candle['rsi'] < 85:
return None
# if last_candle['sma24_deriv1'] > 0 : #and minutes < 180 and baisse < 30: # and last_candle['sma5_deriv1'] > -0.15: # if last_candle['sma24_deriv1'] > 0 : #and minutes < 180 and baisse < 30: # and last_candle['sma5_deriv1'] > -0.15:
# if (minutes < 180): # if (minutes < 180):
# return None # return None
@@ -1410,16 +1404,17 @@ class FrictradeLearning(IStrategy):
# ----- 4) OFFSET : faut-il attendre de dépasser trailing_stop_positive_offset ? ----- # ----- 4) OFFSET : faut-il attendre de dépasser trailing_stop_positive_offset ? -----
if current_trailing_only_offset_is_reached: if current_trailing_only_offset_is_reached and max_profit > current_trailing_stop_positive_offset:
# Max profit pas atteint ET perte < 2 * current_trailing_stop_positive # Max profit pas atteint ET perte < 2 * current_trailing_stop_positive
if profit > max_profit * current_trailing_stop_positive_offset * (count_of_buys - self.pairs[pair]['has_gain']): #2 * current_trailing_stop_positive: if profit > max_profit * (1 - current_trailing_stop_positive): #2 * current_trailing_stop_positive:
print(f"{current_time} trailing non atteint trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} " print(f"{current_time} trailing non atteint trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} "
f"{min(2, current_trailing_stop_positive_offset * (count_of_buys - self.pairs[pair]['has_gain']))}") f"{min(2, max_profit * (1 - current_trailing_stop_positive))}")
return None # ne pas activer le trailing encore return None # ne pas activer le trailing encore
else: else:
print(f"{current_time} trailing atteint trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} " print(f"{current_time} trailing atteint trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} "
f"{min(2, current_trailing_stop_positive_offset * (count_of_buys - self.pairs[pair]['has_gain']))}") f"{min(2,max_profit * (1 - current_trailing_stop_positive))}")
else:
return None
# Sinon : trailing actif dès le début # Sinon : trailing actif dès le début
# ----- 6) Condition de vente ----- # ----- 6) Condition de vente -----
@@ -1427,7 +1422,7 @@ class FrictradeLearning(IStrategy):
self.pairs[pair]['force_buy'] = True self.pairs[pair]['force_buy'] = True
print( print(
f"{current_time} Condition de vente trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} " f"{current_time} Condition de vente trailing_stop={round(trailing_stop,4)} profit={round(profit, 4)} max={round(max_profit, 4)} "
f"{round(min(2, current_trailing_stop_positive_offset * (count_of_buys - self.pairs[pair]['has_gain'])), 4)}") f"{round(min(2, max_profit * (1 - current_trailing_stop_positive)), 4)}")
return f"stop_{count_of_buys}_{self.pairs[pair]['has_gain']}" return f"stop_{count_of_buys}_{self.pairs[pair]['has_gain']}"
return None return None