大单拆分策略

策略说明

买入的报单价总是在买 1 价,卖出的报单价总是在卖 1 价。

下单手数为最大单笔手数,最后剩余手数小于最大单笔手数时,下单手数为最后剩余的手数。

挂单后若不成交且挂盘价已变动,则立即撤单。

挂单若成交,则补平挂单手数至最大单笔手数,最后剩余手数小于最大单笔手数时,下单手数为最后剩余的手数。

源码

  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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-eqTQ.UIv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>天勤-交易</title>
    <!-- base TQ.UI && js -->
    <link href="assets/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/highlight/styles/atom-one-light.css">
    <link rel="stylesheet" href="tq.css">
</head>

<body>
    <div class="container-flTQ.UId main-container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">
                    大单拆分入场策略
                    <span class="pull-right STATE">
                        <span class="label label-danger">停止</span>
                    </span>
                </h3>
            </div>
            <ul class="list-group inputs" style="font-size:12px;">
                <li class="list-group-item">
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                合约代码
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <input type="text" class="form-control  input-sm" id="symbol" placeholder="合约代码" value='SHFE.rb1810'>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                方向
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <label class="radio-inline">
                                <input type="radio" name="direction" value="BUY" checked> 买
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="direction" value="SELL"> 卖
                            </label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                开平
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <label class="radio-inline">
                                <input type="radio" name="offset" value="OPEN" checked> 开
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="offset" value="CLOSE"> 平
                            </label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                总下单手数
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <input type="number" id='all_volume' class="form-control  input-sm" value="60">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                最大单笔手数
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <input type="number" id='max_one_volume' class="form-control  input-sm" value="20">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                价格区间
                            </span>
                        </div>
                        <div class="col col-xs-4">
                            <input type="number" id='price_from' class="form-control  input-sm" value="20">
                        </div>
                        <div class="col col-xs-1">
                            <span class="pull-right">
                                至
                            </span>
                        </div>
                        <div class="col col-xs-4">
                            <input type="number" id='price_to' class="form-control  input-sm" value="4000">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col col-xs-3">
                            <span class="pull-right">
                                当前价格
                            </span>
                        </div>
                        <div class="col col-xs-9">
                            <span id='instrument_id'></span>&nbsp;&nbsp;&nbsp;&nbsp;买价:
                            <span id='bid_price1'></span>
                            最新价:
                            <span id='last_price'></span>
                            卖价:
                            <span id='ask_price1'></span>
                        </div>
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="row">
                        <div class="col col-xs-4">
                            <div class="btn-toolbar" role="toolbar">
                                <div class="btn-group btn-group-xs">
                                    <button type="button" class="btn btn-default START" id='START' data-state="START">
                                        <span class="glyphicon glyphicon-play"></span>&nbsp;开始
                                    </button>
                                    <button type="button" class="btn btn-default STOP" id='STOP'>
                                        <span class="glyphicon glyphicon-stop"></span>&nbsp;停止
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="col col-xs-8">
                            已成交
                            <span id='traded_volume'></span> 手,均价
                            <span id='traded_avgprice'></span> 。
                        </div>
                    </div>
                </li>
            </ul>
        </div>


        <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingOne">
                <h4 class="panel-title">
                    <a role="button" data-toggle="collapse" href="#collapse" aria-expanded="true" aria-controls="collapse">
                        说明:本策略将一个较大的下单手数分解为多笔下单, 每笔挂单都按照当前挂单价格报出, 一旦有部分手数成交, 会继续挂出新的委托单, 直至达到预期手数为止.
                        <span id='collapse_arrow' class="glyphicon glyphicon-menu-down pull-right"></span>
                    </a>
                </h4>
            </div>
            <div id="collapse" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
                <ul class="list-group" style="font-size:12px;">
                    <li class="list-group-item">
                        <div class="row">
                            <div class="col col-xs-12">
                                <b>详细说明:</b>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col col-xs-3">
                                <p class="pull-right">
                                    下单
                                </p>
                            </div>
                            <div class="col col-xs-9">
                                <p>买入的报单价总是在买 1 价。</p>
                                <p>卖出的报单价总是在卖 1 价。</p>
                                <p>下单手数为最大单笔手数,最后剩余手数小于最大单笔手数时,下单手数为最后剩余的手数。</p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col col-xs-3">
                                <span class="pull-right">
                                    挂单
                                </span>
                            </div>
                            <div class="col col-xs-9">
                                <p>挂单后若不成交且挂盘价已变动,则立即撤单。</p>
                                <p>挂单若成交,则补平挂单手数至最大单笔手数,最后剩余手数小于最大单笔手数时,下单手数为最后剩余的手数。</p>
                            </div>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="row">
                            <div class="col col-xs-12">
                                <b>源代码:</b>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col col-xs-12" id="code_container">
                                <pre><code class="javascript"></code></pre>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <script type="text/javascript" src="assets/jquery.min.js"></script>
    <script type="text/javascript" src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="assets/highlight/highlight.pack.js"></script>
    <script type="text/javascript" src="assets/noty.js"></script>

    <script type="text/javascript" src="libs/tqsdk.js"></script>
    <script type="text/javascript" id='TRADE-CODE'>
        const TQ = new TQSDK();

        function* TaskQuote() {
            while (true) {
                let quote = TQ.GET_QUOTE(TQ.UI.symbol);
                TQ.UI(quote);
                let result = yield {
                    UPDATED_QUOTE: function () { return TQ.IS_CHANGING(quote) },
                    CHANGED: TQ.ON_CHANGE('symbol')
                };
            }
        }

        function* TaskSplit() {
            TQ.SET_STATE('START');
            let quote = TQ.GET_QUOTE(TQ.UI.symbol);
            let max_price = Math.max(TQ.UI.price_from, TQ.UI.price_to);
            let min_price = Math.min(TQ.UI.price_from, TQ.UI.price_to);
            // 价格设定为跟盘价格
            let price_field = (TQ.UI.direction === 'SELL') ? 'ask_price1' : 'bid_price1';
            let order_params = Object.assign(TQ.UI(), {unit_id: 'split'});
            let offset_dir = (TQ.UI.offset + '_' + TQ.UI.direction).toLowerCase();

            let orders = TQ.GET_ORDER_DICT();
            let trades = TQ.GET_TRADE_DICT(); //
            let total_volume = TQ.UI.all_volume; //总共要成交的手数, 一直不变
            let traded_volume = 0; //确定已成交的手数
            let order_volume = 0; //当前正在挂单中的手数
            let traded_cost = 0;
            while(true){
                TQ.UI({ traded_volume, traded_avgprice: traded_cost/traded_volume}); // 更新显示已成交手数、已成交均价
                if (traded_volume >= total_volume)
                    break;
                //符合下单条件时下单
                if (quote[price_field] <= max_price && quote[price_field] >= min_price) {
                    order_params.volume = Math.min(total_volume - traded_volume, TQ.UI.max_one_volume - order_volume);
                    if (order_params.volume > 0) {
                        order_params.limit_price = quote[price_field]; // 设置价格
                        TQ.INSERT_ORDER(order_params);
                        order_volume+=order_params.volume;
                        console.log("Insert:" + order_volume);
                    }
                }
                let result = yield {
                    PRICE_CHANGED: function () { return quote[price_field] !== order_params.limit_price },
                    ORDER_CHANGED: function () {return TQ.IS_CHANGING(orders)},
                    NEW_TRADE: function () {return TQ.IS_CHANGING(trades)},
                    USER_CLICK_STOP: TQ.ON_CLICK('STOP')
                };
                if (result.NEW_TRADE){
                    for(let k in trades){
                        let trade = trades[k];
                        if (TQ.IS_CHANGING(trade) && trade.order_id.startsWith("split.")){
                            traded_cost += trade.price * trade.volume;
                            traded_volume += trade.volume;
                            order_volume -= trade.volume;
                            console.log("Trade:" + k +", " + trade.volume + ", " + order_volume);
                        }
                    }
                }
                if (result.ORDER_CHANGED){
                    for(let k in orders){
                        let order = orders[k];
                        if (TQ.IS_CHANGING(order)
                            && order.order_id.startsWith("split.")
                            && order.status == "FINISHED"){
                            order_volume -= order.volume_left;
                            console.log("Order:" + k +", " + order.volume_left + ", " + order_volume);
                        }
                    }
                }
                if (result.PRICE_CHANGED || result.USER_CLICK_STOP) {
                    console.log("Cancel:");
                    TQ.CANCEL_ORDER('split');
                    if (result.USER_CLICK_STOP)
                        break;
                }
            }
            // 任务结束
            TQ.SET_STATE('STOP');
        }

        $(function () {
            TQ.START_TASK(TaskQuote);
            // 开始交易按钮
            $('button.START').on('click', function () {
                TQ.START_TASK(TaskSplit);
            });
        });
    </script>
</body>

</html>