2009年7月30日 星期四

cronw - perl 程式的自動排程

配合前兩篇 - 自期交所自行下載交易檔 perl 程式(win-XP)台指期轉檔程式,perl 程式 2 (配合前支gettick3w.pl-自行下載交易檔)

另外有兩部份 crontab 的設定最後交易日檔, crontab 是自動排程, 最後交易日檔是判斷交易檔中的當月成交資料基的依據。

說明:
  • 程式放在 C:\tick\prg_perl3w\ 下。
  • 資料處理在 C:\tick\prg_perl3w\daily\ 下。
  • 最後交易檔會移到 C:\tick\prg_perl3w\daily\yyyymm\ , 成交檔分好的 tick 及 k線檔(附名csv)會移到 C:\tick\prg_perl3w\tick\yyyymm\ 。以今天為例會移到 C:\tick\prg_perl3w\daily\200907\ 及 C:\tick\prg_perl3w\tick\200907\ , 若是日期為 2009-08-04 會移到 C:\tick\prg_perl3w\daily\200908\ 及 C:\tick\prg_perl3w\tick\200908\ 。




crontab 的設定
  1. 每週 1-5 15:38分,執行這支perl程式 C:\tick\prg_perl3w\gettick3w.pl (到期交所抓檔)
  2. 每週 1-5 15:43分,執行分拆 C:\tick\prg_perl3w\proc_tick3w.pl
  3. crontab 內容(C:\CRONw\crontab.txt)
#      
# m h dom mon dow command
# 分 時 日 月 周 指令
38 15 * * 1-5 c:\perl\bin\perl.exe C:\tick\prg_perl3w\gettick3w.pl
43 15 * * 1-5 c:\perl\bin\perl.exe C:\tick\prg_perl3w\proc_tick3w.pl
參考我的 wiki : windows版 Crontab for Win - cronw


2009年台期所期貨最後交易日檔
2009依日期排順序的最後交易, 2010年(含)以後要自行建立。要用我的成程轉以前的資料, 要自行建立這個 "最後交易日檔"。

內容

20090121
20090218
20090318
20090415
20090520
20090617
20090715
20090819
20090916
20091021
20091118
20091216
參考我的 wiki : 2009 Last Trading Days - 2009年台期所期貨最後交易日


台指期轉檔程式 - perl 程式 2 (配合前支gettick3w.pl-自行下載交易檔)

配合前支程式 : 自期交所自行下載交易檔 perl 程式(win-XP) - gettick3w.pl

  1. 配合環境: windows xp sp2
  2. 程式名稱 : proc3w_tick.pl
  3. 程式目的 : 將當天交易檔分拆成台指期,小台,電指期,金指期當月分tick檔
  4. 程式碼



#!C:\Perl\bin\perl.exe

# 2009-06-25 change output file name
# 2009/6/25 Change directory to C:\\tick\\prg_perl3w\\
# 2009/6/18 For Win32 ActivePerl
# 2009-06-17 for move File to Storage Directory
# 2009-06-06 Process Tick and Make K-Line File

use POSIX qw(strftime);
use integer;

$workDir = 'C:\\tick\\prg_perl3w\\daily\\';
$tickDir = 'C:\\tick\\prg_perl3w\\tick\\';
# 可以改成 5 分K
# my $Minute5K = 5; # 5分K
my $Minute1K = 1; # 1分K

# 讀取最後交易日檔
my $LastTradingDays = "C:\\tick\\prg_perl3w\\last-trading-days";


$today = strftime "%Y%m%d", localtime;

$today_YY = substr $today, 0, 4;
$today_mm = substr $today, 4, 2;
$today_dd = substr $today, 6, 2;

$tick_day = $today_YY . "-" . $today_mm . "-" . $today_dd;

$wkMonth = &get_current_month_product($LastTradingDays);

print "Process $today Tick and Make $Minute1K Minute K-Line File..\n";

chdir $workDir;

&split_Tick ($today_YY, $today_mm, $today_dd, $wkMonth);

$tickdayfile = '???' . $today;

$daily_trade_file = 'Daily_' . $today_YY . '_' . $today_mm . '_' . $today_dd;

$saveDir = substr $today, 0, 6;
$tickStoreDir = $tickDir . $saveDir . '/';
$dailyStoreDir = $workDir . $saveDir . '/';

