数据分析

1 数据导出工具

系统提供了内存数据库查看和导出工具:

内存数据库查看工具帮助

$ kungfu journal -h
Options:
  -h [ --help ]                         Help screen
  -f [ --folder ] arg                   Journal Folder
  -n [ --name ] arg                     Journal Name
  -p [ --page ]                         Just Page Header
  -v [ --verify ]                       Verify hash code
  -k [ --keep ]                         keep listening
  -t [ --time ]                         time visualized
  -d [ --detail ]                       data details
  -c [ --current ]                      start from current
  -l [ --length ] arg (=20)             Char num of frame data to print
  -m [ --msgtype ] arg                  Message type printed, -eg: -m 10,11
  -r [ --rmsgtype ] arg                 Message type not printed, -eg: -r 10,11
  -s [ --start_time ] arg (=20000101-13:30:00)
                                        start time (%Y%m%d-%H:%M:%S)
  -e [ --end_time ] arg (=20200101-00:00:00)
                                        end time (%Y%m%d-%H:%M:%S)

查看内存数据库中指定信息:

$ kungfu journal -f /shared/kungfu/journal/TD/CTP/ -n TD_CTP  -s 20171114-14:40:00 -e 20171114-14:45:00 -d -t -m 206
  StartTime:      20171114-14:40:00
  EndTime:        20171114-14:45:00
  Folder: /shared/kungfu/journal/TD/CTP/
  ShortName:      TD_CTP
  MsgType to Print: 206
  [0] (st)1 (so)1 (na)1510641601378381049 (en)0 (vn)20171114-14:40:01 (fl)302 (dl)264 (hl)38 (hs)4109164010 (mt)206 (lf)1 (id)8000122 (er)0 (cn)9999078622
             BrokerID: (c11) 9999
               UserID: (c16) 078622
           InvestorID: (c19) 078622
         BusinessUnit: (c21)
         InstrumentID: (c31) rb1801
             OrderRef: (c21) 124
           ExchangeID: (c11) SHFE
              TradeID: (c21)       125394
           OrderSysID: (c31)       196229
        ParticipantID: (c11) 9999
             ClientID: (c21) 9999078602
                Price:   (d) 3946
               Volume:   (i) 1
           TradingDay: (c13) 20171114
            TradeTime: (c13) 14:40:00
            Direction:   (t) Sell
           OffsetFlag:   (t) Open
            HedgeFlag:   (t) Speculation

内存数据库导出工具帮助:

$ kungfu dump -h
usage: journal_dumper [-h] [-f FOLDER] [-n NAME] [-m MSG_TYPE] [-o OUTPUT] [-v] [-p] [-s START] [-e END]

optional arguments:
  -h, --help            show this help message and exit
  -f FOLDER, --folder FOLDER
                        folder name
  -n NAME, --name NAME  journal name
  -m MSG_TYPE, --msg_type MSG_TYPE
                        msg type to dump
  -o OUTPUT, --output OUTPUT
                        output file name
  -v, --visualize       visualize the time
  -p, --print_out       print to console while dumping
  -s START, --start START
                        start time (&Y&m&d-&H:&M:&S)
  -e END, --end END     end time (&Y&m&d-&H:&M:&S)

导出内存数据库中指定信息:

$ kungfu dump -f /shared/kungfu/journal/MD/CTP/ -n MD_CTP -s 20171114-09:30:00 -e 20171114-16:00:00 -m 101 -o md_20171114.csv
$ head -n 3 md_20171114.csv
TradingDay(c13),InstrumentID(c31),ExchangeID(c9),ExchangeInstID(c64),LastPrice(d),PreSettlementPrice(d),PreClosePrice(d),PreOpenInterest(d),OpenPrice(d),HighestPrice(d),LowestPrice(d),Volume(i),Turnover(d),OpenInterest(d),ClosePrice(d),SettlementPrice(d),UpperLimitPrice(d),LowerLimitPrice(d),PreDelta(d),CurrDelta(d),UpdateTime(c13),UpdateMillisec(i),BidPrice1(d),BidVolume1(i),AskPrice1(d),AskVolume1(i),BidPrice2(d),BidVolume2(i),AskPrice2(d),AskVolume2(i),BidPrice3(d),BidVolume3(i),AskPrice3(d),AskVolume3(i),BidPrice4(d),BidVolume4(i),AskPrice4(d),AskVolume4(i),BidPrice5(d),BidVolume5(i),AskPrice5(d),AskVolume5(i),h_nano(l),h_msg_type(i),h_request_id(i),h_source(i),h_is_last(i),h_error_id(i),j_name(s)
20171114,rb1801,,,3941.0,3860.0,3894.0,1794552.0,3900.0,3950.0,3873.0,821770,32084979660.0,1697634.0,1.79769313486e+308,1.79769313486e+308,4130.0,3589.0,1.79769313486e+308,1.79769313486e+308,14:19:08,500,3941.0,24,3942.0,340,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1510640349349309138,101,-1,1,1,0,MD_CTP
20171114,rb1801,,,3941.0,3860.0,3894.0,1794552.0,3900.0,3950.0,3873.0,821772,32085058480.0,1697634.0,1.79769313486e+308,1.79769313486e+308,4130.0,3589.0,1.79769313486e+308,1.79769313486e+308,14:19:09,0,3941.0,23,3942.0,341,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1.79769313486e+308,0,1510640349845391063,101,-1,1,1,0,MD_CTP

示例中-m参数为信息分类标记,常用的信息分类如下表,全部信息分类可查看源代码 /longfist/longfist/LFConstants.h

名称 含义
MSG_TYPE_LF_MD 101 tick行情数据,在journal/MD目录下相应接口目录下数据库文件中
MSG_TYPE_LF_BAR_MD 110 bar行情数据,在journal/MD目录下相应接口目录下数据库文件中
MSG_TYPE_LF_QRY_POS 201 查询持仓请求数据,在journal/strategy/相应策略数据库文件中
MSG_TYPE_LF_RSP_POS 202 查询持仓返回数据,在journal/TD/相应接口目录下数据库文件中
MSG_TYPE_LF_ORDER 204 下单请求数据,在journal/strategy/相应策略数据库文件中
MSG_TYPE_LF_RTN_ORDER 205 下单回报数据,在journal/TD/相应接口目录下数据库文件中
MSG_TYPE_LF_RTN_TRADE 206 成交回报数据,在journal/TD/相应接口目录下数据库文件中
MSG_TYPE_LF_ORDER_ACTION 207 撤单请求数据,在journal/strategy/相应策略数据库文件中
MSG_TYPE_LF_RSP_ACCOUNT 209 撤单回报数据,在journal/TD/相应接口目录下数据库文件中

2 交易统计工具

统计分析工具帮助:

$ wingchun report -h
Options:
  -h [ --help ]                         Help screen
  -n [ --name ] arg                     strategy name
  -l [ --list ]                         list all sessions with index
  -o [ --order ]                        show orders
  -t [ --trade ]                        show trades
  -a [ --aggregate ]                    show aggregated latency stat
  -d [ --detail ]                       list order detail nanos
  -i [ --index ] arg (=-1)              session index
  -s [ --start_time ] arg (=20000101-13:30:00)
                                        start time (%Y%m%d-%H:%M:%S)
  -e [ --end_time ] arg (=20200101-00:00:00)
                                        end time (%Y%m%d-%H:%M:%S)

委托记录:

$ wingchun report -n band_demo -o

+-----------------------------------------------------------------------------------------------------------------+
|                                  Kungfu order latency report                                                    |
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
| order_id  | rcv_time        | TTT-b(ns)    | TTT-a(ns)    | ticker   | price    | volume   | offset       | dir |
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
|    8000001|20171204-11:01:27|          5779|         43404|    rb1801|    3967.0|         1|          Open|  Buy|
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
|    8000002|20171204-11:01:28|          5297|         51800|    rb1801|    3967.0|         1|          Open|  Buy|
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
|    8000003|20171204-11:01:29|          4965|         45227|    rb1801|    3967.0|         1|          Open|  Buy|
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
|    8000004|20171204-11:01:31|          6469|         51424|    rb1801|    3967.0|         1|          Open|  Buy|
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+
|    8000005|20171204-11:01:31|          7800|         52847|    rb1801|    3967.0|         1|          Open|  Buy|
+-----------+-----------------+--------------+--------------+----------+----------+----------+--------------+-----+

