第一阶段、一个简单策略入门量化投资

1-2 移动均线交叉策略1

  • 第一阶段一个简单策略入门量化投资
  • 1-2 移动均线交叉策略1
    • 前言
    • 获取数据
    • 移动均线交叉策略
    • 数据可视化
      • 绘制折线图
      • 绘制K线图
      • 绘制移动均线
    • 移动均线交叉策略回测
      • 什么是回测
      • 回溯买卖信号
      • 计算收益
    • 未完待续
    • 完整代码

前言

本学期订了两个目标:探索量化投资与熟练掌握python
以量化投资为切入点,以python为工具,在研究策略过程中又可以练习python,两个目标相辅相成,想法很美好,于是就有了此文。
作为一个初学者,我定下了第一阶段的目标:
通过一个简单策略,梳理研究并编写一个交易策略的整个流程,从而入门量化投资。
我在网上找到了一篇不错的入门教程:
Python股市数据分析教程——学会它,或可以实现半“智能”炒股 (Part 1)
这也是我的主要参考资料,本文是在此基础上梳理出来的,也可以说是学习过程的记录。下面开始正文


获取数据

在1-1股票数据预处理练习中我们已经介绍了如何获取一只股票的数据并且进行一定的修改以满足我们的需要
使用如下代码,获取苹果公司2010年至今的股票数据

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import datetime
import time##### get historical data of a stock
# the first time we get the stock data from the interface of yahoo
# but we may not want to do this step all the time, so we try to save it in .csv
#start = datetime.datetime(2016, 1, 1)
#end = datetime.date.today()
#apple = web.DataReader("AAPL", "yahoo", start, end)
#print(apple.head())
#apple.to_csv(path_or_buf='data_AAPL.csv')##### read the data from csv
apple=pd.read_csv(filepath_or_buffer='data_AAPL.csv')
print(apple.head())
# note that some format(data type) of data we read from .csv has changed
# for example the attribute 'Date' should be the index of the dataframe, and the date type changed from datetime to string
# this changes would made some methods got trouble
# So we need to make the following changes
date_list = []
for i in range(len(apple)):date_str = apple['Date'][i]t = time.strptime(date_str, "%Y-%m-%d")temp_date = datetime.datetime(t[0], t[1], t[2])date_list.append(temp_date)
apple['DateTime'] = pd.Series(date_list,apple.index)
del apple['Date']
apple = apple.set_index('DateTime')

这样,数据准备工作就完成了

移动均线交叉策略

许多交易策略都试图去找到股票价格的趋势,移动均线交叉策略就是其中最简单的一种。我们来讨论下它是如何工作的。
什么是移动均线?
对于序列xt以及时刻t,q天均线表示过去q天股价的均值:也就是说,如果MAtq表示t时刻的q天均线,那么:

显然,移动均线平滑了数据序列,并有助于识别股市的发展趋势。q值越大,移动均线就越难反映序列xt中的短期波动,但也更好的把握了整体的趋势。
什么是均线交叉策略
均线交叉策略的想法是,利用一长一短两条移动均线能够从”噪声”中识别股市的发展趋势。短期均线具有较小的q值,比较紧密地跟随股票的趋势发展,更能代表短期内的股价变动;而长期均线的q值较大,进而使得均线对股票波动的响应较小,而且更加平稳。
因此,移动均线交叉策略描述如下:
当短期均线超越长期均线时,说明股票价格短期内程上涨的趋势,我们将这时作为买入时机。当短期均线跌落到长期均线以下时,认为牛市结束,将股票卖出。
下面,我们通过可视化的方法,绘制出股票的历史数据图,以及两条长短均线,来直观的感受下这个策略。

数据可视化

绘制折线图

我们利用获取的数据中的股票收盘价,绘制出相应的折线图。

apple["Adj Close"].plot(grid=True)

绘制K线图