`C:\\tick\\prg_perl3w\\movefile2dir.pl $workDir $dailyStoreDir $daily_trade_file`;
`C:\\tick\\prg_perl3w\\movefile2dir.pl $workDir $tickStoreDir $tickdayfile`;

exit;

sub process_kline {
# in參數 $tickfile $KMinute

my $startHH = '08'; # 開始時
my $startMM = '45'; # 開始分
my $IdxK = -1; # idx
my $PreIdxK = -1; # idx

my $tickfile = $_[0];
my $KMinute = $_[1];

$workday = substr $tickfile, 3, 8;
my $kline_file = substr($tickfile, 0, 11) . '_' . $KMinute . 'k' . substr($tickfile, 11, 4);

open(infile, $tickfile) || die("Cannot open $tickfile.\n");
open(outfile, ">$kline_file");

print outfile "Date Time,Open,High,Low,Close,Vol\n";

# print "K File Processing...\n";

;

while(my $tk_record = )
{
chomp $tk_record;
$tk_record =~ s/ //g;
($tickTime, $tickPrice, $tickVol)=split(/,/ , $tk_record);

$tkTimeHh = substr $tickTime, 0, 2;
$tkTimeMm = substr $tickTime, 3, 2;
$tkTimeSs = substr $tickTime, 6, 2;

$IdxK = int(((($tkTimeHh - $startHH) * 60) + ($tkTimeMm - $startMM)) / $KMinute);
if ($IdxK == $PreIdxK) { # 同根k
if ($tickPrice > $HPrice) {
$HPrice = $tickPrice;
}
if ($tickPrice < $LPrice) {
$LPrice = $tickPrice;
}
$CPrice = $tickPrice;
$KVol = $KVol + $tickVol;
}
else {
if ($PreIdxK != -1){
print outfile $KTime,$OPrice,$HPrice,$LPrice,$CPrice,$KVol\n";
}
$OPrice = $tickPrice;
$HPrice = $tickPrice;
$LPrice = $tickPrice;
$CPrice = $tickPrice;
$KVol = $tickVol ;

my $KTimeHH = ((($IdxK + 1) * $KMinute) + ($startHH * 60) + $startMM) / 60 + 100;
my $KTimeMM = ((($IdxK + 1) * $KMinute) + ($startHH * 60) + $startMM) % 60 + 100;
$KTimeHH = substr $KTimeHH, 1, 2;
$KTimeMM = substr $KTimeMM, 1, 2;

$KTime = $tick_day . ' ' . $KTimeHH . ':' . $KTimeMM;
$PreIdxK = $IdxK;
}
}
if ($IdxK == $PreIdxK) {
# 同根k
print outfile "$KTime,$OPrice,$HPrice,$LPrice,$CPrice,$KVol\n";
}
close(outfile);
close(infile);
}

# get current month product. ex: 200906 (取得當月月分)
sub get_current_month_product{
my $LastTradingDays_file = $_[0];
open(open_LastTradingDays, $LastTradingDays_file)|| die("Cannot open $LastTradingDays_file.\n");
@recs = ;

for($row=@recs-1; $row >= 0; $row=$row-1)
{
my $LastTradingDays = $recs[$row];
if ($LastTradingDays >= $today ) {
$TradingDays = $LastTradingDays;
}
}
my $wkMonth = substr $TradingDays, 0, 6;

close(open_LastTradingDays);
return $wkMonth;
}

