资源简介
Successful Algorithmic Trading(原书加代码),包含全部章节的内容。
代码片段和文件信息
#!/usr/bin/python
# -*- coding: utf-8 -*-
# cadf.py
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
import pandas.io.data as web
import pprint
import statsmodels.tsa.stattools as ts
from pandas.stats.api import ols
def plot_price_series(df ts1 ts2):
months = mdates.MonthLocator() # every month
fig ax = plt.subplots()
ax.plot(df.index df[ts1] label=ts1)
ax.plot(df.index df[ts2] label=ts2)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(mdates.DateFormatter(‘%b %Y‘))
ax.set_xlim(datetime.datetime(2012 1 1) datetime.datetime(2013 1 1))
ax.grid(True)
fig.autofmt_xdate()
plt.xlabel(‘Month/Year‘)
plt.ylabel(‘Price ($)‘)
plt.title(‘%s and %s Daily Prices‘ % (ts1 ts2))
plt.legend()
plt.show()
def plot_scatter_series(df ts1 ts2):
plt.xlabel(‘%s Price ($)‘ % ts1)
plt.ylabel(‘%s Price ($)‘ % ts2)
plt.title(‘%s and %s Price Scatterplot‘ % (ts1 ts2))
plt.scatter(df[ts1] df[ts2])
plt.show()
def plot_residuals(df):
months = mdates.MonthLocator() # every month
fig ax = plt.subplots()
ax.plot(df.index df[“res“] label=“Residuals“)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_major_formatter(mdates.DateFormatter(‘%b %Y‘))
ax.set_xlim(datetime.datetime(2012 1 1) datetime.datetime(2013 1 1))
ax.grid(True)
fig.autofmt_xdate()
plt.xlabel(‘Month/Year‘)
plt.ylabel(‘Price ($)‘)
plt.title(‘Residual Plot‘)
plt.legend()
plt.plot(df[“res“])
plt.show()
if __name__ == “__main__“:
start = datetime.datetime(2012 1 1)
end = datetime.datetime(2013 1 1)
arex = web.DataReader(“AREX“ “yahoo“ start end)
wll = web.DataReader(“WLL“ “yahoo“ start end)
df = pd.Dataframe(index=arex.index)
df[“AREX“] = arex[“Adj Close“]
df[“WLL“] = wll[“Adj Close“]
# Plot the two time series
plot_price_series(df “AREX“ “WLL“)
# Display a scatter plot of the two time series
plot_scatter_series(df “AREX“ “WLL“)
# Calculate optimal hedge ratio “beta“
res = ols(y=df[‘WLL‘] x=df[“AREX“])
beta_hr = res.beta.x
# Calculate the residuals of the linear combination
df[“res“] = df[“WLL“] - beta_hr*df[“AREX“]
# Plot the residuals
plot_residuals(df)
# Calculate and output the CADF test on the residuals
cadf = ts.adfuller(df[“res“])
pprint.pprint(cadf)
相关资源
- license_plate.zip
- SVM学习笔记+伪码.pdf
- NetworkX安装教程及所有所需软件
- 决策树随机森林代码(莺尾花)
- ffmpeg-win32-v3.2.4.exe
- 精通Scrapy网络爬虫(完整版)274198
- 数据可视化之美中文版.pdf
- flask搭建微电影网站代码
- 新华书店图书数据
- Keras数据集-npz格式(minist boston_housi
- 基于Tensorflow的mnist完整样例
- phantomjs驱动
- RT-MDNet视频目标跟踪论文官方预训练网
- sklearn中文文档
- chromedriver_72.0.3626.7集合win32linux64mac6
- Pandas官方文档CHM格式
- Qt Designer 2019-09-27.dmg
- tensorflow-2.0.0-cp37-cp37m-win_amd64.whl
- 推荐系统实践-高清带目录版 zip解压
- OpenCV3计算机视觉
- 量化交易 如何建立自己的算法交易事
- 数学建模算法与程序 司守奎包括原数
- PyQt5All.rar
- tf_car_dataset
- EEG MI Data.zip
- 虎赢PostMan数据聚合工具官方版.rar
- 一个pygame实现的RPG游戏
- 数据挖掘概念与技术 第三版.pdf
- 我的数据库
- 个人商城项目(django2)
评论
共有 条评论