很显然这种表达过于单调,而且我们获取的数据还包含开盘价、最高价、最低价这些信息没有使用。
众所周知,金融数据通常以日本蜡烛图(即K线图)的形式绘制,这种图表最早在18世纪由日本米市商人命名。matplotlib可以绘制这样的图表,但操作起来比较复杂。于是此文的作者实现了一个函数,可以更容易地在pandas数据框架中创建蜡烛图,并使用它绘制我们的股票数据。你还可以参考matplotlib提供的示例 戳这里.
我们调用这个函数,来可视化股票数据(代码可在文末查看)。

draw_candle.pandas_candlestick_ohlc(apple,stick = 'month')

其中stick参数用于指明绘制每个“小蜡烛”的时间间隔,效果如下:

绘制移动均线

首先为苹果股票创建了三条移动均线,滑动平均的范围分别是20天、50天、200天。随后,将其与股票数据一同绘制在图表中。
pandas提供了轻松计算移动均线的功能,下面的代码展示了这部分功能。

apple["20d"] = np.round(apple["Close"].rolling(window = 20, center = False).mean(), 2)
apple["50d"] = np.round(apple["Close"].rolling(window = 50, center = False).mean(), 2)
apple["100d"] = np.round(apple["Close"].rolling(window = 200, center = False).mean(), 2)
draw_candle.pandas_candlestick_ohlc(apple.loc['2016-01-04':'2017-09-01',:],stick='week',otherseries = ["20d", "50d", "100d"])


多多观察不同滑动长度的移动均线组合而成的交叉策略,以及均线交叉策略作用在不同股票上的图表,能够对该策略有一些更好的认识。

移动均线交叉策略回测

什么是回测

股票回测是指设定了某些股票策略后,基于历史已经发生过的真实行情数据,在历史上某一个时间点开始,严格按照设定的组合进行选股,并模拟真实金融市场交易的规则进行模型买入、模型卖出,得出一个时间段内的盈利率、最大回撤率等数据。
回测是股票模型创建中必不可少的环境,虽然回测效果非常好的策略实盘也不一定表现好,但连回测效果都不好的策略,就无法立足了。

回溯买卖信号

我们现在依照移动均线交叉策略的规则,回溯出在苹果股票上利用该策略产生的买卖信号,进而方便后面计算策略收益。
依照已经制定的策略,定义买卖点如下:
买点:短期均线>长期均线
卖点:短期均线<=长期均线
这个过程可通过下面代码完成:

##### compute the sub of the long time span moving average and the short one
# use regime to represent it
apple['20d-50d'] = apple['20d'] - apple['50d']
apple["Regime"] = np.where(apple['20d-50d'] > 0, 1, 0)
apple["Regime"] = np.where(apple['20d-50d'] < 0, -1, apple["Regime"])
regime_count = apple["Regime"].value_counts()##### use Regime to compute the trading signal
regime_orig = apple.ix[-1, "Regime"]
apple.ix[-1, "Regime"] = 0
apple["Signal"] = np.sign(apple["Regime"] - apple["Regime"].shift(1))
apple.ix[-1, "Regime"] = regime_orig##### build the trading signal dataframe ( it is a list shows the buy and sell operations of the strategy )
apple_signals = pd.concat([pd.DataFrame({"Price": apple.loc[apple["Signal"] == 1, "Close"],"Regime": apple.loc[apple["Signal"] == 1, "Regime"],"Signal": "Buy"}),pd.DataFrame({"Price": apple.loc[apple["Signal"] == -1, "Close"],"Regime": apple.loc[apple["Signal"] == -1, "Regime"],"Signal": "Sell"}),])
apple_signals.sort_index(inplace = True)
print(apple_signals.head())

回溯的买卖信号表单如下所示:

DateTime Price Regime Signal
2010-03-15 31.977142 1.0 Buy
2010-06-11 36.215714 -1.0 Sell
2010-06-18 39.152859 1.0 Buy
2010-07-22 37.002857 -1.0 Sell
2010-08-16 35.377144 0.0 Buy

计算收益

我们称买入-卖出为一次完整的交易,那么利用回溯的买卖点表单,我们可以回测出均线交叉策略在历史数据上进行的每笔交易,以及每笔交易产生的收益。代码如下:

