趋势指标:移动平均

Python 中的金融交易

Chelsea Yang

Data Science Instructor

什么是技术指标?

  • 基于历史市场数据的数学计算
  • 假设市场有效,价格已反映所有公开信息
  • 帮助交易者洞察过去的价格模式
Python 中的金融交易

指标类型

  • 趋势指标:衡量趋势方向或强度
    • 示例:移动平均(MA)、平均趋向指数(ADX)
  • 动量指标:衡量价格变动速度
    • 示例:相对强弱指数(RSI)
  • 波动率指标:衡量价格偏离幅度
    • 示例:布林带
Python 中的金融交易

TA-Lib 包

TA-Lib:技术分析库(Technical Analysis Library)

  • 内置 150+ 个技术指标实现
import talib
Python 中的金融交易

移动平均指标

  • SMA:简单移动平均
  • EMA:指数移动平均

 

  • 特点:
    • 随价格移动
    • 平滑数据,更好指示价格方向
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 中的金融交易

¡Vamos a practicar!

Python 中的金融交易

Preparing Video For Download...