Strategie podążające za trendem

Financial Trading w Pythonie

Chelsea Yang

Data Science Instructor

Dwa rodzaje strategii handlowych

Podążanie za trendem

  • Zakład, że trend cenowy będzie kontynuowany
  • Sygnały transakcyjne budowane na wskaźnikach trendowych, takich jak średnie kroczące, ADX itp.

Powrót do średniej

  • Zakład, że cena powróci do średniej
  • Sygnały transakcyjne budowane na wskaźnikach takich jak RSI, wstęgi Bollingera itp.
Financial Trading w Pythonie

Strategia przecięcia MA

Trend jest Twoim przyjacielem.

 

  • Przecięcie dwóch EMA:
    • Sygnał długi: krótkoterminowa EMA przecina długoterminową EMA od dołu
    • Sygnał krótki: krótkoterminowa EMA przecina długoterminową EMA od góry
Financial Trading w Pythonie

Obliczanie wskaźników

import talib
# Calculate the indicators
EMA_short = talib.EMA(price_data['Close'],
                      timeperiod=10).to_frame()
EMA_long = talib.EMA(price_data['Close'], 
                     timeperiod=40).to_frame()
Financial Trading w Pythonie

Konstruowanie sygnału

# Create the signal DataFrame
signal = EMA_long.copy()
signal[EMA_long.isnull()] = 0

# Construct the signal signal[EMA_short > EMA_long] = 1
signal[EMA_short < EMA_long] = -1
Financial Trading w Pythonie

Wykres sygnału

# Plot the signal, price and MAs
combined_df = bt.merge(signal, price_data, EMA_short, EMA_long)
combined_df.columns = ['Signal', 'Price', 'EMA_short', 'EMA_long']

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

Wykres sygnału EMA_crossover

Financial Trading w Pythonie

Definiowanie strategii z sygnałem

# Define the strategy
bt_strategy = bt.Strategy('EMA_crossover',

[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

Wykres wyników backtestingu

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

Wynik backtestingu MA_crossover

Financial Trading w Pythonie

Czas na ćwiczenia!

Financial Trading w Pythonie

Preparing Video For Download...