-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstock.js
153 lines (144 loc) · 4.6 KB
/
stock.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
var api = "GODA9IY17YCA5VNR"; // get your own api (https://www.alphavantage.co/support/#api-key)
var dps = [];
var start = [];
var stop = [];
var temp = [];
var open = [];
var close = [];
var company = null;
var symbol = null;
var chart = null;
var columns = ["Date", "Open", "High", "Low", "Close"];
var data1 = []
function download(){
window.location = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol="+symbol+"&apikey="+api+"&datatype=csv";
}
function getting_data(){
if(company !== null){
$.getJSON("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol="+symbol+"&outputsize=full&apikey="+api)
.done(function(data){
var date = data["Time Series (Daily)"]
let a = 50;
let b = 7;
for(var d in date){
var r = d.split("-");
if(a-- > 0){
var value = date[d];
dps.unshift({x: new Date(parseInt(r[0]), parseInt(r[1])-1, parseInt(r[2])), y: parseFloat(value["1. open"])});
temp.unshift(new Date(parseInt(r[0]), parseInt(r[1])-1, parseInt(r[2])))
start.unshift(parseFloat(value["2. high"]));
stop.unshift(parseFloat(value["3. low"]));
open.unshift(parseFloat(value["1. open"]));
close.unshift(parseFloat(value["4. close"]));
if(b-- > 0){
let c = [d, value["1. open"], value["2. high"], value["3. low"], value["4. close"]];
data1.push(c);
}
}else{
break;
}
}
graph();
drawTable();
document.getElementById("loading_container").style.display = "none";
document.getElementById("download_data").style.display = "block";
document.getElementById("companies").disabled = false;
document.getElementById("get_data").disabled = false;
document.getElementById("chartContainer").disabled = false;
})
.fail(function(textStatus, error){
alert(textStatus+" "+error+"\nReload the page");
})
}
}
function graph() {
// Function to determine color based on the opening and closing prices
function getColor(open, close) {
return open <= close ? "rgba(0, 128, 0, 0.7)" : "rgba(255, 0, 0, 0.7)"; // Green for rising, blue for falling
}
const rangeColumnData = temp.map(function (time, index) {
return {
x: time,
y: [start[index], stop[index]],
color: getColor(open[index], close[index])
};
});
chart = new CanvasJS.Chart("chartContainer", {
title: {
text: company
},
animationEnabled: true,
theme: "light2",
axisY: {
title: "Prices",
includeZero: false
},
axisX: {
title: "Date",
valueFormatString: "DD-MMM"
},
data: [
{
type: "line",
indexLabelFontSize: 16,
linecolor: "rgba(0, 0, 0, 0)",
dataPoints: dps
},
{
type: "rangeColumn",
dataPoints: rangeColumnData
}
]
});
chart.render();
temp = [];
start = [];
stop = [];
}
function getData(){
if(chart !== null){
chart.destroy();
}
data1 = [];
dps = [];
document.getElementById("table_container").innerHTML = "";
company = document.getElementById("companies").value;
let r = company.split("(");
symbol = r[1].substring(0, r[1].length-1);
document.getElementById("loading_container").style.display = "block";
document.getElementById("download_data").style.display = "none";
document.getElementById("companies").disabled = true;
document.getElementById("get_data").disabled = true;
document.getElementById("chartContainer").disabled = true;
getting_data();
}
function drawTable(){
var table_container = document.getElementById("table_container");
var para = document.createElement("p");
para.id = "para";
var cell = document.createTextNode("RECENT END OF DAY PRICES");
para.appendChild(cell);
table_container.appendChild(para);
var table = document.createElement("table");
table.className = "table";
var row = document.createElement("tr");
for(let i=0;i<columns.length;i++){
var col = document.createElement("th");
col.scope = "col";
cell = document.createTextNode(columns[i]);
col.appendChild(cell);
row.appendChild(col);
}
table.appendChild(row);
for(let i=0;i<7;i++){
row = document.createElement("tr");
for(let j=0;j<5;j++){
col = document.createElement("td");
cell = document.createTextNode(data1[i][j]);
col.appendChild(cell);
row.appendChild(col);
}
table.appendChild(row);
}
table_container.appendChild(table);
}