forked from ccoenraets/react-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.js
64 lines (52 loc) · 1.45 KB
/
feed.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
var interval,
onChangeHandler;
var stocks = [
{symbol: "GM", open: 38.87},
{symbol: "GE", open: 25.40},
{symbol: "MCD", open: 97.05},
{symbol: "UAL", open: 69.45},
{symbol: "WMT", open: 83.24},
{symbol: "AAL", open: 55.76},
{symbol: "LLY", open: 76.12},
{symbol: "JPM", open: 61.75},
{symbol: "BAC", open: 15.84},
{symbol: "BA", open: 154.50}
];
stocks.forEach(function(stock) {
stock.last = stock.open;
stock.high = stock.open;
stock.low = stock.open;
});
function simulateChange() {
var index = Math.floor(Math.random() * stocks.length),
stock = stocks[index],
maxChange = stock.open * 0.005,
change = maxChange - Math.random() * maxChange * 2,
last;
change = Math.round(change * 100) / 100;
change = change === 0 ? 0.01 : change;
last = stock.last + change;
if (last > stock.open * 1.15 || last < stock.open * 0.85)
{
change = -change;
last = stock.last + change;
}
stock.change = change;
stock.last = Math.round(last * 100) / 100;
if (stock.last > stock.high) {
stock.high = stock.last;
}
if (stock.last < stock.low) {
stock.low = stock.last;
}
onChangeHandler(stock.symbol, 'stock', stock);
}
function start(onChange) {
onChangeHandler = onChange;
interval = setInterval(simulateChange, 200);
}
function stop() {
clearInterval(interval);
}
exports.start = start;
exports.stop = stop;