顯示具有 perl 標籤的文章。 顯示所有文章
顯示具有 perl 標籤的文章。 顯示所有文章

2021年3月11日 星期四

結算日-2021年台期所期貨最後交易日檔

110年台期所期貨最後交易日(結算日)檔


20210120
20210217
20210317
20210421
20210519
20210616
20210721
20210818
20210922
20211020
20211117
20211222
20220119

2017年2月19日 星期日

Perl - 貨日交易檔分拆成當月:台指,小台,電指期,金指期

程式 : perl 
說明 : 期貨日交易檔分拆成當月:台指,小台,電指期,金指期
# prg : perl_proc_tick.pl
#       期貨日交易檔分拆成當月 台指, 小台, 電指期, 金指期

# V 1.3.1 2015-04-24 change workDir to ~/trade/prg_perl/trade_data/
#                           tickDir to ~/trade/prg_perl/tick/
# V 1.3.0 2010-01-05 Auto Create Store Dir
# V 1.2.0 2009-10-10 change Last-Trading-Days
# V 1.1.0 2009-05-30 get taiwan future tick 
# V 1.0.0 2009-06-17 change Work Directory and move to Storage

use POSIX qw(strftime);
use integer;
# $workDir = '/home/redjoe/tick/prg_perl/trade_data/';
# $tickDir = '/home/redjoe/tick/prg_perl/tick/';
# my $LastTradingDays_file = "/home/redjoe/tick/last-trading-days/last-trading-days";
$workDir = '/home/redjoe/trade/prg_perl/trade_data/';
$tickDir = '/home/redjoe/trade/prg_perl/tick/';
my $LastTradingDays_file = "/home/redjoe/trade/last-trading-days/last-trading-days";

# $target = 'hm30:~/public_html/future/daily/';

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

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

chdir $workDir;

# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;

($sec, $min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)  = localtime(time);

$year = $year + 1900;
$mon  = $mon  + 1;

# :34,43s/^# //g  :47,55s/^/# /g  # testing use
# :34,43s/^/# /g  :47,55s/^# //g  # Auto get date

# $today_ziptickfile = "Daily_" . $year . "_" . $mon ."_" . $mday . ".zip";

$today             = strftime "%Y%m%d", localtime;
$wtoday            = strftime "%Y/%m/%d", localtime;
$today_ziptickfile = strftime "Daily_%Y_%m_%d.zip", localtime;
$today_tickfile    = strftime "Daily_%Y_%m_%d.rpt", localtime;

$today_wt_tx_tickfile  = strftime "%Y_%m_%d.tx", localtime;
$today_wt_mtx_tickfile = strftime "%Y_%m_%d.mtx", localtime;
$today_wt_te_tickfile  = strftime "%Y_%m_%d.te", localtime;
$today_wt_tf_tickfile  = strftime "%Y_%m_%d.tf", localtime;

$today_wt_tx_kfile  = $today_wt_tx_tickfile .'_' . $MinuteK . 'k';
$today_wt_mtx_kfile = $today_wt_mtx_tickfile .'_' . $MinuteK . 'k';
$today_wt_te_kfile  = $today_wt_te_tickfile .'_' . $MinuteK . 'k';
$today_wt_tf_kfile  = $today_wt_tf_tickfile .'_' . $MinuteK . 'k';

$outRec = "交易日期,商品代號,交割年月,成交時間,成交價格,成交數量";

open(open_LastTradingDays, $LastTradingDays_file)|| die("Cannot open $LastTradingDays_file.\n");
@recs = <open_LastTradingDays>;

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

close(open_LastTradingDays);


open(open_file, $today_tickfile) || die("Cannot open $workDir$today_tickfile.\n");

open(wf_tx, ">$today_wt_tx_tickfile");
open(wf_mtx,">$today_wt_mtx_tickfile");
open(wf_te, ">$today_wt_te_tickfile");
open(wf_tf, ">$today_wt_tf_tickfile");

open(wf_ktx, ">$today_wt_tx_kfile");
# open(wf_kmtx,">$today_wt_mtx_kfile");
# open(wf_kte, ">$today_wt_te_kfile");
# open(wf_ktf, ">$today_wt_tf_kfile");

