candlejs is used for charting real-time market data such as stock or future prices. It's light weight and high performance with no dependency on other packages. It's optimal for intra-day trading scenarios.
git clone https://github.com/rp8/candlejs.git
cd candlejs
npm install
npm install candlejs --save
var cjs = require('candle');
var ticks = new cjs.TickSeries('AAPL');
ticks.on('changed', () => {
...
};
ticks.add(time, price, volume);
ticks.trimOlldData(1000, 10000);
ticks.clear();
for var tick in ticks.data {
...
}
var dailyBars = new cjs.Bars('AAPL', 24*3600*1000);
dailyBars.add(time, open, high, low, close, volume);
dailyBars.addTick(tick);
dailyBars.addTicks(ticks);
dailyBars.on('changed', () => {
...
});
...
dailyBars.clear();
var s = new cjs.Simulator(0.10, 100.00, 100);
s.onData((err, data) => {
...
});
s.start();
s.stop();
var chart = new cjs.CandleChart({candleWidth: 4});
var bars = new cjs.Bars('AAPL', 24*3600*1000);
chart.addSeries(bars);
chart.outputTo(canvas);
chart.render();
chart.setDisplayRange(openTime, closeTime);
...
chart will update with the streaming data coming from bars.
var lr = new cjs.LineReader();
var bars = new cjs.Bars('AAPL', 24*3600*1000);
bars.sort(function(a, b) {
return a[0] - b[0];
});
var bars0 = [];
lr.on('end', function() {
for (i = 0; i < bars0.length; i++) {
var b = bars0[i];
bars.add(b[0], b[1], b[2], b[3], b[4], b[5]);
}
});
lr.on('line', function (line, next) {
if (line.indexOf('Date') === -1) {
var bar = line.split(',');
bars0.push([
new Date(bar[0]).getTime(),
parseFloat(bar[1]),
parseFloat(bar[2]),
parseFloat(bar[3]),
parseFloat(bar[4]),
parseFloat(bar[5])
]);
}
next();
});
lr.read($('file').files[0]);
<html>
<head>
<script type="text/javascript" src="../dist/candle.min.js"></script>
</head>
<body>
<button id="start">Start</button>
<button id="stop">Stop</button>
<hr>
<canvas id="chart" width="800" height="300"></canvas>
<script type="text/javascript">
function $(name) {
return document.getElementById(name);
};
var cjs = require('candle');
var chart = new cjs.CandleChart({candleWidth: 4});
chart.outputTo($('chart'));
var bars = new cjs.Bars('TSL', 3600*24*1000);
chart.addSeries(bars);
var s = new cjs.Simulator(0.10, 99, 10);
var openTime;
s.onData((err, data) => {
openTime += bars.interval/20;
bars.addTick([openTime, data[1], data[2]]);
chart.render();
});
$('start').addEventListener('click', function() {
console.log('simulated price started...');
bars.clear();
openTime = new Date().getTime();
s.start();
});
$('stop').addEventListener('click', function() {
console.log('simulated price stopped...');
s.stop();
});
</script>
</body>
</html>
candlejs is licensed under the MIT License © 2015-2017 Ronggen Pan