This repository has been archived by the owner on Aug 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
MarkitTimeseriesServiceSample.js
175 lines (157 loc) · 4.68 KB
/
MarkitTimeseriesServiceSample.js
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
/**
* Version 2.0
*/
var Markit = {};
/**
* Define the InteractiveChartApi.
* First argument is symbol (string) for the quote. Examples: AAPL, MSFT, JNJ, GOOG.
* Second argument is duration (int) for how many days of history to retrieve.
*/
Markit.InteractiveChartApi = function(symbol,duration){
this.symbol = symbol.toUpperCase();
this.duration = duration;
this.PlotChart();
};
Markit.InteractiveChartApi.prototype.PlotChart = function(){
var params = {
parameters: JSON.stringify( this.getInputParams() )
}
//Make JSON request for timeseries data
$.ajax({
beforeSend:function(){
$("#chartDemoContainer").text("Loading chart...");
},
data: params,
url: "http://dev.markitondemand.com/Api/v2/InteractiveChart/jsonp",
dataType: "jsonp",
context: this,
success: function(json){
//Catch errors
if (!json || json.Message){
console.error("Error: ", json.Message);
return;
}
this.render(json);
},
error: function(response,txtStatus){
console.log(response,txtStatus)
}
});
};
Markit.InteractiveChartApi.prototype.getInputParams = function(){
return {
Normalized: false,
NumberOfDays: this.duration,
DataPeriod: "Day",
Elements: [
{
Symbol: this.symbol,
Type: "price",
Params: ["ohlc"] //ohlc, c = close only
},
{
Symbol: this.symbol,
Type: "volume"
}
]
//,LabelPeriod: 'Week',
//LabelInterval: 1
}
};
Markit.InteractiveChartApi.prototype._fixDate = function(dateIn) {
var dat = new Date(dateIn);
return Date.UTC(dat.getFullYear(), dat.getMonth(), dat.getDate());
};
Markit.InteractiveChartApi.prototype._getOHLC = function(json) {
var dates = json.Dates || [];
var elements = json.Elements || [];
var chartSeries = [];
if (elements[0]){
for (var i = 0, datLen = dates.length; i < datLen; i++) {
var dat = this._fixDate( dates[i] );
var pointData = [
dat,
elements[0].DataSeries['open'].values[i],
elements[0].DataSeries['high'].values[i],
elements[0].DataSeries['low'].values[i],
elements[0].DataSeries['close'].values[i]
];
chartSeries.push( pointData );
};
}
return chartSeries;
};
Markit.InteractiveChartApi.prototype._getVolume = function(json) {
var dates = json.Dates || [];
var elements = json.Elements || [];
var chartSeries = [];
if (elements[1]){
for (var i = 0, datLen = dates.length; i < datLen; i++) {
var dat = this._fixDate( dates[i] );
var pointData = [
dat,
elements[1].DataSeries['volume'].values[i]
];
chartSeries.push( pointData );
};
}
return chartSeries;
};
Markit.InteractiveChartApi.prototype.render = function(data) {
//console.log(data)
// split the data set into ohlc and volume
var ohlc = this._getOHLC(data),
volume = this._getVolume(data);
// set the allowed units for data grouping
var groupingUnits = [[
'week', // unit name
[1] // allowed multiples
], [
'month',
[1, 2, 3, 4, 6]
]];
// create the chart
$('#chartDemoContainer').highcharts('StockChart', {
rangeSelector: {
selected: 1
//enabled: false
},
title: {
text: this.symbol + ' Historical Price'
},
yAxis: [{
title: {
text: 'OHLC'
},
height: 200,
lineWidth: 2
}, {
title: {
text: 'Volume'
},
top: 300,
height: 100,
offset: 0,
lineWidth: 2
}],
series: [{
type: 'candlestick',
name: this.symbol,
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
units: groupingUnits
}
}],
credits: {
enabled:false
}
});
};