# print wf_tx  "成交時間,成交價格,數量\n";
print wf_tx  "Date Time,Price,Vol\n";
print wf_mtx "Date Time,Price,Vol\n";
print wf_te  "Date Time,Price,Vol\n";
print wf_tf  "Date Time,Price,Vol\n";

print wf_ktx  "Date Time,Open,High,Low,Close,Vol\n";
# print wf_kmtx "成交時間,,,,,數量\n";
# print wf_kte  "成交時間,,,,,數量\n";
# print wf_ktf  "成交時間,,,,,數量\n";


# print "Processing...\n";

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

   # print "$record\n";
   $tVol = $tVol / 2;
   # print "$tDays, $tProduct, $tMonth, $tickTime, $tPrice, $tVol, $nPrice, $fPrice\n";

   $tTimeHh = substr $tickTime, 0, 2;
   $tTimeMm = substr $tickTime, 2, 2;
   $tTimeSs = substr $tickTime, 4, 2;
   # my $tTimeMs = substr $tickTime, 6, 6;
   $tTime   = $tTimeHh . ':' . $tTimeMm . ':' . $tTimeSs;

   # print "\$wkMonth = $wkMonth  \n\$tMonth = $tMonth\n";
   # print "\$tProduct = $tProduct  \n";

   # 當月
   if ($tMonth eq $wkMonth)
   {
      if ($tProduct eq $TX_Product) { # 台指
         # # print "\n # $tProduct# $tTime,$tPrice,$tVol,  =$tDays, $tProduct, $tMonth, $nPrice, $fPrice=";
         # print wf_tx "$tTime,$tPrice,$tVol,=$tDays, $tProduct, $tMonth, $nPrice, $fPrice=";
         print wf_tx "$tTime,$tPrice,$tVol\n";
      }
      elsif ($tProduct eq $MTX_Product) { # 小台
         # # print "\n # $tProduct# $tTime,$tPrice,$tVol,  =$tDays, $tProduct, $tMonth, $nPrice, $fPrice=";
         print wf_mtx "$tTime,$tPrice,$tVol\n";
      }
      elsif ($tProduct eq $TE_Product) { # 電指期
         # print "\n # $tProduct# $tTime,$tPrice,$tVol,  =$tDays, $tProduct, $tMonth, $nPrice, $fPrice=";
         print wf_te "$tTime,$tPrice,$tVol\n";
      }
      elsif ($tProduct eq $TF_Product) { # 金指期
         # print "\n # $tProduct# $tTime,$tPrice,$tVol,  =$tDays, $tProduct, $tMonth, $nPrice, $fPrice=";
         print wf_tf "$tTime,$tPrice,$tVol\n";
      }

      if ($tProduct eq $TX_Product) { # 台指
         $IdxK  = int(((($tTimeHh - $startHH) * 60) + ($tTimeMm - $startMM)) / $MinuteK);
         if ($IdxK == $PreIdxK) { # 同根k
            if ($tPrice > $HPrice) {
               $HPrice = $tPrice;
            }
            if ($tPrice < $LPrice) {
               $LPrice = $tPrice;
            }
            $CPrice = $tPrice;
            $KVol   = $KVol + $tVol;
         }
         else {
            if ($PreIdxK != -1){
               print wf_ktx "$KTime,$OPrice,$HPrice,$LPrice,$CPrice,$KVol\n";
            }
            $OPrice = $tPrice;
            $HPrice = $tPrice;
            $LPrice = $tPrice;
            $CPrice = $tPrice;
            $KVol   = $tVol  ;

            # $PreIdxK = $IdxK - 1;
            my $KTimeHH = ((($IdxK + 1) * $MinuteK) + ($startHH * 60) + $startMM) / 60 + 100;
            my $KTimeMM = ((($IdxK + 1) * $MinuteK) + ($startHH * 60) + $startMM) % 60 + 100;
            $KTimeHH = substr $KTimeHH, 1, 2;
            $KTimeMM = substr $KTimeMM, 1, 2;
            $KTime   = $wtoday . ' ' . $KTimeHH . ':' . $KTimeMM;
            # print "\n\$IdxK = $IdxK  \$PreIdxK = $PreIdxK  \$KTime = $KTime  \$MinuteK = $MinuteK  \$tTimeHh = $tTimeHh  \$tTimeMm = $tTimeMm  \$startHH = $startHH  \$startMM = $startMM ";
            $PreIdxK = $IdxK;

         }
      }
   }
}
   if ($IdxK == $PreIdxK) { # 同根k
       print wf_ktx "$KTime,$OPrice,$HPrice,$LPrice,$CPrice,$KVol\n";
       # print "\n\$IdxK = $IdxK  \$PreIdxK = $PreIdxK  \$KTime = $KTime  \$MinuteK = $MinuteK  \$tTimeHh = $tTimeHh  \$tTimeMm = $tTimeMm  \$startHH = $startHH  \$startMM = $startMM ";
   }
