6
Follow
968
Followers
交易所网络原因导致的错误会致使比如exchange.GetAccount这样的函数调用失败, 这时候就需要重试了, 但每个函数都重试一次显的代码过于烦锁, 可以这样解决:
这个函数放到开头
function EnsureCall(method) {
var r;
while (!(r = method.apply(this, Array.prototype.slice.call(arguments).slice(1)))) {
Sleep(300);
}
return r;
}```
然后比如获取账户信息可以这样
```var account = EnsureCall(exchange.GetAccount);```
获取深度可以这样
```var depth = EnsureCall(exchange.GetDepth);```
要传入参数可以这样
```var records = EnsureCall(exchange.GetRecords, PERIOD_M5);```
这样以来,不管交易所网络如何不稳定, EnsureCall会重试得到的都是有效的数据, 不用每个函数都为了容错封装一次了.
当然你也可以把EnsureCall改名为EC或者Call之类好记又短的名子, 这样更方便.
Related Recommendations
Comment
All comments (5)
function EnsureCall(method) {
var r;
while (!(r = method.apply(this, Array.prototype.slice.call(arguments).slice(1)))) {
Sleep(300);
}
return r;
}
function main() {
InitAccount = EnsureCall(exchange.GetAccount());
Log(InitAccount);
}
像上面这种调用,就会出现错误。
11 years ago
- 1

