-
Notifications
You must be signed in to change notification settings - Fork 94
/
basic-tutorial-2.html
613 lines (547 loc) · 47.3 KB
/
basic-tutorial-2.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<!DOCTYPE html>
<html lang="zh"
>
<head>
<title>Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计 - vn.py</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="/images/favicon.png" rel="icon">
<link rel="canonical" href="/basic-tutorial-2.html">
<meta name="author" content="用Python的交易员" />
<meta name="description" content="原创文章,转载请注明出处:用Python的交易员 (本篇教程包含的内容太多也太复杂,有不少读者反应看不懂,因为本身也不是使用vn.py必须掌握的知识,这篇教程暂时处于半完成状态,等多收集些读者的建议后会再做一个比较大的修订) 为什么要封装API 直接原因就是C++的API没法直接在Python里用,不过这个回答有点太简单,这里我们稍微做一些拓展解释: C++ API中很多函数的调用参数是ApiStruct.h(参见上一篇)中定义的结构体,而在Python中我们既无法直接创建这些结构体(主动函数),也无法提取结构体中包含的数据(回调函数)。 Python虚拟机是基于C语言实现的,所有的Python对象,哪怕只是一个整数或者字符串,在C的环境中都是一个PyObject对象(好吧,我知道C里没有对象,只有结构体,但估计90%的读者都不在乎这个区别)。用户如果在Python中直接传递一个参数到C++环境里,C++是无法识别的(Python:买入1手股指, C++:你要买入多少?)。 Python只能加载封装为PyObject对象的模块,因此原生C++的API在Python中连加载都加载不了。 封装后API的工作流程 主动函数 用户在Python程序中调用封装API的主动函数,并直接传入Python变量(PyObject对象)作为参数。 封装API将Python变量转换成C ..." />
<meta property="og:site_name" content="vn.py" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计"/>
<meta property="og:url" content="/basic-tutorial-2.html"/>
<meta property="og:description" content="原创文章,转载请注明出处:用Python的交易员 (本篇教程包含的内容太多也太复杂,有不少读者反应看不懂,因为本身也不是使用vn.py必须掌握的知识,这篇教程暂时处于半完成状态,等多收集些读者的建议后会再做一个比较大的修订) 为什么要封装API 直接原因就是C++的API没法直接在Python里用,不过这个回答有点太简单,这里我们稍微做一些拓展解释: C++ API中很多函数的调用参数是ApiStruct.h(参见上一篇)中定义的结构体,而在Python中我们既无法直接创建这些结构体(主动函数),也无法提取结构体中包含的数据(回调函数)。 Python虚拟机是基于C语言实现的,所有的Python对象,哪怕只是一个整数或者字符串,在C的环境中都是一个PyObject对象(好吧,我知道C里没有对象,只有结构体,但估计90%的读者都不在乎这个区别)。用户如果在Python中直接传递一个参数到C++环境里,C++是无法识别的(Python:买入1手股指, C++:你要买入多少?)。 Python只能加载封装为PyObject对象的模块,因此原生C++的API在Python中连加载都加载不了。 封装后API的工作流程 主动函数 用户在Python程序中调用封装API的主动函数,并直接传入Python变量(PyObject对象)作为参数。 封装API将Python变量转换成C ..."/>
<meta property="article:published_time" content="2015-03-09" />
<meta property="article:section" content="文章" />
<meta property="article:author" content="用Python的交易员" />
<!-- Bootstrap -->
<link rel="stylesheet" href="/theme/css/bootstrap.readable.min.css" type="text/css"/>
<link href="/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="/theme/css/pygments/monokai.css" rel="stylesheet">
<link rel="stylesheet" href="/theme/css/style.css" type="text/css"/>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?e8c7573f82d43fa50c895a8e28c49ceb";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</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="/" class="navbar-brand">
<img src="/images/favicon.png" width=""/> vn.py </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><a href="/pages/quickstart.html">
Quick Start
</a></li>
<li><a href="/pages/blog.html">
日志
</a></li>
<li><a href="/pages/screenshot.html">
截图
</a></li>
<li><a href="/pages/community.html">
社区
</a></li>
<li><a href="/pages/api.html">
API接口
</a></li>
<li><a href="/pages/download.html">
下载
</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="/archives.html"><i class="fa fa-th-list"></i><span class="icon-label">Archives</span></a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<style>
#banner{
background-image:url("/images/banner.png");
}
</style>
<div id="banner">
<div class="container">
<div class="copy">
<h1>vn.py</h1>
<p class="intro">Developed by traders, for traders.</p>
</div>
</div>
</div><!-- End Banner -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="/basic-tutorial-2.html"
rel="bookmark"
title="Permalink to Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计">
Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计
</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="2015-03-09T11:49:28+08:00"> 2015-03-09(周一)</time>
</span>
</footer><!-- /.post-info --> </div>
</div>
<p>原创文章,转载请注明出处:用Python的交易员</p>
<p>(本篇教程包含的内容太多也太复杂,有不少读者反应看不懂,因为本身也不是使用vn.py必须掌握的知识,这篇教程暂时处于半完成状态,等多收集些读者的建议后会再做一个比较大的修订)</p>
<h2>为什么要封装API</h2>
<p>直接原因就是C++的API没法直接在Python里用,不过这个回答有点太简单,这里我们稍微做一些拓展解释:</p>
<ol>
<li>
<p>C++ API中很多函数的调用参数是ApiStruct.h(参见上一篇)中定义的结构体,而在Python中我们既无法直接创建这些结构体(主动函数),也无法提取结构体中包含的数据(回调函数)。</p>
</li>
<li>
<p>Python虚拟机是基于C语言实现的,所有的Python对象,哪怕只是一个整数或者字符串,在C的环境中都是一个PyObject对象(好吧,我知道C里没有对象,只有结构体,但估计90%的读者都不在乎这个区别)。用户如果在Python中直接传递一个参数到C++环境里,C++是无法识别的(Python:买入1手股指, C++:你要买入多少?)。</p>
</li>
<li>
<p>Python只能加载封装为PyObject对象的模块,因此原生C++的API在Python中连加载都加载不了。</p>
</li>
</ol>
<h2>封装后API的工作流程</h2>
<h3>主动函数</h3>
<ol>
<li>
<p>用户在Python程序中调用封装API的主动函数,并直接传入Python变量(PyObject对象)作为参数。</p>
</li>
<li>
<p>封装API将Python变量转换成C++变量。</p>
</li>
<li>
<p>封装API调用原生API的主动函数,并传入C++变量作为参数。</p>
</li>
</ol>
<h3>回调函数</h3>
<ol>
<li>
<p>交易柜台通过原生API的C++回调函数推送数据信息,传入参数为C++变量</p>
</li>
<li>
<p>封装API将C++变量转换为Python变量</p>
</li>
<li>
<p>封装API调用封装后的回调函数向用户的Python程序中推送数据,传入参数为Python变量</p>
</li>
</ol>
<h3>名词定义</h3>
<ul>
<li>
<p>封装API:指的是经过封装后,可以直接在Python中使用的API</p>
</li>
<li>
<p>原生API:指的是由软件公司提供,在C++中使用的API</p>
</li>
<li>
<p>Python变量:包含Python中的数字、字符串、对象等等</p>
</li>
<li>
<p>C++变量:包含C++中的内置数据类型和结构体等</p>
</li>
</ul>
<h2>从Python的角度看原生API的一些问题</h2>
<p>上一篇教程后读者应该对C++ API的结构和使用方法有了基础的了解,这篇教程主要介绍的是对原生的C++ API进行Python封装时的设计和思路,这些构成了<a href="https://github.com/vnpy/vnpy">vn.py</a>开源项目中vn.lts(华宝证券LTS柜台API封装)的基础。首先让我们来从Python的角度看看原生API的一些问题:</p>
<ol>
<li>
<p>原生的API中每个功能分为了两个类:分别是包含回调函数的Spi类和主动函数的Api类,这种设计能让用户更好的分清不同的功能。但是从面向对象的角度,把两个类封装到一起更为方便,实际使用中绝大部分C++的用户也会将接口整合到一个类里面(可以参见网上很多CTP开发的示例代码),因此Python的API中,我们也会将Spi和Api两个类的功能封装到一个类中。</p>
</li>
<li>
<p>原生的API中回调函数被触发后必须快速返回,否则会导致其他数据的推送被阻塞,阻塞时间长了还有可能导致API发生崩溃,因此回调函数中不适合包含耗时较长的计算逻辑。例如某个TICK行情推送后,如果用户在回调函数中写了一些比较复杂的计算(循环计算等等),耗时超过3秒(这个数字只是笔者的一个经验),则在这个3秒中,其他的行情推送用户是收不到的(被阻塞了),且很可能3秒后会出现API崩溃(程序死掉)。这里的解决方案是使用生产者-消费者模型,在API中包含一个缓冲队列,当回调函数收到新的数据信息时只是简单存入缓冲队列中并立即返回,而数据信息的处理和向Python中的推送则由另一个工作线程来执行。</p>
</li>
<li>
<p>API的函数中使用了大量的结构体用于数据传送,这在C++而言是非常自然的设计,但是对Python封装会造成不小的麻烦,所有的结构体都要封装成对应的Python类,工作量太大也非常容易出错。这点我们可以利用Python相对于C++更为高级的数据结构来解决,Python中的dict字典本质是一个哈希表,但是同一个字典内键和值的类型允许不同,这个特性使得字典可以非常方便的用来代替C++的结构体。</p>
</li>
</ol>
<p>明确了以上的问题后,我们就可以开始着手设计Python API的结构了。</p>
<h2>Python API的结构设计</h2>
<p>这里使用行情API作为示例。</p>
<p>...表示省略的代码。</p>
<div class="highlight"><pre><span></span><span class="c1">//API的继承实现</span>
<span class="k">class</span> <span class="nc">MdApi</span> <span class="o">:</span> <span class="k">public</span> <span class="n">CSecurityFtdcMdSpi</span>
<span class="p">{</span>
<span class="k">private</span><span class="o">:</span>
<span class="n">CSecurityFtdcMdApi</span><span class="o">*</span> <span class="n">api</span><span class="p">;</span> <span class="c1">//API对象</span>
<span class="kr">thread</span> <span class="o">*</span><span class="n">task_thread</span><span class="p">;</span> <span class="c1">//工作线程指针(向python中推送数据)</span>
<span class="n">ConcurrentQueue</span><span class="o"><</span><span class="n">Task</span><span class="o">></span> <span class="n">task_queue</span><span class="p">;</span> <span class="c1">//任务队列</span>
<span class="k">public</span><span class="o">:</span>
<span class="n">MdApi</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">function0</span><span class="o"><</span><span class="kt">void</span><span class="o">></span> <span class="n">f</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&</span><span class="n">MdApi</span><span class="o">::</span><span class="n">processTask</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
<span class="kr">thread</span> <span class="nf">t</span><span class="p">(</span><span class="n">f</span><span class="p">);</span>
<span class="k">this</span><span class="o">-></span><span class="n">task_thread</span> <span class="o">=</span> <span class="o">&</span><span class="n">t</span><span class="p">;</span>
<span class="p">};</span>
<span class="o">~</span><span class="n">MdApi</span><span class="p">()</span>
<span class="p">{</span>
<span class="p">};</span>
<span class="p">...</span>
<span class="c1">//登录请求响应</span>
<span class="k">virtual</span> <span class="kt">void</span> <span class="n">OnRspUserLogin</span><span class="p">(</span><span class="n">CSecurityFtdcRspUserLoginField</span> <span class="o">*</span><span class="n">pRspUserLogin</span><span class="p">,</span> <span class="n">CSecurityFtdcRspInfoField</span> <span class="o">*</span><span class="n">pRspInfo</span><span class="p">,</span> <span class="kt">int</span> <span class="n">nRequestID</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">bIsLast</span><span class="p">);</span>
<span class="p">...</span>
<span class="c1">//数据任务处理函数(在工作线程中运行)</span>
<span class="kt">void</span> <span class="n">processTask</span><span class="p">();</span>
<span class="p">...</span>
<span class="c1">//处理登陆请求响应</span>
<span class="kt">void</span> <span class="n">processRspUserLogin</span><span class="p">(</span><span class="n">Task</span> <span class="n">task</span><span class="p">);</span>
<span class="p">...</span>
<span class="c1">//该回调函数在Python中继承</span>
<span class="k">virtual</span> <span class="kt">void</span> <span class="n">onRspUserLogin</span><span class="p">(</span><span class="n">dict</span> <span class="n">data</span><span class="p">,</span> <span class="n">dict</span> <span class="n">error</span><span class="p">,</span> <span class="kt">int</span> <span class="n">id</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">last</span><span class="p">)</span> <span class="p">{};</span>
<span class="p">...</span>
<span class="c1">//请求登陆的主动函数</span>
<span class="kt">int</span> <span class="n">reqUserLogin</span><span class="p">(</span><span class="n">dict</span> <span class="n">req</span><span class="p">,</span> <span class="kt">int</span> <span class="n">nRequestID</span><span class="p">);</span>
<span class="p">...</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.h文件)</p>
<p>注意原生API的函数名开头都是大写字母,为了便于分辨以及符合Python的PEP8编码规则,作者的函数都以小写字母开头。上面的代码中采用的示例是用户登陆UserLogin这个功能。</p>
<h3>封装中的类和函数命名规则</h3>
<ul>
<li>
<p>封装后的Python API的类取名为MdApi,注意这个不是原生API中的CSecurityFtdcMdApi。</p>
</li>
<li>
<p>原生API中以On开头的回调函数(如OnRspUserLogin)对应的Python API的回调函数直接改为以on开头(如onRspUserLogin)。</p>
</li>
<li>
<p>原生API中的主动函数(如ReqUserLogin)对应的封装后API中的主动函数改为首字母小写(如reqUserLogin)。</p>
</li>
</ul>
<h3>MdApi的成员变量</h3>
<ul>
<li>
<p>api:原生API中的CSecurityFtdcMdApi对象,用于实现主动函数的调用。</p>
</li>
<li>
<p>task_thread:一个boost线程指针,用于实现任务线程的工作。 </p>
</li>
<li>
<p>task_queue:一个线程安全的任务队列。</p>
</li>
</ul>
<h3>工作步骤(后面会有具体函数的实现细节)</h3>
<ol>
<li>
<p>用户在Python中调用reqUserLogin函数,传入参数为包含登陆信息(用户名、密码)的字典req以及本次请求号nRequestID,该函数自动将字典中的信息提取并创建原生API使用的结构体后,调用原生API的主动函数ReqUserLogin来进行登录。</p>
</li>
<li>
<p>登录成功后,原生API会调用OnRspUserLogin的回调函数返回登录信息(注意这里On是大写),在回调函数里,只是简单的把结构体数据保存到一个任务对象Task中,并推送到任务队列里。</p>
</li>
<li>
<p>工作线程中运行的函数是processTask,该函数负责检查任务队列中是否有新的任务,如果有则调用对应的process函数进行处理,如果没有则阻塞等待。</p>
</li>
<li>
<p>processTask函数检查到任务队列中OnRspUserLogin推送的一个任务后,调用processRspUserLogin函数进行处理。该函数首先从结构体中提取数据并转换为Python字典,然后调用onRspUserLogin函数(这里的on是小写)推送到Python环境中,onRspUserLogin函数由用户在Python中继承实现。</p>
</li>
</ol>
<h2>Python API的函数实现</h2>
<p>仍然使用之前的示例进行函数实现的讲解,包括MdApi的构造、析构函数,主动函数(reqUserLogin等),原生API回调函数(OnRspUserLogin等)和任务处理函数(processTask和processRspUserLogin等)。</p>
<h3>构造、析构函数</h3>
<div class="highlight"><pre><span></span><span class="n">MdApi</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">function0</span><span class="o"><</span><span class="kt">void</span><span class="o">></span> <span class="n">f</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&</span><span class="n">MdApi</span><span class="o">::</span><span class="n">processTask</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
<span class="kr">thread</span> <span class="nf">t</span><span class="p">(</span><span class="n">f</span><span class="p">);</span>
<span class="k">this</span><span class="o">-></span><span class="n">task_thread</span> <span class="o">=</span> <span class="o">&</span><span class="n">t</span><span class="p">;</span>
<span class="p">};</span>
<span class="o">~</span><span class="n">MdApi</span><span class="p">()</span>
<span class="p">{</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.h文件)</p>
<p>构造函数中仅包含了创建一个工作函数为processTask的工作线程,并将该线程的指针绑定到task_thread上。</p>
<p>析构函数为空,用户在退出前应当主动调用安全退出函数(参见源代码中的exit)。</p>
<h3>主动函数</h3>
<div class="highlight"><pre><span></span><span class="kt">int</span> <span class="n">MdApi</span><span class="o">::</span><span class="n">reqUserLogin</span><span class="p">(</span><span class="n">dict</span> <span class="n">req</span><span class="p">,</span> <span class="kt">int</span> <span class="n">nRequestID</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">//创建原生API函数调用需要的结构体</span>
<span class="n">CSecurityFtdcReqUserLoginField</span> <span class="n">myreq</span> <span class="o">=</span> <span class="n">CSecurityFtdcReqUserLoginField</span><span class="p">();</span>
<span class="c1">//初始化这个结构体的内存</span>
<span class="n">memset</span><span class="p">(</span><span class="o">&</span><span class="n">myreq</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">myreq</span><span class="p">));</span>
<span class="c1">//提取字典中的内容并复制到结构体中</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"MacAddress"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">MacAddress</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"UserProductInfo"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">UserProductInfo</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"UserID"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">UserID</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"AuthCode"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">AuthCode</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"TradingDay"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">TradingDay</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"InterfaceProductInfo"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">InterfaceProductInfo</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"BrokerID"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">BrokerID</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"ClientIPAddress"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">ClientIPAddress</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"OneTimePassword"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">OneTimePassword</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"ProtocolInfo"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">ProtocolInfo</span><span class="p">);</span>
<span class="n">getChar</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="s">"Password"</span><span class="p">,</span> <span class="n">myreq</span><span class="p">.</span><span class="n">Password</span><span class="p">);</span>
<span class="c1">//将结构体的指针和代表请求编号的整数作为参数调用原生API的函数</span>
<span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="k">this</span><span class="o">-></span><span class="n">api</span><span class="o">-></span><span class="n">ReqUserLogin</span><span class="p">(</span><span class="o">&</span><span class="n">myreq</span><span class="p">,</span> <span class="n">nRequestID</span><span class="p">);</span>
<span class="c1">//返回原生API函数的调用结果</span>
<span class="k">return</span> <span class="n">i</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.cpp文件)</p>
<p>原生API中的请求登录函数为ReqUserLogin,传入的参数一共包含两个:一个CSecurityFtdcReqUserLoginField 结构体的指针,一个代表请求编号的整数。</p>
<p>封装后的API函数为reqUserLogin,传入参数同样为两个:一个Python字典对象、一个整数。reqUserLogin函数会从Python字典对象中根据键值依次提取结构体中对应的数据。如结构体中有一个成员叫做BrokerID,则使用getChar函数从字典对象中提取"BrokerID"键对应的值。</p>
<p>getChar函数的实现如下:</p>
<div class="highlight"><pre><span></span><span class="c1">//d为Python字典对象</span>
<span class="c1">//key为d中想要提取的数据的键名</span>
<span class="c1">//value为最终需要这个数据的结构体成员的指针</span>
<span class="kt">void</span> <span class="nf">getChar</span><span class="p">(</span><span class="n">dict</span> <span class="n">d</span><span class="p">,</span> <span class="n">string</span> <span class="n">key</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">value</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">//首先检查字典中是否存在key这个键</span>
<span class="k">if</span> <span class="p">(</span><span class="n">d</span><span class="p">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">key</span><span class="p">))</span>
<span class="p">{</span>
<span class="c1">//提取key这个键对应的值,即Python对象o</span>
<span class="n">object</span> <span class="n">o</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">];</span>
<span class="c1">//生成从o中提取std::string类的提取器</span>
<span class="n">extract</span><span class="o"><</span><span class="n">string</span><span class="o">></span> <span class="n">x</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<span class="c1">//检查提取器是否能提取出数据</span>
<span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="p">.</span><span class="n">check</span><span class="p">())</span>
<span class="p">{</span>
<span class="c1">//执行解包器,提取string对象s</span>
<span class="n">string</span> <span class="n">s</span> <span class="o">=</span> <span class="n">x</span><span class="p">();</span>
<span class="c1">//从s中获取字符串指针buffer</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span> <span class="o">=</span> <span class="n">s</span><span class="p">.</span><span class="n">c_str</span><span class="p">();</span>
<span class="c1">//将字符串指针指向的字符串数组复制到结构体成员的指针上</span>
<span class="c1">//对字符串指针赋值必须使用strcpy_s, vs2013使用strcpy编译通不过</span>
<span class="c1">//+1应该是因为C++字符串的结尾符号?不是特别确定,不加这个1会出错</span>
<span class="n">strcpy_s</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">buffer</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">buffer</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.cpp文件)</p>
<p>由于原生API中用到的底层数据类型主要包括四种:char字符、char[]字符串数组、int整数、double浮点数,可以对应的Python中的数据类型为:string、int、float。因此设计了三个函数getChar、getInt、getDouble来从Python对象中提取所需的C++数据,getInt、getDouble请参见源代码。</p>
<h3>原生API的回调函数</h3>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="n">MdApi</span><span class="o">::</span><span class="n">OnRspUserLogin</span><span class="p">(</span><span class="n">CSecurityFtdcRspUserLoginField</span> <span class="o">*</span><span class="n">pRspUserLogin</span><span class="p">,</span> <span class="n">CSecurityFtdcRspInfoField</span> <span class="o">*</span><span class="n">pRspInfo</span><span class="p">,</span> <span class="kt">int</span> <span class="n">nRequestID</span><span class="p">,</span> <span class="kt">bool</span> <span class="n">bIsLast</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">Task</span> <span class="n">task</span> <span class="o">=</span> <span class="n">Task</span><span class="p">();</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_name</span> <span class="o">=</span> <span class="n">ONRSPUSERLOGIN</span><span class="p">;</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_data</span> <span class="o">=</span> <span class="o">*</span><span class="n">pRspUserLogin</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">pRspInfo</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_error</span> <span class="o">=</span> <span class="o">*</span><span class="n">pRspInfo</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="n">CSecurityFtdcRspInfoField</span> <span class="n">empty_error</span> <span class="o">=</span> <span class="n">CSecurityFtdcRspInfoField</span><span class="p">();</span>
<span class="n">memset</span><span class="p">(</span><span class="o">&</span><span class="n">empty_error</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">empty_error</span><span class="p">));</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_error</span> <span class="o">=</span> <span class="n">empty_error</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_id</span> <span class="o">=</span> <span class="n">nRequestID</span><span class="p">;</span>
<span class="n">task</span><span class="p">.</span><span class="n">task_last</span> <span class="o">=</span> <span class="n">bIsLast</span><span class="p">;</span>
<span class="k">this</span><span class="o">-></span><span class="n">task_queue</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">task</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.cpp文件)</p>
<p>当登录成功后,原生API中的回调函数OnRspUserLogin会被自动调用,通知用户登录相关的信息,传入参数包括四个,分别为CSecurityFtdcRspUserLoginField结构体指针(用户本次登录的相关信息)pRspUserLogin,CSecurityFtdcRspInfoField结构体指针(登录是否存在错误的相关信息)pRspInfo,整数(登陆请求编号)nRequestID和布尔值(是否为该请求的最后一次通知)bIsLast。</p>
<p>在回调函数中,我们通过创建一个Task对象来保存这些信息,并推入task_queue中,等待工作线程的提取处理。其中,由于pRspInfo可能存在空指针的情况,所以需要进行判断,若指针为空,则在Task对象上绑定一个内容为空的CSecurityFtdcRspInfoField结构体(这步等于一个异常情况的处理)。ONRSPUSERLOGIN是一个整数常量(在头文件中定义),用于标识该Task对象包含的是哪个回调函数返回的信息。</p>
<p>Task对象的定义如下:</p>
<div class="highlight"><pre><span></span><span class="c1">//任务结构体</span>
<span class="k">struct</span> <span class="n">Task</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">task_name</span><span class="p">;</span> <span class="c1">//回调函数名称对应的常量</span>
<span class="n">any</span> <span class="n">task_data</span><span class="p">;</span> <span class="c1">//数据结构体</span>
<span class="n">any</span> <span class="n">task_error</span><span class="p">;</span> <span class="c1">//错误结构体</span>
<span class="kt">int</span> <span class="n">task_id</span><span class="p">;</span> <span class="c1">//请求id</span>
<span class="kt">bool</span> <span class="n">task_last</span><span class="p">;</span> <span class="c1">//是否为最后返回</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.h文件)</p>
<p>其中any是boost库中的any类,作用是定义一个可以存放任意类型数据的变量(有点类似于Python里的变量),但是当用户尝试从该变量中获取原本的数据时,需要知道原本数据的类型。原生API中不同回调函数返回的参数类型是不同的,因此为了提高代码的简洁性选择使用boost.any这个泛型类。</p>
<h3>任务处理函数</h3>
<p>首先是负责从任务队列中提取任务,并根据任务名称的不同使用对应的函数进行处理的processTask函数:</p>
<div class="highlight"><pre><span></span><span class="c1">///-------------------------------------------------------------------------------------</span>
<span class="c1">///工作线程从队列中取出数据,转化为python对象后,进行推送</span>
<span class="c1">///-------------------------------------------------------------------------------------</span>
<span class="kt">void</span> <span class="n">MdApi</span><span class="o">::</span><span class="n">processTask</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">while</span> <span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">Task</span> <span class="n">task</span> <span class="o">=</span> <span class="k">this</span><span class="o">-></span><span class="n">task_queue</span><span class="p">.</span><span class="n">wait_and_pop</span><span class="p">();</span>
<span class="k">switch</span> <span class="p">(</span><span class="n">task</span><span class="p">.</span><span class="n">task_name</span><span class="p">)</span>
<span class="p">{</span>
<span class="p">...</span>
<span class="k">case</span> <span class="nl">ONRSPUSERLOGIN</span><span class="p">:</span>
<span class="p">{</span>
<span class="k">this</span><span class="o">-></span><span class="n">processRspUserLogin</span><span class="p">(</span><span class="n">task</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">...</span>
<span class="p">};</span>
<span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.cpp文件)</p>
<p>使用while (1)的方式让processTask处于无限循环中不断运行,从task_queue队列中提取任务对象task后,使用swtich根据任务的回调函数名称task_name,调用对应的函数处理该任务。上面的例子中,当程序检查task_name是ONRSPUSERLOGIN这个常量值后,就会调用processRspUserLogin函数进行处理,其代码如下:</p>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="n">MdApi</span><span class="o">::</span><span class="n">processRspUserLogin</span><span class="p">(</span><span class="n">Task</span> <span class="n">task</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">CSecurityFtdcRspUserLoginField</span> <span class="n">task_data</span> <span class="o">=</span> <span class="n">any_cast</span><span class="o"><</span><span class="n">CSecurityFtdcRspUserLoginField</span><span class="o">></span><span class="p">(</span><span class="n">task</span><span class="p">.</span><span class="n">task_data</span><span class="p">);</span>
<span class="n">dict</span> <span class="n">data</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"MaxOrderRef"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">MaxOrderRef</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"UserID"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">UserID</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"TradingDay"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">TradingDay</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"SessionID"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">SessionID</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"SystemName"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">SystemName</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"FrontID"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">FrontID</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"BrokerID"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">BrokerID</span><span class="p">;</span>
<span class="n">data</span><span class="p">[</span><span class="s">"LoginTime"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_data</span><span class="p">.</span><span class="n">LoginTime</span><span class="p">;</span>
<span class="n">CSecurityFtdcRspInfoField</span> <span class="n">task_error</span> <span class="o">=</span> <span class="n">any_cast</span><span class="o"><</span><span class="n">CSecurityFtdcRspInfoField</span><span class="o">></span><span class="p">(</span><span class="n">task</span><span class="p">.</span><span class="n">task_error</span><span class="p">);</span>
<span class="n">dict</span> <span class="n">error</span><span class="p">;</span>
<span class="n">error</span><span class="p">[</span><span class="s">"ErrorMsg"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_error</span><span class="p">.</span><span class="n">ErrorMsg</span><span class="p">;</span>
<span class="n">error</span><span class="p">[</span><span class="s">"ErrorID"</span><span class="p">]</span> <span class="o">=</span> <span class="n">task_error</span><span class="p">.</span><span class="n">ErrorID</span><span class="p">;</span>
<span class="k">this</span><span class="o">-></span><span class="n">onRspUserLogin</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">task</span><span class="p">.</span><span class="n">task_id</span><span class="p">,</span> <span class="n">task</span><span class="p">.</span><span class="n">task_last</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
<p>(以上代码截取自vnltsmd.cpp文件)</p>
<p>any_cast函数由boost.any库提供,作用之前提到的从any变量中提取出用户需要的数据类型来。dict类由boost.python库提供,使用dict可以直接创建Python环境中的字典,同时当我们使用d[key] = value这种语句进行赋值时,dict中的key和value均会自动转换为对应的Python对象。当我们将返回的业务信息CSecurityFtdcRspUserLoginField结构体和错误信息结构体CSecurityFtdcRspInfoField分别转换为data和error这两个Python字典后,我们就可以通过onRspUserLogin回调函数推送到Python环境中了。</p>
<h1>总结</h1>
<p>之前几段示例代码展示的是用户登陆这个简单业务操作,包括了从用户在Python中调用主动函数到柜台通过回调函数返回信息再推送到Python中的全过程。文章主要是对源代码中的注释起到一个更为细致的解释作用。同样这篇内容对于想用vn.py做量化平台开发的用户而言不是必须掌握的东西,放在这里主要是考虑教程的完整性,看不懂的就先跳过吧。</p>
<p>下一章是vn.py平台中API部分的编译方法,github上项目里的.pyd文件可能由于你的操作系统或者编译器和作者本人的不同没法直接使用,必须自行编译,整个教程会包含一步步的截图和说明,包教包会(还不会的可以到github上提问 :p )。</p>
</div>
<!-- /.entry-content -->
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<section class="well well-sm">
<ul class="list-group list-group-flush">
<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="http://github.com/vnpy/vnpy"><i class="fa fa-github-square fa-lg"></i> Github</a></li>
</ul>
</li>
<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://www.vnpie.com" target="_blank">
官方论坛 - 维恩的派
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.trader" target="_blank">
交易平台
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.event" target="_blank">
事件引擎
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.ctp" target="_blank">
CTP接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.xspeed" target="_blank">
飞创接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.femas" target="_blank">
飞马接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.ksotp" target="_blank">
金仕达期权接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.ksgold" target="_blank">
金仕达黄金接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.sgit" target="_blank">
飞鼠接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/dev/vn.qdp" target="_blank">
QDP接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.oanda" target="_blank">
OANDA接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.ib" target="_blank">
IB接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.shzd" target="_blank">
直达期货接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.okcoin" target="_blank">
OKCoin接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.datayes" target="_blank">
通联数据接口
</a>
</li>
<li class="list-group-item">
<a href="http://github.com/vnpy/vnpy/tree/master/vn.demo" target="_blank">
开发DEMO
</a>
</li>
</ul>
</li>
</ul>
</section>
</aside>
</div>
</div>
</div>
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2017 用Python的交易员
· Powered by <a href="https://github.com/DandyDev/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="/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="/theme/js/respond.min.js"></script>
<script src="/theme/js/bodypadding.js"></script>
</body>
</html>