Repository navigation
Expand file tree
/
Copy pathOCandlestickChart.js
More file actions
406 lines (345 loc) · 16.1 KB
/
Copy pathOCandlestickChart.js
File metadata and controls
406 lines (345 loc) · 16.1 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// OCandlestickChart
// Renders midpoint based candlesticks as a candlestick chart.
// OANDA, 2013
/* Constructor: Creates a new chart, sets up default chart and chart display options, creates and binds chart to control.
* Arguments:
* dashElement : DOM object
* chatElement : DOM object
* controlElement : DOM object
* errorElement : DOM object
* candleOpts : Object
* dimensionOpts : Object
*
* Refer to README.md for more detailed argument information.
*/
function OCandlestickChart(dashElement, chartElement, controlElement, errorElement, candleOpts, dimensionOpts) {
//Store reference to parent container for error display:
this.parentContainer = errorElement;
dimensionOpts = dimensionOpts || {};
dimensionOpts.chart = dimensionOpts.chart || { 'height' : '80%', 'width' : '80%'};
dimensionOpts.control = dimensionOpts.control || { 'height': '80%', 'width' : '100%'};
//Set up default chart options (granularity, start time, end time, instrument, etc).
candleOpts = candleOpts || {};
//Default start time set to beginning of current day.
var todayAtMidnight = new Date();
todayAtMidnight.setHours(0,0,0);
this.startTime = candleOpts.startTime || todayAtMidnight;
this.granularity = candleOpts.granularity || 'M30';
this.instrument = candleOpts.instrument || 'EUR_USD';
//Default end time set to current time.
this.endTime = candleOpts.endTime || new Date();
//Streaming related variables. Streaming off by default.
this.streamingEnabled = candleOpts.streamingEnabled || false;
this.callbacks = { 'timeout' : 0, 'interval' : 0};
//Create dashboard which will control candlestick chart and control.
this.dash = new google.visualization.Dashboard(dashElement);
//Immutable chart options:
this.chartOpts = {
'chartArea' : { 'width' : dimensionOpts.chart.width, 'height' : dimensionOpts.chart.height },
'hAxis' : { 'slantedText' : false },
};
this.chart = new google.visualization.ChartWrapper({
'chartType' : 'CandlestickChart',
'containerId' : chartElement,
});
//Immutable control options:
this.controlOpts = {
'filterColumnIndex': 0,
'ui': {
'chartType': 'LineChart',
'chartOptions': {
'chartArea': {'width': dimensionOpts.control.width || '80%', 'height' : dimensionOpts.control.height},
'hAxis': {'baselineColor': 'none'}
},
'chartView': {
'columns': [0, 3]
},
}
};
this.control = new google.visualization.ControlWrapper({
'controlType' : 'ChartRangeFilter',
'containerId' : controlElement,
});
//Allow the control element to manipulate the chart.
this.dash.bind(this.control, this.chart);
//Set up the granularity table as 'private' variable:
var grans = {
'S5' : 5,
'S10' : 10,
'S15' : 15,
'S30' : 30,
'M1' : 60,
'M2' : 120,
'M3' : 180,
'M5' : 300,
'M10' : 600,
'M15' : 900,
'M30' : 1800,
'H1' : 3600,
'H2' : 7200,
'H3' : 10800,
'H4' : 14400,
'H6' : 21600,
'H8' : 28800,
'H12' : 43200,
'D' : 86400,
'W' : 604800,
'M' : -1,
};
//The 'this' qualified methods are accessible to class instances.
//Month granulairy in seconds will be based on the given year/month.
this.granularityMap = function(year, month) {
var daysInMonth = OCandlestickChart.util.getDaysInMonth(year, month);
grans['M'] = daysInMonth * 24 * 60 * 60;
return grans;
};
//Expose list of all possible granulairy values.
this.granularities = Object.keys(grans);
}
/* render: Queries the OANDA API for candlesticks in the set time range with the set granulairty, and updates the chart with the data.
* If streaming is enabled, a polling loop will be started to fetch and update the chart with new candles.
*/
OCandlestickChart.prototype.render = function() {
//Month granularity in seconds will be based off start time, which may cause synchronization issues while streaming
//monthly candlesticks. It is unlikely that you will stream monthly candlesticks.
var granSecs = this.granularityMap(this.startTime.getFullYear(), this.startTime.getMonth())[this.granularity] * 1000;
//Create instance of data table with the proper columns.
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn('number', 'Low');
data.addColumn('number', 'Close');
data.addColumn('number', 'Open');
data.addColumn('number', 'High');
//Preserve self-reference.
var self = this;
/* Queries the API for a fixed amount of candles, calls the onComplete method with the completed data set.
*/
function queryFixed(onComplete) {
//Since candles can only be returned in groups of 5000 and users may request time intervals where more than
//5000 candles can exist, we must break the time interval into 5000 candles chunks.
var intervals = [self.startTime];
var dateIter = self.startTime;
while((self.endTime - dateIter) >= (granSecs * 5000)) {
dateIter = new Date(dateIter.getTime() + granSecs * 5000);
intervals.push(dateIter);
}
//Add 1 second since API only returns candles past and not on the given end time.
intervals.push(new Date(self.endTime.getTime() + 1000));
//Recursive function that gets candles between all necessary intervals.
function getData(i) {
if(i+1 < intervals.length) {
OANDA.rate.history(self.instrument, { 'start' : intervals[i].toISOString(),
'end' : intervals[i+1].toISOString(),
'candleFormat' : 'midpoint', 'granularity' : self.granularity }, function(response) {
if(response.error) {
console.log(response.error);
return;
}
for(var j = 0 ; j < response.candles.length; j++) {
var candle = response.candles[j];
data.addRow([new Date(candle.time), candle.lowMid, candle.closeMid, candle.openMid, candle.highMid]);
}
getData(i+1);
});
} else {
onComplete(data);
return;
}
}
getData(0);
}
/*
* Create and draw a data table with candles starting from the given start time, up to the current time, adding
* new candles as they become available. Automatically re-draws the graph as candles are provided.
*/
function queryLoop(data) {
function now() {
return new Date();
}
//Do one render to push out any candles that are sitting in the table (i.e. the user turns on streaming
//after leaving chart open for a while).
draw(data, false);
//start time will be set to the last candle, end time will be set to the next possible candle.
var interval = { 'start' : new Date(data.getValue(data.getNumberOfRows() - 1, 0))};
interval.end = new Date(interval.start.getTime() + granSecs + 1000);
//Multiplies granularity added to end time to progress over intervals where no candles are returned.
var granMultiplier = 0;
//Polls the API for new candlesticks, setting the start and end times based on last displayed candle in chart.
var poll = function() {
console.log("Polled at: " + now());
OANDA.rate.history(self.instrument, {'start' : interval.start.toISOString(), 'end' : interval.end.toISOString(),
'candleFormat' : 'midpoint', 'granularity' : self.granularity, 'includeFirst' : true},
function(response) {
for(var j = 0 ; j < response.candles.length; j++) {
var candle = response.candles[j];
//If a candle is returned with a timestamp equal to the last displayed candle, attempt to
//update the last displayed candle with new data.
if(data.getValue(data.getNumberOfRows() - 1, 0).getTime() === new Date(candle.time).getTime()) {
data.setValue(data.getNumberOfRows() - 1, 1, candle.lowMid);
data.setValue(data.getNumberOfRows() - 1, 2, candle.closeMid);
data.setValue(data.getNumberOfRows() - 1, 3, candle.openMid);
data.setValue(data.getNumberOfRows() - 1, 4, candle.highMid);
//Draw now to show candle being update with animatione effect.
draw(data, true);
continue;
}
//Make sure no already seen candles are added as new rows.
//if(interval.start < new Date(candle.time).getTime()) {
if(data.getNumberOfRows() > 15) {
data.removeRow(0);
}
data.addRow([new Date(candle.time), candle.lowMid, candle.closeMid, candle.openMid, candle.highMid]);
granMultiplier = 0;
//}
}
//If no row is added, granMultiplier will be non-zero, and this will be incremented to some value > 1.
//If a row is added, granMultiplier will be zero and will be incremented to one (having no effect).
granMultiplier++;
//Set the start time to be the latest candle displayed.
interval.start = data.getValue(data.getNumberOfRows() - 1, 0);
//Set the end time to the next time where a candle should be returned.
//An offset of 1000 seconds is required since the API only returns candles past a given end time.
interval.end = new Date(interval.start.getTime() + granSecs * granMultiplier + 1000);
//Only re-render if rows were added
if(granMultiplier === 1) {
draw(data, false);
}
});
};
//Attempt to round up current time to match granularity.
var nextCandle = Math.ceil(now().getTime() / granSecs) * granSecs;
//Set a delay before polling interval is started to synchronize each poll with the approximate time
//the next candle should be ready.
var startIn = nextCandle - now().getTime() + 1000;
console.log('It is: ' + now() + ' Starting poll in: ' + startIn / 1000 + ' seconds');
//Poll 4 times as frequently as the granularity to allow updating partial candles (since candlesticks may change before the next candle is released).
self.callbacks.timeout = setTimeout(
function() { self.callbacks.interval = setInterval(poll, granSecs / 4); poll();}, startIn);
}
//If streaming is enabled, first get all candles from the start time to now, then start a polling loop.
if(this.streamingEnabled) {
//update endtime to now.
queryFixed( function(data) { queryLoop(data); });
} else {
queryFixed( function(data) { draw(data, false); });
}
/*
* Renders the data to the chart; Animation is optional since it really messes up the
* sliding action of the chart control.
*/
function draw(data, animate) {
//Set up extra chart options.
self.chartOpts.title = self.instrument + " Candlesticks";
if(animate) {
self.chartOpts.animation = { 'duration' : 1000, 'easing' : 'out' };
}
self.chartOpts.legend = { 'position' : 'none' };
//Set up extra control options:
self.controlOpts.ui.minRangeSize = granSecs * 2;
//Reset the state of the control so the sliders stay in bounds.
self.control.setState({ 'start' : data.getColumnRange(0).min, 'end' : data.getColumnRange(0).max});
self.chart.setOptions(self.chartOpts);
self.control.setOptions(self.controlOpts);
self.dash.draw(data);
}
};
/* Clears the chart and control with their internal reset methods and cancels and polling.
*/
OCandlestickChart.prototype.reset = function() {
if(this.control) {
var controlHandle = this.control.getControl();
controlHandle.resetControl();
}
if(this.chart) {
var chartHandle = this.chart.getChart();
chartHandle.clearChart();
}
if(this.callbacks.interval > 0) {
clearInterval(this.callbacks.interval);
}
if(this.callbacks.timeout > 0) {
clearTimeout(this.callbacks.timeout);
}
};
/* Changes the granularity of the candles displayed on the chart. Will reset the chart.
*/
OCandlestickChart.prototype.setGranularity = function(granularity) {
if(this.granularities.indexOf(granularity) < 0) {
this.timedError("Not a valid granularity!");
} else {
this.granularity = granularity;
this.reset();
this.render();
}
return this.granularity;
};
/* Changes the instrument of the candles shown in the chart. Will reset the chart.
*/
OCandlestickChart.prototype.setInstrument = function(instrument) {
this.instrument = instrument;
this.reset();
this.render();
return this.instrument;
};
/* Changes the start time of the displayed candles. Will reset the chart.
*/
OCandlestickChart.prototype.setStartTime = function(params) {
var newStartTime = new Date(params.year || this.startTime.getFullYear(),
params.month || this.startTime.getMonth(),
params.day || this.startTime.getDate(),
params.hours || this.startTime.getHours(),
params.minutes || this.startTime.getMinutes(),
params.seconds || this.startTime.getSeconds());
if(newStartTime >= this.endTime.getTime()) {
this.timedError("Start time set to be greater than or equal to end time");
} else {
this.startTime = newStartTime;
this.reset();
this.render();
}
return this.startTime;
};
/* Changes the end time of the displayed candles. Will reset the chart.
*/
OCandlestickChart.prototype.setEndTime = function(params) {
var newEndTime = new Date(params.year || this.endTime.getFullYear(),
params.month || this.endTime.getMonth(),
params.day || this.endTime.getDate(),
params.hours || this.endTime.getHours(),
params.minutes || this.endTime.getMinutes(),
params.seconds || this.endTime.getSeconds());
if(newEndTime <= this.startTime.getTime()) {
this.timedError("End time set less than or equal to start time.");
} else {
this.endTime = newEndTime;
this.reset();
this.render();
}
return this.endTime;
};
/* Toggles streaming on & off.
*/
OCandlestickChart.prototype.toggleStreaming = function(streamingEnabled) {
this.streamingEnabled = streamingEnabled;
this.endTime = new Date();
this.reset();
this.render();
};
/*
* Displays and error string for the specified amount of time to the DOM object which acts as the error container.
*/
OCandlestickChart.prototype.timedError = function(errorString, timeout) {
var error = google.visualization.errors.addError(this.parentContainer, errorString, "",
{'type' : 'error',
'style' : 'font-size:1em;'});
setTimeout(function() {google.visualization.errors.removeError(error);}, timeout || 5000);
};
/* 'Static' utility functions
*/
OCandlestickChart.util = {
'getDaysInMonth' : function(year, month) {
var start = new Date(year, month, 1);
var end = new Date(year, parseInt(month, 10) + 1, 1);
return (end - start)/(1000 * 60 * 60 * 24);
},
};