@@ -125,9 +125,6 @@ <h2>Volume: <span class="font-bold">${volume}</span></h2>
125
125
const data = response . data ;
126
126
127
127
// Display stock info
128
-
129
- // feature in testing: <button onclick="getAdditionalStockInfo()" class="btn">Get Additional Information</button>
130
-
131
128
const changeColor = data . d > 0 ? 'green' : 'red' ;
132
129
document . getElementById ( "stockInfo" ) . innerHTML = `
133
130
<h2><b>Stock Information:</b></h2>
@@ -143,22 +140,19 @@ <h2><b>Stock Information:</b></h2>
143
140
<p><br>
144
141
</p>
145
142
` ;
143
+
144
+ // Fetch monthly stock data from Alpha Vantage API and render chart
145
+ await fetchMonthlyStockData ( symbol ) ;
146
146
} catch ( error ) {
147
147
console . error ( error ) ;
148
148
}
149
-
150
- // Subscribe to symbol for real-time updates
151
- subscribe ( symbol ) ;
152
-
153
- // Fetch monthly stock data from Alpha Vantage API and render chart
154
- fetchMonthlyStockData ( symbol ) ;
155
149
}
156
150
157
151
// Function to fetch monthly stock data from Alpha Vantage API and render chart
158
152
async function fetchMonthlyStockData ( symbol ) {
159
153
const rapidApiKey = '48e23c6bf3msh9a6baf3e68d9a4ep14546ajsn1a39e98c4ad5' ;
160
154
const rapidApiHost = 'alpha-vantage.p.rapidapi.com' ;
161
- const url = `https://${ rapidApiHost } /query?function=TIME_SERIES_MONTHLY_ADJUSTED &symbol=${ symbol } &datatype=json&output_size=compact ` ;
155
+ const url = `https://${ rapidApiHost } /query?function=TIME_SERIES_DAILY_ADJUSTED &symbol=${ symbol } &datatype=json&output_size=full ` ;
162
156
163
157
const requestOptions = {
164
158
method : 'GET' ,
@@ -169,13 +163,13 @@ <h2><b>Stock Information:</b></h2>
169
163
} ;
170
164
171
165
try {
172
- const response = await fetch ( url , requestOptions ) ;
173
- const data = await response . json ( ) ;
166
+ const response = await axios . get ( url , requestOptions ) ;
167
+ const data = response . data ;
174
168
175
169
// Process stock data for chart
176
- let seriesData = Object . keys ( data [ 'Monthly Adjusted Time Series' ] ) . map ( key => ( {
170
+ let seriesData = Object . keys ( data [ 'Time Series (Daily) ' ] ) . map ( key => ( {
177
171
x : new Date ( key ) ,
178
- y : parseFloat ( data [ 'Monthly Adjusted Time Series' ] [ key ] [ '5. adjusted close' ] )
172
+ y : parseFloat ( data [ 'Time Series (Daily) ' ] [ key ] [ '5. adjusted close' ] )
179
173
} ) ) ;
180
174
181
175
// Render chart using ApexCharts
@@ -189,7 +183,7 @@ <h2><b>Stock Information:</b></h2>
189
183
function renderStockChart ( seriesData ) {
190
184
var chartOptions = {
191
185
series : [ {
192
- name : 'Monthly Closed Stock Market Data ' ,
186
+ name : 'Time Series (Daily) ' ,
193
187
data : seriesData
194
188
} ] ,
195
189
chart : {
@@ -254,43 +248,6 @@ <h2><b>Stock Information:</b></h2>
254
248
var chart = new ApexCharts ( document . querySelector ( "#stockChart" ) , chartOptions ) ;
255
249
chart . render ( ) ;
256
250
}
257
- // Function to fetch additional stock information
258
- async function getAdditionalStockInfo ( ) {
259
- const symbol = document . getElementById ( "symbol" ) . value ;
260
-
261
- try {
262
- // Fetch additional stock information using the Finnhub API
263
- const additionalInfo = await getAdditionalStockInformation ( symbol ) ;
264
-
265
- // Display the additional stock information
266
- document . getElementById ( "stockInfo" ) . innerHTML += `
267
- <h2><b>Additional Information</b></h2>
268
- <ul>
269
- ${ additionalInfo . map ( info => `
270
- <li><b>Description:</b> ${ info . description } </li>
271
- <li><b>Display Symbol:</b> ${ info . displaySymbol } </li>
272
- <li><b>Type:</b> ${ info . type } </li>
273
- <br>
274
- ` ) . join ( '' ) }
275
- </ul>
276
- ` ;
277
- } catch ( error ) {
278
- console . error ( error ) ;
279
- }
280
- }
281
-
282
- // Function to fetch additional stock information from the Finnhub API
283
- function getAdditionalStockInformation ( symbol ) {
284
- return new Promise ( ( resolve , reject ) => {
285
- finnhubClient . symbolSearch ( symbol , ( error , data , response ) => {
286
- if ( error ) {
287
- reject ( error ) ;
288
- } else {
289
- resolve ( data . result ) ;
290
- }
291
- } ) ;
292
- } ) ;
293
- }
294
251
295
252
</ script >
296
253
0 commit comments