function updateBar bars quote options var lastBar bars bars length var

 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
function updateBar(bars, quote, options){
var lastBar = bars[bars.length - 1];
var multiplier = 60000; // minute
if(options.periodicity === "4") multiplier *= 60 * 24; // if hours
var maxTimeDiff = options.interval * multiplier;
//bar updated, we must re calculate bar
if(Math.abs(lastBar.Date - quote.Date.getTime()) < maxTimeDiff){
lastBar.Date = quote.Date.getTime();
lastBar.Close = quote.Price;
lastBar.High = Math.max(lastBar.High, quote.Price);
lastBar.Low = Math.min(lastBar.Low, quote.Price);
lastBar.Volume += quote.Volume;
bars[bars.length - 1] = lastBar;
}
// create new bar
else {
var newBar = {
Date : quote.Date.getTime(),
Open : quote.Price,
High : quote.Price,
Low : quote.Price,
Close : quote.Price,
Volume : quote.Volume
};
bars.push(newBar);
}
return bars;
}