getYahoo() 原始碼 for gretl


getYahoo() 套件 for gretl

最近版本:見 gretl 官網 (https://gretl.sourceforge.net/current_fnfiles/getYahoo.gfn)

以下為 v2023.2 版本之原始碼

Help text:

This package enables easy and free downloads for financial data
from finance.Yahoo.com.

The main function of this package is getYahoo() which requires
at least one string paramete for the symbol ticker. For example,
to download the Microsoft stock prices, you have to specify
“MSFT" as in the symbol of the Yahoo finance and return it to a
list (e.g., xL) as follows:

xL = getYahoo(“MSFT", null, null, 0, null)

  • 1st parameter: the ticker in Yahoo Finance
  • 2nd parameter: a string in ISO 8601 format with the starting date for downloading the data (eg “2022-06-24″). “null" or omitted means the same as the current dataset.
  • 3rd parameter: the a string in ISO 8601 format with the ending date for downloading the data (eg “2023-06-24″). “null" or omitted means the same as the current dataset.
  • 4th parameter: an integer between 0 and 5, representing the frequency of the data to be downloaded. 0 or omitted -> same as the current dataset,

1 -> daily
2 -> weekly
3 -> monthly

  • 5th parameter: the notes of the data to be downloaded. “null"
    means the same as the current dataset.

==Change log==

2023/02/03
ver. 2023.2:adding Help text.
2022/12/08
ver. 2022.2:fixing crash and depreciated commands/funcitons
issues for gretl 2022a.
2017/02/28
ver. 0.70: minor fix of errors in GUI when the API key is
null .
2017/02/04
ver. 0.69: minor fix due to changes of Yahoo API when the
API key is null.
2017/12/04
ver. 0.68: Remove Chinese chacters from getYahoo() to comply
with
requirement by gretl official.Chinese version can
be installed
via:
install https://sites.google.com/site/gretlmenu/
package/getYahoo.gfn
2017/12/03
ver. 0.67: Fix the use of “=" as a Boolean operator.
2014.10.21:
ver.0.62 fix errors in downloading stock close prices from
source {WIKI}
2014.10.21: ver.0.61 -First (beta) testing version release
known issues: daily-data may work for gretl 1.9.12

適用於 gret-2019a 或以上版本

# hansl code from package getYahoo 2023.2 (2023-02-08)
# private functions
function string getYahoo_url (string sQcode[null] "Yahoo Codes",
                              string s1[null] "Sample date from (樣本開始自) YYYY-MM-DD: ",
                              string s2[null] "to (樣本結束至) YYYY-MM-DD:",
                              int nFrequency[0:5:0] "Frequency (資料頻率)" {"current sample 依目前樣本", "daily 日資料", "weekly 週資料", "monthly 月資料", "quarterly 季資料", "annual 年資料"},
                              int source[1:5:1] "Source (資料庫):" {"ODA (Open Data for Africa initiative from IMF)", "World Bank", "United Nations", "FRED Economic Data", "WIKI for stock prices (股票資料)"},
                              string sScode[null] "Use your own Source Code (填入自訂資料庫代碼)",
                              bool bUserCode[0] "Use your own code (使用自訂的代碼 )? ",
                              string sUserCode[null] "Fill your code, i.e., {Source code}/{COUNTRY}_{INDICATOR} (填入代碼):",
                              string sAuthToken[null] "Yahoo API key (Yahoo 授權碼):")
    baseURL="https://query1.finance.yahoo.com/v7/finance/download/"
    string s1=start_date(strsub(s1,"/","-"))
    string s2=end_date(strsub(s2,"/","-"))
    if nFrequency ==  0
        if (($pd == 5)||($pd == 6)||($pd == 7))
            nFrequency=1
            sInterval="1d"
        elif $pd==52
            nFrequency=2
            sInterval="1wk"
        elif $pd==12
            nFrequency=3
            sInterval="1mo"
        elif $pd==4
            nFrequency=4
            sInterval="1mo"
        elif $pd==1
            nFrequency=5
            sInterval="1mo"
        endif
    endif
    if nFrequency ==  1
        collapse="daily"
        sInterval="1d"
    elif nFrequency ==  2
        collapse="weekly"
        sInterval="1wk"
    elif nFrequency ==  3
        collapse="monthly"
        sInterval="1mo"
    elif nFrequency ==  4
        collapse="quarterly"
        sInterval="1mo"
    elif nFrequency ==  5
        collapse="yearly"
        sInterval="1mo"
    endif
    if bUserCode
        string sFullcode=baseURL~ sUserCode ~ ".csv"
    else
        if source==1
            string src="ODA"
        endif
        if source==2
            string src="WORLDBANK"
        endif
        if source==3
            string src="UNDATA"
        endif
        if source==4
            string src="FRED"
        endif
        if source==5
            string src="Yahoo"
        endif
        if source==5
            string src="WIKI"
        endif
        if sScode!=""
            string src=sScode
        endif
        string sFullcode=baseURL~ "" ~ sQcode
    endif
    sFullcode = sFullcode ~ "?period1="~ s1 ~ "&period2=" ~ s2 ~ "&interval=" ~ sInterval~ "&events=history"
    return sFullcode
end function

function string write_tmpCSV (string URL,
                              scalar *err,
                              string req,
                              string sQcode)
    rnd = randgen1(i,1,9999)
    if $windows
        fname = sprintf("@dotdir\\%s_%04d.csv",sQcode,rnd)
    else
        fname = sprintf("@dotdir/%s_%04d.csv",sQcode,rnd)
    endif
    use_curl = 0
    if use_curl
        bundle bun = null
        bun.URL = URL
        bun.header = "Content-type: application/x-www-form-urlencoded"
        err = curl(&bun)
        if !err
            check = strstr(bun.output, "Not Found")
            if strlen(check) > 0
                err = 404
            endif
        else
            printf "Error %d!\n", err
        endif
        if !err
            s = bun.output
        endif
    else
        catch s = readfile(URL)
        print "<><><><>"
        err = $error
    endif
    if !err
        check = strstr(s, "Requested entity does not exist.")
        if strlen(check) > 0
            err = 404
        endif
    else
        printf "Error %d!\n", err
    endif
    if !err
        s=strsub(s,"null","NA")
        outfile "@fname" --quiet
            print s
        end outfile
    else
        string fname =null
    endif
    return fname
end function

function string start_date (string sDate)
    if (($pd == 5)||($pd == 6)||($pd == 7))
        sDate1=sDate
    elif $pd==52
        sDate1=sDate
    elif $pd==12
        sDate1=strsub(sDate,":","-")~"-01"
    elif $pd==4
        sDate1=strsub(sDate,":1","-01-01")
        sDate1=strsub(sDate1,":2","-04-01")
        sDate1=strsub(sDate1,":3","-07-01")
        sDate1=strsub(sDate1,":4","-09-01")
    elif $pd==1
        sDate1=sDate ~ "-01-01"
    endif
    sDate1=strsub(sDate1,"/","-")
    string sDateTimeStamp=sprintf("%d",strptime(sDate1,"%Y-%m-%d"))
    return sDateTimeStamp
end function

function string end_date (string sDate)
    if (($pd == 5)||($pd == 6)||($pd == 7))
        sDate1=sDate
    elif $pd==52
        sDate1=sDate
    elif $pd==12
        sDate1=strsub(sDate,":01","-01-31")
        sDate1=strsub(sDate1,":02","-02-29")
        sDate1=strsub(sDate1,":03","-03-31")
        sDate1=strsub(sDate1,":04","-04-30")
        sDate1=strsub(sDate1,":05","-05-31")
        sDate1=strsub(sDate1,":06","-06-30")
        sDate1=strsub(sDate1,":07","-07-31")
        sDate1=strsub(sDate1,":08","-08-31")
        sDate1=strsub(sDate1,":09","-09-30")
        sDate1=strsub(sDate1,":10","-10-31")
        sDate1=strsub(sDate1,":11","-11-30")
        sDate1=strsub(sDate1,":12","-12-31")
    elif $pd==4
        sDate1=strsub(sDate,":1","-01-31")
        sDate1=strsub(sDate1,":2","-04-30")
        sDate1=strsub(sDate1,":3","-07-31")
        sDate1=strsub(sDate1,":4","-09-30")
    elif $pd==1
        sDate1=sDate ~ "-12-31"
    endif
    sDate1=strsub(sDate1,"/","-")
    string sDateTimeStamp=sprintf("%d",strptime(sDate1,"%Y-%m-%d")+86000)
    return sDateTimeStamp
end function

# public functions

function list getYahoo (string sQcode[null] "Yahoo Symbol(Ticker)",
                        string s1[null] "Sample date from YYYY-MM-DD: ",
                        string s2[null] "End",
                        int nFrequency[0:5:0] "Frequency" {"as current", "daily", "weekly", "monthly", "quarterly", "annual"},
                        string slabel[null] "Descriptive label")
    source=5
    bUserCode=0
    verbose=0
    if exists(sQcode)
        if !exists(s1)
            string s1=obslabel($t1)
        endif
        if !exists(s2)
            string s2=obslabel($t2)
        endif
        if !exists(sScode)
            sScode=""
        endif
        if !bUserCode
            sUserCode=""
        endif
        if verbose==1
            printf "==here=\n"
        endif
        if !exists(sAuthToken)
            sAuthToken=""
        endif
        string s1=s1
        string s2=s2
        sQcode=strsub(sQcode,"(","")
        sQcode=strsub(sQcode,")","")
        sQcode=strsub(sQcode,"-",".")
        URL=getYahoo_url(sQcode,s1,s2,nFrequency,source,sScode,bUserCode,sUserCode,sAuthToken)
        if verbose==1
            printf "URL(in main)=%s\n",URL
        endif
        scalar err=0
        string req=""
        sName=write_tmpCSV(URL,&err,req,sQcode)
        string sColname="Value"
        if source==1
            string src="ODA"
        endif
        if source==2
            string src="WORLDBANK"
        endif
        if source==3
            string src="UNDATA"
        endif
        if source==4
            string src="FRED"
        endif
        if source==5
            string src="WIKI"
            string sColname="Close"
        endif
        if sScode!=""
            string src=sScode
            if sScode=="BOE"
                string sColname="Value"
            elif sScode=="QUANDL"
                string sColname="Rate"
            elif sScode=="ECB"
                string sColname="Value"
            elif sScode=="BNP"
                string sColname=sQcode
                string sQcode=strsub(sQcode,"/","_")
            elif sScode=="WIKI"
                string sColname="Close"
            endif
        endif
        if sName==""
            printf "========================= getYahoo(): ==================================\n"
            printf "Error %d! Requested entity '%s' does not exist in '%s'.\n", err,sQcode,src
            printf "=========================================================================\n"
            string sQcode=""
            list tmplist= const
        else
            if $datatype==0
                open "@sName"
            else
            endif
            if src=="WIKI" || source==5
                if instring(sQcode, ".")
                    mS=strsplit(sQcode,".")
                    sQcode=mS[2]~mS[1]
                endif
                if instring(sQcode, "=")
                    sQcode=strsub(sQcode,"=","_")
                endif
                if instring(sQcode, "^")
                    sQcode=strsub(sQcode,"^","")
                endif
                loop foreach j Open High Low Close AdjClose Volume -q
                    string tmp=sQcode
                    string tmp=tmp~"_$j"
                    catch join "@sName" @tmp --tkey=Date --data=$j
                endloop
            else
                catch join "@sName" @sQcode --tkey=Date --data=@sColname
            endif
            err = $error
            if err
                printf "There is an Error: %d! ====================>.\n", err
            else
                if verbose==1
                    printf "NO Error %d! ---->.\n", err
                endif
            endif
            if src=="WIKI" || source==5
                if !exists(slabel)
                    slabel=""
                endif
                loop foreach j Open High Low Close AdjClose Volume -q
                    setinfo @sQcode_$j -d "@slabel Yahoo finance" --graph-name="@slabel"
                    list tmplist= const @sQcode_*
                endloop
            else
                setinfo @sQcode -d "@slabel:@src"
                list tmplist= const @sQcode
            endif
        endif
        return tmplist
    else
        printf "\n================ Error! ==================\n"
        printf " There is no Yahoo ticker code!!!!!\n"
        printf " Please enter the Yahoo ticker code.\n"
        printf "==========================================\n"
    endif
end function

本篇發表於 function package, script, textbook 並標籤為 。將永久鏈結加入書籤。

發表留言