# close(wf_ktf);
# close(wf_kte);
# close(wf_kmtx);
close(wf_ktx);

close(wf_tf);
close(wf_te);
close(wf_mtx);
close(wf_tx);

close(open_file);

# 2009-08-01 stop
# my $scp_content = `/usr/bin/scp $today_wt_tx_kfile $target`;


# 2009-06-17   for move File to Storage Directory

$workdayfile       = strftime "%Y_%m_%d", localtime;
$daily_trade_file  = 'Daily_' . $workdayfile;

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

unless (-e $tickStoreDir     ) { mkdir($tickStoreDir     , 0700) || print $!; }
unless (-e $dailyStoreDir    ) { mkdir($dailyStoreDir    , 0700) || print $!; }

for $file (<$daily_trade_file*.*>) {
  $targetFile = $dailyStoreDir . $file;
  rename $file, $targetFile;
  # print "Rename File : source(\$file)=$file  destination(\$targetFile)=$targetFile\n";
}

for $file (<$workdayfile*.*>) {
  $targetFile = $tickStoreDir  . $file;
  rename $file, $targetFile;
  # print "Rename File : source(\$file)=$file  destination(\$targetFile)=$targetFile\n";
}

2016年12月23日 星期五

結算日-2017年台期所期貨最後交易日檔

2017(中華民國106年)台期所期貨最後交易日(結算日)檔

2016-12-24
106年台期所期貨最後交易日檔

資訊來源 -> 臺灣期貨交易所首頁 > 交易制度 > 行事曆

20170118
20170215
20170315
20170419
20170517
20170622
20170719
20170816
20170920
20171018
20171115
20171220
20180117
20180221
20180321
20180418
20180516
20180620
20180718
20180815
20180919
20181017
20181121
20181219
20190116

--

2015年12月2日 星期三

2016年台期所期貨最後交易日(結算日)檔

2016(中華民國105年)台期所期貨最後交易日(結算日)檔

2015-12-03

2016年台期所期貨最後交易日檔

資訊來源 -> 首頁 > 交易制度 > 行事曆

20160120
20160217
20160316
20160420
20160518
20160615
20160720
20160817
20160921
20161019
20161116
20161221

--

2014年12月16日 星期二

2015年台期所期貨最後交易日檔

2015(中華民國104年)台期所期貨最後交易日檔

2014-12-17

2015台期所期貨最後交易日檔(結算日)

資訊來源 - 期貨交易所 - 最新消息 - 公告 - 1205期交所-年曆卡(底黑).pdf

20150121
20150218
20150318
20150415
20150520
20150617
20150715
20150819
20150916
20151021
20151118
20151216

--

2012年8月3日 星期五

安裝 geniustrader

2012-05-08

加入路徑

~/.profile
# set PATH so it includes user's geniustrader if it exists
if [ -d "$HOME/geniustrader/Scripts" ] ; then
PATH="$PATH:$HOME/geniustrader/Scripts"
fi

perl 相關 modules 安裝


  • Date::Manip (debian package: libdate-manip-perl)
  • Date::Calc (debian package: libdate-calc-perl)
  • Getopt::Long (debian package: libgetopt-long-descriptive-perl)
  • XML::LibXML (debian package: libxml-libxml-perl)
  • Compress::Zlib (debian package: wheezy/libcompress-zlib-perl)
  • Storable (standard in Perl 5.14, debian package for wheezy/perl 5.14: libstorable-perl)
  • GD (debian package: wheezy/libgd-gd2-perl)
  • Data::Dumper
  • HTML::Mason
  • Math::Trig
  • POSIX
  • Pod::Simple
  • Pod::Usage
  • Pod::Parser

