Last active
August 6, 2024 04:21
-
-
Save chienpm304/4923c225f2731ed82051ee2632328f27 to your computer and use it in GitHub Desktop.
Simple script to get VN stock price
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stockPrice(symbol) { | |
var now = Math.floor(Date.now() / 1000); | |
var threeDaysAgo = now - (3 * 24 * 60 * 60); | |
var url = 'https://services.entrade.com.vn/chart-api/v2/ohlcs/stock?from=' + threeDaysAgo + '&to=' + now + '&symbol=' + symbol + '&resolution=1D'; | |
Logger.log(url) | |
var headers = { | |
'accept': 'application/json, text/plain, */*', | |
'accept-language': 'en-US,en;q=0.9,vi-VN;q=0.8,vi;q=0.7', | |
'origin': 'https://www.dnse.com.vn', | |
'priority': 'u=1, i', | |
'referer': 'https://www.dnse.com.vn/', | |
'sec-ch-ua': '"Google Chrome";v="125", "Chromium";v="125", "Not.A/Brand";v="24"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-ch-ua-platform': '"macOS"', | |
'sec-fetch-dest': 'empty', | |
'sec-fetch-mode': 'cors', | |
'sec-fetch-site': 'cross-site', | |
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36' | |
}; | |
var options = { | |
'method': 'get', | |
'headers': headers, | |
'muteHttpExceptions': true | |
}; | |
var response = UrlFetchApp.fetch(url, options); | |
var jsonResponse = JSON.parse(response.getContentText()); | |
if (jsonResponse.c && jsonResponse.c.length > 0) { | |
var lastClosingPrice = jsonResponse.c[jsonResponse.c.length - 1]; | |
Logger.log(symbol + ": " + lastClosingPrice) | |
return lastClosingPrice*1000; | |
} else { | |
throw new Error('No closing price data available'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment