-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocklist.js
1020 lines (812 loc) · 30.1 KB
/
stocklist.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var portfolio=[];
getUserPortfolio();
function touchZoomPlugin(opts) {
function init(u, opts, data) {
let plot = u.root.querySelector(".u-over");
let rect, oxRange, oyRange, xVal, yVal;
let fr = {x: 0, y: 0, dx: 0, dy: 0};
let to = {x: 0, y: 0, dx: 0, dy: 0};
let fpos = {f1x:0, f1y:0, f2x:0, f2y:0};
function storePos(t, e) {
let ts = e.touches;
let t0 = ts[0];
let t0x = t0.clientX - rect.left;
let t0y = t0.clientY - rect.top;
fpos.f1x=t0x;
fpos.f1y=t0y;
if (ts.length == 1) {
t.x = t0x;
//t.y = t0y;
t.d = t.dx = t.dy = 1;
u.setCursor({"left": fpos.f1x, "top": -10});
}
else {
let t1 = e.touches[1];
let t1x = t1.clientX - rect.left;
let t1y = t1.clientY - rect.top;
fpos.f2x=t1x;
fpos.f2y=t1y;
u.setSelect({"left": Math.min(fpos.f1x,fpos.f2x), "top": 0,"width": Math.max(fpos.f1x,fpos.f2x)-Math.min(fpos.f1x,fpos.f2x),"height":300});
u.root.children[1].children[1].children[1].innerHTML= (100*(u.data[1][u.posToIdx(Math.max(t0x,t1x))]-u.data[1][u.posToIdx(Math.min(t0x,t1x))])/(u.data[1][u.posToIdx(Math.min(t0x,t1x))])).toFixed(2)+"%";
u.root.children[1].children[0].children[1].innerHTML="Change:";
let xMin = Math.min(t0x, t1x);
let yMin = Math.min(t0y, t1y);
let xMax = Math.max(t0x, t1x);
let yMax = Math.max(t0y, t1y);
// midpts
t.y = (yMin+yMax)/2;
t.x = (xMin+xMax)/2;
t.dx = xMax - xMin;
t.dy = yMax - yMin;
// dist
t.d = Math.sqrt(t.dx * t.dx + t.dy * t.dy);
}
}
let rafPending = false;
function zoom() {
rafPending = false;
let left = to.dx;
let top = to.dy;
//
// non-uniform scaling
let xFactor = fr.dx / to.dx;
//let yFactor = fr.dy / to.dy;
let yFactor =1;
// uniform x/y scaling
//let xFactor = fr.d / to.d;
//let yFactor = fr.d / to.d;
let leftPct = left/rect.width;
let btmPct = 1 - top/rect.height;
let nxRange = oxRange * xFactor;
let nxMin = xVal - leftPct * nxRange;
let nxMax = nxMin + nxRange;
let nyRange = oyRange * yFactor;
let nyMin = yVal - btmPct * nyRange;
let nyMax = nyMin + nyRange;
u.batch(() => {
/*u.setScale("x", {
min: nxMin,
max: nxMax,
});*/
/*u.setScale("y", {
min: nyMin,
max: nyMax,
});*/
});
}
function touchmove(e) {
storePos(to, e);
if (!rafPending) {
rafPending = true;
requestAnimationFrame(zoom);
}
}
plot.addEventListener("touchstart", function(e) {
rect = plot.getBoundingClientRect();
storePos(fr, e);
oxRange = u.scales.x.max - u.scales.x.min;
oyRange = u.scales.y.max - u.scales.y.min;
let left = fr.x;
let top = fr.y;
xVal = u.posToVal(left, "x");
yVal = u.posToVal(top, "y");
document.addEventListener("touchmove", touchmove, {passive: true});
});
plot.addEventListener("touchend", function(e) {
document.removeEventListener("touchmove", touchmove, {passive: true});
});
}
return {
hooks: {
init
}
};
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//Date utils for yahoo and for charts
function getRemoteDateTime(offset/*ms*/,date=new Date()){
let NYD =new Date(date.getTime()+parseInt(offset));
return {
"year": NYD.getUTCFullYear(),
"month":NYD.getUTCMonth(),
"date": NYD.getUTCDate(),
"hour":NYD.getUTCHours(),
"minute":NYD.getUTCMinutes(),
"dayofweek":NYD.getUTCDay(),
};
}
function getUTCfromRemote(offset,year,month,date,hour=0,minute=0){
return new Date(Date.UTC(year,month,date,hour,minute)-parseInt(offset));
}
////Array utils
Array.prototype.cleanNull = function(){
//var result = []
for(var i = 0; i < this.length; i++) {
if( !this[i] ) this[i]=this[i-1];
}
return this;
}
Array.prototype.vectorAdd = function(other){
var result = []
for(var i = 0; i < this.length; i++) {
result.push( this[i] + other[i])
}
return result;
}
var charts= new Map();;
/*function addChart(symbol,node,interval="2m", range="2d"){
fetch("https://jsonp.afeld.me/?url="+ encodeURIComponent("https://query1.finance.yahoo.com/v8/finance/chart/"+symbol+"?interval=5m&range="+range+"&includePrePost=true"))
.then(response => response.json())
.then(result => {
timestp=result.chart.result[0].timestamp;
closeprices=result.chart.result[0].indicators.quote[0].close;
closeprices.cleanNull();
if (!charts.has(symbol)) {
charts.set(symbol,plotChart(timestp,closeprices,node,symbol+"ch"));
}else{
charts.get(symbol).setData([timestp,closeprices]);
charts.get(symbol).redraw();
}
});
}*/
//var preTimestamps = [];
function isInMarket(tsp,tradeperiods){
for (tp of tradeperiods){
if(tsp>= tp[0].start && tsp<=tp[0].end){
return true;
}
}
return false;
}
///////MAIN CHART FUNCTION
function addChartRange(symbol,node,offset,regMT,range=1){
let mt=getRemoteDateTime(offset,new Date(regMT*1000));
prestart=parseInt(getUTCfromRemote(offset,mt.year,mt.month,mt.date,4,0)/1000)-(range-1)*24*60*60;
now=parseInt(new Date()/1000);
//Valid intervals: [1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo]
interval="1m";
if(range>=5) interval="5m";
if(range>=30) interval="30m";
if(range>=60) interval="1h";
if(range>=190) interval="1d";
prepost=document.getElementById("prepost").checked;
// let proxy="https://jsonp.afeld.me/?url=";
//let churl="https://query1.finance.yahoo.com/v8/finance/chart/"+symbol+"?interval="+interval+"&period1="+prestart+"&period2="+now+"&includePrePost="+prepost;
let churl="https://y.krz.workers.dev/y/ch/"+symbol+"?interval="+interval+"&period1="+prestart+"&period2="+now+"&includePrePost="+prepost;
//fetch("https://jsonp.afeld.me/?url="+ encodeURIComponent("https://query1.finance.yahoo.com/v8/finance/chart/"+symbol+"?interval=5m&range="+range+"&includePrePost=true"))
//fetch(proxies[0]+encodeURIComponent(churl))
fetch(/*proxies[0]+*/churl)
.then(response => response.json())
.then(result => {
timestp=result.chart.result[0].timestamp;
closeprices=result.chart.result[0].indicators.quote[0].close;
closeprices.cleanNull();
//testing new series
premarketts=[];
postmarketts=[];
if(interval!="1d" && range<180 && prepost){
pretp=result.chart.result[0].meta.tradingPeriods.pre;
premarketts=timestp.map((v,i) => isInMarket(v,pretp)? closeprices[i]:null);
posttp=result.chart.result[0].meta.tradingPeriods.post;
postmarketts=timestp.map((v,i) => isInMarket(v,posttp)? closeprices[i]:null);
}
if (!charts.has(symbol)) {
charts.set(symbol,plotChart(timestp,closeprices,node,symbol+"ch",premarketts, postmarketts, getCost(symbol,timestp)));//added the series
}else{
charts.get(symbol).origLength=timestp.length;
//charts.get(symbol).chosenrange=range;
charts.get(symbol).setData([timestp,closeprices,premarketts, postmarketts,getCost(symbol,timestp)]);
}
charts.get(symbol).chosenrange=range;
});
}
function getCost(symbol, timestamps){
lsres=getHolding(symbol);
if(lsres && document.getElementById("costline").checked){
if(lsres.p>0){
return timestamps.map(x=>lsres.p);
}
}
return [];
}
function toggle(e){
ele=this;
//console.log(e.target);
/*
speechSynthesis.cancel();
var tex = "Hello there!";
var u=new SpeechSynthesisUtterance(); u.lang='en-US';u.rate=1; u.text=tex; u.onstart = function(event) { resumeInfinity(); }; u.onend = function(event) { clearTimeout(timeoutResumeInfinity); };window.speechSynthesis.speak(u);*/
if(ele.nextSibling.style.display=="" ){
ele.nextSibling.style.display="block";
//addChart(ele.id,ele.nextSibling);
addChartRange(ele.id,ele.nextSibling,ele.dataset.gmtoffset,ele.dataset.regMarketTime);
}else {
ele.nextSibling.style.display="";
//ele.nextSibling.innerHTML="";
}
console.log(getHolding(ele.id));
document.getElementById("hsymb").value=ele.id;
document.getElementById("hprice").value=getHolding(ele.id).p;
document.getElementById("hnumber").value=getHolding(ele.id).n;
}
function floatToLength(num,maxLength,maxPrecision){
if(num){
return num.toFixed(Math.max(0,Math.min(maxPrecision,maxLength-parseInt(Math.abs(num)).toString().length)))
}
return " N/A ";
}
function colorDiv(div, val, maxAt){
if (val>0){
intensity=Math.min(parseInt(val*(200/maxAt)),200);
div.style.backgroundColor="rgb(0,"+intensity+",0)";
}else {
intensity=Math.min(parseInt(Math.abs(val)*(255/maxAt)),255);
div.style.backgroundColor="rgb("+intensity+",0,0)";
//d.nextSibling.style.backgroundImage= "linear-gradient(rgb("+red+",0,0),black,black, black)";
}
}
function addStock(quote){
// CREATING DIVS IF NOT EXISTING
d=document.getElementById(quote.symbol);
if(!d){ //
d=document.createElement("div");
d.id=quote.symbol;
d.className="stock";
document.getElementById("container").appendChild(d);
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
d.appendChild(document.createElement("div")).className="data";
ch=document.createElement("div");
ch.className="chart";
d.after(ch);
chartcontrols=document.createElement("div");
chartcontrols.className="chartcontrols";
chartcontrols.innerHTML="<div>1d</div><div>2d</div><div>1w</div><div>2w</div><div>1m</div><div>3m</div><div>6m</div><div>1y</div><div>5y</div>";
ch.appendChild(chartcontrols);
chartcontrolsover=document.createElement("div");
chartcontrolsover.className="chartcontrolsover";
chartcontrolsover.dataset.symbol=quote.symbol;
chartcontrolsover.innerHTML="<div data-range='1'></div><div data-range='2'></div><div data-range='7'></div><div data-range='14'></div><div data-range='30'></div><div data-range='91'></div><div data-range='183'></div><div data-range='365'></div><div data-range='1826'></div>";
ch.appendChild(chartcontrolsover);
priceinchart=document.createElement("div");
priceinchart.className="priceinchart";
priceinchart.innerHTML="----.--";
ch.appendChild(priceinchart);
d.dataset.gmtoffset=quote.gmtOffSetMilliseconds;
d.dataset.regMarketTime=quote.regularMarketTime;
d.onclick=toggle;
let touchstartms=0;
/*d.ontouchstart=(e)=>{
touchstartms=new Date();
setTimeout(()=>{if(touchstartms>0) alert("long press");touchstartms=0;},1000)
};
d.ontouchend=(e)=>{
touchstartms=0;
//if((new Date()-touchstartms)>500 ) {alert("long press"); touchstartms=0;}
};
*/
// Chart period events
chartcontrolsover.onclick=(e)=>{
let range=e.target.dataset.range;
addChartRange(quote.symbol,ch,quote.gmtOffSetMilliseconds,quote.regularMarketTime,range);
console.log(e);
}
}
// IF DIVS EXIST, FILL WITH DATA
if(d){
//DETERMINING WHICH PRICE TO USE
if(quote.marketState=="PRE" && quote.preMarketPrice){
price= quote.preMarketPrice;
change=quote.preMarketChangePercent;
markettime=quote.preMarketTime;
chartseries=2;
}else if(["PREPRE","POST","POSTPOST","CLOSED"].includes(quote.marketState) && quote.postMarketPrice){
price= quote.postMarketPrice;
//change=quote.postMarketChangePercent;
change=100*(quote.postMarketChange+quote.regularMarketChange)/quote.regularMarketPreviousClose;
markettime=quote.postMarketTime;
chartseries=3;
}
else{
price= quote.regularMarketPrice;
change=quote.regularMarketChangePercent;
markettime=quote.regularMarketTime;
chartseries=1;
}
d.nextSibling.children[2].style.color=change<0 ? "rgb(234,0, 0,0.48)" : "rgb(0,234, 0,0.48)";
d.nextSibling.children[2].innerHTML=price.toFixed(2);
// FILLING IN THE DATA
d.children[0].innerHTML="<div>"+quote.symbol +"</div>" ;
d.children[1].innerHTML="<div class='mk"+chartseries+"'>"+price.toFixed(2)+"</div><div class='mk"+chartseries+"'>"+change.toFixed(2)+"%"+"</div>";
d.children[2].innerHTML="<div>"+quote.fiftyDayAverage.toFixed(2)+"</div><div>"+(100*quote.fiftyDayAverageChangePercent).toFixed(2)+"%"+"</div>";
d.children[3].innerHTML="<div>"+quote.twoHundredDayAverage.toFixed(2)+"</div><div>"+(100*quote.twoHundredDayAverageChangePercent).toFixed(2)+"%"+"</div>";
d.children[4].innerHTML="<div>"+floatToLength(quote.fiftyTwoWeekHigh,4,2)+"</div><div>"+floatToLength(100*quote.fiftyTwoWeekHighChangePercent,3,1)+"%"+"</div>";
d.children[5].innerHTML="<div>"+floatToLength(quote.fiftyTwoWeekLow,4,2)+"</div><div>"+floatToLength(100*quote.fiftyTwoWeekLowChangePercent,3,1)+"%"+"</div>";
d.children[6].innerHTML="<div>"+floatToLength(quote.trailingPE,4,2)+"</div><div>"+floatToLength(quote.forwardPE,4,2)+"</div>";
d.children[7].innerHTML="<div>"+floatToLength(quote.priceToBook,3,2)+"</div>";
//Coloring
colorDiv(d.children[0],change,10);
colorDiv(d.children[1],change,10);
colorDiv(d.children[2],100*quote.fiftyDayAverageChangePercent,20);
colorDiv(d.children[3],100*quote.twoHundredDayAverageChangePercent,100);
colorDiv(d.children[4],100*quote.fiftyTwoWeekHighChangePercent,100);
colorDiv(d.children[5],100*quote.fiftyTwoWeekLowChangePercent,200);
//colorDiv(d.children[6],100*quote.fiftyTwoWeekHighChangePercent,100);
//colorDiv(d.children[7],100*quote.fiftyTwoWeekHighChangePercent,100);
//updating chart
if (charts.has(quote.symbol) && d.nextSibling.style.display=="block" && !charts.get(quote.symbol).data[0].includes(markettime) && document.getElementById("liveup").checked){
charts.get(quote.symbol).data[0].push(markettime);
charts.get(quote.symbol).data[1].push(price);
chartseries>1 ? charts.get(quote.symbol).data[chartseries].push(price) : null;
charts.get(quote.symbol).setData(charts.get(quote.symbol).data);
}
}
}
const proxies=["https://secret-tundra-11774.herokuapp.com/","http://www.whateverorigin.org/get?url=","https://jsonp.afeld.me/?url="];
var winactive=true;
var marketactive=true;
function loadQuotes(){
if (winactive){
// ((window.location.hostname!="")?(fetch('https://jsonp.afeld.me/?url='+encodeURIComponent('https://query1.finance.yahoo.com/v7/finance/quote'+window.location.search))):(fetch('https://query1.finance.yahoo.com/v7/finance/quote'+window.location.search)))
//((window.location.hostname!="")?(fetch(proxies[0]+/*encodeURIComponent(*/'https://query1.finance.yahoo.com/v7/finance/quote?symbols='+getWatchlist()/*)*/)):(fetch('https://query1.finance.yahoo.com/v7/finance/quote?symbols='+getWatchlist())))
((window.location.hostname!="")?(fetch('https://y.krz.workers.dev/y/q?symbols='+getWatchlist())):(fetch('https://query1.finance.yahoo.com/v7/finance/quote?symbols='+getWatchlist())))
.then(response => response.json())
.then(result => {
quotes=result.quoteResponse.result;
let mactive=false;
for(quote of quotes){
addStock(quote);
if(["PRE","POST","REGULAR"].includes(quote.marketState)) mactive=true;
}
mactive ? marketactive=true : marketactive=false;
speak(quotes);
calcPortfolio(quotes);
});
//console.log(setTimeout(function(){loadQuotes();}, marketactive ? 2000 : 60000));
setTimeout(function(){loadQuotes();}, marketactive ? 2000 : 60000);
}
}
function init(){
document.getElementById("portfoliocontrols").onclick=(e)=>{
let range=e.target.dataset.range;
getAllStocks(range);
//console.log(e);
}
speechSynthesis.cancel();
loadQuotes();
document.getElementById("ver").innerHTML="v1.2";
if(!pchart) getAllStocks(1);
}
////////////////////////
//SPEAKING FUNCTIONALITY
var timeoutResumeInfinity=0;
function resumeInfinity() { window.speechSynthesis.resume(); timeoutResumeInfinity = setTimeout(resumeInfinity, 2000); }
var utt=new SpeechSynthesisUtterance();
utt.lang='en-GB';
utt.rate=1;
utt.onstart = function(event) { resumeInfinity(); };
utt.onend = function(event) { clearTimeout(timeoutResumeInfinity); };
utt.text="Hello trader";
function getRandomWord(words) {
return words[Math.floor(Math.random() * words.length)];
}
function speak(quotes){
if(window.speechSynthesis.speaking || !document.getElementById("spk").checked){
//console.log("speaking:" +window.speechSynthesis.speaking + " checked:"+ document.getElementById("spk").checked);
}else{
let price, change, market;
function getmarketprice(quote){
if(quote.marketState=="PRE" && quote.preMarketPrice){
price= quote.preMarketPrice;
change=quote.preMarketChangePercent;
market="pre-market";
}else if(["PREPRE","POST","POSTPOST","CLOSED"].includes(quote.marketState) && quote.postMarketPrice){
price= quote.postMarketPrice;
//change=quote.postMarketChangePercent;
change=100*(quote.postMarketChange+quote.regularMarketChange)/quote.regularMarketPreviousClose;
market="after hours trading";
}
else{
price= quote.regularMarketPrice;
change=quote.regularMarketChangePercent;
market="regular session";
}
}
// idea: save last spoken change and speak only if the new one is significant
let text="";
let prestocks=[];
let poststocks=[];
textpre="";
textpost="";
function changetotext(change){
return (change>0 ? (change>5 ? getRandomWord([" stock surged "," shares shoot up "," stock jumped up "]): getRandomWord([" is up "," rose ", " jumped "])) : (change<-5 ? getRandomWord([" shares fell "," stock dropped ", " shares plummetted "] ): getRandomWord([" is down "," lost ", " shares shrink "])))+ Math.abs(change.toFixed(1)) + " percent. ";
}
for(q of quotes){
//text+=q.displayName? q.displayName: q.shortName;
getmarketprice(q);
if(market=="pre-market") textpre+= (q.displayName? q.displayName: q.shortName) + changetotext(change);
if(market=="regular session") text+= (q.displayName? q.displayName: q.shortName) +changetotext(change);
if(market=="after hours trading") textpost+= (q.displayName? q.displayName: q.shortName) +changetotext(change);
}
//console.log(text);
utt.text= (textpre.length>0? "In the pre-market session, "+textpre :"") + (textpost.length>0? "In the after-hour trading, "+textpost : "")+(text.length>0 ? "In the regular trading session, " + text : "");
//window.speechSynthesis.cancel();
window.speechSynthesis.speak(utt);
}
}
var skipcount=4;//for live portfolio chart
function calcPortfolio(quotes){
let price, changep, change, market;
let sum=0;
let sumchange=0;
let invested=0;
let chper=0;
function getmarketprice(quote){
if(quote.marketState=="PRE" && quote.preMarketPrice){
price= quote.preMarketPrice;
changep=quote.preMarketChangePercent;
market="pre-market";
change=quote.preMarketChange;
}else if(quote.marketState=="PRE" && !quote.preMarketPrice){
price= quote.regularMarketPrice;
changep=0;
market="pre-market";
change=0;
}else if(["PREPRE","POST","POSTPOST","CLOSED"].includes(quote.marketState) && quote.postMarketPrice){
price= quote.postMarketPrice;
//changep=quote.postMarketChangePercent;
changep=100*(quote.postMarketChange+quote.regularMarketChange)/quote.regularMarketPreviousClose;
market="after hours trading";
change=quote.postMarketChange+quote.regularMarketChange;
}
else{
price= quote.regularMarketPrice;
changep=quote.regularMarketChangePercent;
market="regular session";
change=quote.regularMarketChange;
}
}
for(q of quotes){
//text+=q.displayName? q.displayName: q.shortName;
getmarketprice(q);
sum=sum+price*getHolding(q.symbol).n;
sumchange=sumchange+change*getHolding(q.symbol).n;
chper=100*sumchange/(sum-sumchange);
invested=invested + getHolding(q.symbol).n*getHolding(q.symbol).p;
}
document.getElementById("portfolio").innerHTML="Portfolio: "+sum.toFixed(2)+" Change: "+sumchange.toFixed(2)+ " "+chper.toFixed(2)+"% P/L: "+(sum-invested).toFixed(2);
skipcount++;
if(pchart && skipcount>4){
pchart.data[0].push((new Date()).getTime()/1000);
pchart.data[1].push(sum);
document.getElementById("costline").checked ? pchart.data[4].push(invested):null;
pchart.setData(pchart.data);
skipcount=0;
}
}
function getHolding(symbol){
/*let holding=JSON.parse(localStorage.getItem(symbol));
if (holding==null) holding={"price":0,"number":0};
return holding;
*/
for(stock of portfolio){
if(stock.s==symbol) return stock;
}
return null;
}
function getWatchlist(){
ls_keys=portfolio.map(x=>x.s);//Object.keys(localStorage);
//if(window.location.search=="") window.location.search="?symbols=TSLA,AAPL,MSFT,AMZN,NVDA,NFLX,T,AVGO,INTC,WMT,FB"
if (ls_keys==""){
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
ls_keys=urlParams.get("symbols");
if (ls_keys) return ls_keys.toString();
else return "";
}
let portf=[];
let watchl=[];
for ( stock of portfolio/*k of ls_keys*/){
//let h=JSON.parse(localStorage.getItem(k));
if(stock.n>0) portf.push(stock)//[k,h])
else watchl.push(stock.s);//k);
}
portf.sort((a,b)=>{return (b.p*b.n)-(a.p*a.n)});
watchl.sort();
return portf.map(x=>x.s).concat(watchl).join();
}
function getSize() {
//alert(window.visualViewport.width);
return {
width: document.documentElement.clientWidth,
height: 180,
}
}
function throttle(cb, limit) {
var wait = false;
return () => {
if (!wait) {
requestAnimationFrame(cb);
wait = true;
setTimeout(() => {
wait = false;
}, limit);
}
}
}
function plotChart(timestamps, priceseries, target,chartid, preseries=[null],postseries=[null],cost=[]){
/*if(document.getElementById("chart1")){
document.getElementById("chart1").remove();
}*/
let data = [
timestamps, // x-values (timestamps)
priceseries, // y-values (series 1)
preseries,
postseries,
cost
];
let opts = {
id: chartid,
tzDate: ts => uPlot.tzDate(new Date(ts * 1e3), "America/New_York"),
class: "my-chart",
...getSize(),
plugins: [
touchZoomPlugin()
],
series: [
{
spanGaps: false,
},
{
show: true,
//spanGaps: false,
// in-legend display
label: "Price",
value: (self, rawValue) => "$" + rawValue.toFixed(2) + " Change: "+ (((rawValue.toFixed(2)/self.data[1][0])-1)*100).toFixed(2)+"%",
// series style
stroke: "rgb(0, 255, 255)", /*"rgb(19, 139, 234)"*/
width: 1,
//fill: "rgba(9, 139, 234, 0.3)",
//dash: [10, 5],
},
{ //PREMARKET
spanGaps: false,
show: true,
stroke: "rgba(0, 120, 150, 1)" /*#fca"*/,
width: 1,
//fill: "rgba(0, 0, 0, 0.25)",
//scale:"pre",
},
{ //AFTER MARKET
spanGaps: false,
show: true,
stroke: "rgba(100, 100, 150,1)",
width: 1,
//fill: "rgba(0, 0, 0, 0.5)",
//scale:"pre",
},
{ //COST LINE
show: true,
stroke: "rgba(200, 0, 0,1)",
width: 1,
//fill: "rgba(0, 0, 0, 0.5)",
//scale:"pre",
dash: [10, 5],
}
],
scales: {
x: {
distr: 2,
},
"pre": {
range: [-1,1],
}
},
axes: [
{
show:true,
side:2,
size:0,
gap:-30,
stroke:'#fff',
font: "8px Oswald",
grid: {
show: true,
stroke: "rgba(255,255,255,0.1)",
width: 1,
dash: [],
},
space: 40,
incrs: [
// minute divisors (# of secs)
1,
5,
10,
15,
30,
// hour divisors
60,
60 * 5,
60 * 10,
60 * 15,
60 * 30,
// day divisors
3600,
// ...
],
// [0]: minimum num secs in found axis split (tick incr)
// [1]: default tick format
// [2-7]: rollover tick formats
// [8]: mode: 0: replace [1] -> [2-7], 1: concat [1] + [2-7]
values: [
// tick incr default year month day hour min sec mode
[3600 * 24 * 365, "{YYYY}", null, null, null, null, null, null, 1],
[3600 * 24 * 28, "{MMM}", "\n{YYYY}", null, null, null, null, null, 1],
[3600 * 24, "{M}/{D}", "\n{YYYY}", null, null, null, null, null, 1],
[3600, "{h}{aa}", "\n{M}/{D}/{YY}", null, "\n{M}/{D}", null, null, null, 1],
[60, "{h}:{mm}{aa}", "\n{M}/{D}/{YY}", null, "\n{M}/{D}", null, null, null, 1],
[1, ":{ss}", "\n{M}/{D}/{YY} {h}:{mm}{aa}", null, "\n{M}/{D} {h}:{mm}{aa}", null, "\n{h}:{mm}{aa}", null, 1],
[0.001, ":{ss}.{fff}", "\n{M}/{D}/{YY} {h}:{mm}{aa}", null, "\n{M}/{D} {h}:{mm}{aa}", null, "\n{h}:{mm}{aa}", null, 1],
],
//splits: (s) => {return [s.origLength];}
//labelSize:0,
//label:"Time",
},
{
show:true,
gap:-30,
size: 0,
font: "8px Oswald",
stroke:'#fff',
}
],
};
let uplot = new uPlot(opts, data, target);
//hack add original datapoint length
uplot.origLength=timestamps.length;
return uplot;
}
function blurred(){
winactive=false;
//alert("blurred");
speechSynthesis.cancel();
}
function active(){
winactive=true;
init();
}
window.onfocus = active;
window.onblur = blurred;
window.addEventListener("resize", () => {
//alert(document.documentElement.clientWidth);
for(u of charts.values()){
u.setSize({width:document.documentElement.clientWidth,height:180});
//u.setSize(getSize());
//setTimeout(()=> {u.setSize(getSize())},100);
}
pchart.setSize({width:document.documentElement.clientWidth,height:180});
});
//alert(window.visualViewport.width + "x"+window.visualViewport.height);
function toggleindex(){
ele=this;
//console.log(e.target);
if(ele.nextSibling.style.display=="" ){
ele.nextSibling.style.display="block";
//addChart(ele.id,ele.nextSibling);
addChartRange(ele.id,ele.nextSibling,ele.dataset.gmtoffset,ele.dataset.regMarketTime);
}else {
ele.nextSibling.style.display="";
//ele.nextSibling.innerHTML="";
}
}
function triggerspeak(){
//speechSynthesis.cancel();
if(document.getElementById("spk").checked) {
utt.text=" ";
window.speechSynthesis.speak(utt);
resumeInfinity();
}else{
speechSynthesis.cancel();
}
}
function addHolding(){
hsymb=document.getElementById("hsymb").value;
hprice=0;
hnumber=0;
isNaN(hprice=parseFloat(document.getElementById("hprice").value)) ? hprice=0 : 1;
isNaN(hnumber=parseInt(document.getElementById("hnumber").value)) ? hnumber=0 : 1;
if(hsymb!=""){
localStorage.setItem(hsymb,'{"price":'+hprice+',"number":'+hnumber+'}');
}
}
function removeHolding(){
hsymb=document.getElementById("hsymb").value;
localStorage.removeItem(hsymb);
let el= document.getElementById(hsymb);
if(el){
el.nextSibling.remove();
el.remove();
if(charts.has(hsymb)) charts.delete(hsymb);
}
}
var yfvid='<iframe width="560" height="315" src="https://www.youtube.com/embed/live_stream?channel=UCEAZeUIeJs0IjQiqTCdVSIg&playsinline=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
var nbcvid='<iframe width="560" height="315" src="https://www.youtube.com/embed/live_stream?channel=UCeY0bbntWzzVIaj2z3QigXg&playsinline=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
function showvid(e){
if(e.textContent.includes("Show")){
let d=document.createElement("div");
d.id=e.id+"div";
d.className="iframe-container";
d.innerHTML=e.dataset.url;
document.body.appendChild(d);
e.textContent=e.textContent.replace("Show ","Hide ");
}else {
e.textContent=e.textContent.replace("Hide ","Show ");
document.getElementById(e.id+"div").remove();
}
}
var pchart;
async function getUserPortfolio(){
let response=await fetch("https://tbkv.krz.workers.dev").then(r=>r.text());
let portA=JSON.parse(response);
portfolio=portA;
let symbols=portA.map(x=>x.s);
}
////PORTFOLIO CHART
function getAllStocks(range){
//if(document.getElementById("portfoliochart")) document.getElementById("portfoliochart").remove();
let churl;
var portfolioArray = [];
var timestp =[];
//let symbols=//Object.keys(localStorage);
if(portfolio.length==0) return null;
let invested=0;
//Valid intervals: [1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo]
interval="1m";
if(range>=5) interval="5m";
if(range>=30) interval="30m";
if(range>=60) interval="1h";
if(range>=190) interval="1d";
var promises = [];
for( s of portfolio/*symbols*/){
if(s.n>0){
//churl="https://query1.finance.yahoo.com/v8/finance/chart/"+s+"?interval="+interval+"&range="+range+"d&includePrePost=false";
//promises.push(fetch(proxies[0]+churl));
promises.push(fetch("https://y.krz.workers.dev/y/ch/"+s.s+"?interval="+interval+"&range="+range+"d&includePrePost=false"));
invested+=s.n*s.p;
}
}
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;