量化交易第一节课:一个简单低买高卖策略
2022-07-20 00:32:11
323次阅读
0个评论
最后修改时间:2022-07-20 00:33:42

量化交易环境配置

网上下载anaconda并且自己网上搜索安装教程,然后自己学习一下jupyter Notebook的基本使用方法。由于篇幅有限,再此就不详细去讲具体的安装和学习过程。


用真实的股票数据练手


#安装数据源
pip install pandas_datareader --upgrade
安装结果
<pre class="prettyprint"><code>Collecting pandas_datareader
  Downloading pandas_datareader-0.10.0-py3-none-any.whl (109 kB)
     |████████████████████████████████| 109 kB 1.2 MB/s eta 0:00:01
Requirement already satisfied: lxml in ./opt/anaconda3/lib/python3.9/site-packages (from pandas_datareader) (4.8.0)
Requirement already satisfied: requests>=2.19.0 in ./opt/anaconda3/lib/python3.9/site-packages (from pandas_datareader) (2.27.1)
Requirement already satisfied: pandas>=0.23 in ./opt/anaconda3/lib/python3.9/site-packages (from pandas_datareader) (1.4.2)
Requirement already satisfied: python-dateutil>=2.8.1 in ./opt/anaconda3/lib/python3.9/site-packages (from pandas>=0.23->pandas_datareader) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in ./opt/anaconda3/lib/python3.9/site-packages (from pandas>=0.23->pandas_datareader) (2021.3)
Requirement already satisfied: numpy>=1.18.5 in ./opt/anaconda3/lib/python3.9/site-packages (from pandas>=0.23->pandas_datareader) (1.21.5)
Requirement already satisfied: six>=1.5 in ./opt/anaconda3/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas>=0.23->pandas_datareader) (1.16.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./opt/anaconda3/lib/python3.9/site-packages (from requests>=2.19.0->pandas_datareader) (1.26.9)
Requirement already satisfied: charset-normalizer~=2.0.0 in ./opt/anaconda3/lib/python3.9/site-packages (from requests>=2.19.0->pandas_datareader) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in ./opt/anaconda3/lib/python3.9/site-packages (from requests>=2.19.0->pandas_datareader) (2021.10.8)
Requirement already satisfied: idna<4,>=2.5 in ./opt/anaconda3/lib/python3.9/site-packages (from requests>=2.19.0->pandas_datareader) (3.3)
Installing collected packages: pandas-datareader
Successfully installed pandas-datareader-0.10.0
Note: you may need to restart the kernel to use updated packages.</code></pre>
1.下载股票数据 

<pre class="prettyprint lang-py"><code>###获取【国美电器】股票数据
import pandas_datareader as web
start_date='2020-01-01'
end_date = '2020-03-08'
data = web.data.DataReader('0493.HK','yahoo',start_date,end_date)
data.head()</code> </pre>
<img src="file/topic/2022-07-20/image/39a3b3ee23ae47aea84e62a19396df92b1.png" alt="">

2.最简单的数据处理

#计算每日涨跌幅
data['diff']=data['Close'].diff()
data.head()

3.设计简单的交易策略
#设置一个简单交易策略【当天股价上涨,则卖出。股价下跌则买入】
import numpy as np
data['Signal'] = np.where(data['diff']>0,1,0)
data.head()

4.交易信号可视化
#将买入和卖出信号绘图出来,红色买入,绿色卖出
import matplotlib.pyplot as plt
plt.figure(figsize=(10,5))
data['Close'].plot(linewidth=2,color='k',grid=True)
plt.scatter(data['Close'].loc[data.Signal==1].index,data['Close'].loc[data.Signal==1],marker='v',s=80,c='g')
plt.scatter(data['Close'].loc[data.Signal==0].index,data['Close'].loc[data.Signal==0],marker='^',s=80,c='r')
plt.show()

结果分析:

红色正三角代表买入,绿色倒三角代表卖出。主观来看,股价下跌的天数比上涨的天数多。假设每次买入和卖出是一样数量的股票,那持有这只股票的仓位会越来越重。至于这种交易策略带来的回报是多少?还需要通过回测(backtesting)来进行评估。关于评测的具体方法,我会在下一节课告诉大家。


收藏 0 0

登录 后评论。没有帐号? 注册 一个。

harries

VIP角色
  • 0 回答
  • 0 粉丝
  • 0 关注