【量化笔记】均线交易策略

简单移动平均数

简单求过去5天的价格平均数

S M A t = 5 = p 1 + p 2 + p 3 + p 4 + p 5 5 SMA_{t=5}=\frac{p_1+p_2+p_3+p_4+p_5}{5} SMAt=5=5p1+p2+p3+p4+p5

#计算青岛啤酒的5日简单移动平均线
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
TsingTao=pd.read_csv('TsingTao.csv')
TsingTao.index=TsingTao.iloc[:,1]
TsingTao.index=pd.to_datetime(TsingTao.index, format='%Y-%m-%d')
TsingTao=TsingTao.iloc[:,2:]
TsingTao.head(n=3)
Open High Low Close Volume
Date
2014-01-02 48.80 48.98 46.90 47.81 2592800
2014-01-03 47.60 48.38 47.01 47.59 1560700
2014-01-06 47.63 47.79 46.62 46.70 1860900
Close=TsingTao.Close
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.plot(Close,'k')
plt.xlabel('date')
plt.ylabel('Close')
plt.title('2014年青岛啤酒股票收盘价时序图')
/Users/yaochenli/anaconda3/lib/python3.7/site-packages/pandas/plotting/_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
	>>> from pandas.plotting import register_matplotlib_converters
	>>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)





Text(0.5, 1.0, '2014年青岛啤酒股票收盘价时序图')



/Users/yaochenli/anaconda3/lib/python3.7/site-packages/matplotlib/font_manager.py:1241: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans.
  (prop.get_family(), self.defaultFamily[fontext]))

在这里插入图片描述

Sma5=pd.Series(0.0,index=Close.index)
for i in range(4,len(Close)):
    Sma5[i]=np.mean(Close[(i-4):(i+1)])

Sma5.tail()
Date
2015-04-24    46.522
2015-04-27    46.462
2015-04-28    45.936
2015-04-29    45.430
2015-04-30    44.950
dtype: float64
plt.plot(Close[4:],label="Close",color='g')
plt.plot(Sma5[4:],label="Sma5",color='r',linestyle='dashed')
plt.title("青岛啤酒股票价格图")
plt.ylim(35,50)
plt.legend()
<matplotlib.legend.Legend at 0x12242bef0>

在这里插入图片描述

封装成函数,包含一个参数表示几期平均数

def smaCal(tsPrice,k):
    import pandas as pd
    Sma=pd.Series(0.0,index=tsPrice.index)
    for i in range(k-1,len(tsPrice)):
        Sma[i]=sum(tsPrice[(i-k+1):(i+1)])/k
    return(Sma)
sma5=smaCal(Close ,5)    
sma5.tail()
Date
2015-04-24    46.522
2015-04-27    46.462
2015-04-28    45.936
2015-04-29    45.430
2015-04-30    44.950
dtype: float64

加权平均移动

W M A t = 5 = w 1 p 1 + w 2 p 2 + w 3 p 3 + w 4 p 4 + w 5 p 5 WMA_{t=5}=w_1p_1+w_2 p_2+w_3 p_3+w_4 p_4 + w_5 p_5 WMAt=5=w1p1+w2p2+w3p3+w4p4+w5p5

# 定义权重
b=np.array([1,2,3,4,5])
w=b/sum(b)
w
array([0.06666667, 0.13333333, 0.2       , 0.26666667, 0.33333333])
#计算加权平均移动
Wma5=pd.Series(0.0,index=Close.index)
for i in range(4,len(Close)):
    Wma5[i]=sum(w*Close[(i-4):(i+1)])
Wma5[2:7]
Date
2014-01-06     0.000000
2014-01-07     0.000000
2014-01-08    46.778667
2014-01-09    46
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值