其他 modules


modules (程式) 備註
File::Find
File::Spec
IO::Handle
Perl core 一部份
GD (backtest.pl) perl 圖形套件隨著 c gd 程式庫 libgd
DBI
DBD::Pg
DBD::mysql
DBD::SQLite
DBD::SQLite2
DBD::File
DBD::ODBC
若有用 SQL 後端儲存價格資料
Finance::BeanCounter 推薦的股票價格數據庫應用程序
Time::Local 使用 CSV (GT/DB/CSV.pm) 基礎價格資料
與 GT/DB/Text.pm text based prices data 不同
List::Util 用在 future max/min 方法
Test::More Test::Differences tool kit modules 執行測試
LWP 若有使用 GT/DB/HTTP.pm
XML::Simple 只在 backtest_multi.pl 用到, 其他 GT 應用程式使用 XML::LibXML
perldoc 不一定需要


測試

  • script 在 <geniustrader>/Scripts
  • RSI txf2012 (txf 201201 - 20120703)
    cd Scripts
    ./display_indicator.pl I:RSI txf2012 | less

結果

Calculating indicator RSI[14, {I:Prices CLOSE}] ...
RSI[14, {I:Prices CLOSE}][2012/01/31] = 79.7701
RSI[14, {I:Prices CLOSE}][2012/02/01] = 78.6667
RSI[14, {I:Prices CLOSE}][2012/02/02] = 80.8903
RSI[14, {I:Prices CLOSE}][2012/02/03] = 79.8857
RSI[14, {I:Prices CLOSE}][2012/02/04] = 84.7966
RSI[14, {I:Prices CLOSE}][2012/02/06] = 83.0189
RSI[14, {I:Prices CLOSE}][2012/02/07] = 81.4645
RSI[14, {I:Prices CLOSE}][2012/02/08] = 84.3478
RSI[14, {I:Prices CLOSE}][2012/02/09] = 84.7458
RSI[14, {I:Prices CLOSE}][2012/02/10] = 81.8926
RSI[14, {I:Prices CLOSE}][2012/02/13] = 88.1086
RSI[14, {I:Prices CLOSE}][2012/02/14] = 83.4540
RSI[14, {I:Prices CLOSE}][2012/02/15] = 87.0250
RSI[14, {I:Prices CLOSE}][2012/02/16] = 75.9036
RSI[14, {I:Prices CLOSE}][2012/02/17] = 74.6032
RSI[14, {I:Prices CLOSE}][2012/02/20] = 73.7418
RSI[14, {I:Prices CLOSE}][2012/02/21] = 68.9913
RSI[14, {I:Prices CLOSE}][2012/02/22] = 71.0465
RSI[14, {I:Prices CLOSE}][2012/02/23] = 64.1089
RSI[14, {I:Prices CLOSE}][2012/02/24] = 66.4103
RSI[14, {I:Prices CLOSE}][2012/02/29] = 71.3974
RSI[14, {I:Prices CLOSE}][2012/03/01] = 66.0622
RSI[14, {I:Prices CLOSE}][2012/03/02] = 65.2058
RSI[14, {I:Prices CLOSE}][2012/03/03] = 67.4451
RSI[14, {I:Prices CLOSE}][2012/03/05] = 58.6701
RSI[14, {I:Prices CLOSE}][2012/03/06] = 54.3478
RSI[14, {I:Prices CLOSE}][2012/03/07] = 42.5703
RSI[14, {I:Prices CLOSE}][2012/03/08] = 54.8077
RSI[14, {I:Prices CLOSE}][2012/03/09] = 54.9932
RSI[14, {I:Prices CLOSE}][2012/03/12] = 47.7876
RSI[14, {I:Prices CLOSE}][2012/03/13] = 54.8098
RSI[14, {I:Prices CLOSE}][2012/03/14] = 57.2939
RSI[14, {I:Prices CLOSE}][2012/03/15] = 59.3647
RSI[14, {I:Prices CLOSE}][2012/03/16] = 56.3996
RSI[14, {I:Prices CLOSE}][2012/03/19] = 46.1827
RSI[14, {I:Prices CLOSE}][2012/03/20] = 40.8768
RSI[14, {I:Prices CLOSE}][2012/03/21] = 39.6882
RSI[14, {I:Prices CLOSE}][2012/03/22] = 48.0132
RSI[14, {I:Prices CLOSE}][2012/03/23] = 52.4096
RSI[14, {I:Prices CLOSE}][2012/03/26] = 50.6993
RSI[14, {I:Prices CLOSE}][2012/03/27] = 56.6938
RSI[14, {I:Prices CLOSE}][2012/03/28] = 53.3835
RSI[14, {I:Prices CLOSE}][2012/03/29] = 41.6143
RSI[14, {I:Prices CLOSE}][2012/03/30] = 47.9075
RSI[14, {I:Prices CLOSE}][2012/04/02] = 39.7294
RSI[14, {I:Prices CLOSE}][2012/04/03] = 27.0886
RSI[14, {I:Prices CLOSE}][2012/04/05] = 22.6695
RSI[14, {I:Prices CLOSE}][2012/04/06] = 28.9559
RSI[14, {I:Prices CLOSE}][2012/04/09] = 26.5286
RSI[14, {I:Prices CLOSE}][2012/04/10] = 30.8086
RSI[14, {I:Prices CLOSE}][2012/04/11] = 33.1343
RSI[14, {I:Prices CLOSE}][2012/04/12] = 25.2759
RSI[14, {I:Prices CLOSE}][2012/04/13] = 34.1487
RSI[14, {I:Prices CLOSE}][2012/04/16] = 36.9703
RSI[14, {I:Prices CLOSE}][2012/04/17] = 29.3478
RSI[14, {I:Prices CLOSE}][2012/04/18] = 29.9020
RSI[14, {I:Prices CLOSE}][2012/04/19] = 36.2663
RSI[14, {I:Prices CLOSE}][2012/04/20] = 29.6009
RSI[14, {I:Prices CLOSE}][2012/04/23] = 28.9902
RSI[14, {I:Prices CLOSE}][2012/04/24] = 32.7014
RSI[14, {I:Prices CLOSE}][2012/04/25] = 44.6112
RSI[14, {I:Prices CLOSE}][2012/04/26] = 37.5691
RSI[14, {I:Prices CLOSE}][2012/04/27] = 40.2963
RSI[14, {I:Prices CLOSE}][2012/04/30] = 39.0318
RSI[14, {I:Prices CLOSE}][2012/05/02] = 52.1945
RSI[14, {I:Prices CLOSE}][2012/05/03] = 51.2224
RSI[14, {I:Prices CLOSE}][2012/05/04] = 43.6828
RSI[14, {I:Prices CLOSE}][2012/05/07] = 37.7030
RSI[14, {I:Prices CLOSE}][2012/05/08] = 45.4902
RSI[14, {I:Prices CLOSE}][2012/05/09] = 40.0000
RSI[14, {I:Prices CLOSE}][2012/05/10] = 41.9118
RSI[14, {I:Prices CLOSE}][2012/05/11] = 41.1058
RSI[14, {I:Prices CLOSE}][2012/05/14] = 43.1078
RSI[14, {I:Prices CLOSE}][2012/05/15] = 43.6025
RSI[14, {I:Prices CLOSE}][2012/05/16] = 33.9367
RSI[14, {I:Prices CLOSE}][2012/05/17] = 40.1557
RSI[14, {I:Prices CLOSE}][2012/05/18] = 34.8120
RSI[14, {I:Prices CLOSE}][2012/05/21] = 37.1747
RSI[14, {I:Prices CLOSE}][2012/05/22] = 25.7958
RSI[14, {I:Prices CLOSE}][2012/05/23] = 23.7374
RSI[14, {I:Prices CLOSE}][2012/05/24] = 23.2089
RSI[14, {I:Prices CLOSE}][2012/05/25] = 25.4425
RSI[14, {I:Prices CLOSE}][2012/05/28] = 28.3741
RSI[14, {I:Prices CLOSE}][2012/05/29] = 42.0304
RSI[14, {I:Prices CLOSE}][2012/05/30] = 38.6239
RSI[14, {I:Prices CLOSE}][2012/05/31] = 42.3968
RSI[14, {I:Prices CLOSE}][2012/06/01] = 37.8500
RSI[14, {I:Prices CLOSE}][2012/06/04] = 30.4151
RSI[14, {I:Prices CLOSE}][2012/06/05] = 39.1705
RSI[14, {I:Prices CLOSE}][2012/06/06] = 38.1733
RSI[14, {I:Prices CLOSE}][2012/06/07] = 46.1879
RSI[14, {I:Prices CLOSE}][2012/06/08] = 39.8968
RSI[14, {I:Prices CLOSE}][2012/06/11] = 44.2584
RSI[14, {I:Prices CLOSE}][2012/06/12] = 45.1954
RSI[14, {I:Prices CLOSE}][2012/06/13] = 46.7678
RSI[14, {I:Prices CLOSE}][2012/06/14] = 50.4603
RSI[14, {I:Prices CLOSE}][2012/06/15] = 51.7129
RSI[14, {I:Prices CLOSE}][2012/06/18] = 48.7889
RSI[14, {I:Prices CLOSE}][2012/06/19] = 51.4986
RSI[14, {I:Prices CLOSE}][2012/06/20] = 54.2147
RSI[14, {I:Prices CLOSE}][2012/06/21] = 50.6633
RSI[14, {I:Prices CLOSE}][2012/06/22] = 58.1905
RSI[14, {I:Prices CLOSE}][2012/06/25] = 48.7899
RSI[14, {I:Prices CLOSE}][2012/06/26] = 46.3074
RSI[14, {I:Prices CLOSE}][2012/06/27] = 47.0993
RSI[14, {I:Prices CLOSE}][2012/06/28] = 52.4520
RSI[14, {I:Prices CLOSE}][2012/06/29] = 51.1501
RSI[14, {I:Prices CLOSE}][2012/07/02] = 56.7442
RSI[14, {I:Prices CLOSE}][2012/07/03] = 59.6529
:
  • ./backtest.pl TFS 13000 | less
  • ./backtest.pl 'TFS[30,7,7]' 13000 | less

