-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcg.js
85 lines (78 loc) · 1.94 KB
/
rcg.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
const N = 500;
const log1_01 = Math.log(1.01)
function generateGraph() {
const INITAL_SHARE_PRICE = 100;
const SIGMA = 3;
let arr = Array(N).fill(0)
let currentSharePrice = INITAL_SHARE_PRICE;
for (let i = 0; i < N; i++) {
let randomPercentage = SIGMA * randomNormal()
// convert percentage so that +X% followed by -X% results in a net zero change
let arithmeticChange = Math.exp(log1_01 * randomPercentage);
currentSharePrice *= arithmeticChange;
arr[i] = currentSharePrice
}
return arr;
}
// https://stackoverflow.com/a/36481059/961254
// Standard Normal variate using Box-Muller transform.
function randomNormal() {
var u = 0, v = 0;
while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
while(v === 0) v = Math.random();
return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}
function dataset(label, color, data) {
return {
label: label,
fill: false,
data: data,
backgroundColor: color,
borderColor: color,
borderWidth: 1,
lineTension: 0 // linear interpolation
}
}
function initChart(x_size) {
zeroes = Array(x_size).fill(0)
var config = {
type: 'line',
data: {
labels: zeroes,
datasets: [ dataset('price', 'blue', zeroes) ]
},
options: {
elements: {
point:{
radius: 0
}
}
}
}
var ctx = document.getElementById('chartCanvas');
window.myChart = new Chart(ctx, config);
}
window.onload = function() {
updadeChart();
};
function updadeChart() {
let arr = generateGraph();
// show table
if (window.myChart == null) {
initChart(N);
}
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
let endDate = new Date();
xLabels = Array(N)
for (let i = 0; i < N; i++) {
let days = i - N;
xLabels[i] = endDate.addDays(days).toISOString().substring(0, 10);;
}
window.myChart.config.data.labels = xLabels
window.myChart.config.data.datasets[0].data = arr
window.myChart.update()
}