70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
import pandas as pd
|
|
|
|
|
|
def detect_btc_crashes_and_rebounds(df, drop_threshold=-10, rebound_threshold=5):
|
|
"""
|
|
Identifie les plus grosses chutes du BTC et le rebond suivant.
|
|
|
|
Paramètres :
|
|
- df : DataFrame contenant une colonne 'close' avec les prix du BTC.
|
|
- drop_threshold : Seuil de chute en % (ex: -10 pour une chute > 10%).
|
|
- rebound_threshold : Seuil de rebond en % après la chute.
|
|
|
|
Retourne :
|
|
- Une liste des chutes avec le pourcentage de rebond correspondant.
|
|
"""
|
|
df['return'] = df['close'].pct_change() * 100 # Variation en %
|
|
crashes = []
|
|
|
|
in_crash = False
|
|
crash_start, crash_end, bottom, rebound_end = None, None, None, None
|
|
|
|
for i in range(1, len(df)):
|
|
change = df.loc[i, 'return']
|
|
|
|
# Début d'une chute
|
|
if change < drop_threshold and not in_crash:
|
|
crash_start = i - 1
|
|
in_crash = True
|
|
|
|
# En pleine chute, trouver le creux
|
|
if in_crash:
|
|
if bottom is None or df.loc[i, 'close'] < df.loc[bottom, 'close']:
|
|
bottom = i
|
|
|
|
# Fin de la chute et début du rebond
|
|
if in_crash and change > 0:
|
|
crash_end = i
|
|
|
|
# Identifier un rebond
|
|
if in_crash and crash_end:
|
|
rebound_percent = (df.loc[i, 'close'] - df.loc[bottom, 'close']) / df.loc[bottom, 'close'] * 100
|
|
if rebound_percent > rebound_threshold:
|
|
rebound_end = i
|
|
crashes.append({
|
|
'crash_start': df.index[crash_start],
|
|
'crash_end': df.index[crash_end],
|
|
'bottom': df.index[bottom],
|
|
'rebound_end': df.index[rebound_end],
|
|
'drop_percent': (df.loc[bottom, 'close'] - df.loc[crash_start, 'close']) / df.loc[
|
|
crash_start, 'close'] * 100,
|
|
'rebound_percent': rebound_percent
|
|
})
|
|
in_crash = False # Reset pour détecter une nouvelle chute
|
|
|
|
return crashes
|
|
|
|
import yfinance as yf
|
|
|
|
df = yf.download("BTC-USD", start="2020-01-01", end="2025-01-01", interval="1d")
|
|
df.to_csv("btc_prices.csv")
|
|
print("Données enregistrées !")
|
|
|
|
|
|
# Exemple d'utilisation avec un DataFrame BTC (OHLCV)
|
|
df = pd.read_csv("btc_prices.csv", parse_dates=["date"], index_col="date")
|
|
crashes = detect_btc_crashes_and_rebounds(df)
|
|
|
|
for crash in crashes:
|
|
print(crash)
|