1. 只選股不擇時
2. 以20支股票形成投資組合
3. 每個月交易一次為最佳(迅速反應,耐心持有,定期換股,留強汰弱)
#!/bin/bash
getColumnByName(){
csv_file="$1"
column_name="$2"
column_number=$(awk -F ',' 'NR==1 { for(i=1; i<=NF; i++) { if($i == column_name) { print i; break } } }' column_name="$column_name" "$csv_file")
echo $column_number
}
YMD=$(date +%Y%m%d)
day_of_week=$(date +%u)
if [ "$day_of_week" -eq 6 ] || [ "$day_of_week" -eq 7 ]; then
current_timestamp=$(date +%s)
days_to_friday=$(( day_of_week % 5 ))
friday_timestamp=$((current_timestamp - (days_to_friday * 24 * 3600)))
YSTDAY=$(date -d "@$friday_timestamp" "+%Y%m%d")
else
YSTDAY=$(date +%Y%m%d)
fi
echo $YSTDAY
FILE_INCOME="income_${YMD}.csv"
FILE_BS="bs_${YMD}.csv"
FILE_PRICE="price_${YMD}.csv"
FILE_MERGE="merge_${YMD}.csv"
FILE_RAW="raw_${YMD}.csv"
FILE_TMP="temp_${YMD}.tmp"
YYY=$(( $(date +%Y) - 1911 ))
SS="0$(( ($(date +%_m) - 1 ) / 3 ))"
if [ $SS -eq 0 ] ; then
YYY=$(( YYY - 1 ))
SS=04
fi
curl 'https://mops.twse.com.tw/mops/web/ajax_t163sb04' \
-H 'Accept: */*' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: jcsession=jHttpSession@199002cc; _ga=GA1.3.1478531031.1681117556; _gid=GA1.3.1147883650.1684137366; _gat=1' \
-H 'Origin: https://mops.twse.com.tw' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw "encodeURIComponent=1&step=1&firstin=1&off=1&isQuery=Y&TYPEK=sii&year=${YYY}&season=${SS}" \
--compressed -o $FILE_TMP
grep -o "t163sb04_${YMD}_..........csv" $FILE_TMP | sort -u | while read -r CSV ; do
curl 'https://mops.twse.com.tw/server-java/t105sb02' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Cache-Control: max-age=0' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: _ga=GA1.3.1478531031.1681117556; _gid=GA1.3.1147883650.1684137366' \
-H 'Origin: null' \
-H 'Sec-Fetch-Dest: document' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-User: ?1' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw "firstin=true&step=10&filename=${CSV}&saveCSV=Y" \
--compressed -o income.${CSV}
done
FILE_X=$(ls -l income.t163sb04_${YMD}_*.csv | awk '{print $5, $9}' | sort -nr | head -n 1 | awk '{print $2}')
cp $FILE_X $FILE_INCOME
curl 'https://mops.twse.com.tw/mops/web/ajax_t163sb05' \
-H 'Accept: */*' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: jcsession=jHttpSession@279737b5; _ga=GA1.3.1478531031.1681117556; _gid=GA1.3.1147883650.1684137366; _gat=1' \
-H 'Origin: https://mops.twse.com.tw' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw "encodeURIComponent=1&step=1&firstin=1&off=1&isQuery=Y&TYPEK=sii&year=${YYY}&season=${SS}" \
--compressed -o $FILE_TMP
grep -o "t163sb04_${YMD}_..........csv" $FILE_TMP | sort -u | while read -r CSV ; do
curl 'https://mops.twse.com.tw/server-java/t105sb02' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Cache-Control: max-age=0' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: _ga=GA1.3.1478531031.1681117556; _gid=GA1.3.1147883650.1684137366; _gat=1' \
-H 'Origin: null' \
-H 'Sec-Fetch-Dest: document' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-User: ?1' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--data-raw "firstin=true&step=10&filename=$CSV" \
--compressed -o bs.${CSV}
done
FILE_X=$(ls -l bs.t163sb04_${YMD}_*.csv | awk '{print $5, $9}' | sort -nr | head -n 1 | awk '{print $2}')
cp $FILE_X $FILE_BS
curl "https://www.twse.com.tw/rwd/zh/afterTrading/MI_INDEX?date=${YSTDAY}&type=ALLBUT0999&response=csv" \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H 'Connection: keep-alive' \
-H 'Referer: https://www.twse.com.tw/zh/trading/historical/mi-index.html' \
-H 'Sec-Fetch-Dest: document' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-User: ?1' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
--compressed -o ${FILE_PRICE}
tr -d '\r' < ${FILE_PRICE} > $FILE_TMP
iconv -f big5 -t utf-8 $FILE_TMP | sed 's/\"證券代號\"/公司代號/g' | sort > $FILE_PRICE
join -t ',' -1 4 -2 4 $FILE_INCOME $FILE_BS | iconv -f big5 -t utf-8 | sort > $FILE_MERGE
join -t ',' -1 1 -2 1 $FILE_MERGE $FILE_PRICE > $FILE_RAW
sed -i '1h;1d;$!H;$!d;G' $FILE_RAW
sed -i '1s/\"//g' $FILE_RAW
tr -d '\r' < ${FILE_RAW} > $FILE_TMP; mv $FILE_TMP $FILE_RAW
C1=$(getColumnByName "$FILE_RAW" "每股參考淨值")
C2=$(getColumnByName "$FILE_RAW" "收盤價")
C3=$(getColumnByName "$FILE_RAW" "本期綜合損益總額")
C4=$(getColumnByName "$FILE_RAW" "權益總計")
C5=$(getColumnByName "$FILE_RAW" "基本每股盈餘(元)")
C6=$(getColumnByName "$FILE_RAW" "公司名稱")
FILE_GVI=gvi_${YMD}.csv
echo "每股參考淨值 收盤價 本期綜合損益總額 權益總計 基本每股盈餘(元)"
echo "$C1 $C2 $C3 $C4 $C5"
awk -F'","' -v C1="$C1" -v C2="$C2" -v C3="$C3" -v C4="$C4" -v C5="$C5" -v C6="$C6" '{print $1,$C1,$C2,$C3,$C4,$C5,$C6 }' $FILE_RAW | sed 's/^\"//' > $FILE_TMP
tail -n +2 $FILE_TMP | awk '{gvi=($3/$2) * ((1+4*$6/$2)^5) ; print $1,gvi,$7}' | sort -r -n -k 2 > $FILE_GVI
rm -f $FILE_TMP
rm -f income.*.csv bs.*.csv