2010年5月4日 星期二

期貨契約 4 期交所

底下是對台指期留倉(未平倉,交易口數與契約金額) 的資料下載, 可以使用底下的程式抓期交所的 Data. 我們是透過 HTML parse 出我們要的Data. Link可參考 http://www.taifex.com.tw/chinese/3/7_12_3.asp 如果是對當日的成交筆數或者是每分K線有興趣可以參考 http://funningboy.blogspot.com/2010/04/k.html 程式碼 :
#!/usr/bin/perl -w
use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple;
use Data::Dumper;
use HTML::TableExtract;

my %GbHsPtr;

GetHtml();
Exp2Exl();

sub GetHtml{
   
    my $ua = LWP::UserAgent->new;
    $ua->agent("MyApp/0.1");

    my $req = HTTP::Request->new(POST => 'http://www.taifex.com.tw/chinese/3/7_12_3.asp');
    my $res = $ua->request($req);
    return unless $res->is_success;
   
    #print $res->content();
    my $html_string = $res->content();
 
 my $te = HTML::TableExtract->new( depth => 4, count => 0);
    $te->parse($html_string);
 
 my $cot=0;
 my ($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM);
 my ($NID,$NNM);

 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900;
      $mon  += 1;
      
      if($mon<10){  $mon="0".$mon}
      if($mday<10){ $mday="0".$mday;}
      
my  $oMyFile = "../rem/".$year."_".$mon."_".$mday.".csv";
open (oMyFilePtr,">$oMyFile") || die "open oMyFile(Rem) Error";

 foreach my $ts ($te->tables) {
    foreach my $row ( $ts->rows ) { 
         my $st = join('.',@{$row});
            $st =~ s/\,//g; $st =~ s/\s+//g; $st=~ s/\./\,/g;
             
            printf oMyFilePtr ($st."\n");
           
          if($cot>=3 && $cot<=$#{$ts->rows}-4){
              ($ID,$NM,$BYER,$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM) = split(',',$st);
               
               if( $ID || $NM ){ ($NID,$NNM)=($ID,$NM); }
               else{             ($ID,$NM) = ($NID,$NNM)}
     
              $GbHsPtr{$NM}{$BYER} = {
                 "TBC" => $TBC,
                 "TBM" => $TBM,
                 "TSC" => $TSC,
                 "TSM" => $TSM,
                 "TBSC" => $TBSC,
                 "TBSM" => $TBSM,
                 "RBC"  => $RBC,
                 "RBM"  => $RBM,
                 "RSC"  => $RSC,
                 "RSM"  => $RSM,
                 "RBSC" => $RBSC,
                 "RBSM" => $RBSM,
                };
              }
              
          $cot++;
         }
      }
#    print Dumper (\%GbHsPtr);
}

sub Exp2Exl {
 
  my ($NM,$BYER,$file,$by);
  my %lt = (
    "&#33274;&#32929;&#26399;&#36008;" => "txf",
    "&#38651;&#23376;&#26399;&#36008;" => "exf",
    "&#37329;&#34701;&#26399;&#36008;" => "fxf",
    "&#23567;&#22411;&#33274;&#25351;" => "mxf",
  );
 
  my %By = (
    "&#33258;&#29151;&#21830;"     => "internal_bank",
    "&#25237;&#20449;"       => "internal_stock",   
    "&#22806;&#36039;&#21450;&#38520;&#36039;" => "external_stock",
    );
 
 my $oMyfile;
    foreach ( keys %GbHsPtr ){
        $NM =$_;
       
        if( defined($lt{$NM})){
            $file = $lt{$NM};
          
          foreach ( keys %{$GbHsPtr{$NM}} ){
              $BYER = $_;
              $by = $By{$BYER};
              
              $oMyfile = "../rem/".$file."/".$by.".csv";
              open(oMyFilePtr,">>$oMyfile") || die "open $oMyfile Error\n";
              
              my $TBC = $GbHsPtr{$NM}{$BYER}{"TBC"};
              my $TBM = $GbHsPtr{$NM}{$BYER}{"TBM"};
        my $TSC = $GbHsPtr{$NM}{$BYER}{"TSC"};
        my $TSM = $GbHsPtr{$NM}{$BYER}{"TSM"};
        my $TBSC = $GbHsPtr{$NM}{$BYER}{"TBSC"}
        my $TBSM = $GbHsPtr{$NM}{$BYER}{"TBSM"};
        my $RBC  = $GbHsPtr{$NM}{$BYER}{"RBC"};
        my $RBM  = $GbHsPtr{$NM}{$BYER}{"RBM"};       
        my $RSC  = $GbHsPtr{$NM}{$BYER}{"RSC"};   
        my $RSM  = $GbHsPtr{$NM}{$BYER}{"RSM"};   
        my $RBSC  = $GbHsPtr{$NM}{$BYER}{"RBSC"};     
        my $RBSM  = $GbHsPtr{$NM}{$BYER}{"RBSM"};   

              printf oMyFilePtr ("$TBC,$TBM,$TSC,$TSM,$TBSC,$TBSM,$RBC,$RBM,$RSC,$RSM,$RBSC,$RBSM\n");
             }
        }
    }
}
 
或者你可以下載打包好的 Path, 會有 code, rem, cur的資料夾 %code : 程式碼 %cur : 每日的 1分K 線 (可以自行定義成 5分K線) %rem : 成交筆數跟未平倉Data 使用步驟 %source fu_ticket.sh Download http://sites.google.com/site/funningboy/perl_code/future_v1.rar?attredirects=0&d=1

2 則留言:

  1. PS: 底下轉檔出現亂碼部分要改成###之後的版本才對
    #原始檔
    my %lt = (
    "臺股期貨" => "txf",
    "電子期貨" => "exf",
    "金融期貨" => "fxf",
    "小型臺指" => "mxf",
    );

    my %By = (
    "自營商" => "internal_bank",
    "投信" => "internal_stock",
    "外資及陸資" => "external_stock",
    );

    #################################################
    #更新檔
    my %lt = (
    "臺股期貨" => "txf",
    "電子期貨" => "exf",
    "金融期貨" => "fxf",
    "小型臺指" => "mxf",
    );

    my %By = (
    "自營商" => "internal_bank",
    "投信" => "internal_stock",
    "外資及陸資" => "external_stock",
    );

    回覆刪除
  2. Bug report

    "小型臺指" => "mxf",

    請改成
    "小型臺指期貨" => "mxf",

    THX

    回覆刪除