sub split_Tick {
chdir $workDir;
# in參數 $today_YY $today_mm $today_dd $wkMonth

$workday_YY = $_[0];
$workday_mm = $_[1];
$workday_dd = $_[2];
$workMonth = $_[3];

$workdayfile = $workday_YY . $workday_mm . $workday_dd;


$trade_file = 'Daily_' . $workday_YY . '_' . $workday_mm . '_' . $workday_dd . '.rpt';

my $TX_Product = 'TX'; # 台指
my $MTX_Product = 'MTX'; # 小台
my $TE_Product = 'TE'; # 電指期
my $TF_Product = 'TF'; # 金指期

# 2009-06-25 change output file name
$tx_tickfile = 'txf' . $workdayfile . '.csv'; # 台指
$mtx_tickfile = 'mxf' . $workdayfile . '.csv'; # 小台
$te_tickfile = 'exf' . $workdayfile . '.csv'; # 電指期
$tf_tickfile = 'fxf' . $workdayfile . '.csv'; # 金指期

open(read_file, $trade_file) || die("Cannot open $trade_file.\n");


open(out_tx, ">$tx_tickfile");
open(out_mtx,">$mtx_tickfile");
open(out_te, ">$te_tickfile");
open(out_tf, ">$tf_tickfile");

print out_tx "Date Time,Price,Vol\n";
print out_mtx "Date Time,Price,Vol\n";
print out_te "Date Time,Price,Vol\n";
print out_tf "Date Time,Price,Vol\n";

# print "Split Tick File Processing...\n";

while(my $record = )
{
chomp $record;
$record =~ s/ //g;
# 交易日期,商品代號,交割年月,成交時間,成交價格,成交數量(B+S),近月價格,遠月價格
($tDays, $tProduct, $tMonth, $tickTime, $tPrice, $tVol, $nPrice, $fPrice)=split(/,/ , $record);
# print "$tDays, $tProduct, $tMonth, $tickTime, $tPrice, $tVol, $nPrice, $fPrice\n";

$tVol = $tVol / 2;

$tTimeHh = substr $tickTime, 0, 2;
$tTimeMm = substr $tickTime, 2, 2;
$tTimeSs = substr $tickTime, 4, 2;

$tTime = $tTimeHh . ':' . $tTimeMm . ':' . $tTimeSs;

# 當月
if ($tMonth eq $workMonth)
{
if ($tProduct eq $TX_Product) { # 台指
print out_tx "$tTime,$tPrice,$tVol\n";
}
elsif ($tProduct eq $MTX_Product) { # 小台
print out_mtx "$tTime,$tPrice,$tVol\n";
}
elsif ($tProduct eq $TE_Product) { # 電指期
print out_te "$tTime,$tPrice,$tVol\n";
}
elsif ($tProduct eq $TF_Product) { # 金指期
print out_tf "$tTime,$tPrice,$tVol\n";
}
}
}

close(out_tf);
close(out_te);
close(out_mtx);
close(out_tx);

close(read_file);

&process_kline($tx_tickfile, $Minute1K);
}

參考我的 wiki : Process split tick - proc3w_tick.pl

2009年7月14日 星期二

自期交所自行下載交易檔 perl 程式(win-XP)

在Windows XP使用 perl script 程式,配合cronw自動排程,自動抓台期交所當天交易檔
  1. 環境: windows xp sp2
  2. 程式名稱 : gettick3w.pl
  3. 程式目的 : 抓台期交所當天交易檔
  4. 程式碼




#!C:\Perl\bin\perl.exe

# 2009/6/25 Change directory to C:\\tick\\prg_perl3w\\
# 2009/6/18 For Win32 ActivePerl
#!/usr/bin/perl # 原程式是在 Debian linux 下
# 2009-05-30 get taiwan future tick

use POSIX qw(strftime);

$workDir = 'C:\\tick\\prg_perl3w\\daily\\';

$today_ziptickfile = strftime "Daily_%Y_%m_%d.zip", localtime;
$today_tickfile = strftime "Daily_%Y_%m_%d.rpt", localtime;

$LogFile = $workDir . "perl_crontab.log";

$tickfile = "http://www.taifex.com.tw/DailyDownload/" .$today_ziptickfile ;

chdir $workDir;

# 用 wget 抓台期交所當天交易檔
my $content = `C:\\tool\\wget.exe -c -a $LogFile -P $workDir $tickfile`;

if (-e $today_ziptickfile) {
# 解 zip 檔
# `unzip $today_ziptickfile`;
`c:\\tool\\unzip $today_ziptickfile`;
}
else {
die("File not exist : $today_ziptickfile.\n");
}

參考我的 wiki : Get Taiwan future tick - gettick3w.pl

2009年7月2日 星期四

台指期轉檔程式 - Linux Bash

自行到期交所下載交易檔及轉台指期的1分 K 的 bash 程式

