forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 1
/
多平台强制滑动平仓(买一价).js
78 lines (67 loc) · 1.82 KB
/
多平台强制滑动平仓(买一价).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
/*
策略出处: https://www.botvs.com/strategy/99
策略名称: 多平台强制滑动平仓(买一价)
策略作者: Zero
策略描述:
支持多平台一块平仓, 有多少币卖多少币
参数 默认值 描述
------------- ----- -------
RetryInterval 5 尝试间隔(秒)
SlidePrice 0.3 滑动值
KeepStock false 保留币数
*/
function GetAccount() {
var account;
while (!(account = exchange.GetAccount())) {
Sleep(2000);
}
return account;
}
function GetTicker() {
var ticker;
while (!(ticker = exchange.GetTicker())) {
Sleep(2000);
}
return ticker;
}
function cancelAllOrders() {
var orders = null;
while (!(orders = exchange.GetOrders())) {
Sleep(Interval);
}
if (orders.length > 0) {
for (var j = 0; j < orders.length; j++) {
exchange.CancelOrder(orders[j].Id);
if (j < (orders.length-1)) {
Sleep(2000);
}
}
}
}
function sellAll() {
cancelAllOrders();
var initAccount = GetAccount();
Log(exchange.GetName(), exchange.GetCurrency(), initAccount);
if (initAccount.Stocks == 0) {
Log("空仓");
return;
}
var remaind = initAccount.Stocks - KeepStock;
var account = initAccount;
while (remaind >= exchange.GetMinStock()) {
var ticker = GetTicker();
exchange.Sell(ticker.Buy - SlidePrice, remaind);
Sleep(RetryInterval * 1000);
cancelAllOrders();
account = GetAccount();
remaind = account.Stocks - KeepStock;
}
Log("平均卖出价", (account.Balance - initAccount.Balance) / (initAccount.Stocks - account.Stocks));
Log(account);
}
function main() {
for (var i = 0; i < exchanges.length; i++) {
exchange = exchanges[i];
sellAll();
}
}