Stratégie de retour à la moyenne

Négociation financière en Python

Chelsea Yang

Data Science Instructor

Stratégie RSI de retour à la moyenne

Acheter quand la peur domine, vendre quand l'avidité prend le dessus

 

  • Stratégie de retour à la moyenne fondée sur le RSI :
    • Signal de vente : RSI > 70
      • Indique que l'actif est sans doute suracheté et que le prix pourrait se retourner bientôt
    • Signal d'achat : RSI < 30
      • Indique que l'actif est sans doute survendu et que le prix pourrait rebondir bientôt
Négociation financière en Python

Calculer l'indicateur

import talib
# Calculate the RSI
stock_rsi = talib.RSI(price_data['Close']).to_frame()
Négociation financière 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
Négociation financière en Python

Tracer le 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

Négociation financière 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()])
Négociation financière en Python

Tester en historique la stratégie fondée sur le signal

# Create the backtest and run it
bt_backtest = bt.Backtest(bt_strategy, price_data)
bt_result = bt.run(bt_backtest)
Négociation financière en Python

Tracer le résultat du test historique

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

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

Négociation financière en Python

Passons à la pratique !

Négociation financière en Python

Preparing Video For Download...