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

--

2016年3月2日 星期三

台灣期交所下載資料位置變更

台灣期交所下載資料位置變更(105-02-26)

2016-03-03

台灣期交所位置: 首頁 > 交易資訊 > 盤後資訊 > 期貨 > 前30個交易日期貨每筆成交資料

前30個交易日期貨每筆成交資料, 增加了 .csv 下載

以前的最近30天成交記錄 rpt 下載 ( daily_yyyy_mm_dd.zip ,  其中 yyyy 為西元年, mm 為月份, dd 為日期)

原位置 : http://www.taifex.com.tw/DailyDownload/
新位置 : http://www.taifex.com.tw/DailyDownload/DailyDownload/

程式中有用到這個位置的要改...

記錄一下..

----

2015年12月2日 星期三

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

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

2015-12-03

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

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

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

--

2015年10月22日 星期四



2015-04-17

訂可以完成的目標,別做不切實際的夢。



----

2015年10月16日 星期五

HSI(恆生指數)期貨數據下載

HSI(恆生指數)期貨數據下載

香港交易所: 主頁 > 統計數據及研究 > 衍生產品市場統計數據 > 每日交易及未平倉合約摘要(期貨及指數期權)



位置 : http://www.hkex.com.hk/eng/stat/dmstat/OI/oi_f.asp

wget  http://www.hkex.com.hk/eng/stat/dmstat/OI/DTOP_F_yyyymmdd.zip

今天(2015-10-16)的 yyyymmdd 是 20151016

下載 :
http://www.hkex.com.hk/eng/stat/dmstat/OI/DTOP_F_20151016.zip

記錄一下, 改天排程加上..

----

2015年6月16日 星期二

選股-TAURAS公式(財富守護星)

TAURAS-選股公式(財富守護星)

2015-04-21
coco-in - http://www.coco-in.net/thread-39960-1-1.html


財富守護星選股公式百科
K線型態
長紅K棒 @RK()&($RBDY/$BDY>0.5)&($HIGH/$LOW>1.03)
長黑K棒 @BK()&($RBDY/$BDY>0.5)&($LOW/$HIGH>1.03)
流星線 ($USDW/$BDY>0.66)&($BDY>$CLOSE*0.03)
倒T字 ($LSDW/$BDY>0.66)&($BDY>$CLOSE*0.03)
十字線 ($USDW/$BDY>0.33)&($LSDW/$BDY>0.33)&($BDY>$CLOSE*0.03)
爆量長紅K棒 @RK()&($RBDY/$BDY>0.5)&($HIGH/$LOW>1.03)&($AMOUNT>$QMA1*2)
爆量長黑K棒 @BK()&($RBDY/$BDY>0.5)&($LOW/$HIGH>1.03)&($AMOUNT>$QMA1*2)
爆量 ($AMOUNT>$QMA1*2)
放量 @MAX(-4,4,AMOUNT)*3“>$AMOUNT>=@MAX(-4,4,AMOUNT)*3
KD指標
日KD多方訊號
KD黃金交叉 ($K>$D)&($K[-1]<$D[-1])&($K>20)
高檔鈍化 @MIN(-2,3,K)>=80
KD多方排列 ($K>$K[-1])&($K>$D)
周KD多方訊號
KD黃金交叉 ($K[W:0]>$D[W:0])&($K[W:-1]<$D[W:-1])&($K[W:0]>20)
高檔鈍化 @MIN(W:-2,3,K)>=80
KD多方排列 ($K[W:0]>$K[W:-1])&($K[W:0]>$D[W:0])
月KD多方訊號
月KD黃金交叉 ($K[M:0]>$D[M:0])&($K[M:-1]<$D[M:-1])&($K[M:0]>20)
高檔鈍化 @MIN(M:-2,3,K)>=80
KD多方排列 ($K[M:0]>$K[M:-1])&($K[M:0]>$D[M:0])
日KD空方訊號
KD死亡交叉 ($K<$D)&($K[-1]>$D[-1])&($K<80)
低檔鈍化 @MAX(-2,3,K)<=20
KD空方排列 ($K<$K[-1])&($K<$D)
周KD空方訊號
周KD死亡交叉 ($K[W:0]<$D[W:0])&($K[-1]>$D[-1])&($K[-1]<80)
低檔鈍化 @MAX(W:-2,3,K)<=20
KD空方排列 ($K[W:0]<$K[W:-1])&($K[W:0]<$D[W:0])
月KD空方訊號
月KD死亡交叉 ($K[M:0]<$D[M:0])&($K[M:-1]>$D[M:-1])&($K[M:-1] < 80 )
低檔鈍化 @MAX(M:-2,3,K)<=20
KD空方排列 ($K[M:0]<$K[M:-1])&($K[M:0]<$D[M:0])
RSI指標
日RSI多方訊號
RSI黃金交叉 ($RSI1>$RSI2)&($RSI1[-1]<$RSI2[-1])&($RSI1>20)
周RSI多方訊號
RSI黃金交叉 ($RSI1[W:0]>$RSI2[W:0])&($RSI1[W:-1]<$RSI2[W:-1])&($RSI1[W:0]>20)
月RSI多方訊號
RSI黃金交叉 ($RSI1[M:0]>$RSI2[M:0])&($RSI1[M:-1]<$RSI2[M:-1])&($RSI1[M:0]>20)
日RSI空方訊號
RSI死亡交叉 ($RSI1<$RSI2)&($RSI1[-1]>$RSI2[-1])&($RSI1 < 80)
周RSI空方訊號
RSI死亡交叉 ($RSI1[W:0]<$RSI2[W:0])&($RSI1[W:-1]>$RSI2[W:-1])&($RSI1[W:0] < 80 )
月RSI空方訊號
RSI死亡交叉 ($RSI1[M:0]<$RSI2[M:0])&($RSI1[M:-1]<$RSI2[M:-1])&($RSI1[M:0] < 80)
築底築頭型態
短期築底(6~17) @BOT(0,17,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
中期築底(18~53) @BOT(0,53,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
長期築底(54~161) @BOT(0,161,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
超長期大底(162~485) @BOT(0,485,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
*短期築頭(6~17) @TOP(0,17,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
*中期築頭(18~53) @TOP(0,53,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
*長期築頭(54~161) @TOP(0,161,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
*超長期大頭(162~485) @TOP(0,485,0.07,HIGH,LOW)*($CLOSE>8&$AMOUNT>100)
均線
多頭排列 ($MA1>$MA2)&($MA2>$MA3)&($MA1[-1]>$MA2[-1])&($MA2[-1]>$MA3[-1])
空頭排列 ($MA1<$MA2)&($MA2<$MA3)&($MA1[-1]<$MA2[-1])&($MA2[-1]<$MA3[-1])
均線糾結3%以內 ((@MAX(MA1,@MAX(MA2,MA3))-@MIN(MA1,@MIN(MA2,MA3)))/$CLOSE)<=0.03
距離月線3%以內 @ABS($CLOSE-$MA3)/$CLOSE<=0.03
距離季線3%以內 @ABS($CLOSE-$MA4)/$CLOSE<=0.03
距離年線3%以內 @ABS($CLOSE-$MA6)/$CLOSE<=0.03

---

聯絡表單

名稱

以電子郵件傳送 *

訊息 *