forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOKCoin期货条件触发委托带止损.js
180 lines (162 loc) · 5.35 KB
/
OKCoin期货条件触发委托带止损.js
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
/*
策略出处: https://www.fmz.com/strategy/2169
策略名称: OKCoin期货条件触发委托带止损
策略作者: Zero
策略描述:
OKCoin期货条件触发下单, 成交后可条件触发自动止损
参数 默认值 描述
--------------- ----- -----------------
CType 0 货币类型: 人民币|美元
ContractType 0 合约类型: 当周|下周|当月|季度
OpType 0 操作类型: 开多|开空
MLevel 0 杠杆大小: 10倍|20倍
TriggerType 0 触发方式: 涨超|跌落
TriggerPrice 2000 开仓触发价格(元)
OrderPrice 2005 开仓委托价(元)
OrderAmount true 开仓委托量
StopLossTrigger 1800 止损触发价(元)
StopOrderPrice 1790 止损委托价(元)
LoopInterval 300 轮询间隔(毫秒)
*/
var _ContractType = ["this_week", "next_week", "month", "quarter"][ContractType];
var _TradeType = ["buy", "sell"][OpType];
var _MarginLevel = [10, 20][MLevel];
var Interval = 300;
function GetTicker() {
var ticker;
while (!(ticker = exchange.GetTicker())) {
Sleep(Interval);
}
return ticker;
}
function GetOrders() {
var orders;
while (!(orders = exchange.GetOrders())) {
Sleep(Interval);
}
return orders;
}
function GetAccount(maxRetry) {
var account;
var counter = 0;
while (!(account = exchange.GetAccount())) {
Sleep(Interval);
counter++;
if (typeof(maxRetry) == 'number' && counter > maxRetry) {
break;
}
}
return account;
}
function now() {
var t = new Date();
var year = t.getFullYear();
var month = t.getMonth() + 1;
var day = t.getDate();
var hour = t.getHours();
var minute = t.getMinutes();
var second = t.getSeconds();
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
if (hour < 10) {
hour = '0' + hour;
}
if (minute < 10) {
minute = '0' + minute;
}
if (second < 10) {
second = '0' + second;
}
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
function main() {
if (exchange.GetName() != 'Futures_OKCoin') {
throw "该策略为OKCoin期货专用策略";
}
if (CType == 1) {
exchange.SetRate(1);
}
Log(CType == 0 ? "货币为RMB" : "货币为美元...");
var account = GetAccount(10);
if (!account) {
throw "获取账户信息失败, 请检测API配置是否正确, 只支持逐仓模式";
}
SetErrorFilter("502:|503:|network|timeout|WSARecv|Connect|GetAddr|no such|reset");
Log(account);
Log('当前机器人ID: ', _G(), '开始运行...');
exchange.SetContractType(_ContractType);
exchange.SetMarginLevel(_MarginLevel);
LoopInterval = Math.max(LoopInterval, 100);
var ticker = GetTicker();
var preLast = 0;
Log('当前价格:', ticker.Last, '等待价格', TriggerType == 0 ? '涨超' : '跌破', TriggerPrice, '元...');
while (true) {
if (TriggerType == 1 && ticker.Last < TriggerPrice) {
Log('价格跌破 ', TriggerPrice, '元, 开始按计划委托');
break;
} else if (TriggerType == 0 && ticker.Last > TriggerPrice) {
Log('价格涨超 ', TriggerPrice, '元, 开始按计划委托');
break;
}
ticker = GetTicker();
if (ticker.Last != preLast) {
preLast = ticker.Last;
LogStatus("最新成交价: ", ticker.Last, now());
}
Sleep(LoopInterval);
}
Log("最后成交价: ", ticker.Last);
exchange.SetDirection(_TradeType);
var pfn = _TradeType == "buy" ? exchange.Buy : exchange.Sell;
var orderId = pfn(OrderPrice, OrderAmount);
if (!orderId) {
Log("第一次下单失败, 重试最后一次!");
orderId = pfn(OrderPrice, OrderAmount);
}
if (!orderId) {
throw "下单失败";
}
Log("开始等待订单完成, 订单号:", orderId);
while (true) {
var order = exchange.GetOrder(orderId);
if (order && order.Status == ORDER_STATE_CLOSED) {
break;
}
Sleep(LoopInterval);
}
Log("订单完成, 开始等待止损...");
var ticker = GetTicker();
Log('当前价格:', ticker.Last, '止损条件:', _TradeType == "buy" ? '跌破' : '涨超', StopLossTrigger, '元');
while (true) {
if (_TradeType == "buy" && ticker.Last <= StopLossTrigger) {
Log('价格跌破 ', StopLossTrigger, '元, 开始止损.');
break;
} else if (_TradeType == "sell" && ticker.Last >= StopLossTrigger) {
Log('价格涨超 ', StopLossTrigger, '元, 开始止损.');
break;
}
ticker = GetTicker();
if (ticker.Last != preLast) {
preLast = ticker.Last;
LogStatus("最新成交价: ", ticker.Last, now());
}
Sleep(LoopInterval);
}
for (var i = 0; i < 2; i++) {
if (_TradeType == "buy") {
exchange.SetDirection("closebuy");
orderId = exchange.Sell(StopOrderPrice, OrderAmount);
} else {
exchange.SetDirection("closesell");
orderId = exchange.Buy(StopOrderPrice, OrderAmount);
}
if (orderId) {
break;
}
}
Log("完成所有操作, 策略退出.");
}