量化交易核心策略之最优配对交易

本内容基于论文《最优配对交易》,策略是基于相互间的走势相近的股票对,当两者间的差价偏离均值时,我们认为他们的关系会回归长期的均值,根据策略进行交易,以便从这一回归行为中获利。

标准的例子是同一行业中的一对高度相关的股票。配对交易策略仅仅依赖于证券对之间的相对关系,而不是市场的整体走势,它是一种市场的中性的策略。

论文中作者开发了一个假设股票间对数价差符合OU过程的模型。OU过程是一个平稳的高斯-马尔可夫过程,其数学期望为0且指数函数为核函数。将动态投资组合优化问题转化为随机控制问题。假设投资者可以基于价差进行交易或者将资金放入无风险资产中,他们能够为将资产净值作为控制目标的控制问题找到一个解析解。

通过代码测试了上海贝岭和士兰微最近300个交易日的数据,通过策略可以获得3倍收益。(仅为理论参考)

但是,在回测中国银行和中国建设银行的历史数据上,效果并不理想。

可以优化的地方很多,希望得到更多的意见。


这是中国银行和建设银行策略组合的收益曲线,可以说是悬崖是回撤。

import pandas as pd

import numpy as np

from math import exp

import matplotlib.pyplot as plt

df2 = pd.read_csv(

filepath_or_buffer=r'C:\Users\kongx\Desktop\KONG_QUANT\Data\stock\sh600171.csv',

encoding='gbk',

sep=',',

# 该参数代表跳过数据文件的的第1行不读入

skiprows=1,

# parse_dates=['交易日期'],

# index_col=['交易日期'],

)

df1 = pd.read_csv(

filepath_or_buffer=r'C:\Users\kongx\Desktop\KONG_QUANT\Data\stock\sh600460.csv',

encoding='gbk',

sep=',',

# 该参数代表跳过数据文件的的第1行不读入

skiprows=1,

# parse_dates=['交易日期'],

# index_col=['交易日期'],

)

data1 = df1.tail(300)

data2 = df2.tail(300)

data1 = data1.iloc[:, 5].values

data2 = data2.iloc[:, 5].values

print(data1)

print(data2)

S_data = np.log(data2)

X_data = np.log(data1) - np.log(data2)

'''

真实回测前,需预设一些参数:

'''

N = 60

T = 1

r = -100

V_list = [1]

for i in range(N, len(data1) - 1): # 滚动窗口计算参数估计值

m = (S_data[i] - S_data[i - N]) / N

SS = (np.power((pd.Series(S_data[i - N:i + 1]) - pd.Series(S_data[i - N:i + 1]).shift(1)), 2)[1:].sum() - 2 * m * (

S_data[i] - S_data[i - N]) + N * m ** 2) / N

p = 1 / (N * np.power(X_data[i - N:i], 2).sum() - X_data[i - N:i].sum() ** 2) * (

N * (pd.Series(X_data[i - N:i + 1]) * pd.Series(X_data[i - N:i + 1]))[1:].sum() - (

X_data[i] - X_data[i - N]) * X_data[i - N:i].sum() - np.power(X_data[i - N:i].sum(), 2))

if p<0:

p=-p

q = (X_data[i] - X_data[i - N] + X_data[i - N:i].sum() * (1 - p)) / N

VV = 1 / N * (X_data[i] ** 2 - X_data[i - N] ** 2 + (1 + p ** 2) * np.power(X_data[i - N:i], 2).sum() -

2 * p * (pd.Series(X_data[i - N:i + 1]) * pd.Series(X_data[i - N:i + 1]))[1:].sum() - N * q)

if VV<0:

VV=-VV

C = 1 / (N * VV ** 0.5 * SS ** 0.5) * (

(pd.Series(X_data[i - N:i + 1]) * (pd.Series(S_data[i - N:i + 1]) - pd.Series(S_data[i - N:i + 1]).shift(1)))[

1:].sum() - p * (pd.Series(X_data[i - N:i + 1]) * (

pd.Series(S_data[i - N:i + 1]).shift(-1) - pd.Series(S_data[i - N:i + 1])))[:-1].sum() - m * (

X_data[i] - X_data[i - N]) - m * (1 - p) * pd.Series(X_data[i - N:i]).sum())

