-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocks.js
196 lines (165 loc) · 5.15 KB
/
stocks.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
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
var stock;
var time;
var allData = new Array();
var intervalMode = false; //true only if integral is not the whole chart
var percentChange = null;
var cx = '001725181769903031181:a_rhctpgo-y';
var chart;
var stocks=["AAPL","AMZN","GOOG","beiojfaiowefjioejfiojswefoijsaeofijoisjioefjs"];
function getStockJSON(stock, time) {
console.log(stock)
console.log(time)
var xml = new XMLHttpRequest();
var url = "https://api.iextrading.com/1.0/stock/" + stock + "/chart/" + time + "/?chartInterval=1";
xml.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
allData = JSON.parse(this.responseText);
}
}
xml.open("GET", url, false);
xml.send();
}
function getStockList() {
var xml = new XMLHttpRequest();
var url = "https://api.iextrading.com/1.0/ref-data/symbols"
var stockList = new Array();
xml.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
stockList = JSON.parse(this.responseText);
stockSelect = document.getElementById("stock");
for(i in stockList) {
if(stockList[i].isEnabled){
var option = document.createElement("option");
option.text = stockList[i].symbol + " - " + stockList[i].name;
option.setAttribute("ticker",stockList[i].symbol);
stockSelect.add(option);
}
}
}
}
xml.open("GET", url, false);
xml.send();
}
function drawChart() {
var data = new google.visualization.DataTable();
if(time == '1d') {
data.addColumn('')
}
data.addColumn('date', 'X');
data.addColumn('number', 'Price');
data.addColumn('number', 'Price');
getStockJSON(stock,time);
var bound1 = 0, bound2 = allData.length-1;
if(intervalMode) {
var selection = chart.getSelection();
bound1 = selection[0].row;
bound2 = selection[1].row;
}
for(i in allData) {
date = new Date(allData[i].date);
date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000); //standardize based on time zone
if(true) {
if((i > bound1 && i < bound2 )|| (i < bound1 && i > bound2 )) {
data.addRows([[date, null, allData[i].close]]);
} else if (i == bound1 || i == bound2) {
data.addRows([[date, allData[i].close, allData[i].close]]);
} else {
data.addRows([[date, allData[i].close, null]]);
}
}
}
if (bound1 < bound2) {
percentChange = (allData[bound2].close-allData[bound1].close)/allData[bound1].close*100;
} else {
percentChange = (allData[bound1].close-allData[bound2].close)/allData[bound2].close*100;
}
var graphColor = "#ffffff"; //graph outside of interval
if (percentChange < 0) {
intervalColor = "rgb(200,0,0)";
} else {
intervalColor = "#00aa00";
}
var options = {
axisTitlesPosition: 'none',
backgroundColor: '#101010',
selectionMode: 'multiple',
legend: {
position: 'none'
},
hAxis:{
textStyle: {
color: '#cccccc'
},
gridlines: {
color: '#333'
}
},
vAxis:{
textStyle: {
color: '#cccccc'
},
gridlines: {
color: '#777'
}
},
colors: [graphColor,intervalColor],
series: {
0: {
lineWidth: 1,
areaOpacity: 0.0
},
1: { lineWidth: 2 },
},
};
chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
//google.visualization.events.addListener(chart, 'click', clickHandler);
google.visualization.events.addListener(chart, 'select', selectionHandler);
chart.draw(data, options);
}
function selectionHandler() {
//set max selection amount to 2
var selectedIndices = chart.getSelection();
if (selectedIndices.length == 3) {
chart.setSelection(selectedIndices.slice(1,selectedIndices.length));
}
if(chart.getSelection().length == 2) {
intervalMode = true;
drawChart();
} else if(chart.getSelection().length == 1) {
refreshNews(allData[chart.getSelection()[0].row].date);
}
}
function refreshNews(date) {
console.log(date);
console.log(toJulian(date));
document.getElementById("gsc-i-id1").value = document.getElementById("stock").options[document.getElementById("stock").selectedIndex].label + " stock news " + date;
document.getElementsByClassName("gsc-search-button")[1].click();
$('.gsc-adBlock').first().css("display","none");
}
function toJulian(date) {
d = new Date(date);
days = d.getTime()/(1000*60*60*24);
return Math.floor((days)+2440587.5);
}
document.addEventListener('DOMContentLoaded', (event) => {
timeSelect = document.getElementById("timeframe");
time = timeSelect.options[timeSelect.selectedIndex].getAttribute('time');
timeSelect.addEventListener("change", (event) => {
intervalMode = false;
time = timeSelect.options[timeSelect.selectedIndex].getAttribute('time');
drawChart();
});
});
$(document).ready(function(){
$("#stock").select2({});
getStockList();
stock = "A";
$('#stock').change(function () {
console.log($('#stock').val())
stock = $('option:selected', this).attr("ticker");
console.log(stock)
drawChart();
});
})