-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
194 lines (177 loc) · 5.26 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const {
initFeed,
BaseDataFeed,
BaseRecordSource,
TimeSeriesContainer,
Fields,
formatPriceWithDecimals,
asyncReady,
PubSub,
ChunkPart,
Topics,
SeriesKind,
} = Barchart.RealtimeWidgets.Charts;
const ohlcFields = [
Fields.DateTime,
Fields.Open,
Fields.High,
Fields.Low,
Fields.Close,
Fields.Volume,
Fields.Change,
];
const chunkSize = 180;
let _jsonData = {};
const dailyFileName = "AMZN_Daily.json";
const eventsFileName = "AMZN_Events.json";
async function getData(fileName) {
if (_jsonData[fileName] == null) {
const resp = await fetch(`./${fileName}`);
_jsonData[fileName] = await resp.json();
}
return _jsonData[fileName];
}
class ExampleTimeSeries {
constructor(query) {
this.query = query;
this.container = null;
this.canLoadMoreData = true;
this.isLoading = false;
}
get hasData() {
return this.container !== null;
}
async loadMoreData(headChunk = true) {
if (this.isLoading) return Promise.resolve();
this.isLoading = true;
const data = await getData(dailyFileName);
const loaded = this.container !== null ? this.container.size : 0;
if (data.length === loaded) {
this.canLoadMoreData = false;
return;
}
const container = new TimeSeriesContainer(ohlcFields);
let precedingClose = null;
const to = data.length - loaded;
const from = Math.max(0, to - chunkSize);
for (let i = from; i < to; ++i) {
const row = data[i];
container.addDataPoint(Fields.DateTime, new Date(row.date));
container.addDataPoint(Fields.Open, row.open);
container.addDataPoint(Fields.High, row.high);
container.addDataPoint(Fields.Low, row.low);
container.addDataPoint(Fields.Close, row.close);
container.addDataPoint(Fields.Volume, row.volume);
container.addDataPoint(Fields.Change, precedingClose != null ? row.close - precedingClose : null);
precedingClose = row.close;
}
if (!headChunk) this.container = container;
else this.container.prepend(container);
this.isLoading = false;
if (headChunk) {
// the first chunk is loaded implicitly during the call to ready method below
// so there's no need for notification
PubSub.publish(Topics.TS_MANYCHANGED, {
series: this,
part: ChunkPart.Head,
});
}
}
async ready() {
return asyncReady.call(this, async () => {
await this.loadMoreData(false);
return true;
});
}
}
class ExampleEventsSeries {
constructor(query) {
this.query = query;
this.container = null;
}
get hasData() {
return this.container !== null;
}
async loadAndParse() {
const data = await getData(eventsFileName);
const suffix = "T00:00:00";
this.container = new TimeSeriesContainer([
Fields.DateTime,
Fields.Dividends,
Fields.Earnings,
Fields.Splits,
]);
const addValue = (field, row) => {
const matching = field.id === row.kind ? row.value : null;
this.container.addDataPoint(field, matching);
};
data.forEach(row => {
this.container.addDataPoint(Fields.DateTime, new Date(Date.parse(row.date + suffix)));
this.container.fields.filter(f => f !== Fields.DateTime).forEach(f => addValue(f, row));
});
return Promise.resolve(true);
}
async ready() {
return asyncReady.call(this, this.loadAndParse);
}
}
class ExampleTimeSeriesSource {
getTimeSeries(query) {
const kind = query.seriesKind || SeriesKind.Normal;
const isAmazon = query.symbol === "AMZN";
if (kind === SeriesKind.Normal) return isAmazon ? new ExampleTimeSeries(query) : null;
else if (kind === SeriesKind.Events) return isAmazon ? new ExampleEventsSeries(query) : null;
return null;
}
}
class ExampleMetaData {
constructor(decimals) {
this._decimals = decimals;
}
async ready() {
return true;
}
format(price, _field, _options) {
return formatPriceWithDecimals(price, this._decimals);
}
}
class ExampleMetaDataSource {
getMetaData(symbol) {
return new ExampleMetaData(symbol === "AMZN" ? 2 : 0);
}
}
class ExampleRecord {
async ready() {
return true;
}
hasField(_field) {
return false;
}
getValue(_field) {
return null;
}
}
class ExampleRecordSource extends BaseRecordSource {
getRecord(symbol) {
return symbol === "AMZN" ? new ExampleRecord() : null;
}
}
class ExampleFeed extends BaseDataFeed {
constructor(config) {
super(config);
this.timeSeriesSource = new ExampleTimeSeriesSource();
this.metaDataSource = new ExampleMetaDataSource();
this.recordSource = new ExampleRecordSource();
}
}
const feed = initFeed(ExampleFeed, {
throttleMillis: 250,
apiKey: "<YOUR_API_KEY>",
});
feed.ready().then(done => {
if (done) {
feed.addChart("root", {
symbol: `AMZN`,
});
}
});