##### use the trading signal dataframe apple_signals to compute the profit
apple_long_profits = pd.DataFrame({"Price": apple_signals.loc[(apple_signals["Signal"] == "Buy") &apple_signals["Regime"] == 1, "Price"],"Profit": pd.Series(apple_signals["Price"] - apple_signals["Price"].shift(1)).loc[apple_signals.loc[(apple_signals["Signal"].shift(1) == "Buy") & (apple_signals["Regime"].shift(1) == 1)].index].tolist(),"End Date": apple_signals["Price"].loc[apple_signals.loc[(apple_signals["Signal"].shift(1) == "Buy") & (apple_signals["Regime"].shift(1) == 1)].index].index})
print(apple_long_profits.head())
draw_candle.pandas_candlestick_ohlc(apple, stick = 45, otherseries = ["20d", "50d"])

得到的交易清单如下:

DateTime End Date Price Profit
2010-03-15 2010-06-11 31.977142 4.238572
2010-06-18 2010-07-22 39.152859 -2.150002
2010-09-20 2011-03-30 40.461430 9.342857
2011-05-12 2011-05-27 49.509998 -1.308571
2011-07-14 2011-11-17 51.110001 2.805713


进行简单统计可以看到:
苹果股票在2010年1月1日至今这段时间内
如果我们从第一天买入股票,一直持有股票,最后一天卖出,获得的收益是每股124.02美元,收益率为412%
如果按照我们的策略进行买卖,总共完成了21笔交易,收益为美股82.35美元,收益率为273%

未完待续…

我们可以很直观的看到,得到的结果有点让人哭笑不得,显然按照移动均线交叉策略得到的结果还不如不使用任何策略来的靠谱。事实真的是这样吗?
此外,我们的策略还有很多漏洞,回测的过程也有许多问题,这些都将会在后面的工作中进行完善。

完整代码

