Ajout calcul de probabilité fonction de 2 indicateurs / revert

This commit is contained in:
Jérôme Delacotte
2025-05-15 21:32:57 +02:00
parent 1af4fe8460
commit 7f1ae58bff

View File

@@ -225,41 +225,62 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# H5 14.8 21.6 22.2 35.3 19.3 31.6 38.3 59.6 65.2 56.8 59.6
# Données sous forme de dictionnaire
data = {
'B5': [41.2, 41.9, 35.3, 29.5, 26.6, 32.7, 28.8, 30.4, 25.3, 20.2, 14.6],
'B4': [45.2, 40.1, 38.6, 31.2, 30.1, 29.5, 27.1, 24.2, 24.7, 21.6, 18.4],
'B3': [46.7, 41.6, 37.3, 33.4, 27.3, 28.6, 28.8, 24.6, 24.2, 24.8, 23.2],
'B2': [49.4, 47.9, 45.9, 39.8, 34.6, 28.6, 26.2, 28.9, 24.7, 29.7, 28.5],
'B1': [74.1, 61.7, 61.7, 58.2, 50.2, 43.5, 38.1, 32.5, 33.9, 32.8, 25.3],
'N0': [67.8, 58.3, 67.8, 61.7, 58.0, 56.2, 46.7, 42.2, 44.4, 39.7, 30.3],
'H1': [72.6, 66.2, 70.8, 69.1, 70.8, 65.3, 56.3, 51.2, 52.6, 53.9, 48.1],
'H2': [77.2, 78.7, 81.0, 75.9, 73.6, 73.0, 65.4, 63.3, 63.8, 56.9, 52.8],
'H3': [78.8, 76.6, 78.8, 81.2, 76.7, 71.9, 69.2, 67.8, 66.1, 61.3, 58.1],
'H4': [75.5, 79.0, 78.0, 78.5, 73.6, 66.4, 71.2, 63.8, 67.0, 62.6, 59.3],
'H5': [75.1, 78.6, 81.0, 72.3, 70.0, 71.4, 67.8, 66.0, 66.2, 64.5, 59.9]
}
# data = {
# "B5": [41.0, 41.2, 34.1, 27.5, 35.0, 30.6, 25.2, 29.8, 25.7, 30.6, 14.8],
# "B4": [47.2, 35.8, 39.7, 27.9, 26.5, 19.9, 28.7, 20.8, 29.4, 27.5, 21.6],
# "B3": [48.1, 48.4, 42.8, 32.3, 24.4, 23.6, 28.6, 23.9, 22.7, 25.1, 22.2],
# "B2": [45.6, 46.5, 47.0, 33.2, 34.9, 30.8, 25.8, 30.4, 29.8, 22.6, 35.3],
# "B1": [74.0, 59.9, 63.3, 61.9, 50.0, 41.9, 35.9, 34.4, 37.7, 30.8, 19.3],
# "N0": [65.9, 60.2, 64.5, 67.1, 59.2, 59.2, 44.2, 37.5, 47.1, 34.1, 31.6],
# "H1": [66.5, 75.8, 71.5, 70.8, 69.4, 67.5, 60.1, 52.7, 59.9, 50.9, 38.3],
# "H2": [83.8, 79.4, 80.4, 79.5, 72.8, 70.6, 68.8, 66.1, 68.5, 59.8, 59.6],
# "H3": [77.8, 84.6, 82.0, 81.3, 79.8, 74.0, 67.7, 69.8, 66.5, 57.0, 65.2],
# "H4": [72.1, 83.0, 86.6, 73.6, 77.4, 63.0, 69.6, 67.5, 68.6, 68.6, 56.8],
# "H5": [81.0, 78.5, 76.6, 81.9, 69.5, 75.0, 80.9, 62.9, 66.4, 63.7, 59.6]
# smooth_smadiff_matrice = {
# 'B5': [41.2, 41.9, 35.3, 29.5, 26.6, 32.7, 28.8, 30.4, 25.3, 20.2, 14.6],
# 'B4': [45.2, 40.1, 38.6, 31.2, 30.1, 29.5, 27.1, 24.2, 24.7, 21.6, 18.4],
# 'B3': [46.7, 41.6, 37.3, 33.4, 27.3, 28.6, 28.8, 24.6, 24.2, 24.8, 23.2],
# 'B2': [49.4, 47.9, 45.9, 39.8, 34.6, 28.6, 26.2, 28.9, 24.7, 29.7, 28.5],
# 'B1': [74.1, 61.7, 61.7, 58.2, 50.2, 43.5, 38.1, 32.5, 33.9, 32.8, 25.3],
# 'N0': [67.8, 58.3, 67.8, 61.7, 58.0, 56.2, 46.7, 42.2, 44.4, 39.7, 30.3],
# 'H1': [72.6, 66.2, 70.8, 69.1, 70.8, 65.3, 56.3, 51.2, 52.6, 53.9, 48.1],
# 'H2': [77.2, 78.7, 81.0, 75.9, 73.6, 73.0, 65.4, 63.3, 63.8, 56.9, 52.8],
# 'H3': [78.8, 76.6, 78.8, 81.2, 76.7, 71.9, 69.2, 67.8, 66.1, 61.3, 58.1],
# 'H4': [75.5, 79.0, 78.0, 78.5, 73.6, 66.4, 71.2, 63.8, 67.0, 62.6, 59.3],
# 'H5': [75.1, 78.6, 81.0, 72.3, 70.0, 71.4, 67.8, 66.0, 66.2, 64.5, 59.9]
# }
smooth_smadiff_matrice = {
"B5": [41.0, 41.2, 34.1, 27.5, 35.0, 30.6, 25.2, 29.8, 25.7, 30.6, 14.8],
"B4": [47.2, 35.8, 39.7, 27.9, 26.5, 19.9, 28.7, 20.8, 29.4, 27.5, 21.6],
"B3": [48.1, 48.4, 42.8, 32.3, 24.4, 23.6, 28.6, 23.9, 22.7, 25.1, 22.2],
"B2": [45.6, 46.5, 47.0, 33.2, 34.9, 30.8, 25.8, 30.4, 29.8, 22.6, 35.3],
"B1": [74.0, 59.9, 63.3, 61.9, 50.0, 41.9, 35.9, 34.4, 37.7, 30.8, 19.3],
"N0": [65.9, 60.2, 64.5, 67.1, 59.2, 59.2, 44.2, 37.5, 47.1, 34.1, 31.6],
"H1": [66.5, 75.8, 71.5, 70.8, 69.4, 67.5, 60.1, 52.7, 59.9, 50.9, 38.3],
"H2": [83.8, 79.4, 80.4, 79.5, 72.8, 70.6, 68.8, 66.1, 68.5, 59.8, 59.6],
"H3": [77.8, 84.6, 82.0, 81.3, 79.8, 74.0, 67.7, 69.8, 66.5, 57.0, 65.2],
"H4": [72.1, 83.0, 86.6, 73.6, 77.4, 63.0, 69.6, 67.5, 68.6, 68.6, 56.8],
"H5": [81.0, 78.5, 76.6, 81.9, 69.5, 75.0, 80.9, 62.9, 66.4, 63.7, 59.6]
}
index_labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
matrix_df = pd.DataFrame(data, index=index_labels)
smooth_smadiff_matrice_df = pd.DataFrame(smooth_smadiff_matrice, index=index_labels)
# Récupération des labels ordonnés
ordered_labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
label_to_index = {label: i for i, label in enumerate(ordered_labels)}
# Extraction de la matrice numérique
numeric_matrix = matrix_df.reindex(index=ordered_labels, columns=ordered_labels).values
smooth_smadiff_numeric_matrice = smooth_smadiff_matrice_df.reindex(index=ordered_labels, columns=ordered_labels).values
smooth_pct_max_hour_matrice = {
'B5': [43.5, 52.7, 62.3, 65.5, 86.9, 63.1, 81.5, 86.7, 90.2, 90.1, 93.0],
'B4': [34.9, 46.3, 53.6, 60.4, 75.8, 83.3, 81.5, 83.0, 86.4, 86.9, 91.1],
'B3': [20.5, 35.4, 43.7, 54.5, 69.7, 71.6, 80.4, 84.7, 86.7, 84.9, 85.9],
'B2': [11.5, 25.4, 36.4, 47.9, 62.3, 65.7, 76.5, 82.0, 81.8, 82.8, 77.7],
'B1': [3.6, 14.9, 26.8, 41.1, 55.6, 71.4, 74.3, 79.8, 80.8, 82.3, 75.1],
'N0': [0.0, 6.9, 18.3, 32.0, 47.2, 62.1, 69.1, 74.8, 78.3, 76.6, 71.6],
'H1': [0.7, 3.8, 9.4, 24.2, 40.6, 59.7, 67.8, 70.9, 73.4, 72.1, 70.0],
'H2': [0.0, 0.6, 6.5, 13.6, 33.6, 51.7, 64.9, 70.2, 68.4, 67.8, 65.8],
'H3': [1.4, 0.6, 2.6, 6.6, 23.3, 50.2, 56.2, 63.6, 65.7, 64.5, 64.7],
'H4': [1.6, 0.3, 3.0, 3.2, 11.4, 32.7, 44.0, 54.9, 61.7, 60.6, 63.6],
'H5': [1.8, 2.6, 0.6, 1.1, 9.7, 12.9, 26.2, 44.5, 52.6, 54.5, 56.2],
}
smooth_pct_max_hour_matrice_df = pd.DataFrame(smooth_pct_max_hour_matrice, index=index_labels)
# Récupération des labels ordonnés
# ordered_labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
# label_to_index = {label: i for i, label in enumerate(ordered_labels)}
# Extraction de la matrice numérique
smooth_smadiff_numeric_matrice = smooth_pct_max_hour_matrice_df.reindex(index=ordered_labels, columns=ordered_labels).values
def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float, time_in_force: str,
@@ -281,7 +302,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
mid_smooth_label = self.get_mid_smooth_label(last_candle['mid_smooth_deriv1_1h']) # ex. 'B2'
sma24_diff_label = self.get_sma24_diff_label(last_candle['sma24_diff_1h'])
val = self.approx_val_from_bins(row_label=sma24_diff_label, col_label=mid_smooth_label)
val = self.approx_val_from_bins(matrice=self.smooth_smadiff_matrice_df, row_label=sma24_diff_label, col_label=mid_smooth_label)
# allow_to_buy = True #(not self.stop_all) #& (not self.all_down)
allow_to_buy = not self.pairs[pair]['stop'] and val > 50 #not last_candle['tendency'] in ('B-', 'B--') # (rate <= float(limit)) | (entry_tag == 'force_entry')
@@ -389,12 +410,12 @@ class Zeus_8_3_2_B_4_2(IStrategy):
if (last_candle['tendency'] in ('H++', 'H+')) and (last_candle['rsi'] < 80):
return None
mid_smooth_label = self.get_mid_smooth_label(last_candle['mid_smooth_deriv1_1h']) # ex. 'B2'
sma24_diff_label = self.get_sma24_diff_label(last_candle['sma24_diff_1h'])
val = self.approx_val_from_bins(row_label=sma24_diff_label, col_label=mid_smooth_label)
if val > 50:
return None
# mid_smooth_label = self.get_mid_smooth_label(last_candle['mid_smooth_deriv1_1h']) # ex. 'B2'
# sma24_diff_label = self.get_sma24_diff_label(last_candle['sma24_diff_1h'])
#
# val = self.approx_val_from_bins(matrice=self.smooth_smadiff_matrice_df, row_label=sma24_diff_label, col_label=mid_smooth_label)
# if val > 50:
# return None
baisse = self.pairs[pair]['max_profit'] - current_profit
mx = self.pairs[pair]['max_profit'] / 5
@@ -694,6 +715,8 @@ class Zeus_8_3_2_B_4_2(IStrategy):
print(f"amount= {amount}")
dataframe['percent_with_previous_day'] = 100 * (dataframe['close'] - dataframe['close_1d']) / dataframe['close']
dataframe['percent_with_max_hour'] = 100 * (dataframe['close'] - dataframe['max12_1h']) / dataframe['close']
dataframe['futur_percent_1h'] = 100 * (dataframe['close'].shift(-12) - dataframe['close']) / dataframe['close']
dataframe['futur_percent_3h'] = 100 * (dataframe['close'].shift(-36) - dataframe['close']) / dataframe['close']
@@ -796,14 +819,16 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# pd.set_option('display.max_columns', None)
# Colonnes à traiter
futur_cols = ['futur_percent_1h', 'futur_percent_3h', 'futur_percent_5h', 'futur_percent_12h']
# futur_cols = ['futur_percent_1h', 'futur_percent_3h', 'futur_percent_5h', 'futur_percent_12h']
futur_cols = ['futur_percent_3h']
# Tranches équitables par quantiles
# Exemple pour 10 quantiles
labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
indic_1 = 'mid_smooth_deriv1_1h'
indic_2 = 'sma24_diff_1h'
# indic_2 = 'sma24_diff_1h'
indic_2 = 'percent_with_max_hour'
df[f"{indic_1}_bin"], bins_1h = pd.qcut(df[f"{indic_1}"], q=11, labels=labels, retbins=True, duplicates='drop')
df[f"{indic_2}_bin"], bins_1d = pd.qcut(df[f"{indic_2}"], q=11, labels=labels, retbins=True, duplicates='drop')
@@ -811,9 +836,9 @@ class Zeus_8_3_2_B_4_2(IStrategy):
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 300) # largeur max affichage
# Afficher les bornes
print(f"Bornes des quantiles pour {indic_1} :", bins_1h)
print(f"Bornes des quantiles pour {indic_2} :", bins_1d)
# Affichage formaté pour code Python
print(f"Bornes des quantiles pour {indic_1} : [{', '.join([f'{b:.4f}' for b in bins_1h])}]")
print(f"Bornes des quantiles pour {indic_2} : [{', '.join([f'{b:.4f}' for b in bins_1d])}]")
# Agrégation
grouped = df.groupby([f"{indic_2}_bin", f"{indic_1}_bin"], observed=True)[futur_cols].agg(['mean', 'count'])
@@ -834,6 +859,16 @@ class Zeus_8_3_2_B_4_2(IStrategy):
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print((proba_up * 100).round(1))
# Affichage formaté des valeurs comme tableau Python
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
df_formatted = (proba_up * 100).round(1)
print("data = {")
for index, row in df_formatted.iterrows():
row_values = ", ".join([f"{val:.1f}" for val in row])
print(f"'{index}': [{row_values}], ")
print("}")
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
@@ -903,7 +938,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
mid_smooth_label = self.get_mid_smooth_label(last_candle['mid_smooth_deriv1_1h']) # ex. 'B2'
sma24_diff_label = self.get_sma24_diff_label(last_candle['sma24_diff_1h'])
val = self.approx_val_from_bins(row_label=sma24_diff_label, col_label=mid_smooth_label)
val = self.approx_val_from_bins(matrice=self.smooth_smadiff_matrice_df, row_label=sma24_diff_label, col_label=mid_smooth_label)
# print(f"Valeur approximée pour B3 / H2 : {val:.2f}")
# if (days_since_open > count_of_buys) & (0 < count_of_buys <= max_buys) & (current_rate <= limit) & (last_candle['enter_long'] == 1):
@@ -970,6 +1005,14 @@ class Zeus_8_3_2_B_4_2(IStrategy):
# Calculer le minimum des 14 derniers jours
base_stake_amount = self.config.get('stake_amount', 100) # Montant de base configuré
# if (self.pairs[pair]['count_of_buys'] == 0):
# mid_smooth_label = self.get_mid_smooth_label(last_candle['mid_smooth_deriv1_1h']) # ex. 'B2'
# percent_with_max_hour = self.get_sma24_diff_label(last_candle['percent_with_max_hour'])
#
# val = self.approx_val_from_bins(matrice=self.smooth_pct_max_hour_matrice_df, row_label=percent_with_max_hour, col_label=mid_smooth_label)
#
# base_stake_amount = base_stake_amount * (1 + val / 500)
first_price = self.pairs[pair]['first_buy']
if (first_price == 0):
first_price = last_candle['close']
@@ -1081,8 +1124,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
return df
def get_mid_smooth_label(self, value):
bins = [-6.6534, -0.1788, -0.0771, -0.0375, -0.0149, 0.0000, 0.0045, 0.0232, 0.0488, 0.0923, 0.1961, 2.7892]
# bins = [-2.0622, -0.1618, -0.0717, -0.0353, -0.0135, 0.0, 0.0085, 0.0276, 0.0521, 0.0923, 0.1742, 2.3286]
bins = [-2.0622, -0.1618, -0.0717, -0.0353, -0.0135, 0.0, 0.0085, 0.0276, 0.0521, 0.0923, 0.1742, 2.3286]
labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
for i in range(len(bins) - 1):
if bins[i] <= value < bins[i + 1]:
@@ -1090,11 +1132,8 @@ class Zeus_8_3_2_B_4_2(IStrategy):
return labels[-1] # cas limite pour la borne max
def get_sma24_diff_label(self, value):
# bins = [-0.84253877, -0.13177195, -0.07485074, -0.04293497, -0.02033502, -0.00215711,
# 0.01411933, 0.03308264, 0.05661652, 0.09362708, 0.14898214, 0.50579505]
bins = [-2.36157394, - 0.14917999, - 0.08269905, - 0.04657017, - 0.02168984, - 0.00339744,
0.01284104, 0.03306543, 0.05950946, 0.09880605, 0.16573819, 1.05922354]
bins = [-0.84253877, -0.13177195, -0.07485074, -0.04293497, -0.02033502, -0.00215711,
0.01411933, 0.03308264, 0.05661652, 0.09362708, 0.14898214, 0.50579505]
labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
for i in range(len(bins) - 1):
if bins[i] <= value < bins[i + 1]:
@@ -1124,7 +1163,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
return np.nan
# Conversion des labels -> matrice
matrix = self.matrix_df.reindex(index=labels, columns=labels).values
matrix = self.smooth_smadiff_matrice_df.reindex(index=labels, columns=labels).values
# Coordonnées entières (inférieures)
i = int(np.floor(row_pos))
@@ -1153,7 +1192,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
)
return interpolated
def approx_val_from_bins(self, row_label, col_label):
def approx_val_from_bins(self, matrice, row_label, col_label):
"""
Renvoie une approximation de la valeur à partir des labels binaires (e.g. B5, H1)
en utilisant une interpolation simple basée sur les indices.
@@ -1168,7 +1207,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
"""
# Vérification des labels
if row_label not in self.matrix_df.index or col_label not in self.matrix_df.columns:
if row_label not in matrice.index or col_label not in matrice.columns:
return np.nan
# Index correspondant
@@ -1176,7 +1215,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
col_idx = self.label_to_index.get(col_label)
# Approximation directe (aucune interpolation complexe ici, juste une lecture)
return self.numeric_matrix[row_idx, col_idx]
return self.smooth_smadiff_numeric_matrice[row_idx, col_idx]
# @property