-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_vo.js
59 lines (50 loc) · 1.57 KB
/
stock_vo.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
"use strict";
function PriceSample(time, open, close, volume){
return {
time: time,
open: open,
close: close,
volume: volume
};
}
/**
*
* @param {string} ticker - ticker id used to request price
* @param {string} name - Stock name/company name
* @param {PriceSample[]} prices - Array of price objects for the day. Expected to be
* be a list that starts at 9am and extends until 4pm (or the last quote if not 4pm yet)
* @param {number} prevClose - Price at close of previous day
* @param {number} time - Time, in milliseconds since epoc, that the stock was last updated.
* @param {object} rawData -
*/
function create(ticker, name, prices, prevClose, time, rawData){
return {
_type: "stock_vo",
metadata: {
name: name,
ticker: ticker
},
prices: prices,
prevClose: prevClose,
rawData: rawData,
lastUpdate: time
};
}
function createWithDefaults(ticker, name){
if (!ticker || !name) throw new Error("Invalid parameters");
return create(ticker, name, [], 0, 0, {});
}
function fromJSON(json /*string*/){
let data = JSON.parse(json);
let ret = createWithDefaults(data.metadata.ticker, data.metadata.name);
if (!!data.prices) ret.prices = data.prices;
if (!!data.prevClose) ret.prevClose = data.prevClose;
if (!!data.rawData) ret.rawData = data.rawData;
if (!!data.lastUpdate) ret.lastUpdate = data.lastUpdate;
return ret;
}
module.exports = {
PriceSample: PriceSample,
create: create,
fromJSON: fromJSON
};