geniustrader 交易系統

geniustrader - perl 語言寫的 open source 的交易系統(可回測)

主頁 - http://geniustrader.org/

最近在寫新策略以及回測, 使用 perl 來找最佳化及整理資料, 真是方便太多了

----

2012年1月12日 星期四

2012年台期所期貨最後交易日檔

有用的人補一下吧, 2012(中華民國101年)台期所期貨最後交易日檔

2012-01-13

2012台期所期貨最後交易日檔(結算日)

資訊來源 - 期貨交易所 - 交易制度 - 台灣行事曆


20120118
20120215
20120321


20120418
20120516
20120620
20120718
20120815
20120919
20121017
20121121
20121219
20130119



--

2010年12月20日 星期一

2011年台期所期貨最後交易日檔(結算日)

2010-12-21

每年都要補的內容,最後交易日也就是結算日, 期交所每年定訂,在程式交易中會用到, 是得到當月期貨及換倉重要參考。

2011(中華民國100年)台期所期貨最後交易日檔(結算日)

資訊來源 - 期貨交易所 - 交易制度 - 台灣行事曆



20101117
20101215
20110119
20110216
20110316
20110420
20110518
20110615
20110720
20110817
20110921
20111019
20111116
20111221





-
--
-

2010年6月25日 星期五