成交记录:

$ wingchun report -n band_demo -t

+-------------------------------------------------------------------------------------------+
|                                  Kungfu trade latency report                              |
+-----------+---+-----------------+--------------+----------+---------+----------+----------+
| order_id  |idx| rcv_time        | OTR(ns)      | ticker   | t_time  | t_price  | t_volume |
+-----------+---+-----------------+--------------+----------+---------+----------+----------+
|    8000000|  1|20171114-14:28:03|      64549194|    rb1801| 14:28:03|    3938.0|         1|
+-----------+---+-----------------+--------------+----------+---------+----------+----------+
|    8000000|  2|20171114-14:41:32|  808582415662|    rb1801| 14:41:31|    3946.0|         1|
+-----------+---+-----------------+--------------+----------+---------+----------+----------+
|    8000001|  1|20171114-14:28:08|      66985968|    rb1801| 14:28:07|    3939.0|         1|
+-----------+---+-----------------+--------------+----------+---------+----------+----------+
|    8000001|  2|20171114-14:42:11|  843622609364|    rb1801| 14:42:11|    3948.0|         1|
+-----------+---+-----------------+--------------+----------+---------+----------+----------+

延迟统计:

$ wingchun report -n band_demo -a

+------------------------------------------------------------------------------------------------------+
|                                       Kungfu latency statistics                                      |
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
| type | description       | mean(ns)     | std(ns)      | min(ns)      | max(ns)      | valid | total |
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
| TTT  | (tick-to-trade)   |                                                                           |
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | before API        |          4323|   3.88215e+03|          2361|         52612|   1000|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | after API         |         50880|   1.61630e+05|         35586|       5064745|   1000|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
| STR  | strategy calc     |          1164|   2.43951e+03|           401|         47234|   1000|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
| OTR  | (order-to-return) |                                                                           |
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | rsp order insert  |             0|   0.00000e+00|             0|             0|      0|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | first rtn order   |      39174735|   1.97535e+07|      30131196|     210217560|   1000|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | first rtn trade   |             0|   0.00000e+00|             0|             0|      0|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+
|      | all rtn trade     |             0|   0.00000e+00|             0|             0|      0|   1000|
+------+-------------------+--------------+--------------+--------------+--------------+-------+-------+

注:以上样例主要展示功能性,延迟具体数字在开发机上得到,不具备参考性。关于如何得到更优的延迟数字,稍后会添加相关文档说明。

3 持仓设置工具

交易系统支持持仓导出为 csv 文件以及 csv 文件设置为持仓。

查看持仓设置工具帮助:

$ wingchun pos -h
Options:
  -h [ --help ]         Help screen
  -t [ --type ] arg     type ("set"/"get")
  -s [ --source ] arg   source index (eg CTP=1)
  -n [ --name ] arg     strategy name
  -o [ --output ]       output to file
  -r [ --raw ]          print raw data
  -d [ --detail ]       print user info details
  -l [ --list ]         list all files
  -j [ --json ] arg     json_file
  -c [ --csv ] arg      csv_file format:
                        ticker, net_tot, net_yd, long_tot, long_yd, short_tot,
                        short_yd                    

导出持仓和使用csv文件设置持仓:

$ wingchun pos -t get -n band_demo -s 1 -o -c band_demo.csv
=========== (name) band_demo (source)14 =========
nano:	1514453987908204897 (20171228-17:39:47)
ok:	Yes
-------
rb1805	(net)0,0	(long)6,6	(short)6,6	(net_c)0,0	(long_c)0,0	(short_c)0,0
-------
printed to csv file: band_demo.csv
-------
$ wingchun pos -t set -n band_demo -s 1 -c band_demo.csv
set pos from csv file: band_demo.csv

运行过程中向策略传参数

运行过程中可以通过 kungfu command 命令向运行的策略实时传参数,策略通过 on_command 回调接受参数,

传入参数:示例

$ kungfu command \'this is a strategy command with space\!\'

接受参数示例

def on_command(context, command_str):
    print command_str