forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSI指标买卖.js
120 lines (103 loc) · 2.96 KB
/
RSI指标买卖.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
/*
策略出处: https://www.botvs.com/strategy/31
策略名称: RSI指标买卖
策略作者: Zero
策略描述:
RSI简单买卖测试, 默认 70-100卖出,0-30买入
参数 默认值 描述
---------- ----- ---------
RSIPeriod 14 RSI周期
SlidePrice 0.3 下单滑动值
RSIBuyL false RSI买入点(低)
RSIBuyH 30 RSI买入点(高)
RSISellL 70 RSI卖出点(低)
RSISellH 100 RSI卖出点(高)
Interval 1000 出错重试(毫秒)
*/
function adjustFloat(v) {
return Math.floor(v*1000)/1000;
}
function cancelPendingOrders() {
while (true) {
var orders = exchange.GetOrders();
if (orders) {
if (orders.length == 0) {
break;
}
for (var j = 0; j < orders.length; j++) {
exchange.CancelOrder(orders[j].Id);
Sleep(Interval);
}
} else {
Sleep(Interval);
}
}
}
var STATE_WAIT_BUY = 0;
var STATE_WAIT_SELL = 1;
var STATE_BUY = 2;
var STATE_SELL = 3;
var STATE_WAIT_SELL_ALL = 4;
var State = STATE_WAIT_BUY;
var InitAccount;
function MustGetAccount() {
var account;
while (!(account = exchange.GetAccount())) {
Sleep(2000);
}
return account;
}
function onTick(exchange) {
var records = exchange.GetRecords();
if (!records || records.length < (RSIPeriod + 5)) {
return;
}
rsi = TA.RSI(records, RSIPeriod);
var rsiValue = rsi[records.length - 1];
if (State == STATE_WAIT_BUY && rsiValue >= RSIBuyL && rsiValue <= RSIBuyH) {
State = STATE_BUY;
} else if (State == STATE_WAIT_SELL && rsiValue >= RSISellL && rsiValue <= RSISellH) {
State = STATE_SELL;
} else if (State != STATE_WAIT_SELL_ALL){
return;
}
// Buy or Sell, Cancel pending orders first
cancelPendingOrders();
var account = MustGetAccount();
var ticker = exchange.GetTicker();
if (!account || !ticker) {
return;
}
if (State == STATE_BUY) {
var price = ticker.Last + SlidePrice;
var amount = adjustFloat(account.Balance / price);
if (amount >= exchange.GetMinStock()) {
if (exchange.Buy(price, amount)) {
State = STATE_WAIT_SELL;
}
}
} else {
if (account.Stocks > exchange.GetMinStock()) {
// STATE_WAIT_SELL or STATE_WAIT_SELL_ALL
State = STATE_WAIT_SELL_ALL;
exchange.Sell(ticker.Last - SlidePrice, account.Stocks);
} else {
// No stocks, wait buy and log profit
LogProfit(account.Balance - InitAccount.Balance);
Log(account);
State = STATE_WAIT_BUY;
}
}
}
function main() {
InitAccount = MustGetAccount();
Log(InitAccount);
if (InitAccount.Stocks > 0) {
throw "必须空仓介入";
return;
}
while (true) {
onTick(exchange);
Sleep(30000);
}
}