forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 1
/
均线.js
199 lines (172 loc) · 5.63 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
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
/*
策略出处: https://www.botvs.com/strategy/10023
策略名称: 均线
策略作者: topgun
策略描述:
均线买入
参数 默认值 描述
------------ ------ -------
bc1 -1e-06 金叉后前一柱
ac1 0.005 金叉后第一柱
SlidePrice 0.3 滑动价格
TrailingStop 0.5 止损点
orderTimeout 30 买单超时(秒)
*/
Fixed = function(v) {
return Math.floor(v*1000)/1000;
};
// for orders
WaitOrder = function(exchange, orderId, timeoutToCancel) {
var ts = (new Date()).getTime();
while (true) {
Sleep(3000);
var orderInfo = exchange.GetOrder(orderId);
if (!orderInfo) {
continue;
}
if (orderInfo.Status == ORDER_STATE_CLOSED || orderInfo.Status == ORDER_STATE_CANCELED) {
return orderInfo;
}
if (((new Date()).getTime() - ts) > timeoutToCancel) {
exchange.CancelOrder(orderId);
}
}
};
Buy = function(exchange, maxPrice, slidePrice, balanceRatio, timeoutS) {
var ts = (new Date()).getTime();
var account;
var dealAmount = 0.0;
var usedBlance = 0.0;
var maxBalanceUse = 0.0;
var isFirst = true;
do {
if (isFirst) {
isFirst = false;
} else {
Sleep(3000);
}
var ticker = exchange.GetTicker();
if (!ticker) {
continue;
}
var buyPrice = ticker.Sell + slidePrice;
// Price too high, wait...
if (buyPrice > maxPrice) {
continue;
}
// Initialize at first
if (!account) {
account = exchange.GetAccount();
if (!account) {
continue;
}
// Initialize maxBalanceUse
maxBalanceUse = account.Balance * balanceRatio;
}
var buyAmount = Fixed((maxBalanceUse - usedBlance) / buyPrice);
if (buyAmount < exchange.GetMinStock()) {
break;
}
orderId = exchange.Buy(buyPrice, buyAmount);
if (!orderId) {
Log(buyPrice, buyAmount, maxBalanceUse, usedBlance);
continue;
}
var orderInfo = WaitOrder(exchange, orderId, timeoutS);
dealAmount += orderInfo.DealAmount;
usedBlance += orderInfo.Price * orderInfo.DealAmount;
if (orderInfo.Status == ORDER_STATE_CLOSED) {
break;
}
} while (((new Date()).getTime() - ts) < timeoutS);
return {amount: dealAmount, price: (dealAmount > 0 ? usedBlance / dealAmount : 0)};
};
Sell = function(exchange, sellAmount, slidePrice) {
// Account info must set
var account = exchange.GetAccount();
while (!account) {
Sleep(2000);
account = exchange.GetAccount();
}
sellAmount = Math.min(sellAmount, account.Stocks);
var cash = 0.0;
var remain = sellAmount;
while (remain >= exchange.GetMinStock()) {
var ticker = exchange.GetTicker();
if (!ticker) {
Sleep(2000);
continue;
}
var sellPrice = ticker.Buy - slidePrice;
var sellOrderId = exchange.Sell(sellPrice, remain);
if (!sellOrderId) {
Sleep(2000);
continue;
}
var orderInfo = WaitOrder(exchange, sellOrderId, 10000);
remain -= orderInfo.DealAmount;
cash += orderInfo.Price * orderInfo.DealAmount;
}
return {amount: sellAmount, price: (sellAmount > 0 ? cash / sellAmount : 0)};
};
var BuyInfo;
var BanlanceRatio = 1.0;
var Profit = 0.0;
var timeAtBuy = 0;
function onTick(exchange) {
var ticker = exchange.GetTicker();
var records = exchange.GetRecords();
if (!ticker || !records || records.length < 45) {
return;
}
var ticks = [];
for (var i = 0; i < records.length; i++) {
ticks.push(records[i].Close);
}
var macd = TA.MACD(records, 12, 26, 9);
var dif = macd[0];
var dea = macd[1];
var his = macd[2];
var op = 0;
if (!BuyInfo) {
if (dif[ticks.length-1] > 0 && his[ticks.length-1] > ac1 && his[ticks.length-2] < bc1) {
op = 1;
}
} else {
if (records[records.length-2].Time > timeAtBuy && records[records.length-1].Close < records[records.length-1].Open - 0.5
&& records[records.length-2].Close < records[records.length-2].Open - 0.5
&& records[records.length-1].Close < records[records.length-2].Close - 0.5) {
op = 2;
} else if (records[records.length-2].Time > timeAtBuy && BuyInfo.price > records[records.length-1].Close && records[records.length-1].Close < records[records.length-1].Open - 0.5) {
op = 2;
} else if ((BuyInfo.price < ticker.Last || dif[ticks.length-1] < 0) && his[ticks.length-1] <= 0) {
op = 2;
} else if ((BuyInfo.price > ticker.Last) && ((BuyInfo.price - ticker.Last) / BuyInfo.price > TrailingStop)) {
op = 2;
}
}
if (op == 1) {
var info = Buy(exchange, ticker.Sell + (SlidePrice * 3), SlidePrice, BanlanceRatio, orderTimeout * 1000);
if (info.amount > 0) {
BuyInfo = info;
timeAtBuy = records[records.length-1].Time;
}
} else if (op == 2) {
var info = Sell(exchange, BuyInfo.amount, SlidePrice);
if (info.amount > 0) {
Profit += info.amount * (info.price - BuyInfo.price);
LogProfit(Profit);
BuyInfo = null;
}
}
}
function main() {
var account = exchange.GetAccount();
if (account) {
Log(exchange.GetName(), exchange.GetCurrency(), account);
}
while (true) {
onTick(exchange);
Sleep(30000);
}
}