トレンド指標の移動平均

Pythonで学ぶ金融トレーディング

Chelsea Yang

Data Science Instructor

テクニカル指標とは

  • 過去のマーケットデータに基づく数理計算
  • 市場は効率的で、価格に公開情報が織り込まれていると仮定
  • 過去の価格パターンの把握を支援
Pythonで学ぶ金融トレーディング

指標の種類

  • トレンド系指標: トレンドの方向や強さを測定
    • 例: 移動平均(MA)、平均方向性指数(ADX)
  • モメンタム指標: 価格変動の勢いを測定
    • 例: 相対力指数(RSI)
  • ボラティリティ指標: 価格の振れ幅を測定
    • 例: ボリンジャーバンド
Pythonで学ぶ金融トレーディング

TA-Lib パッケージ

TA-Lib : Technical Analysis Library

  • 150 以上のテクニカル指標を実装
import talib
Pythonで学ぶ金融トレーディング

移動平均の指標

  • SMA: Simple Moving Average(単純移動平均)
  • EMA: Exponential Moving Average(指数移動平均)

 

  • 特徴:
    • 価格に追随
    • データを平滑化して方向を示す
Pythonで学ぶ金融トレーディング

単純移動平均(SMA)

$ SMA = (P_1+P_2+...+P_n)/n $

# Calculate two SMAs
stock_data['SMA_short'] = talib.SMA(stock_data['Close'], timeperiod=10)
stock_data['SMA_long'] = talib.SMA(stock_data['Close'], timeperiod=50)

# Print the last five rows print(stock_data.tail())
             Close  SMA_short  SMA_long
Date                                   
2020-11-24 1768.88    1758.77   1594.74
2020-11-25 1771.43    1760.65   1599.75
2020-11-27 1793.19    1764.98   1605.70
2020-11-30 1760.74    1763.35   1611.71
2020-12-01 1798.10    1765.02   1619.05

Pythonで学ぶ金融トレーディング

SMA のプロット

import matplotlib.pyplot as plt

# Plot SMA with the price
plt.plot(stock_data['SMA_short'], 
         label='SMA_short')
plt.plot(stock_data['SMA_long'], 
         label='SMA_long')
plt.plot(stock_data['Close'], 
         label='Close')

# Customize and show the plot
plt.legend()
plt.title('SMAs')
plt.show()

SMAのプロット

Pythonで学ぶ金融トレーディング

指数移動平均(EMA)

$EMA_n = P_n \times multiplier + \text{previous EMA} \times (1-multiplier)$

$multiplier = 2 / (n + 1)$

# Calculate two EMAs
stock_data['EMA_short'] = talib.EMA(stock_data['Close'], timeperiod=10)
stock_data['EMA_long'] = talib.EMA(stock_data['Close'], timeperiod=50)
# Print the last five rows
print(stock_data.tail())
             Close  EMA_short  EMA_long
Date                                   
2020-11-24 1768.88    1748.65   1640.98
2020-11-25 1771.43    1752.79   1646.09
2020-11-27 1793.19    1760.13   1651.86
2020-11-30 1760.74    1760.24   1656.13
2020-12-01 1798.10    1767.13   1661.70
Pythonで学ぶ金融トレーディング

EMA のプロット

import matplotlib.pyplot as plt
# Plot EMA with the price
plt.plot(stock_data['EMA_short'], 
         label='EMA_short')
plt.plot(stock_data['EMA_long'], 
         label='EMA_long')
plt.plot(stock_data['Close'], 
         label='Close')

# Customize and show the plot
plt.legend()
plt.title('EMAs')
plt.show()

EMAのプロット

Pythonで学ぶ金融トレーディング

SMA と EMA の比較

EMA は直近の価格変動により敏感です

同一の期間で計算した SMA と EMA の比較プロット

Pythonで学ぶ金融トレーディング

練習しましょう!

Pythonで学ぶ金融トレーディング

Preparing Video For Download...