2017年2月24日金曜日

Get JGB historical data from MOF

財務省のサイトより日本国債の金利データを取得し、XTS形式で扱えるように変換する。すなわち、第一フィールドの日付データが和暦(Sで昭和を、Hで平成を表している)に、第二フィールド以降の金利データを小数点以下3桁固定のフォーマットに変換する。


  1. data comes as csv format and filename is jgbcm_all.csv
  2. execute wget command.
  3. skip 1st and 2nd lines
  4. convert data(see 7.).
  5. execute system("perl jgb.pl");jgb_xts <- as.xts(read.zoo(read.csv("jgb_seireki.csv")));
  6. don't forget to remove jgbcm_all.csv!
  7. data comes as "S49.10.3,10.388,9.378,8.839,8.520,8.354,8.298,8.244,8.120,8.203"
    1. 1st field is date. S49.10.3 must be converted to 1974-10-3.
    2. H for heisei, S for showa.
    3. other fields contain interest data. must do zero padding.




system("wget http://www.mof.go.jp/jgbs/reference/interest_rate/data/jgbcm_all.csv ");
$file="./jgbcm_all.csv";
$outfile="> ./jgb_seireki.csv";

open(IN,$file) or die "$!";
open(OUT,$outfile) or die "$!";
print("# start\n");
print OUT "Date,oneY,twoY,threeY,fourY,fiveY,sixY,sevenY,eightY,nineY\n";  # output head line
$line_num = 0;
while(<IN>){
  if($line_num > 1){  # skip 1st and 2nd lines.

    $gengou = substr($_,0,1);   # pick up the first character of the line.
    @buff = split(/,/, $_); # split the line by comma.
    @data = split(/\./, $buff[0]); # split the first field by period.
    $data[0];
    $year = substr($data[0],1,length($data[0])); # pick up the numerical part of wareki data.
    # print OUT "$gengou";
    if($gengou eq 'S'){  # if the first character = S, data belongs to showa.
        $year=$year+1925;  # add 1925 to adjust as showa period started in 1926.
      }
      else{  # just for the case of heisei until 2019.
      $year=$year+1988;  # adjust for heisei period.
    }
    for ($count = 1; $count < 10; $count++){
      $interest[$count] = sprintf("%.3f", $buff[$count]);  # zero padding each interest data.
    }
    $output = $interest[1].",".$interest[2].",".$interest[3].",".$interest[4].",".$interest[5].",".$interest[6].",".$interest[7].",".$interest[8].",".$interest[9];

    print OUT   "$year-$data[1]-$data[2],$output\n";
  }
    #  print OUT "$year-$data[1]-$data[2],$buff[2],$buff[9]\n";}
  $line_num++;
 # print
}
close(IN);
close(OUT);
system("rm jgbcm_all.csv")


0 件のコメント: