-
Notifications
You must be signed in to change notification settings - Fork 1
/
bug-record-coutinues-contract-switch.html
executable file
·415 lines (375 loc) · 21 KB
/
bug-record-coutinues-contract-switch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml">
<head>
<title>主力合约切换的Bug记录 - TechLoveDeath</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://brycemeng.com/images/favicon.png" rel="icon">
<link rel="canonical" href="https://brycemeng.com/bug-record-coutinues-contract-switch.html">
<meta name="author" content="Bryce" />
<meta name="keywords" content="Quant,Futures" />
<meta name="description" content="主力合约切换的Bug记录" />
<meta property="og:site_name" content="TechLoveDeath" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="主力合约切换的Bug记录"/>
<meta property="og:url" content="https://brycemeng.com/bug-record-coutinues-contract-switch.html"/>
<meta property="og:description" content="主力合约切换的Bug记录"/>
<meta property="article:published_time" content="2019-01-14" />
<meta property="article:section" content="Quant" />
<meta property="article:tag" content="Quant" />
<meta property="article:tag" content="Futures" />
<meta property="article:author" content="Bryce" />
<!-- Bootstrap -->
<link rel="stylesheet" href="https://brycemeng.com/theme/css/bootstrap.min.css" type="text/css"/>
<link href="https://brycemeng.com/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://brycemeng.com/theme/css/pygments/native.css" rel="stylesheet">
<link rel="stylesheet" href="https://brycemeng.com/theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://brycemeng.com/" class="navbar-brand">
<img class="img-responsive pull-left gap-right" src="https://brycemeng.com/images/favicon.png" width="24"/> TechLoveDeath </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li >
<a href="https://brycemeng.com/category/android.html">Android</a>
</li>
<li >
<a href="https://brycemeng.com/category/misc.html">Misc</a>
</li>
<li >
<a href="https://brycemeng.com/category/music.html">Music</a>
</li>
<li >
<a href="https://brycemeng.com/category/programming.html">Programming</a>
</li>
<li class="active">
<a href="https://brycemeng.com/category/quant.html">Quant</a>
</li>
<li >
<a href="https://brycemeng.com/category/tech.html">Tech</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<!-- Content Container -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="https://brycemeng.com/bug-record-coutinues-contract-switch.html"
rel="bookmark"
title="Permalink to 主力合约切换的Bug记录">
主力合约切换的Bug记录
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2019-01-14T20:54:00+08:00"> Mon 14 January 2019</time>
</span>
<span class="label label-default">Tags</span>
<a href="https://brycemeng.com/tag/quant.html">Quant</a>
/
<a href="https://brycemeng.com/tag/futures.html">Futures</a>
</footer><!-- /.post-info --> </div>
</div>
<p>全名应该是商品期货主力合约切换导致的Bug记录</p>
<p><strong>品种</strong>:商品期货 rb</p>
<p><strong>回测数据</strong>:tb提取的分钟线</p>
<p><strong>实时数据</strong>:ctp获取</p>
<p><strong>Bug描述</strong>:策略当中需要一段时间的历史数据进行指标计算,主力切换导致这段历史数据里面含有两份合约的数据,分别是rb1901和rb1905,这两个合约于20181128 21:00切换,价差竟然达到了300点左右,以当时rb1901 3700的价格,也达到了8%,导致指标计算的误差非常大,开了错单。</p>
<p>很早前就知道这个问题的存在,但是一直没有动力去调整,一直用策略的鲁棒性安慰着自己。但是这次的切换价差到达了8%,还开了错单,没有这么考验鲁棒性的,还是打算修复一下。</p>
<p>我是先改的线上策略,线上策略的主力切换数据从新浪财经网站上获取。</p>
<p><strong>线上修改方案</strong>:以该bug为例子,主力切换后我在rb1905上进行交易,把rb1901的OHLC数据全部减去300点的价差,这个300是20181128 15:00(rb1901) 和 20181128 21:00(rb1905)的价差。也叫左侧价差调整。</p>
<p>但是交易了几单全部都在亏损,发现有点问题,忽然想到有问题,虽然还没有达到下架策略的条件,但是也有点奇怪。后来发现是回测的问题,我的回测优化是在主力切换价差未做调整的情况下做的,现在实盘又是调整后的数据。。。。。。</p>
<p>为什么要用tb的数据呢?其实我是有tick数据的,以前尝试过清洗一次,发现早期的数据问题太多了就一直没弄,个人量化者就是啥都得干。就直接提取tb的分钟线,质量还行又方便,为了节省时间嘛, 所以这次还是在tb的数据上做文章。</p>
<p><strong>回测修改方案</strong>:</p>
<p>找出tb主连合约分钟线的切换点
在指标计算的价格buffer中进行左侧价差调整
给策略发信号让策略在切换的前半个小时就平仓而且暂停交易,切换后再重新开启交易</p>
<p><strong>* 找出tb主连合约分钟线的切换点</strong></p>
<p>一个相对好点的办法去识别就是<strong>持仓量的变化</strong>,因为大部分的主力切换规则都与持仓量相关,比如:次主力的持仓量在某一天大于主力的持仓量后,第二天进行切换。但是每个公司的规则都不一样,我对比过新浪财经上的切换时间和tb的就不一样,所以我觉得最后还是得把自己的数据搞全,这样回测和实盘都用自己的主力切换规则,这样才是最好的。</p>
<p>我发一张tb分钟线的图就一目了然了</p>
<p><img alt="rb888 分钟线" src="http://sinacloud.net/mhblog/2019/01/tb_contunues_data.jpg"></p>
<p>这次tb数据的切换比新浪财经的晚一天。红线上面和红线下面就是切换前后的数据,变化最明显的就是最后一列数据:<strong>持仓量</strong>。</p>
<p>所以我设置了一个阀值,把持仓量变化大于了这个阀值的点就当做主力切换点,当然这个是近似值,出来的结果我都人工review了的。</p>
<p>看看我找出来的rb的主力切换时间列表:</p>
<div class="highlight"><pre><span></span><code><span class="n">switch_time_list</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">"20090720_0900"</span><span class="p">,</span> <span class="s2">"20090810_0900"</span><span class="p">,</span> <span class="s2">"20090901_0900"</span><span class="p">,</span> <span class="s2">"20091012_0900"</span><span class="p">,</span> <span class="s2">"20091110_0900"</span><span class="p">,</span> <span class="s2">"20091201_0900"</span><span class="p">,</span>
<span class="s2">"20100309_0900"</span><span class="p">,</span> <span class="s2">"20101014_0900"</span><span class="p">,</span>
<span class="s2">"20110210_0900"</span><span class="p">,</span> <span class="s2">"20110808_0900"</span><span class="p">,</span> <span class="s2">"20111103_0900"</span><span class="p">,</span>
<span class="s2">"20120302_0900"</span><span class="p">,</span> <span class="s2">"20120712_0900"</span><span class="p">,</span> <span class="s2">"20121023_0900"</span><span class="p">,</span>
<span class="s2">"20130219_0900"</span><span class="p">,</span> <span class="s2">"20130704_0900"</span><span class="p">,</span> <span class="s2">"20131031_0900"</span><span class="p">,</span>
<span class="s2">"20140305_0900"</span><span class="p">,</span> <span class="s2">"20140708_0900"</span><span class="p">,</span> <span class="s2">"20141021_0900"</span><span class="p">,</span>
<span class="c1"># 夜盘开始</span>
<span class="s2">"20150311_2100"</span><span class="p">,</span> <span class="s2">"20150720_2100"</span><span class="p">,</span> <span class="s2">"20151104_2100"</span><span class="p">,</span>
<span class="s2">"20160311_2100"</span><span class="p">,</span> <span class="s2">"20160817_2100"</span><span class="p">,</span> <span class="s2">"20161125_2100"</span><span class="p">,</span>
<span class="s2">"20170322_2100"</span><span class="p">,</span> <span class="s2">"20170807_2100"</span><span class="p">,</span> <span class="s2">"20171108_2100"</span><span class="p">,</span>
<span class="s2">"20180328_2100"</span><span class="p">,</span> <span class="s2">"20180816_2100"</span><span class="p">,</span> <span class="s2">"20181129_2100"</span><span class="p">,</span>
<span class="p">]</span>
</code></pre></div>
<p>2和3应该不复杂,我是用的vnpy,我是在ArrayManager和BarManager中做了调整,很快就能实现。</p>
<p>这个bug修改了之后,重新做一次回测优化,发现参数变得还不少,看来这个bug对回测准确性的影响确实不小。</p>
<p>我把主力切换数据和寻找切换时间的代码放在了github上,暂时只有rb的数据,如有需要的朋友可以直接使用,也希望有兴趣的朋友可以新增其他品种的数据。</p>
<p><a href="https://github.com/mhxueshan/dominant_contract_switch">mhxueshan/dominant_contract_switch</a></p>
</div>
<!-- /.entry-content -->
<section class="well" id="related-posts">
<h4>Related Posts:</h4>
<ul>
<li><a href="https://brycemeng.com/some_exp_of_two_arg_strategy.html">最近的一些经验总结</a></li>
<li><a href="https://brycemeng.com/AdvFinML-Standard-Bars.html">AdvFinML - Standard Bars</a></li>
<li><a href="https://brycemeng.com/something-about-financial-analysis.html">财务分析相关</a></li>
<li><a href="https://brycemeng.com/hidden-markov-model.html">HMM 稳形马尔科夫</a></li>
<li><a href="https://brycemeng.com/numpy-version-pandas-ewm-mean-var.html">numpy pandas ewm mean/var</a></li>
</ul>
</section>
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'brycemeng'; // required: replace example with your forum shortname
var disqus_config = function () {
this.language = "en";
this.page.identifier = '2019-01-14-bug-record-coutinues-contract-switch';
this.page.url = 'https://brycemeng.com/bug-record-coutinues-contract-switch.html';
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<!-- Sidebar -->
<section class="well well-sm">
<ul class="list-group list-group-flush">
<!-- Sidebar/Social -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://github.com/BryceMeng"><i class="fa fa-github-square fa-lg"></i> Github</a></li>
</ul>
</li>
<!-- End Sidebar/Social -->
<!-- Sidebar/Tag Cloud -->
<li class="list-group-item">
<a href="https://brycemeng.com/"><h4><i class="fa fa-tags fa-lg"></i><span class="icon-label">Tags</span></h4></a>
<ul class="list-group list-inline tagcloud" id="tags">
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/101alpha.html">101Alpha</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/adb.html">adb</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/advfinml.html">AdvFinML</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/android.html">Android</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/async.html">async</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/cryptocurrency.html">Cryptocurrency</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/elite3.html">Elite3</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/english.html">English</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/fifthcircle.html">fifthcircle</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/fun.html">Fun</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/futures.html">Futures</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/genetic-algorithm.html">Genetic Algorithm</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/hidden-markov.html">Hidden Markov</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/hifi.html">Hifi</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/javascript.html">javascript</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/kalman-filter.html">Kalman Filter</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/life.html">Life</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/math.html">Math</a>
</li>
<li class="list-group-item tag-1">
<a href="https://brycemeng.com/tag/misc.html">Misc</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/ml.html">ML</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/mongodb.html">Mongodb</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/monkey.html">monkey</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/multithread.html">multithread</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/music.html">Music</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/numpy.html">Numpy</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/optimization.html">Optimization</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/pandas.html">Pandas</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/programming.html">programming</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/python.html">Python</a>
</li>
<li class="list-group-item tag-1">
<a href="https://brycemeng.com/tag/quant.html">Quant</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/router.html">Router</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/stats.html">Stats</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/stock.html">Stock</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/system.html">System</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/tech.html">Tech</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/tech-indicator.html">Tech Indicator</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/time-series.html">Time Series</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/translation.html">Translation</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/trap.html">Trap</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/ubuntu.html">Ubuntu</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/vm.html">VM</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/wu-du-quan.html">五度圈</a>
</li>
</ul>
</li>
<!-- End Sidebar/Tag Cloud -->
<!-- Sidebar/Links -->
<li class="list-group-item">
<h4><i class="fa fa-external-link-square fa-lg"></i><span class="icon-label">Links</span></h4>
<ul class="list-group" id="links">
<li class="list-group-item">
<a href="http://makerchen.com" target="_blank">MakerChen</a>
</li>
</ul>
</li>
<!-- End Sidebar/Links -->
</ul>
</section>
<!-- End Sidebar --> </aside>
</div>
</div>
</div>
<!-- End Content Container -->
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2024 Bryce
· Powered by <a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> </div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="https://brycemeng.com/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://brycemeng.com/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://brycemeng.com/theme/js/respond.min.js"></script>
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'brycemeng'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
</body>
</html>