std_ = (SS / 1) ** 0.5

u_ = m / 1 + 0.5 * std_ ** 2

k_ = -(np.log(p) / 1)

theta_ = q / (1 - p)

eta_ = (2 * k_ * VV / (1 - p ** 2)) ** 0.5

rho_ = k_ * C * (VV ** 0.5) * (SS ** 0.5) / (eta_ * theta_ * (1 - p))

beta_t = 1 / (

2 * eta_ ** 2 * (1 - 101 ** 0.5 - (1 + 101 ** 0.5) * exp(2 * k_ * (T - 0) / (101 ** 0.5)))) * (

-100 * 101 ** 0.5 * (eta_ ** 2 + 2 * rho_ * std_* eta_) * (

(1 - exp(2 * k_ * (T - 0 ) / (101 ** 0.5))) ** 2) + 100 * (

eta_ ** 2 + 2 * rho_ * std_ * eta_ + 2 * k_*theta_) * (

1 - exp(2 * k_ * (T - 0) / (101 ** 0.5))))

alpha_t=k_ * (1 - 101 ** 0.5) / (2 * eta_ ** 2) * (1 + 2 * 101 ** 0.5 / (

1 - 101 ** 0.5 - (1 + 101 ** 0.5) * exp(2 * k_ * (T - 0 ) / (101 ** 0.5))))

h_t=1 / 101 * (beta_t + 2 * X_data[i] * alpha_t - k_ * (X_data[i] - theta_) / (eta_ ** 2) + rho_ * std_ / eta_ + 0.5)

dV_t = V_list[-1] * (

h_t * (data1[i + 1] - data1[i]) / data1[i] - h_t * (data2[i + 1] - data2[i]) / data2[i])

V_list.append(V_list[-1] + dV_t)

'''第一幅图为价差曲线'''

pd.Series(X_data).plot()

plt.show()

'''第二幅图为收益曲线'''

pd.Series(V_list).plot()

plt.show()

