Strategies
从第三方网站获取K线-0804更新
数·狂 · javascript · Patterns and Candlesticks · 2016-08-04 22:35:27
No English write-up in the source (no author description).
Source code (49 lines)
$.AltRecords = function(exchange, timeframe, size, includeLastBar) {
var symbol;
var info;
var record = [];
if (!size) size="";
// 目前只支持以下三个交易所,其余交易所接口可参考https://www.btc123.com/api
if (exchange.GetName().indexOf('Futures_BitVC') != -1) {
symbol = "bitvcbtccnyfuture";
}
else if (exchange.GetName().indexOf('BTCC') != -1 && exchange.GetCurrency().indexOf('BTC') != -1) {
symbol = "btcchinabtccny";
}
else if (exchange.GetName().indexOf('CHBTC') != -1 && exchange.GetCurrency().indexOf('ETH') != -1) {
symbol = "chbtcethcny";
}
else if (exchange.GetName().indexOf('CHBTC') != -1 && exchange.GetCurrency().indexOf('ETC') != -1) {
symbol = "chbtcetccny";
}
if (symbol) {
try {
info = JSON.parse(HttpQuery('https://www.btc123.com/market/kline?symbol='+symbol+'&type='+timeframe+'&size='+(includeLastBar ? size : size+1)));
if (info && info.isSuc) {
info = JSON.parse(info.datas.data);
}
else {
Log("获取K线时发生错误:", info && info.des ? info.des : "网络错误");
return null;
}
} catch (e) {
Log("获取K线时发生错误:", info && info.des ? info.des : "网络错误");
return null;
}
for (var i = 0; i < (includeLastBar ? info.length : info.length-1); i++) {
record.push({"Time": info[i][0], "Open": info[i][1], "High": info[i][2], "Low": info[i][3], "Close": info[i][4], "Volume": info[i][5]});
}
return record;
}
return exchange.GetRecords(); // 不支持的交易所采用默认方式处理(忽略所有参数,如时间周期、长度等)。
};
function main() {
Log(exchange.GetName());
var rec = $.AltRecords(exchange, "5min", 100); // 获取5分钟K线, 100条, 不含最后一条Bar
if (rec) Log(rec.length, rec[rec.length-1]);
rec = $.AltRecords(exchange, "4hour", 100, 1); // 获取4小时K线, 100条, 含最后一条Bar
if (rec) Log(rec.length, rec[rec.length-1]);
}