Strategia powrotu do średniej

Financial Trading w Pythonie

Chelsea Yang

Data Science Instructor

Strategia mean reversion oparta na RSI

Kupuj w strachu, sprzedawaj w chciwości

 

  • Strategia mean reversion oparta na RSI:
    • Sygnał krótki: RSI > 70
      • Sugeruje wykupienie aktywu i możliwe odwrócenie trendu
    • Sygnał długi: RSI < 30
      • Sugeruje wyprzedanie aktywu i możliwe odbicie ceny
Financial Trading w Pythonie

Obliczanie wskaźnika

import talib
# Calculate the RSI
stock_rsi = talib.RSI(price_data['Close']).to_frame()
Financial Trading w Pythonie

Konstruowanie sygnału

# 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
Financial Trading w Pythonie

Wizualizacja sygnału

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

Wykres 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'])

Wykres sygnału opartego na RSI

Financial Trading w Pythonie

Definiowanie strategii na podstawie sygnału

# Define the strategy
bt_strategy = bt.Strategy('RSI_MeanReversion', 
                          [bt.algos.WeighTarget(signal),
                           bt.algos.Rebalance()])
Financial Trading w Pythonie

Backtesting strategii opartej na sygnale

# Create the backtest and run it
bt_backtest = bt.Backtest(bt_strategy, price_data)
bt_result = bt.run(bt_backtest)
Financial Trading w Pythonie

Wizualizacja wyników backtestingu

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

Wynik backtestingu strategii RSI mean-reversion

Financial Trading w Pythonie

Czas na ćwiczenia!

Financial Trading w Pythonie

Preparing Video For Download...