這部份是在linux上的shell script程式,配合crontab自動排程
  • Crontab for User
    • 自動排程crontab
    • 編輯 user 的 crontab (執行 crontab -l )
    • 每週 1-5 16:44分,執行這支bash程式(~/prg_bash/cron_proc_tick.bash)
    • crontab 內容
    • # m  h  dom mon dow command
      # 分 時 日 月 周 指令
      44 16 * * 1-5 ~/prg_bash/cron_proc_tick.bash



  • 抓交易檔轉成tick程式
    • 環境: debian linux lenny
    • 程式名稱 : cron_proc_tick.bash
    • 程式目的 : 抓台灣期貨交易所當天交易檔,並分別轉成tick(.csv)
    • BASH 程式碼
    • #!/bin/sh

      # Get Tick

      today_tickfile=http://www.taifex.com.tw/DailyDownload/Daily_`date +%Y_%m_%d`.zip

      wget -c -a ~/tick/prg_bash/rj_crontab.log -P ~/tick/prg_bash/daily/ $today_tickfile


      # Process Tick

      cd ~/tick/prg_bash/daily/

      todayfile=`date +%Y_%m_%d`
      today=`date +%Y%m%d`
      todayMonth=`date +%Y%m`
      zipfile=Daily_$todayfile.zip

      workmonth=./tmp1

      wkproduct=`ls -l Daily_$todayfile.zip | cut -d ' ' -f 5 `

      if [ "$wkproduct" -ne '93' ]; then

      unzip $zipfile

      # echo Daily_$todayfile.rpt
      # echo $todayfile.TX

      # ltd=`tac last-trading-days`
      ltd=`tac ~/tick/last-trading-days`
      for td in $ltd
      do
      #echo "$td="$td , "$today="$today
      if [ "$td" -ge "$today" ]; then
      #echo " $td > $today "
      wkmonth=`echo $td`
      fi
      done

      echo $wkmonth > $workmonth

      wkproduct=`cut -c 1-6 $workmonth`
      # echo $wkproduct


      # TX 台指
      head -n 1 Daily_$todayfile.rpt > $todayfile.TX
      sed '/MTX/d' Daily_$todayfile.rpt | sed -n '/TX/p' | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $1,$2,$3,$4,$5,$6/2 }' >> $todayfile.TX
      echo "TickTime,Price,Vol" > txf$today.csv
      sed '/MTX/d' Daily_$todayfile.rpt | sed -n '/TX/p' | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $4,$5,$6/2 }' >> txf$today.csv

      # MTX 小台
      head -n 1 Daily_$todayfile.rpt > $todayfile.MTX
      sed -n '/MTX/p' Daily_$todayfile.rpt | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $1,$2,$3,$4,$5,$6/2 }' >> $todayfile.MTX
      echo "TickTime,Price,Vol" > mxf$today.csv
      sed -n '/MTX/p' Daily_$todayfile.rpt | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $4,$5,$6/2 }' >> mxf$today.csv


      # TE 電指期
      head -n 1 Daily_$todayfile.rpt > $todayfile.TE
      sed -n '/TE/p' Daily_$todayfile.rpt | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $1,$2,$3,$4,$5,$6/2 }' >> $todayfile.TE
      echo "TickTime,Price,Vol" > exf$today.csv
      sed -n '/TE/p' Daily_$todayfile.rpt | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $4,$5,$6/2 }' >> exf$today.csv

      ## TF 金指期
      head -n 1 Daily_$todayfile.rpt > $todayfile.TF
      sed '/GTF/d' Daily_$todayfile.rpt | sed -n '/TF/p' | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $1,$2,$3,$4,$5,$6/2 }' >> $todayfile.TF
      echo "TickTime,Price,Vol" > fxf$today.csv
      sed '/GTF/d' Daily_$todayfile.rpt | sed -n '/TF/p' | sed -n 's/ //gp' | awk -F, 'BEGIN{OFS=","}($3=='$wkproduct'){ print $4,$5,$6/2 }' >> fxf$today.csv

      rm $workmonth


      if [ ! -d $todayMonth ]; then
      mkdir $todayMonth
      fi
      mv Daily_$todayfile.rpt $zipfile $todayMonth/

      if [ ! -d "~/tick/prg_bash/tick/$todayMonth" ]; then
      mkdir ~/tick/prg_bash/tick/$todayMonth
      fi
      mv $todayfile.TX txf$today.csv $todayfile.MTX mxf$today.csv $todayfile.TE exf$today.csv $todayfile.TF fxf$today.csv ~/tick/prg_bash/tick/$todayMonth/

      fi


參考我的 wiki : Get Taiwan future tick - cron_proc_tick.bash


原提供 "轉自期交所的台指期1分K" 將停止

最近幾次會將 提供轉自期交所的台指期1分K線 的程式放上來。

而原提供 "轉自期交所的台指期1分K", 將在下個月1日(2009/08/01)停止, 不再提供轉好的1分K線csv檔(好像也沒人用)。

若需要的人, 可自行按程式說明建置或修改自己的格式(5分,15分,30分,60分,1日K)。


聯絡表單

名稱

以電子郵件傳送 *

訊息 *