這部份是在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
沒有留言:
張貼留言