AutoHotKey補一個月的轉檔主程式

這支程式是用AutoHotKey寫的,用來補前幾天(前30天)沒有做轉檔, 要配合
台指期轉檔程式 - part4(懶人包), 自動取得交易檔, 轉Tick及1分K線。

程式中用到他人的 SimpleArray.ahk 要放在同個資料夾(目錄)下。下載SimpleArray.zip, 下載後要解zip檔。

getTick.ahk程式碼


;
; AutoHotkey Version: 1.0.0
; Platform: Win XP
; Author: R.J
; Date : 2010/6/11
; Script Function:
; Get Tick
;
#Include SimpleArray.ahk

; 1 2 3 4 5 6 7 8 9 10 11 12
Month = 31|28|31|30|31|30|31|31|30|31|30|31

PerlExe = c:\perl\bin\perl.exe
plGetTick = c:\tick\prg_perl3w\gettick3w.pl
plProc3wTick = c:\tick\prg_perl3w\proc3w_tick.pl

ToDay = %A_YYYY%-%A_MM%-%A_DD%

if (%A_MM% == 1 ){
sStartMonth := 12 ; 前月
} else {
sStartMonth := A_MM + 99
sStartMonth := SubStr(sStartMonth, 2, 2) ; 前月
}

sStartDay = %A_YYYY%-%sStartMonth%-%A_DD%
sStopDay = %A_YYYY%-%A_MM%-%A_DD%


