第一部分 新手入门
- 一 量化投资视频学习课程
- 二 Python 手把手教学
- 量化分析师的Python日记【第1天:谁来给我讲讲Python?】
- 量化分析师的Python日记【第2天:再接着介绍一下Python呗】
- 量化分析师的Python日记【第3天:一大波金融Library来袭之numpy篇】
- 量化分析师的Python日记【第4天:一大波金融Library来袭之scipy篇】
- 量化分析师的Python日记【第5天:数据处理的瑞士军刀pandas】
- 量化分析师的Python日记【第6天:数据处理的瑞士军刀pandas下篇
- 量化分析师的Python日记【第7天:Q Quant 之初出江湖】
- 量化分析师的Python日记【第8天 Q Quant兵器谱之函数插值】
- 量化分析师的Python日记【第9天 Q Quant兵器谱之二叉树】
- 量化分析师的Python日记【第10天 Q Quant兵器谱 -之偏微分方程1】
- 量化分析师的Python日记【第11天 Q Quant兵器谱之偏微分方程2】
- 量化分析师的Python日记【第12天:量化入门进阶之葵花宝典:因子如何产生和回测】
- 量化分析师的Python日记【第13天 Q Quant兵器谱之偏微分方程3】
- 量化分析师的Python日记【第14天:如何在优矿上做Alpha对冲模型】
- 量化分析师的Python日记【第15天:如何在优矿上搞一个wealthfront出来】
第二部分 股票量化相关
- 一 基本面分析
- 1.1 alpha 多因子模型
- 1.2 基本面因子选股
- 1.3 财报阅读 • [米缸量化读财报] 资产负债表-投资相关资产
- 1.4 股东分析
- 1.5 宏观研究
- 二 套利
- 三 事件驱动
- 四 技术分析
- 4.1 布林带
- 4.2 均线系统
- 4.3 MACD
- 4.4 阿隆指标 • 技术指标阿隆( Aroon )全解析
- 4.5 CCI • CCI 顺势指标探索
- 4.6 RSI
- 4.7 DMI • DMI 指标体系的构建及简单应用
- 4.8 EMV • EMV 技术指标的构建及应用
- 4.9 KDJ • KDJ 策略
- 4.10 CMO
- 4.11 FPC • FPC 指标选股
- 4.12 Chaikin Volatility
- 4.13 委比 • 实时计算委比
- 4.14 封单量
- 4.15 成交量 • 决战之地, IF1507 !
- 4.16 K 线分析 • 寻找夜空中最亮的星
- 五 量化模型
- 5.1 动量模型
- 5.2 Joseph Piotroski 9 F-Score Value Investing Model
- 5.3 SVR
- 5.4 决策树、随机树
- 5.5 钟摆理论
- 5.6 海龟模型
- 5.7 5217 策略
- 5.8 SMIA
- 5.9 神经网络
- 5.10 PAMR
- 5.11 Fisher Transform
- 5.12 分型假说, Hurst 指数
- 5.13 变点理论
- 5.14 Z-score Model
- 5.15 机器学习
- 5.16 DualTrust 策略和布林强盗策略
- 5.17 卡尔曼滤波
- 5.18 LPPL anti-bubble model
- 六 大数据模型
- 6.1 市场情绪分析
- 6.2 新闻热点
- 七 排名选股系统
- 八 轮动模型
- 九 组合投资
- 十 波动率
- 十一 算法交易
- 十二 中高频交易
- 十三 Alternative Strategy
第三部分 基金、利率互换、固定收益类
- 一 分级基金
- 二 基金分析
- 三 债券
- 四 利率互换
第四部分 衍生品相关
- 一 期权数据
- 二 期权系列
- 三 期权分析
- 四 期货分析
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Paired trading
配对交易
策略思路
寻找走势相关且股价相近的一对股票,根据其价格变动买卖
策略实现
历史前五日的Pearson相关系数若大于给定的阈值则触发买卖操作
from scipy.stats.stats import pearsonr
start = datetime(2013, 1, 1)
end = datetime(2014, 12, 1)
benchmark = 'HS300'
universe = ['000559.XSHE', '600126.XSHG']
capital_base = 1e6
corlen = 5
def initialize(account):
add_history('hist', corlen)
account.cutoff = 0.9
account.prev_prc1 = 0
account.prev_prc2 = 0
account.prev_prcb = 0
def handle_data(account, data):
stk1 = universe[0]
stk2 = universe[1]
prc1 = data[stk1]['closePrice']
prc2 = data[stk2]['closePrice']
prcb = data['HS300']['return']
px1 = account.hist[stk1]['closePrice'].values
px2 = account.hist[stk2]['closePrice'].values
pxb = account.hist['HS300']['return'].values
corval, pval = pearsonr(px1, px2)
mov1, mov2 = adj(prc1, prc2, prcb, account.prev_prc1, account.prev_prc2, account.prev_prcb)
amount =1e4 / prc2
if (mov1 > 0) and (abs(corval) > account.cutoff):
order(stk2, amount)
elif (mov1 < 0) and (abs(corval) > account.cutoff):
if (account.position.stkpos.get(stk2, 0) > amount):
order(stk2, -amount)
else:
order_to(stk2, 0)
amount =1e4 / prc1
if (mov2 > 0) and (abs(corval) > account.cutoff):
order(stk1, amount)
elif (mov2 < 0) and (abs(corval) > account.cutoff):
if (account.position.stkpos.get(stk1, 0) > amount):
order(stk1, -amount)
else:
order_to(stk1, 0)
account.prev_prc1 = prc1
account.prev_prc2 = prc2
account.prev_prcb = prcb
def dmv(curr, prev):
delta = curr / prev - 1
return delta
def adj(x, y, base, prev_x, prev_y, prev_base):
dhs = dmv(base, prev_base)
dx = dmv(x, prev_x) - dhs
dy = dmv(y, prev_y) - dhs
return (dx, dy)
min(bt.cash)
232096.85369499651
import pandas as pd
import numpy as np
from datetime import datetime
import quartz
import quartz.backtest as qb
import quartz.performance as qp
from quartz.api import *
from scipy.stats.stats import pearsonr
start = datetime(2013, 1, 1) # 回测起始时间
end = datetime(2014, 12, 1) # 回测结束时间
benchmark = 'HS300' # 使用沪深 300 作为参考标准
capital_base = 1e6 # 起始资金
corlen = 5
def initialize(account): # 初始化虚拟账户状态
add_history('hist', corlen)
account.cutoff = 0.9
account.prev_prc1 = 0
account.prev_prc2 = 0
account.prev_prcb = 0
def handle_data(account, data): # 每个交易日的买入卖出指令
stk1 = universe[0]
stk2 = universe[1]
prc1 = data[stk1]['closePrice']
prc2 = data[stk2]['closePrice']
prcb = data['HS300']['return']
px1 = account.hist[stk1]['closePrice'].values
px2 = account.hist[stk2]['closePrice'].values
pxb = account.hist['HS300']['return'].values
corval, pval = pearsonr(px1, px2)
mov1, mov2 = adj(prc1, prc2, prcb, account.prev_prc1, account.prev_prc2, account.prev_prcb)
#amount = int( 0.08 * capital_base / prc2)
amount =1e4 / prc2
if (mov1 > 0) and (abs(corval) > account.cutoff):
order(stk2, amount)
elif (mov1 < 0) and (abs(corval) > account.cutoff):
if (account.position.stkpos.get(stk2, 0) > amount):
order(stk2, -amount)
else:
order_to(stk2, 0)
#amount = int(0.08 * capital_base / prc1)
amount =1e4 / prc1
if (mov2 > 0) and (abs(corval) > account.cutoff):
order(stk1, amount)
elif (mov2 < 0) and (abs(corval) > account.cutoff):
if (account.position.stkpos.get(stk1, 0) > amount):
order(stk1, -amount)
else:
order_to(stk1, 0)
account.prev_prc1 = prc1
account.prev_prc2 = prc2
account.prev_prcb = prcb
def dmv(curr, prev):
delta = curr / prev - 1
return delta
def adj(x, y, base, prev_x, prev_y, prev_base):
dhs = dmv(base, prev_base)
dx = dmv(x, prev_x) - dhs
dy = dmv(y, prev_y) - dhs
return (dx, dy)
pool_raw = pd.read_csv("po.pair.2012.csv")
pool = []
for i in range(len(pool_raw)):
s1, s2 = pool_raw.loc[i].tolist()
if [s2, s1] not in pool:
pool.append([s1, s2])
outfile = []
for i, universe in enumerate(pool):
print i
try:
bt = qb.backtest(start, end, benchmark, universe, capital_base, initialize = initialize, handle_data = handle_data)
perf = qp.perf_parse(bt)
outfile.append(universe + [perf["annualized_return"], perf["sharpe"]])
except:
pass
keys = ['stock1', 'stock2', 'annualized_return', 'sharpe']
outdict = {}
outfile = zip(*sorted(outfile, key=lambda x:x[2], reverse=True))
for i,k in enumerate(keys):
outdict[k] = outfile[i]
outdict = pd.DataFrame(outdict).loc[:, keys]
outdict
['000066.XSHE', '000707.XSHE']
['000066.XSHE', '600117.XSHG']
['000066.XSHE', '600126.XSHG']
['000066.XSHE', '600819.XSHG']
['000089.XSHE', '600035.XSHG']
['000089.XSHE', '600037.XSHG']
['000089.XSHE', '600595.XSHG']
['000159.XSHE', '000967.XSHE']
['000159.XSHE', '600595.XSHG']
['000417.XSHE', '000541.XSHE']
['000417.XSHE', '000685.XSHE']
['000417.XSHE', '600875.XSHG']
['000425.XSHE', '000528.XSHE']
['000507.XSHE', '600391.XSHG']
['000541.XSHE', '000987.XSHE']
['000541.XSHE', '600330.XSHG']
['000541.XSHE', '600883.XSHG']
['000554.XSHE', '000707.XSHE']
['000559.XSHE', '600026.XSHG']
['000559.XSHE', '600126.XSHG']
['000559.XSHE', '600477.XSHG']
['000559.XSHE', '600581.XSHG']
['000559.XSHE', '601666.XSHG']
['000635.XSHE', '000707.XSHE']
['000635.XSHE', '600068.XSHG']
['000635.XSHE', '600117.XSHG']
['000635.XSHE', '600188.XSHG']
['000635.XSHE', '600295.XSHG']
['000635.XSHE', '600550.XSHG']
['000635.XSHE', '600819.XSHG']
['000635.XSHE', '601168.XSHG']
['000635.XSHE', '601233.XSHG']
['000650.XSHE', '600261.XSHG']
['000683.XSHE', '000936.XSHE']
['000683.XSHE', '600595.XSHG']
['000685.XSHE', '000988.XSHE']
['000685.XSHE', '601101.XSHG']
['000698.XSHE', '000949.XSHE']
['000707.XSHE', '000911.XSHE']
['000707.XSHE', '000969.XSHE']
['000707.XSHE', '000987.XSHE']
['000707.XSHE', '600117.XSHG']
['000707.XSHE', '600295.XSHG']
['000707.XSHE', '600550.XSHG']
['000707.XSHE', '600831.XSHG']
['000707.XSHE', '601168.XSHG']
['000707.XSHE', '601233.XSHG']
['000708.XSHE', '600327.XSHG']
['000709.XSHE', '601107.XSHG']
['000709.XSHE', '601618.XSHG']
['000717.XSHE', '600282.XSHG']
['000717.XSHE', '600307.XSHG']
['000717.XSHE', '600808.XSHG']
['000761.XSHE', '600320.XSHG']
['000761.XSHE', '600548.XSHG']
['000822.XSHE', '600117.XSHG']
['000830.XSHE', '600068.XSHG']
['000830.XSHE', '600320.XSHG']
['000830.XSHE', '600550.XSHG']
['000877.XSHE', '601519.XSHG']
['000898.XSHE', '600022.XSHG']
['000898.XSHE', '600808.XSHG']
['000911.XSHE', '600550.XSHG']
['000916.XSHE', '600033.XSHG']
['000916.XSHE', '600035.XSHG']
['000916.XSHE', '600126.XSHG']
['000930.XSHE', '600026.XSHG']
['000932.XSHE', '600569.XSHG']
['000933.XSHE', '600348.XSHG']
['000933.XSHE', '600595.XSHG']
['000936.XSHE', '600477.XSHG']
['000937.XSHE', '600348.XSHG']
['000937.XSHE', '600508.XSHG']
['000937.XSHE', '600997.XSHG']
['000937.XSHE', '601001.XSHG']
['000939.XSHE', '600819.XSHG']
['000967.XSHE', '600879.XSHG']
['000969.XSHE', '600831.XSHG']
['000973.XSHE', '600460.XSHG']
['000987.XSHE', '600636.XSHG']
['000987.XSHE', '600827.XSHG']
['000987.XSHE', '601001.XSHG']
['600008.XSHG', '600035.XSHG']
['600012.XSHG', '600428.XSHG']
['600020.XSHG', '600033.XSHG']
['600020.XSHG', '600035.XSHG']
['600026.XSHG', '600068.XSHG']
['600026.XSHG', '600089.XSHG']
['600026.XSHG', '600126.XSHG']
['600026.XSHG', '600307.XSHG']
['600026.XSHG', '600331.XSHG']
['600026.XSHG', '600375.XSHG']
['600026.XSHG', '600581.XSHG']
['600026.XSHG', '600963.XSHG']
['600026.XSHG', '601666.XSHG']
['600026.XSHG', '601898.XSHG']
['600033.XSHG', '600035.XSHG']
['600035.XSHG', '600126.XSHG']
['600035.XSHG', '600269.XSHG']
['600035.XSHG', '600307.XSHG']
['600035.XSHG', '600586.XSHG']
['600037.XSHG', '600327.XSHG']
['600068.XSHG', '600126.XSHG']
['600068.XSHG', '600269.XSHG']
['600068.XSHG', '600320.XSHG']
['600068.XSHG', '600550.XSHG']
['600068.XSHG', '601001.XSHG']
['600068.XSHG', '601666.XSHG']
['600089.XSHG', '600581.XSHG']
['600100.XSHG', '600117.XSHG']
['600117.XSHG', '600295.XSHG']
['600117.XSHG', '600339.XSHG']
['600117.XSHG', '601168.XSHG']
['600117.XSHG', '601233.XSHG']
['600126.XSHG', '600282.XSHG']
['600126.XSHG', '600327.XSHG']
['600126.XSHG', '600569.XSHG']
['600126.XSHG', '600581.XSHG']
['600126.XSHG', '600808.XSHG']
['600126.XSHG', '600963.XSHG']
['600160.XSHG', '600449.XSHG']
['600160.XSHG', '601216.XSHG']
['600160.XSHG', '601311.XSHG']
['600188.XSHG', '600295.XSHG']
['600188.XSHG', '601001.XSHG']
['600231.XSHG', '600282.XSHG']
['600269.XSHG', '601618.XSHG']
['600282.XSHG', '600307.XSHG']
['600282.XSHG', '600569.XSHG']
['600282.XSHG', '600808.XSHG']
['600282.XSHG', '600963.XSHG']
['600307.XSHG', '600581.XSHG']
['600307.XSHG', '600808.XSHG']
['600307.XSHG', '600963.XSHG']
['600320.XSHG', '600548.XSHG']
['600320.XSHG', '601600.XSHG']
['600330.XSHG', '600883.XSHG']
['600330.XSHG', '601268.XSHG']
['600331.XSHG', '600581.XSHG']
['600348.XSHG', '600508.XSHG']
['600348.XSHG', '600997.XSHG']
['600348.XSHG', '601001.XSHG']
['600368.XSHG', '600527.XSHG']
['600375.XSHG', '600581.XSHG']
['600391.XSHG', '601100.XSHG']
['600449.XSHG', '601311.XSHG']
['600449.XSHG', '601519.XSHG']
['600460.XSHG', '601908.XSHG']
['600477.XSHG', '600581.XSHG']
['600508.XSHG', '600546.XSHG']
['600508.XSHG', '600997.XSHG']
['600522.XSHG', '600973.XSHG']
['600550.XSHG', '600831.XSHG']
['600569.XSHG', '600808.XSHG']
['600569.XSHG', '600963.XSHG']
['600581.XSHG', '600963.XSHG']
['600581.XSHG', '601001.XSHG']
['600581.XSHG', '601168.XSHG']
['600581.XSHG', '601666.XSHG']
['600586.XSHG', '601268.XSHG']
['600595.XSHG', '601001.XSHG']
['600595.XSHG', '601168.XSHG']
['600595.XSHG', '601666.XSHG']
['600688.XSHG', '600871.XSHG']
['600785.XSHG', '600827.XSHG']
['600808.XSHG', '600963.XSHG']
['600827.XSHG', '601001.XSHG']
['600875.XSHG', '601001.XSHG']
['600883.XSHG', '601268.XSHG']
['601001.XSHG', '601101.XSHG']
['601001.XSHG', '601168.XSHG']
['601001.XSHG', '601666.XSHG']
['601101.XSHG', '601666.XSHG']
['601168.XSHG', '601666.XSHG']
stock1 | stock2 | annualized_return | sharpe | |
---|---|---|---|---|
0 | 000761.XSHE | 600548.XSHG | 0.489473 | 2.411514 |
1 | 000708.XSHE | 600327.XSHG | 0.447337 | 2.021270 |
2 | 600126.XSHG | 600327.XSHG | 0.438380 | 1.946916 |
3 | 000554.XSHE | 000707.XSHE | 0.431123 | 1.331038 |
4 | 000939.XSHE | 600819.XSHG | 0.409471 | 1.919758 |
5 | 600026.XSHG | 600963.XSHG | 0.408791 | 1.681338 |
6 | 600037.XSHG | 600327.XSHG | 0.395624 | 1.691877 |
7 | 600808.XSHG | 600963.XSHG | 0.391988 | 1.724114 |
8 | 000559.XSHE | 600126.XSHG | 0.389043 | 1.413595 |
9 | 000761.XSHE | 600320.XSHG | 0.384325 | 1.807262 |
10 | 600126.XSHG | 600963.XSHG | 0.378064 | 1.662569 |
11 | 600126.XSHG | 600808.XSHG | 0.375825 | 1.513791 |
12 | 000936.XSHE | 600477.XSHG | 0.375135 | 1.707097 |
13 | 000930.XSHE | 600026.XSHG | 0.372924 | 1.524350 |
14 | 600320.XSHG | 600548.XSHG | 0.372499 | 2.083496 |
15 | 000507.XSHE | 600391.XSHG | 0.365637 | 1.813873 |
16 | 000559.XSHE | 601666.XSHG | 0.350235 | 0.925901 |
17 | 600012.XSHG | 600428.XSHG | 0.327834 | 1.722317 |
18 | 000916.XSHE | 600033.XSHG | 0.327795 | 1.406093 |
19 | 600035.XSHG | 600126.XSHG | 0.326167 | 1.442674 |
20 | 600827.XSHG | 601001.XSHG | 0.322705 | 0.957791 |
21 | 000717.XSHE | 600808.XSHG | 0.320737 | 1.293439 |
22 | 000559.XSHE | 600477.XSHG | 0.306670 | 1.218095 |
23 | 000685.XSHE | 000988.XSHE | 0.302593 | 1.692933 |
24 | 000683.XSHE | 000936.XSHE | 0.301804 | 1.550496 |
25 | 000559.XSHE | 600026.XSHG | 0.295510 | 1.279449 |
26 | 600269.XSHG | 601618.XSHG | 0.294215 | 1.486413 |
27 | 600026.XSHG | 600126.XSHG | 0.293884 | 1.441490 |
28 | 600068.XSHG | 600126.XSHG | 0.289457 | 1.261351 |
29 | 000159.XSHE | 600595.XSHG | 0.288982 | 0.946365 |
30 | 600020.XSHG | 600033.XSHG | 0.288243 | 1.489764 |
31 | 600126.XSHG | 600569.XSHG | 0.287607 | 1.371374 |
32 | 000635.XSHE | 600819.XSHG | 0.285135 | 1.364688 |
33 | 600068.XSHG | 600320.XSHG | 0.273513 | 1.262845 |
34 | 600785.XSHG | 600827.XSHG | 0.272658 | 0.842093 |
35 | 000089.XSHE | 600595.XSHG | 0.269903 | 1.256524 |
36 | 000898.XSHE | 600808.XSHG | 0.269717 | 1.074201 |
37 | 000717.XSHE | 600282.XSHG | 0.267478 | 1.270872 |
38 | 600282.XSHG | 600808.XSHG | 0.266402 | 1.181157 |
39 | 000916.XSHE | 600035.XSHG | 0.264325 | 1.079520 |
40 | 000089.XSHE | 600037.XSHG | 0.264201 | 1.467101 |
41 | 600026.XSHG | 600068.XSHG | 0.263959 | 1.107977 |
42 | 600026.XSHG | 600331.XSHG | 0.261025 | 0.977858 |
43 | 600020.XSHG | 600035.XSHG | 0.260176 | 1.119975 |
44 | 600569.XSHG | 600963.XSHG | 0.260006 | 1.154372 |
45 | 600307.XSHG | 600963.XSHG | 0.258488 | 1.322409 |
46 | 000898.XSHE | 600022.XSHG | 0.258246 | 1.100292 |
47 | 600282.XSHG | 600963.XSHG | 0.257496 | 1.175741 |
48 | 600307.XSHG | 600808.XSHG | 0.256071 | 1.062023 |
49 | 600126.XSHG | 600282.XSHG | 0.255657 | 1.318676 |
50 | 600033.XSHG | 600035.XSHG | 0.255634 | 1.055682 |
51 | 000709.XSHE | 601618.XSHG | 0.253129 | 1.062565 |
52 | 600026.XSHG | 600307.XSHG | 0.253119 | 0.985825 |
53 | 600026.XSHG | 600375.XSHG | 0.250793 | 1.063874 |
54 | 000066.XSHE | 600126.XSHG | 0.247493 | 1.469341 |
55 | 000830.XSHE | 600320.XSHG | 0.247001 | 1.370327 |
56 | 600320.XSHG | 601600.XSHG | 0.246534 | 0.966634 |
57 | 000717.XSHE | 600307.XSHG | 0.245805 | 1.202750 |
58 | 000417.XSHE | 000685.XSHE | 0.245031 | 1.189700 |
59 | 600330.XSHG | 600883.XSHG | 0.243437 | 1.086147 |
... | ... | ... | ... |
174 rows × 4 columns
a = list(outfile[2])
'percentage of outperform HS300: %f' % (1.*len([x for x in a if x>0.117]) / len(a))
'percentage of outperform HS300: 0.741379'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论