BaşlayınÜcretsiz Başlayın

Conduct a strategy optimization

You have an SMA-based signal strategy to trade stocks. However, you are not sure what lookback period to use for calculating the SMA that can optimize the strategy performance. You plan to run multiple backtests on different input parameters. Also, you want the capability to assess the strategy on trading different stocks or based on different historical periods.

The bt package has been imported for you. Also, the historical price data of the Tesla stock has been preloaded in price_data.

Bu egzersiz

Financial Trading in Python

kursunun bir parçasıdır
Kursu Görüntüle

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

def signal_strategy(price_data, period, name):
    # Calculate SMA
    sma = price_data.____
    # Define the signal-based Strategy
    bt_strategy = bt.Strategy(name, 
                              [____,
                               bt.algos.WeighEqually(),
                               bt.algos.Rebalance()])
    # Return the backtest
    return ____(bt_strategy, price_data)
Kodu Düzenle ve Çalıştır