=GOOGLEFINANCE(" ", "price") - ETF
By Suhail Hany
at 2022-10-10T14:28
at 2022-10-10T14:28
Table of Contents
若有以下顧慮:
1.'GOOGLEFINANCE' 擔心延遲
2.'IMPORTHTML' 只能 return table or list
3.'IMPORTXML' 的 'xpath_query' 不好寫
4.'IMPORTHTML' 和 'IMPORTXML' 有更新延遲的問題
可以嘗試用主欄目下 'Extensions' > 'Apps Script' 來撰寫腳本,達成:
1. 網頁抓取數值
2. 即時更新
# 網頁抓取數值
'''
function get_prc(url) {
var response = UrlFetchApp.fetch(url);
content = response.getContentText();
var str = content.split("\n");
var key_lst = 'id="last_last" dir="ltr">';
var key_bid = '-bid">';
var key_ask = '-ask">';
var key_end = '<';
var prc = [];
var slc;
for (var i = 0; i < str.length; i++) {
if (str[i].includes(key_lst)) {
slc = str[i];
var pnt_sta_lst = slc.indexOf(key_lst) + key_lst.length;
var pnt_end_lst = slc.indexOf(key_end, pnt_sta_lst);
prc.push(parseFloat(slc.slice(pnt_sta_lst, pnt_end_lst)));
} else if (str[i].includes(key_bid)) {
slc = str[i];
var pnt_sta_bid = slc.indexOf(key_bid) + key_bid.length;
var pnt_end_bid = slc.indexOf(key_end, pnt_sta_bid);
var pnt_sta_ask = slc.indexOf(key_ask) + key_ask.length;
var pnt_end_ask = slc.indexOf(key_end, pnt_sta_ask);
prc.push(parseFloat(slc.slice(pnt_sta_bid, pnt_end_bid)));
prc.push(parseFloat(slc.slice(pnt_sta_ask, pnt_end_ask)));
break;
}
}
return [prc];
}
'''
# 即時更新
'''
function upd_rng(rng) {
var fml = rng.getFormulas();
rng.setValue("");
SpreadsheetApp.flush();
rng.setFormulas(fml);
}
function upd_prc() {
var sht = SpreadsheetApp.getActiveSheet();
upd_rng(sht.getRange("C2:C4"));
upd_rng(sht.getRange("I2"));
}
'''
# 即時更新(的 UI)
'''
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Custom Menu')
.addItem('Update', 'upd_prc')
.addToUi();
}
'''
呼叫邏輯的關係,定義(貼上 code)的順序要是:
'onOpen' > 'upd_rng' > 'upd_prc' > 'get_prc'
range (比如 "C2:C4") 由個人的試算表編排決定。
---
sheets 範例連結:
https://reurl.cc/KQanpq
筆記連結:
https://hackmd.io/o17R_8S8QKCEQkhBVuxEwg
--
1.'GOOGLEFINANCE' 擔心延遲
2.'IMPORTHTML' 只能 return table or list
3.'IMPORTXML' 的 'xpath_query' 不好寫
4.'IMPORTHTML' 和 'IMPORTXML' 有更新延遲的問題
可以嘗試用主欄目下 'Extensions' > 'Apps Script' 來撰寫腳本,達成:
1. 網頁抓取數值
2. 即時更新
# 網頁抓取數值
'''
function get_prc(url) {
var response = UrlFetchApp.fetch(url);
content = response.getContentText();
var str = content.split("\n");
var key_lst = 'id="last_last" dir="ltr">';
var key_bid = '-bid">';
var key_ask = '-ask">';
var key_end = '<';
var prc = [];
var slc;
for (var i = 0; i < str.length; i++) {
if (str[i].includes(key_lst)) {
slc = str[i];
var pnt_sta_lst = slc.indexOf(key_lst) + key_lst.length;
var pnt_end_lst = slc.indexOf(key_end, pnt_sta_lst);
prc.push(parseFloat(slc.slice(pnt_sta_lst, pnt_end_lst)));
} else if (str[i].includes(key_bid)) {
slc = str[i];
var pnt_sta_bid = slc.indexOf(key_bid) + key_bid.length;
var pnt_end_bid = slc.indexOf(key_end, pnt_sta_bid);
var pnt_sta_ask = slc.indexOf(key_ask) + key_ask.length;
var pnt_end_ask = slc.indexOf(key_end, pnt_sta_ask);
prc.push(parseFloat(slc.slice(pnt_sta_bid, pnt_end_bid)));
prc.push(parseFloat(slc.slice(pnt_sta_ask, pnt_end_ask)));
break;
}
}
return [prc];
}
'''
# 即時更新
'''
function upd_rng(rng) {
var fml = rng.getFormulas();
rng.setValue("");
SpreadsheetApp.flush();
rng.setFormulas(fml);
}
function upd_prc() {
var sht = SpreadsheetApp.getActiveSheet();
upd_rng(sht.getRange("C2:C4"));
upd_rng(sht.getRange("I2"));
}
'''
# 即時更新(的 UI)
'''
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Custom Menu')
.addItem('Update', 'upd_prc')
.addToUi();
}
'''
呼叫邏輯的關係,定義(貼上 code)的順序要是:
'onOpen' > 'upd_rng' > 'upd_prc' > 'get_prc'
range (比如 "C2:C4") 由個人的試算表編排決定。
---
sheets 範例連結:
https://reurl.cc/KQanpq
筆記連結:
https://hackmd.io/o17R_8S8QKCEQkhBVuxEwg
--
Tags:
ETF
All Comments
By Queena
at 2022-10-11T09:04
at 2022-10-11T09:04
By Faithe
at 2022-10-12T03:39
at 2022-10-12T03:39
By Jack
at 2022-10-12T22:14
at 2022-10-12T22:14
By Hedda
at 2022-10-13T16:49
at 2022-10-13T16:49
By Annie
at 2022-10-14T11:24
at 2022-10-14T11:24
By Elvira
at 2022-10-15T05:59
at 2022-10-15T05:59
By Anonymous
at 2022-10-16T00:34
at 2022-10-16T00:34
By Candice
at 2022-10-16T19:09
at 2022-10-16T19:09
By David
at 2022-10-17T13:44
at 2022-10-17T13:44
By Kumar
at 2022-10-17T12:08
at 2022-10-17T12:08
Related Posts
槓桿ETF的選擇,SSO or UPRO
By Annie
at 2022-10-08T13:39
at 2022-10-08T13:39
買最賺錢公司股票不就好了!!
By John
at 2022-10-05T15:47
at 2022-10-05T15:47
信貸投資
By Victoria
at 2022-10-04T20:36
at 2022-10-04T20:36
37男 理財建議
By Aaliyah
at 2022-10-03T21:24
at 2022-10-03T21:24
美股槓桿etf 抄底選擇qld or upro
By Heather
at 2022-10-02T14:31
at 2022-10-02T14:31