自定义组合下单

策略说明

合约系数为正数,表示与下单方向相同;合约系数为负数,表示与下单方向相反。

下单时用对手价下单,超时未成交继续追平。

挂单后若不成交且对手价已变动,则立即撤单,重新下单。

先在软件中,在自编组合页面新建自定义组合,设置一组合约及其对应的权重。

在页面选择买卖、方向和手数后,点击开始即会开始下单。

源码

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

<head>
    <meta charset="utf-8">
    <meta http-equiv="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 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-fluid 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="combine_id" placeholder="组合代码" value='rr'>
                        </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>
                            <label class="radio-inline">
                                <input type="radio" name="offset" value="CLOSETODAY"> 平今
                            </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='volume' class="form-control input-sm" value="1">
                        </div>
                    </div>
                </li>
                <li class="list-group-item">
                    <div class="row">
                        <div class="col col-xs-12">
                            <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>
                </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>合约系数为正数,表示与下单方向相同;</p>
                                <p>合约系数为负数,表示与下单方向相反。</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>
                            </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/func/basefuncs.js"></script>
    <script type="text/javascript" src="libs/tqsdk.js"></script>
    <script type="text/javascript" id='TRADE-CODE'>
        const TQ = new TQSDK();

        function* TaskCombine() {
            TQ.SET_STATE('START');
            var weights = TQ.GET_COMBINE(TQ.UI.combine_id);

            var TaskList = [];
            for (var symbol in weights) {
                var volume = Math.abs(Math.round(TQ.UI.volume * weights[symbol])); // 手数(四舍五入至整数)
                if (volume > 0) {
                    var direction = weights[symbol] > 0 ? TQ.UI.direction : (TQ.UI.direction === 'SELL' ? 'BUY' : 'SELL'); // 交易方向
                    var offset = TQ.UI.offset;
                    var price_field = direction === 'SELL' ? 'bid_price1' : 'ask_price1'; // 对手价
                    TaskList.push(TQ.START_TASK(TaskOrder, {
                        symbol, offset, direction, volume, price_field,prefix: 'combine'
                    }));
                }
            }

            var result = yield {
                USER_CLICK_STOP: TQ.ON_CLICK('STOP'),
                ALL_FINISHED: function(){
                    return TaskList.reduce((a, b) => a.stopped && b.stopped);
                },
            };
            if(result.USER_CLICK_STOP){
                for (var task of TaskList)
                    TQ.STOP_TASK(task);
                TQ.CANCEL_ORDER('combine');
            }
            TQ.SET_STATE('STOP');
        }

        function* TaskOrder(order_params) {
            var quote = TQ.GET_QUOTE(order_params.symbol);
            order_params.limit_price = quote[order_params.price_field];
            var order = TQ.INSERT_ORDER(order_params);

            while (true) {
                let result = yield {
                    ORDER_TRADE: function () { return order.volume_orign > order.volume_left; },
                    PRICE_CHANGED: function () {
                        var new_price  = order_params.direction === 'SELL' ? quote.bid_price1 : quote.ask_price1;
                        return new_price !== order_params.limit_price;
                    },
                };
                // 错单
                if (order.status == 'FINISHED' && order.exchange_order_id == '')
                    return 'error';
                if (order.volume_left == 0)
                    break;
                if (result.PRICE_CHANGED && order.status == "ALIVE")
                    TQ.CANCEL_ORDER(order);
                let wait_cancel = yield {
                    ORDER_CANCELED: function () { return order.status == 'FINISHED'; },
                };
                if (order.volume_left > 0) {
                    order_params.limit_price = order_params.direction === 'SELL' ? quote.bid_price1 : quote.ask_price1;
                    order_params.volume = order.volume_left;
                    order = TQ.INSERT_ORDER(order_params);
                }
            }
        }

        $(function () {
            $('#START').on('click', function () {
                TQ.START_TASK(TaskCombine);
            });
        });
    </script>
</body>

</html>