// K线周期合成
var cloneObj = function(obj){ // 深拷贝 对象函数
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'){
return;
} else if(JSON){
str = JSON.stringify(obj), //系列化对象
newobj = JSON.parse(str); //还原
} else {
for(var i in obj){
newobj[i] = typeof obj[i] === 'object' ?
cloneObj(obj[i]) : obj[i];
}
}
return newobj;
};
function AssembleRecords(records){
var AssRecords = records.slice(0); // 深拷贝
var AfterAssRecords = [];
// 整点时间 0 ,4 ,8 , 12 ,16, 20, 24(0)
// 判断时间戳
var objTime = new Date();
for(var i = 0 ; i < AssRecords.length; i++){
//Log(objTime.setTime(AssRecords[i].Time)); // ceshi
objTime.setTime(AssRecords[i].Time);
var hour = objTime.getHours();
if(hour === 0 || hour === 4 || hour === 8 || hour === 12 || hour === 16 || hour === 20){
for(j = 0; j < i ; j++){
AssRecords.shift(); // 排除
}
break;
}
}
var BarObj = {
Time : 0,
Open : 0,
High : 0,
Low : 0,
Close : 0,
Volume : 0,
};
for(var n = 0; n < AssRecords.length - 4 ; n+=4){
/*
{
Time :一个时间戳, 精确到毫秒,与Javascript的 new Date().getTime() 得到的结果格式一样
Open :开盘价
High :最高价
Low :最低价
Close :收盘价
Volume :交易量
}
*/
BarObj.Time = AssRecords[n].Time;
BarObj.Open = AssRecords[n].Open;
//Log(records.length, n, AssRecords); // ceshi
BarObj.High = Math.max(AssRecords[n].High, AssRecords[n + 1].High, AssRecords[n + 2].High, AssRecords[n + 3].High);
BarObj.Low = Math.min(AssRecords[n].Low, AssRecords[n + 1].Low, AssRecords[n + 2].Low, AssRecords[n + 3].Low);
BarObj.Close = AssRecords[n + 3].Close;
BarObj.Volume = AssRecords[n + 3].Volume;
AfterAssRecords.push(cloneObj(BarObj));
}
return AfterAssRecords;
}
function main(){
var records = exchange.GetRecords();
while(!records || records.length < 24){
records = exchange.GetRecords();
}
records = AssembleRecords(records);
$.PlotRecords(records, 'BTC');
}
Der Test lief und zeigte:
Der Interessante ist, wie genau die Daten sind.
LouisDanke, Tan!
SchlangeDas war sehr hilfreich.