所有评论(59)

  1. adonisbitok说道:

    This message is used to verify that this feed (feedId:71902613342727168) belongs to me (userId:71902365683728384). Join me in enjoying the next generation information browser https://follow.is.

  2. It’s remarkable designed for me to have a weeb page,
    which is valuable for my knowledge. thanks admin https://w4i9o.Mssg.me/

  3. Hello There. I found your blog using msn. This is a
    very well written article. I will be sure to bookmark it and retuirn to read more of
    your useful info. Thanks for the post. I’ll definitely comeback. https://Glassi-India.mystrikingly.com/

  4. I havee read so many content concernjng the blogger lovers however this
    piece of writing is reeally a fastidious piece of writing, keep it up. https://timviec24h.com.vn/companies/tonebet-casino/

  5. Alethea说道:

    how many native united statesn casinos are there,
    australia slot machine games and united kingdom slots machines, or casino stone gambling in india (Alethea) australia near detroit

  6. £20 free no deposit the habit graton casino uk, canada online casino no deposit and
    australian casino free spins, or online casino guide uk

  7. Darrin说道:

    buy poker machine united kingdom, online pokies australia real money paysafe and how many casino are in australia,
    or best payout online slots australia

    my blog gambling away life savings [Darrin]

  8. Ophelia说道:

    the blackjacks homepage canada, best payout online casino united kingdom wishful and
    rules why jeuteng is considered as illegal practice of gambling in the philippines [Ophelia]
    roulette uk, or united kingdom online pokies paypal

  9. Libby说道:

    gambling illegal usa, how much does united statesn roulette pay and no deposit slots usa, or casino
    wood green (Libby) usa bonus

  10. are there casinos in montreal australia, 2021 online casino new zealand and td australia visa debit gambling,
    or new zealandn roulette odds payout

    Have a look at my web-site – Bitcasino promo code 2022

  11. 777 cheri heights casino promotions united kingdom, las vegas casino online usa and
    usa gambling regulation changes, or best online casinos united states 2021

  12. insna.info说道:

    wettbüro saarbrücken

    Here is my website: sportwetten Tipps vorhersagen Gratis (insna.info)

  13. sportwette online

    Feel free to visit my blog; neuer wettanbieter – Povaboutcoffee.org.in,

  14. Mauricio说道:

    schleswig holstein sportwetten lizenz

    Also visit my web site; wetten spiel abgesagt (Mauricio)

  15. sportwetten online österreich

    Also visit my webpage … wetten heute tipps; https://jewellery.arsmintglobal.Com/,

  16. sportwetten startguthaben ohne einzahlung

    my web site: wetten beim pferderennen

  17. Makayla说道:

    online wetten bonus vergleich – Makayla, dass
    unfall samuel koch heute

  18. magosan.com.br说道:

    wetten mit freunden app

    Stop by my web blog … wettprognosen, magosan.com.br,

  19. Gregory说道:

    wettbüro duisburg

    Here is my blog; beste sportwetten online (Gregory)

  20. wett vorhersagen von profis

    Feel free to visit my webpage online wettanbieter deutschland

  21. wetten dass gewinner sound

    Also visit my web-site … Wettbüro konstanz

  22. spanien deutschland wettquoten

    Also visit my blog post – sportwetten schweiz Anbieter

  23. Sabine说道:

    sportwetten österreich legal

    Here is my page – wetten schweiz (Sabine)

  24. instant sports wetten erfahrungen

    my page; wettstrategien livewetten – https://ixkool.com,

  25. Connie说道:

    wettbüro nürnberg

    My web page sportwetten prognosen heute (Connie)

  26. JB.Retaily.asia说道:

    ergebnis pferderennen köln wetten – JB.Retaily.asia – live

  27. neue wettanbieter

    Feel free to visit my website dfb Pokal Wett tipps

  28. quoten rechner wetten

    my web page – sportwetten ohne oasis paysafecard; https://d-b-M.pl/,

  29. ergebnis wetten live

    Here is my website – sportwetten mit bonus – https://terrier.kh.ua/,

  30. online wetten schweiz

    Feel free to surf to my blog post … professionelle sportwetten tipps

  31. Malinda说道:

    sportwetten analyse heute

    My blog post; halbzeit endstand wetten strategie (Malinda)

  32. mokus-Ors.hu说道:

    live wetten österreich

    Have a look at my web page – Besten sportwetten tipps heute (mokus-Ors.hu)

  33. Sheila说道:

    größte wettanbieter deutschland

    Also visit my blog post; sportwetten heute vorhersagen [Sheila]

  34. Seth说道:

    beste us open wettanbieter

    Also visit my site – pferderennen meran wetten (Seth)

  35. Darcy说道:

    wettanbieter test

    Also visit my blog :: online sportwetten app; Darcy,

  36. consumerbd.Org说道:

    sportwetten deutsche lizenz

    my web site gratiswetten – consumerbd.Org,

  37. sichere wetten für heute

    Feel free to surf to my blog; beste sportwetten app deutschland

  38. wett tipp heute说道:

    mehrfach kombiwette rechner

    Feel free to visit my site: wett tipp heute

  39. sportwetten strategie system

    my blog post … aktuelle gratiswetten

  40. wett tipps pferderennen

    Here is my website :: Sportwetten Prognose Heute

  41. Deanne说道:

    Your way of telling everything in this piece of writing is actually
    nice, all be capable of effortlessly know it, Thanks
    a lot.

    Visit my web blog what states has the most casinos (Deanne)

  42. is gambling allowed in united states, united kingdom gambling revenue and usa
    casinos free spins, or low deposit big casino Trading canada

  43. homepage说道:

    Amazing! Its really remarkable post, I have got much clear idea
    concerning from this post.

    Feel free to visit my blog; homepage

  44. profi wett tipps heute

    Stop by my web blog … Europameister wettquoten (https://www.Formresonance.Com)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注