[GnuCash] GnuCash 線上更新 股價&匯率

軟體:Gnucash
Package:strawberry-perl-5.22.1.1-32bit.msi


功能:線上更新 股價&匯率

安裝後,請至 開始 -> 所有程式 -> Gnucash -> 安裝網路報價截取功能,如下圖所示

之後更新方法:
  1. 開啟命令提示字元
  2. 輸入 cpan install Finance::Quote

股價更新
  1. 請到證券編輯器
  2. 按下加入
  3. 假設要更新的為 台灣50
    全名:任意值
    標記:0050.TW (上市公司皆是以 .TW 結尾,上櫃公司皆是以 .TWO 結尾)
    類型:TSEC
    最小單位:1/1000 (因台灣以 1000 股為單位)
    單一來源:選擇 Yahoo USA
  4. 選擇價格編輯器
  5. 按下取得報價
  6. 得到股價結果
匯率更新
因匯率鋃行不同,故需更正程式碼
程式語言:Perl
https://github.com/z-Wind/gnuCash_Quote
  1. 找到此檔案 C:\Perl\site\lib\Finance\Quote.pm
  2. 內容中有此 function
  
  sub currency {

  my $this = shift if (ref($_[0]));

  $this ||= _dummy();


  my ($from, $to) = @_;

  return undef unless ($from and $to);


  $from =~ s/^\s*(\d*\.?\d*)\s*//;

  my $amount = $1 || 1;


  # Don't know if these have to be in upper case, but it's

  # better to be safe than sorry.

  $to = uc($to);

  $from = uc($from);


  return $amount if ($from eq $to); # Trivial case.
  # insert by z-Wind
  # begin ////////////////////
  sub third_url {     
      #use LWP::Simple;
      my $ua = $this->user_agent;
      $ua->timeout(2);
      

      #依需求更正網址與正規表達式      
      my $content = $ua->request(GET 'https://www.cathaybk.com.tw/cathaybk/personal_info07.asp')->content;     
     
      #my $content = get 'https://www.cathaybk.com.tw/cathaybk/personal_info07.asp';
      return undef unless defined $content;
      return undef unless $content =~ /(\d+.\d+)<\/td>/;;
      my $quotes_rate = $1 + 0.03;
      return $quotes_rate;
    }

  if($from =~ /USD/ && $to =~ /TWD/)
  {
     return third_url();
  }
  # end ////////////////////
  my $ua = $this->user_agent;


  # The response should be a single value (the exchange rate)

  my $data = $ua->request(GET "${YAHOO_CURRENCY_URL}${from}${to}=X")->content;

  my $exchange_rate = $data;


  $exchange_rate =~ s/,// ; # solve a bug when conversion rate

                            # involves thousands. yahoo inserts

                            # a comma when thousands occur


  {

    local $^W = 0;  # Avoid undef warnings.


    # We force this to a number to avoid situations where

    # we may have extra cruft, or no amount.

    return undef unless ($exchange_rate+0);

  }


if ( $exchange_rate < 0.001 ) {

    # exchange_rate is too little. we'll get more accuracy by using

    # the inverse rate and inverse it

    my $inverse_rate = $this->currency( $to, $from );

    {

        local $^W = 0;

        return undef unless ( $exchange_rate + 0 );

    }

    $exchange_rate = int( 100000000 / $inverse_rate + .5 ) / 100000000;

}


  return ($exchange_rate * $amount);

}

留言