import numpy as np
import pandas as pd
import pandas_datareader.data as web
import datetime
import time
import matplotlib.pyplot as plt
import pylab
import draw_candle##### get historical data of a stock
# the first time we get the stock data from the interface of yahoo
# but we may not want to do this step all the time, so we try to save it in .csv
#start = datetime.datetime(2016, 1, 1)
#end = datetime.date.today()
#apple = web.DataReader("AAPL", "yahoo", start, end)
#print(apple.head())
#apple.to_csv(path_or_buf='data_AAPL.csv')##### read the data from csv
apple=pd.read_csv(filepath_or_buffer='data_AAPL.csv')
#print(apple.head())
# note that some format(data type) of data we read from .csv has changed
# for example the attribute 'Date' should be the index of the dataframe, and the date type changed from datetime to string
# this changes would made our methods in draw_candle.py got trouble
# So we need to make the following changes
date_list = []
for i in range(len(apple)):date_str = apple['Date'][i]t = time.strptime(date_str, "%Y-%m-%d")temp_date = datetime.datetime(t[0], t[1], t[2])date_list.append(temp_date)
apple['DateTime'] = pd.Series(date_list,apple.index)
del apple['Date']
apple = apple.set_index('DateTime')##### we can visualize the data roughly
pylab.rcParams['figure.figsize'] = (10, 6)
#apple["Adj Close"].plot(grid=True)
#draw_candle.pandas_candlestick_ohlc(apple,stick = 'month')##### compute the moving average of the history data for different time span
# use np.round to cut data to a certain accuracy
apple["20d"] = np.round(apple["Close"].rolling(window = 20, center = False).mean(), 2)
apple["50d"] = np.round(apple["Close"].rolling(window = 50, center = False).mean(), 2)
apple["100d"] = np.round(apple["Close"].rolling(window = 200, center = False).mean(), 2)
#draw_candle.pandas_candlestick_ohlc(apple.loc['2016-01-04':'2017-09-01',:],stick='week',otherseries = ["20d", "50d", "100d"])##### compute the sub of the long time span moving average and the short one
# use regime to represent it
apple['20d-50d'] = apple['20d'] - apple['50d']
apple["Regime"] = np.where(apple['20d-50d'] > 0, 1, 0)
apple["Regime"] = np.where(apple['20d-50d'] < 0, -1, apple["Regime"])
regime_count = apple["Regime"].value_counts()
#print(regime_count)
#apple.loc['2017-01-01':'2017-09-01',"Regime"].plot(ylim = (-2,2)).axhline(y = 0, color = "black", lw = 2)
#apple["Regime"].plot(ylim = (-2,2)).axhline(y = 0, color = "black", lw = 2)##### use Regime to compute the trading signal
regime_orig = apple.ix[-1, "Regime"]
apple.ix[-1, "Regime"] = 0
apple["Signal"] = np.sign(apple["Regime"] - apple["Regime"].shift(1))
apple.ix[-1, "Regime"] = regime_orig##### build the trading signal dataframe ( it is a list shows the buy and sell operations of the strategy )
# the apple_signals looks like:
#                  Price  Regime Signal
# Date
# 2010-03-15   31.977142     1.0    Buy
# 2010-06-11   36.215714    -1.0   Sell
# 2010-06-18   39.152859     1.0    Buy
# 2010-07-22   37.002857    -1.0   Sell#print(apple.loc[apple["Signal"] == 1, "Close"])
#print(apple.loc[apple["Signal"] == -1, "Close"])
apple_signals = pd.concat([pd.DataFrame({"Price": apple.loc[apple["Signal"] == 1, "Close"],"Regime": apple.loc[apple["Signal"] == 1, "Regime"],"Signal": "Buy"}),pd.DataFrame({"Price": apple.loc[apple["Signal"] == -1, "Close"],"Regime": apple.loc[apple["Signal"] == -1, "Regime"],"Signal": "Sell"}),])
apple_signals.sort_index(inplace = True)##### use the trading signal dataframe apple_signals to compute the profit
# the apple_long_profits looks like:
#               End Date       Price     Profit
# Date
# 2010-03-15  2010-06-11   31.977142   4.238572
# 2010-06-18  2010-07-22   39.152859  -2.150002
# 2010-09-20  2011-03-30   40.461430   9.342857
apple_long_profits = pd.DataFrame({"Price": apple_signals.loc[(apple_signals["Signal"] == "Buy") &apple_signals["Regime"] == 1, "Price"],"Profit": pd.Series(apple_signals["Price"] - apple_signals["Price"].shift(1)).loc[apple_signals.loc[(apple_signals["Signal"].shift(1) == "Buy") & (apple_signals["Regime"].shift(1) == 1)].index].tolist(),"End Date": apple_signals["Price"].loc[apple_signals.loc[(apple_signals["Signal"].shift(1) == "Buy") & (apple_signals["Regime"].shift(1) == 1)].index].index})
#print(apple_long_profits)
#draw_candle.pandas_candlestick_ohlc(apple, stick = 45, otherseries = ["20d", "50d"])##### take a simple analysis
# compute a rough profit (don't consider fee of the deal)
rough_profit = apple_long_profits['Profit'].sum()
print(rough_profit)# compute the profit if we don't take any operation
# (take long position at the first day and sale it on the last day of the date)
no_operation_profit = apple['Close'][-1]-apple['Close'][0]
print(no_operation_profit)plt.show()

