Stratégie de retour à la moyenne

Trading financier en Python

Chelsea Yang

Data Science Instructor

Stratégie RSI de retour à la moyenne

Acheter quand la peur domine, vendre quand la cupidité s'emballe

 

  • Stratégie de retour à la moyenne basée sur le RSI :
    • Signal vendeur : RSI > 70
      • Indique un actif probablement suracheté, avec possible retournement de prix
    • Signal acheteur : RSI < 30
      • Indique un actif probablement survendu, avec possible reprise des prix
Trading financier en Python

Calculer l'indicateur

import talib
# Calculate the RSI
stock_rsi = talib.RSI(price_data['Close']).to_frame()
Trading financier en Python

Construire le signal

# Create the same DataFrame structure as RSI
signal = stock_rsi.copy()
signal[stock_rsi.isnull()] = 0

# Construct the signal signal[stock_rsi < 30] = 1
signal[stock_rsi > 70] = -1
signal[(stock_rsi <= 70) & (stock_rsi >= 30)] = 0
Trading financier en Python

Plot the signal

# Plot the RSI
stock_rsi.plot()
plt.title('RSI')

Graphique du RSI

# Merge data into one DataFrame
combined_df = bt.merge(signal, stock_data)
combined_df.columns = ['Signal', 'Price']
# Plot the signal with price

combined_df.plot(secondary_y = ['Signal'])

Graphique du signal basé sur le RSI

Trading financier en Python

Définir la stratégie avec le signal

# Define the strategy
bt_strategy = bt.Strategy('RSI_MeanReversion', 
                          [bt.algos.WeighTarget(signal),
                           bt.algos.Rebalance()])
Trading financier en Python

Backtester la stratégie basée sur le signal

# Create the backtest and run it
bt_backtest = bt.Backtest(bt_strategy, price_data)
bt_result = bt.run(bt_backtest)
Trading financier en Python

Tracer le résultat du backtest

# Plot the backtest result
bt_result.plot(title='Backtest result')

Résultat du backtest de la stratégie RSI de retour à la moyenne

Trading financier en Python

Passons à la pratique !

Trading financier en Python

Preparing Video For Download...