forked from estuary/flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.ts
45 lines (43 loc) · 1.7 KB
/
flow.ts
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
import { IDerivation, Document, SourceFromTicks } from 'flow/stock/daily-stats.ts';
import moment from "https://deno.land/x/[email protected]/mod.ts";
// Implementation for derivation stock/daily-stats.
export class Derivation extends IDerivation {
fromTicks(source: { doc: SourceFromTicks }): Document[] {
const tick = source.doc;
// Current bid/ask price spread of the tick.
const spread = tick.ask.price - tick.bid.price;
// Truncate full UTC timestamp to current date.
const date = moment.utc(tick.time).format('YYYY-MM-DD');
return [
{
exchange: tick.exchange,
security: tick.security,
date: date,
// Price stat uses a by-volume weighted average of trades.
price: {
low: tick.last.price,
high: tick.last.price,
avgN: tick.last.price * tick.last.size,
avgD: tick.last.size,
},
// Bid, ask, and spread stats use equal weighting of observed prices across ticks.
bid: {
low: tick.bid.price,
high: tick.bid.price,
avgN: tick.bid.price,
avgD: 1,
},
ask: {
low: tick.ask.price,
high: tick.ask.price,
avgN: tick.ask.price,
avgD: 1,
},
spread: { low: spread, high: spread, avgN: spread, avgD: 1 },
volume: tick.last.size,
first: tick.last,
last: tick.last,
},
];
}
}