-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglestock.html
258 lines (210 loc) · 6.46 KB
/
singlestock.html
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<html>
<head>
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="dist/uPlot.min.css">
<script src="dist/uPlot.iife.min.js"></script>
<script type = "application/javascript">
var ts=[];
var cp=[];
var u;
function getSize() {
return {
width: window.innerWidth - 10,
height: window.innerHeight - 80,
}
}
function throttle(cb, limit) {
var wait = false;
return () => {
if (!wait) {
requestAnimationFrame(cb);
wait = true;
setTimeout(() => {
wait = false;
}, limit);
}
}
}
function plotChart(timestamps, priceseries){
if(document.getElementById("chart1")){
document.getElementById("chart1").remove();
}
let data = [
timestamps, // x-values (timestamps)
priceseries, // y-values (series 1)
];
let opts = {
title: "",
id: "chart1",
class: "my-chart",
...getSize(),
//width: 1200,
//height: 500,
series: [
{},
{
// initial toggled state (optional)
show: true,
spanGaps: false,
// in-legend display
label: "Price",
value: (self, rawValue) => "$" + rawValue.toFixed(2) + " Change: "+ (((rawValue.toFixed(2)/priceseries[0])-1)*100).toFixed(2),
// series style
stroke: "red",
width: 1,
fill: "rgba(255, 0, 0, 0.3)",
//dash: [10, 5],
}
],
scales: {
x: {
distr: 2,
},
},
};
let uplot = new uPlot(opts, data, document.body);
return uplot;
}
///////////////////////////////
Array.prototype.vectorAdd = function(other){
var result = []
for(var i = 0; i < this.length; i++) {
result.push( this[i] + other[i])
}
return result;
}
Array.prototype.cleanNull = function(){
//var result = []
for(var i = 0; i < this.length; i++) {
if( !this[i] ) this[i]=this[i-1];
}
return this;
}
var portfolioArray = [];
var timestp =[];
var range=1;
var gran="1m";
function getAllStocks(symbols){
var promises = [];
for( s of symbols){
promises.push(fetch("https://jsonp.afeld.me/?url=https://query1.finance.yahoo.com/v8/finance/chart/"+s[0]+"?interval="+gran+"&range="+range+"d&includePrePost=false"));
}
Promise.all(promises).then(function (responses) {
// Get a JSON object from each of the responses
return Promise.all(responses.map(function (response) {
return response.json();
}));
}).then(function (data) {
// Log the data to the console
// You would do something with both sets of data here
for(i=0; i<data.length; i++){
if(timestp.length==0) timestp=data[i].chart.result[0].timestamp;
cp=data[i].chart.result[0].indicators.quote[0].close;
cp.cleanNull();
if(portfolioArray.length==0) portfolioArray = cp.map(x => x * symbols[i][1]);
else portfolioArray=portfolioArray.vectorAdd(cp.map(x => x * symbols[i][1]));
}
u=plotChart(timestp,portfolioArray);
//console.log(data);
}).catch(function (error) {
// if there's an error, log it
console.log(error);
});
}
/*
function addStock(symbol){
fetch("https://query1.finance.yahoo.com/v8/finance/chart/"+symbol[0]+"?interval="+gran+"&range="+range+"d&includePrePost=false")
.then(response => response.json())
.then(result => {
if(timestp.length==0) timestp=result.chart.result[0].timestamp;
cp=result.chart.result[0].indicators.quote[0].close;
cp.cleanNull();
if(portfolioArray.length==0) portfolioArray = cp.map(x => x * symbol[1]);
else portfolioArray=portfolioArray.vectorAdd(cp.map(x => x * symbol[1]));
plotChart(timestp,portfolioArray);
});
}
function addDelayed(symbols){
if(symbols.length>0){
addStock(symbols.shift());
setTimeout( function(){
addDelayed(symbols);
},Math.floor(Math.random() * 1000));
}
}
*/
function init(){
portfolioArray = [];
timestp =[];
range=parseInt(document.getElementById("period").value);
gran=document.getElementById("gran").value;
//var symbolsa=["TSLA","AAPL","MSFT","AMZN","NVDA","NKLA","FB","AMD","NFLX","T","AVGO","INTC"];
var symbolsa=[
["TSLA",4],
["AAPL",80],
["T",20],
["AMZN",1],
["AVGO",6],
["BAC",50],
["KO",15],
["DAL",40],
["XOM",30],
["INTC",10],
["MCD",10],
["MSFT",40],
["NVDA",4],
["NFLX",2]
];
//addDelayed(symbolsa);
// TRY WITH PROMISE.ALL
symbolsa=JSON.parse(document.getElementById("symb").value);
getAllStocks(symbolsa);
//setTimeout(function(){init();}, 4000+Math.floor(Math.random() * 2000));
}
///////////////////
function loadJSON() {
var now=new Date();
now=parseInt(now.getTime()/1000);
var range=parseInt(document.getElementById("period").value*86400);
var gran=document.getElementById("gran").value;
var symb=document.getElementById("symb").value;
var from=now-range;
var data_file = "https://query1.finance.yahoo.com/v7/finance/chart/TSLA?interval=1m&range=1d";
var data_file = "https://query1.finance.yahoo.com/v8/finance/chart/"+symb+"?period1="+from+"&period2="+now+"&interval="+gran+"&includePrePost=true"
var http_request = new XMLHttpRequest();
try{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}catch (e) {
// Internet Explorer Browsers
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4 ) {
// Javascript function JSON.parse to parse JSON data
var jsonObj = JSON.parse(http_request.responseText);
ts=jsonObj.chart.result[0].timestamp;
cp=jsonObj.chart.result[0].indicators.quote[0].close;
plotChart(ts, cp);
}
}
http_request.open("GET", data_file, true);
http_request.send();
}
window.addEventListener("resize", throttle(() => u.setSize(getSize()), 100));
</script>
</head>
<body onload="init()">
<input type="text" id="period" onblur="init()" value="1" size="3"/><input type="text" id="gran" onblur="init()" value="1m" size="3"/>
<input type="text" id="symb" onblur="init()" size="150" value='[["TSLA",4],["AAPL",80],["T",20],["AMZN",1],["AVGO",6],["BAC",50],["KO",15],["DAL",40],["XOM",30],["INTC",10],["MCD",10],["MSFT",40],["NVDA",4],["NFLX",2]]'/>
</body>