1-2 移动均线交叉策略1相关推荐

  1. 1-3移动均线交叉策略2

    第一阶段.一个简单策略入门量化投资 1-3移动均线交叉策略2 上一篇文章1-2 移动均线交叉策略1中我们最后提到: 如果我们从第一天买入股票,一直持有股票,最后一天卖出,获得的收益是每股124.02美 ...

  2. 量化交易学习(10)均线交叉策略

    均线交叉策略 均线介绍 均线交叉策略 回测策略 优化策略参数 代码 总结 均线介绍 均线计算: 在某一时间段的收盘价之和进行算术平均的方法,并随着时间的推移将这些平均值连成一条线便可得出SMA. 趋势 ...

  3. Python量化交易学习笔记(14)——均线交叉策略

    本文使用均线交叉策略,对平安银行自2018年1月1日至2020年2月28日的日线数据进行回测分析. 策略会用到短期移动均线及长期移动均线两个技术指标,在backtrader自定义策略init方法中,添 ...

  4. 用 Pandas 分析均线交叉策略收益率

    在这篇文章中,我们会利用上一篇文章中的均线交叉策略回测中获得的结果(<用 Python 基于均线交叉策略进行回测>),并花一些时间更深入地挖掘权益曲线并生成一些关键绩效指标和一些有趣的数据 ...

  5. 1-4移动均线交叉策略3

    第一阶段.一个简单策略入门量化投资 1-4移动均线交叉策略3 上一文1-3移动均线交叉策略2中,我们得到的结果是令人失望的.但我们的探索还要继续. 我们知道,使用投资组合的方式进行分散投资是降低风险的 ...

  6. 用 Python 基于均线交叉策略进行回测

    本篇文章中,我将用 Python 构建一个简单的移动平均线交叉交易策略进行回测,并使用 标准普尔 500 指数(S&P500) 进行测试. 一个简单的移动平均线交叉策略可能是使用技术指标的量化 ...

  7. 均线交叉策略、海龟策略:基于backtrader框架的实现

    原创文章第74篇,专注"个人成长与财富自由.世界运作的逻辑, AI量化投资". 前面系列文章,把backtrader的方方面面介绍了一下,如何使用,内部运作机制,如何扩展. 客观讲 ...

  8. 75 [backtrader期货策略]十大经典策略-分时均线交叉策略

    很多交易者进行日内交易的时候,一个很重要的参考依据就是分时均线,本文尝试构建一个新的指标来近似代替分时均线,然后尝试基于均线\分时均线\日内高低点\跟踪止损条件,构建了一个分时均线日内交易策略. 策略 ...

  9. mq5 EA模板及双均线交叉策略EAdemo

    //+------------------------------------------------------------------+ //| mt5_ea_demo.mq5 | //| Cop ...

最新文章

  1. 华数机器人码垛_冲压机器人研究现状与发展方向
  2. DHCP中继代理;DHCP突破vlan限制
  3. facl:文件访问控制列表
  4. elasticsearch删除索引_一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)
  5. Python批量导入图片生成能治疗颈椎病的HTML5版课件
  6. seo技术_基础知识_网站pr值的意义_日思663.带你入门SEO基础知识
  7. PaddlePaddle(5)——简单高效的部署与推理方法(Ⅱ)
  8. 阿里云:linux 一键安装web环境
  9. 聊聊flink的KvStateRegistryGateway
  10. [dfs] 洛谷 P2535 收集资源
  11. 联想微型计算机设置键盘开机,联想台式机怎么样设置键盘开机
  12. 贪吃蛇代码--c语言版 visual c++6.0打开
  13. 华为联运游戏审核驳回:在未安装或需更新HMS Core的手机上,提示安装,点击取消后,游戏卡屏(集成的6.1.0.301版本游戏SDK)
  14. ieeetran_IEEEtran BibTex样式
  15. OpenCV可以识别文字吗?
  16. android6.0-nexus5 mac上docker 编译刷机
  17. 漫谈WIFI破解那些事
  18. 主机ip6容器ip6以及应用ip6
  19. Maya 凹凸贴图与置换贴图
  20. 黑马程序员——堆和栈的区别(转载)

热门文章

  1. html引用css文件无效,关于html引用文件无效。
  2. HTTP/HTTPS账号密码获取
  3. linux vi打不开文件,在Linux中使用vi/vim打开一个文件时出现的的问题
  4. HAL库版STM32双轮自平衡车(五) ———— 调参
  5. Pro/E产品设计:电风扇扇叶的设计方法
  6. 宇视2016c语言招聘试题,宇视科技2016年招聘试题.docx
  7. 做强信息产业 拓宽智慧城市发展路径
  8. 设置centos笔记本合盖不休眠
  9. 微信支付分 - 完结支付分订单API
  10. iOS 开发人才市场饱和了吗?