Intégration fonctions polynomiales
This commit is contained in:
@@ -506,6 +506,7 @@ class Zeus_8_3_2_B_4_2(IStrategy):
|
|||||||
################### INFORMATIVE 1h
|
################### INFORMATIVE 1h
|
||||||
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1h")
|
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1h")
|
||||||
informative = self.calculateTendency(informative, 3)
|
informative = self.calculateTendency(informative, 3)
|
||||||
|
informative = self.apply_regression_derivatives(informative, column='mid', window=50, degree=3)
|
||||||
informative['volatility'] = talib.STDDEV(informative['close'], timeperiod=14) / informative['close']
|
informative['volatility'] = talib.STDDEV(informative['close'], timeperiod=14) / informative['close']
|
||||||
informative['atr'] = (talib.ATR(informative['high'], informative['low'], informative['close'], timeperiod=14)) / informative['close']
|
informative['atr'] = (talib.ATR(informative['high'], informative['low'], informative['close'], timeperiod=14)) / informative['close']
|
||||||
informative['rsi'] = talib.RSI(informative['close'], length=7)
|
informative['rsi'] = talib.RSI(informative['close'], length=7)
|
||||||
@@ -514,12 +515,14 @@ class Zeus_8_3_2_B_4_2(IStrategy):
|
|||||||
|
|
||||||
informative['sma5'] = talib.SMA(informative, timeperiod=5)
|
informative['sma5'] = talib.SMA(informative, timeperiod=5)
|
||||||
informative['sma5_pct'] = 100 * (informative['sma5'] - informative['sma5'].shift(1)) / informative['sma5']
|
informative['sma5_pct'] = 100 * (informative['sma5'] - informative['sma5'].shift(1)) / informative['sma5']
|
||||||
|
|
||||||
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1h", ffill=True)
|
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1h", ffill=True)
|
||||||
|
|
||||||
################### INFORMATIVE 1d
|
################### INFORMATIVE 1d
|
||||||
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1d")
|
informative = self.dp.get_pair_dataframe(pair=metadata['pair'], timeframe="1d")
|
||||||
informative = self.calculateTendency(informative, 3)
|
informative = self.calculateTendency(informative, 3)
|
||||||
|
|
||||||
|
informative = self.apply_regression_derivatives(informative, column='mid', window=50, degree=3)
|
||||||
informative['rsi'] = talib.RSI(informative['close'], length=7)
|
informative['rsi'] = talib.RSI(informative['close'], length=7)
|
||||||
informative['rsi_diff'] = informative['rsi'].diff()
|
informative['rsi_diff'] = informative['rsi'].diff()
|
||||||
informative['rsi_diff_2'] = informative['rsi_diff'].diff()
|
informative['rsi_diff_2'] = informative['rsi_diff'].diff()
|
||||||
@@ -546,6 +549,8 @@ class Zeus_8_3_2_B_4_2(IStrategy):
|
|||||||
informative['mid_min_max'] = min_14_days + (max_14_days - min_14_days) / 2
|
informative['mid_min_max'] = min_14_days + (max_14_days - min_14_days) / 2
|
||||||
informative['middle'] = informative['lowest_4'] + (informative['highest_4'] - informative['lowest_4']) / 2
|
informative['middle'] = informative['lowest_4'] + (informative['highest_4'] - informative['lowest_4']) / 2
|
||||||
informative['mid_min_max_0.98'] = informative['mid_min_max'] * 0.98
|
informative['mid_min_max_0.98'] = informative['mid_min_max'] * 0.98
|
||||||
|
|
||||||
|
|
||||||
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1d", ffill=True)
|
dataframe = merge_informative_pair(dataframe, informative, self.timeframe, "1d", ffill=True)
|
||||||
|
|
||||||
dataframe['count_buys'] = 0
|
dataframe['count_buys'] = 0
|
||||||
@@ -1391,3 +1396,38 @@ class Zeus_8_3_2_B_4_2(IStrategy):
|
|||||||
down_pct_values[i] = 100 * (dataframe['close'].iloc[i] - dataframe['close'].iloc[i - shift_value]) / \
|
down_pct_values[i] = 100 * (dataframe['close'].iloc[i] - dataframe['close'].iloc[i - shift_value]) / \
|
||||||
dataframe['close'].iloc[i - shift_value]
|
dataframe['close'].iloc[i - shift_value]
|
||||||
return down_pct_values
|
return down_pct_values
|
||||||
|
|
||||||
|
def apply_regression_derivatives(self, dataframe: DataFrame, column: str = 'close', window: int = 50, degree: int = 3):
|
||||||
|
df = dataframe.copy()
|
||||||
|
deriv1 = []
|
||||||
|
deriv2 = []
|
||||||
|
|
||||||
|
for i in range(len(df)):
|
||||||
|
if i < window:
|
||||||
|
deriv1.append(np.nan)
|
||||||
|
deriv2.append(np.nan)
|
||||||
|
continue
|
||||||
|
|
||||||
|
y = df[column].iloc[i - window:i].values
|
||||||
|
x = np.arange(window)
|
||||||
|
|
||||||
|
# Régression polynomiale
|
||||||
|
coeffs = np.polyfit(x, y, degree)
|
||||||
|
poly = np.poly1d(coeffs)
|
||||||
|
|
||||||
|
# Dérivées
|
||||||
|
d1 = np.polyder(poly, 1) # première dérivée
|
||||||
|
d2 = np.polyder(poly, 2) # seconde dérivée
|
||||||
|
|
||||||
|
# On calcule les valeurs de la dérivée à la dernière bougie
|
||||||
|
val_d1 = d1(window - 1)
|
||||||
|
val_d2 = d2(window - 1)
|
||||||
|
|
||||||
|
deriv1.append(val_d1)
|
||||||
|
deriv2.append(val_d2)
|
||||||
|
|
||||||
|
df['regression_deriv1'] = deriv1
|
||||||
|
df['regression_deriv2'] = deriv2
|
||||||
|
|
||||||
|
return df
|
||||||
|
|
||||||
|
|||||||
109
tools/polynomiale.py
Normal file
109
tools/polynomiale.py
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
def polynomial_features(series, degree=3, verbose=True):
|
||||||
|
x = np.arange(len(series))
|
||||||
|
y = series.values
|
||||||
|
|
||||||
|
# Fit du polynôme
|
||||||
|
coeffs = np.polyfit(x, y, degree)
|
||||||
|
poly = np.poly1d(coeffs)
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print("⚙️ Coefficients (du plus haut degré au plus bas) :")
|
||||||
|
for i, coef in enumerate(coeffs):
|
||||||
|
deg = degree - i
|
||||||
|
print(f" a{deg} = {coef:.6f}")
|
||||||
|
|
||||||
|
print(f"\n📈 Fonction polynomiale :\n f(x) = {poly}")
|
||||||
|
|
||||||
|
# ✅ Première dérivée(variation ou pente)
|
||||||
|
# Positive: la courbe est croissante → tendance haussière.
|
||||||
|
# Négative: la courbe est décroissante → tendance baissière.
|
||||||
|
# Proche de 0: la courbe est plate → marché stable ou en transition.
|
||||||
|
#
|
||||||
|
# Applications:
|
||||||
|
# Détecter les points d’inflexion(changement de tendance) quand elle s’annule.\
|
||||||
|
# Analyser la vitesse d’un mouvement(plus elle est forte, plus le mouvement est impulsif).
|
||||||
|
#
|
||||||
|
# ✅ Seconde dérivée(accélération ou concavité)
|
||||||
|
# Positive: la pente augmente → accélération de la hausse ou ralentissement de la baisse.
|
||||||
|
# Négative: la pente diminue → accélération de la baisse ou ralentissement de la hausse.
|
||||||
|
# Changement de signe: indique souvent un changement de courbure, utile pour prévoir des retournements.
|
||||||
|
#
|
||||||
|
# Exemples:
|
||||||
|
# 🟢 Dérivée 1 > 0 et dérivée 2 > 0: tendance haussière qui s’accélère.
|
||||||
|
# 🟡 Dérivée 1 > 0 et dérivée 2 < 0: tendance haussière qui ralentit → essoufflement potentiel.
|
||||||
|
# 🔴 Dérivée 1 < 0 et dérivée 2 < 0: tendance baissière qui s’accélère.
|
||||||
|
# 🟠 Dérivée 1 < 0 et dérivée 2 > 0: tendance baissière qui ralentit → possible bottom.
|
||||||
|
#
|
||||||
|
# Filtrer les signaux: ne prendre un signal haussier que si dérivée1 > 0 et dérivée2 > 0.
|
||||||
|
# Détecter les zones de retournement: quand dérivée1 ≈ 0 et que dérivée2 change de signe.
|
||||||
|
|
||||||
|
# Dérivées
|
||||||
|
deriv1 = 10 * poly.deriv()
|
||||||
|
deriv2 = 100 * deriv1.deriv()
|
||||||
|
|
||||||
|
trend = poly(x)
|
||||||
|
slope = deriv1(x)
|
||||||
|
accel = deriv2(x)
|
||||||
|
|
||||||
|
return trend, slope, accel, poly
|
||||||
|
|
||||||
|
def polynomial_tail(series, degree=3, window=50, verbose=True):
|
||||||
|
if len(series) < window:
|
||||||
|
raise ValueError("Not enough data points for the specified window")
|
||||||
|
|
||||||
|
# On garde les N dernières valeurs
|
||||||
|
tail = series[-window:]
|
||||||
|
x = np.arange(window)
|
||||||
|
y = tail.values
|
||||||
|
|
||||||
|
# Fit du polynôme
|
||||||
|
coeffs = np.polyfit(x, y, degree)
|
||||||
|
poly = np.poly1d(coeffs)
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(f"Régression sur les {window} derniers points :")
|
||||||
|
print(poly)
|
||||||
|
|
||||||
|
# Évaluation sur la même fenêtre
|
||||||
|
trend = poly(x)
|
||||||
|
slope = poly.deriv()(x)
|
||||||
|
accel = poly.deriv().deriv()(x)
|
||||||
|
|
||||||
|
return trend, slope, accel, poly
|
||||||
|
|
||||||
|
|
||||||
|
# Exemple avec une série synthétique
|
||||||
|
np.random.seed(0)
|
||||||
|
x = np.linspace(0, 20, 250)
|
||||||
|
y = 0.05 * x**3 - x**2 + 2 * x + 5 + np.random.normal(0, 15, size=len(x))
|
||||||
|
series = pd.Series(y)
|
||||||
|
|
||||||
|
trend, slope, accel, poly = polynomial_features(series, degree=3)
|
||||||
|
|
||||||
|
# 🔍 Visualisation
|
||||||
|
plt.figure(figsize=(10, 6))
|
||||||
|
plt.plot(series, label="Original", alpha=0.5)
|
||||||
|
plt.plot(trend, label="Trend (Poly)", linewidth=2)
|
||||||
|
plt.plot(slope, label="1st Derivative", linestyle='--')
|
||||||
|
plt.plot(accel, label="2nd Derivative", linestyle=':')
|
||||||
|
plt.legend()
|
||||||
|
plt.title("Régression polynomiale + dérivées")
|
||||||
|
plt.grid(True)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# trend, slope, accel, poly = polynomial_tail(series, degree=3, window=50)
|
||||||
|
#
|
||||||
|
# # Visualisation de la portion utile
|
||||||
|
# plt.figure(figsize=(10, 5))
|
||||||
|
# plt.plot(series[-50:].values, label="Original (Last 50)")
|
||||||
|
# plt.plot(trend, label="Trend (Poly)", linewidth=2)
|
||||||
|
# plt.plot(slope, label="1st Derivative", linestyle='--')
|
||||||
|
# plt.plot(accel, label="2nd Derivative", linestyle=':')
|
||||||
|
# plt.title("Régression polynomiale sur les 50 dernières valeurs")
|
||||||
|
# plt.legend()
|
||||||
|
# plt.grid(True)
|
||||||
|
# plt.show()
|
||||||
42
tools/polynomiale2.py
Normal file
42
tools/polynomiale2.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
def rolling_polyfit_curves(series, degree=3, window=50, step=10):
|
||||||
|
curves = [] # Liste des courbes
|
||||||
|
positions = [] # Index de départ pour chaque courbe
|
||||||
|
|
||||||
|
for i in range(window, len(series), step):
|
||||||
|
y = series[i - window:i].values
|
||||||
|
x = np.arange(window)
|
||||||
|
coeffs = np.polyfit(x, y, degree)
|
||||||
|
poly = np.poly1d(coeffs)
|
||||||
|
curve = poly(x)
|
||||||
|
curves.append(curve)
|
||||||
|
positions.append(i)
|
||||||
|
|
||||||
|
return curves, positions
|
||||||
|
|
||||||
|
# Exemple d'utilisation
|
||||||
|
|
||||||
|
# Exemple avec une série synthétique
|
||||||
|
np.random.seed(0)
|
||||||
|
x = np.linspace(0, 20, 250)
|
||||||
|
y = 0.05 * x**3 - x**2 + 2 * x + 5 + np.random.normal(0, 15, size=len(x))
|
||||||
|
series = pd.Series(y)
|
||||||
|
|
||||||
|
# series = pd.Series(np.sin(np.linspace(0, 10*np.pi, 300)) + np.random.normal(0, 0.2, 300))
|
||||||
|
curves, positions = rolling_polyfit_curves(series, degree=3, window=50, step=10)
|
||||||
|
|
||||||
|
# Visualisation
|
||||||
|
plt.figure(figsize=(12, 6))
|
||||||
|
plt.plot(series.values, label="Original")
|
||||||
|
|
||||||
|
for curve, pos in zip(curves, positions):
|
||||||
|
x_global = np.arange(pos - 50, pos)
|
||||||
|
plt.plot(x_global, curve, alpha=0.6)
|
||||||
|
|
||||||
|
plt.title("Régressions polynomiales glissantes (chaque 10 bougies sur 50 précédentes)")
|
||||||
|
plt.legend()
|
||||||
|
plt.grid(True)
|
||||||
|
plt.show()
|
||||||
Reference in New Issue
Block a user