Gui, Font, S12,
Gui, Add, Text, x10 y25 w70 h20 , Start Date :
Gui, Add, Text, x10 y85 w70 h20 , Stop Date :
Gui, Add, Edit, x90 y23 w90 h20 r1 vStartEdit, %sStartDay%
Gui, Add, Edit, x90 y83 w90 h20 r1 vStopEdit, %sStopDay%
Gui, Add, Button, x220 y18 w46 h29 , Start
Gui, Show, x300 y150 h250 w320, Get Future Daily-Trade from TAIFEX
Return

GuiClose:
ExitApp


ButtonStart:
Gui, Submit
stopDate := StopEdit

stYYYY := SubStr(StartEdit, 1, 4)
stMM := SubStr(StartEdit, 6, 2)
stDD := SubStr(StartEdit, 9, 2)

WorkDate = %stYYYY%-%stMM%-%stDD%

; While (stopDate >= WorkDate) {
While (WorkDate <= stopDate) {
if (stDD > SA_Get(Month, stMM)) {
; 換月
stDD -= SA_Get(Month, stMM)
if (stDD < 10) {
stDD += 100
stDD := SubStr(stDD, 2, 2)
}
stMM += 1
if (stMM > 12) {
stMM := 01
stYYYY += 1
} else if (stMM < 10) {
stMM += 100
stMM := SubStr(stMM, 2, 2)
}
}
WorkDate = %stYYYY%-%stMM%-%stDD%

RunWait, %comspec% /c date %WorkDate%, , min
sleep, 1000

if (A_WDay >= 2 and A_WDay <= 6) {
RunWait, %PerlExe% %plGetTick%
RunWait, %PerlExe% %plProc3wTick%
}

stDD += 1
if (stDD < 10) {
stDD += 100
stDD := SubStr(stDD, 2, 2)
}
WorkDate = %stYYYY%-%stMM%-%stDD%
}

RunWait, %comspec% /c date %ToDay%, , min ;; Reset Today DateTime
Return


-

2010年6月17日 星期四

2010年台期所期貨最後交易日檔

2010-06-18

2010台期所期貨最後交易日檔


20100120
20100217
20100317
20100421
20100519
20100616
20100721
20100818
20100915
20101020
20101117
20101215


參考 - 台灣期貨交易所 - 行事曆

-
--
-

2010年5月17日 星期一

perl程式 - 取出今天K線匯入TS

目的:為了即時補上斷線後的資料, 再匯入策略軟體(TS), 將看盤軟體匯出的一分K線, 只取出今天的K線, 且去除不完整的最後一筆(時間沒到)。

輸入 - C:\tmp\1.csv
輸出 - C:\tickhist\txf.csv

再配合 autoit 或 AutoHotKey , 就可以按一下自動補上今日K線資料

today1k.pl 程式


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

# 取出今天的K線, 去除最後一筆

# 輸入一分K線檔 - C:\tmp\1.csv
# 輸出一分K線檔 - C:\tickhist\txf.csv

use POSIX qw(strftime);
use integer;

$src_k1m = 'C:\\tmp\\1.csv'; # 1mk source file
$tmpfile = 'C:\\tickhist\\tmp001.csv'; # tmp file
$tar_k1m = 'C:\\tickhist\\txf.csv'; # 1mk target file

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

print "Process $today Today 1 Minute K To : $tar_k1m.\n";


open(infile, $src_k1m) || die("Cannot open $src_k1m.\n");
open(tempfile, ">$tmpfile");

$rec_no=0;
;

while(my $k1m_record = )
{
chomp $k1m_record;
# $k1m_record =~ s/ //g;
# ($kDate, $kTime, $kOpen, $kHigh, $kLow, $kClose, $kVol)=split(/, / , $k1m_record);
($kDateTime, $kOpen, $kHigh, $kLow, $kClose, $kVol)=split(/,/ , $k1m_record);
($kDate, $kTime)=split(/ / , $kDateTime);

if ($kDate eq $today ){
print tempfile "$kDate,$kTime,$kOpen,$kHigh,$kLow,$kClose,$kVol\n";
$rec_no = $rec_no + 1;
}
}

close(tempfile);
close(infile);

open(tmp_file, $tmpfile) || die("Cannot open $tmpfile.\n");
open(outfile, ">$tar_k1m");

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

for ( $i=1; $i<$rec_no; $i++ ) {
my $tmp_record = ;
print outfile $tmp_record;

# print "$i\n";
}

close(tmp_file);
close(outfile);

# 刪除暫存檔
unlink $tmpfile;



註:
1.其時用附屬工具 grep 來做會更精簡。
2.看盤軟體"康和全都賺"匯出一分K, 會有將近三個月的K線資料。

---

2010年4月27日 星期二

台指期轉檔程式 - part4(懶人包)

因應 JameX 網友所提的"懶人包",

1. tick.zip
2. tool.zip
3. CRONw_crontab.zip

為配合TS的匯入, 這次改了一分K線的檔案名稱, 及輸出目錄, 並刪除抬頭

安裝說明, 參照 台指期轉檔程式 - part4

---

2010年4月20日 星期二

台指期轉檔程式 - part4

因為我比較懶, 用K線轉檔機還要按按鈕, 所以用 CRONw 排程, 自行到期交所抓當日的成交檔, 分類 tick, 再轉K線...

最近在 win XP sp3 下安裝
  • perl - ActivePerl-5.8.2.808-MSWin32-x86.msi
  • CRONw - CRONw_2.0.zip
  • tool - wget.exe unzip.exe
  • perl 轉檔程式
之前的操作忘了, 這次記錄一下。



1. perl
ActivePerl-5.8.2.808-MSWin32-x86.msi
2. CRONw
CRONw_2.0.zip
a.unzip CRONw_2.0.zip
b. CRONw -> C:\CRONw
3. tool.zip
a.unzip put at c:\tool\
b.file
1. c:\tool\wget.exe
2. c:\tool\unzip.exe
3. c:\tool\unzip32.dll
4. tick.zip (perl script)
a. unzip put at c:\tick
b.file
1. c:\tick\prg_perl3w\gettick3w.bat
2. c:\tick\prg_perl3w\proc3w_tick.bat
3. c:\tick\prg_perl3w\gettick3w.pl
4. c:\tick\prg_perl3w\proc3w_tick.pl
5. c:\tick\prg_perl3w\last-trading-days
6. c:\tick\prg_perl3w\movefile2dir.pl
(其他-手動補之前的K線)
7. c:\tick\ext\make_kline
8. c:\tick\ext\win_crontab.txt
9. c:\tick\ext\user_proc3w_tick.ba
10. c:\tick\ext\tick2kline.pl
c. 排程時間 copy c:\tick\ext\win_crontab.txt to c:\CRONw\crontab.txt
(add crontab rule)
27 15 * * 1-5 c:\perl\bin\perl.exe C:\tick\prg_perl3w\gettick3w.pl
29 15 * * 1-5 c:\perl\bin\perl.exe C:\tick\prg_perl3w\proc3w_tick.pl

5. install CRONW modules -> read \INSTALL.txt
a.
installer.pl

6. install CRONw to WinXP Service
a. 安裝服務 - perl cronHelper.pl --install

b. 起動服務 - net start CRON
c. 檢查 -> Cron Service (CRONw) -
net start

時間定在 15:27 以後, 是確保當時可以抓得到今日的成交檔


相關 -  台指期轉檔程式 - part4(懶人包)
 

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年6月19日 星期五

Win XP 下安裝自動排程 CRONw 2.0

Win XP 下 安裝 自動排程 CRONw 2.0。

這個 CRONw 是用 perl 寫的自動排程工具, 先要裝 perl, 為了安裝方便, 請參考 CRONw 的安裝 這篇來安裝 ActivePerl 及 CRONw 2.0 。

這樣就可以使用 CRONw 這個自動排程工具, 下篇再寫自動排程 CRONw 的使用設定。

我的 Wiki -- 2009-06-20 cronw 安裝 (包括ActivePerl)



聯絡表單

名稱

以電子郵件傳送 *

訊息 *