การปรับแต่งกลยุทธ์และการเปรียบเทียบมาตรฐาน

การเทรดทางการเงินด้วย Python

Chelsea Yang

Data Science Instructor

จะกำหนดค่าพารามิเตอร์อินพุตอย่างไร?

  • คำถาม:

    • ควรใช้ช่วงย้อนหลัง SMA เท่าใดในสัญญาณ "Price > SMA"?
  • วิธีแก้: การปรับแต่งกลยุทธ์

    • ลองใช้ค่าพารามิเตอร์อินพุตหลายช่วงในการทดสอบย้อนหลัง แล้วเปรียบเทียบผลลัพธ์
การเทรดทางการเงินด้วย Python

ตัวอย่างการปรับแต่งกลยุทธ์

def signal_strategy(ticker, period, name, start='2018-4-1', end='2020-11-1'):

# Get the data and calculate SMA price_data = bt.get(ticker, start=start, end=end) sma = price_data.rolling(period).mean()
# Define the signal-based strategy bt_strategy = bt.Strategy(name, [bt.algos.SelectWhere(price_data>sma), bt.algos.WeighEqually(), bt.algos.Rebalance()])
# Return the backtest return bt.Backtest(bt_strategy, price_data)
1 www.datacamp.com/courses/writing-functions-in-python
การเทรดทางการเงินด้วย Python

ตัวอย่างการปรับแต่งกลยุทธ์

ticker = 'aapl'
sma20 = signal_strategy(ticker, 
                        period=20, name='SMA20')
sma50 = signal_strategy(ticker, 
                        period=50, name='SMA50')
sma100 = signal_strategy(ticker, 
                        period=100, name='SMA100')

# Run backtests and compare results bt_results = bt.run(sma20, sma50, sma100) bt_results.plot(title='Strategy optimization')

กราฟแสดงผลการปรับแต่งกลยุทธ์

การเทรดทางการเงินด้วย Python

Benchmark คืออะไร?

มาตรฐานหรือจุดอ้างอิงที่ใช้เปรียบเทียบหรือประเมินกลยุทธ์

  • ตัวอย่าง:
    • กลยุทธ์ที่ใช้สัญญาณซื้อขายหุ้นแบบ active สามารถใช้กลยุทธ์ buy and hold แบบ passive เป็น benchmark ได้
    • ดัชนี S&P 500 มักถูกใช้เป็น benchmark สำหรับหุ้น
    • พันธบัตรรัฐบาลสหรัฐฯ ใช้วัดความเสี่ยงและผลตอบแทนของตราสารหนี้
การเทรดทางการเงินด้วย Python

ตัวอย่างการใช้ benchmark

def buy_and_hold(ticker, name, start='2018-11-1', end='2020-12-1'):

# Get the data price_data = bt.get(ticker, start=start_date, end=end_date)
# Define the benchmark strategy bt_strategy = bt.Strategy(name, [bt.algos.RunOnce(), bt.algos.SelectAll(), bt.algos.WeighEqually(), bt.algos.Rebalance()])
# Return the backtest return bt.Backtest(bt_strategy, price_data)
การเทรดทางการเงินด้วย Python

ตัวอย่างการใช้ benchmark

benchmark = buy_and_hold(ticker, name='benchmark')

# Run all backtests and plot the resutls bt_results = bt.run(sma20, sma50, sma100, benchmark) bt_results.plot(title='Strategy benchmarking')

ผลลัพธ์การเปรียบเทียบ benchmark

การเทรดทางการเงินด้วย Python

มาฝึกกันเถอะ!

การเทรดทางการเงินด้วย Python

Preparing Video For Download...