์ค๋ช
- Bash ์ ธ ์คํฌ๋ฆฝํธ๋ก, KRX์์ ์ ๊ณตํ๋ CSV ํ์์ ์ข ๋ชฉ ๋ฆฌ์คํธ๋ฅผ ์์ค๋ก ํ์ฌ, ์ต๊ทผ 3๋ ๊ฐ์ 1์ผ๋ด ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ค๋ฐ๋๋ค.
- http://data.krx.co.kr/contents/MDC/MDI/mdiLoader/index.cmd?menuId=MDC0201020101 stock_list.csv ํ์ผ์, ์ฌ๊ธฐ์์ ๋ด๋ ค๋ฐ๋ ํ์ผ์ ์์ ์์ด ์ด๋ฆ๋ง ๋ฐ๊ฟ ์ฌ์ฉํ๋ค.
- ํ ๋ ๊ทธ๋จ ๋ด์ ์ฌ์ฉํ์ฌ ๋ฉ์์ง๋ฅผ ์ด ์ฃผ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋, ํ ํฐ์ ์ ์ ํ ์ ๋ ฅํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
- ์ด๊ฑฐ ์ผ๋ด ๊ธฐ์ค์ผ๋ก ์์ด 100์ต ์ด์์ธ ๊ฒ๋ค ๋ค์ด๋ก๋ ๋ฐ๋๋ก ๋๋ฆฌ๋ฉด ๊ฑฐ์ 2600๊ฐ ๋๋ ์ข ๋ชฉ์ ํ์ผ์ ๊ณ์ ๋ด๋ ค๋ฐ๋๋ฐ, ๋๋์ก์ ํ ์๊ฐ์ ๋ ๊ฑธ๋ฆฐ๋ค. `nohup downloader.sh &` ๋ช ๋ น ์ฌ์ฉํด์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๋๋ ค๋๊ณ ๋ด ์ง ํ์.
#!/bin/bash
# telegram tokens setting
bot_token="Telegram bot token ID"
chat_id="Telegram chat ID"
# timestamps
start_date_readable=$(date -d "3 years ago" +"%m/%d/%Y")
end_date_readable=$(date +"%m/%d/%Y")
start_date=$(date --date="$start_date_readable 00:00:00" +"%s")
end_date=$(date --date="$end_date_readable 23:55:59" +"%s")
# the interval must be one of 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo
interval="1d"
# no / at the end!
data_dir="The directory where the stock price data will be stored"
# message="Starts%20at:%20$start_date_readable%0AEnds%20at:%20$end_date_readable"
# curl -s "https://api.telegram.org/$bot_token/sendmessage?chat_id=$chat_id&text=$message"
i=0
while IFS=, read -r code name exchange category close delta delta_percentage open high low volume volume_price marketcap stock_quantity; do
((i++))
# skip the header
if [[ ! "$exchange" == "\"KOSPI\"" && ! "$exchange" == "\"KOSDAQ\"" && ! "$exchange" == "\"KOSDAQ GLOBAL\"" && ! "$exchange" == "\"KONEX\"" ]]; then
continue
fi
# Market cap filter
# it filters stocks with market cap under 10B KRW or in KONEX
# if [ ${marketcap//\"/} -lt 30000000000000 ] || [ "$exchange" = "\"KONEX\"" ]; then
if [ ${marketcap//\"/} -lt 10000000000 ] | [ "$exchange" = "\"KONEX\"" ]; then
continue
fi
# KOSPI
if [ "$exchange" = "\"KOSPI\"" ]; then
exc="KS"
# KOSDAQ or KOSDAQ GLOBAL
else
exc="KQ"
fi
code=${code//\"/}
url="https://query1.finance.yahoo.com/v7/finance/download/$code.$exc?period1=$start_date&period2=$end_date&interval=$interval&events=history&includeAdjustedClose=true"
echo "$i ${name//\"/} [$code]" | iconv -f EUC-KR --t UTF-8
start=$(date +%s.%N)
curl -s "$url" --output - | iconv -f UTF-8 -t EUC-KR > "$data_dir/$code.csv"
end=$(date +%s.%N)
elapsed=$(echo "$end - $start" | bc)
#Yahoo finance allows only one api call every 1 second.
if (( $(echo "$elapsed < 1" | bc -l) )); then
sleep $(echo "1.1 - $elapsed" | bc)
fi
done < "stock_list.csv"
# Deletes some useless files. comment this if you want to check out what they look like.
find $data_dir -type f -size -200c -delete
# Displays the number of stock files available
#number_of_stock=$(ls -1 $data_dir | wc -l)
# message="$number_of_stock%20files%20have%20been%20downloaded."
# curl -s "https://api.telegram.org/$bot_token/sendmessage?chat_id=$chat_id&text=$message"
if [ $number_of_stock -eq 0 ]; then
exit 0
fi
# any consequent files here
# python3 main.py
# Delete the downloaded files if you want.
# rm -rf $data_dir/*
# message="Done!"
# curl -s "https://api.telegram.org/$bot_token/sendmessage?chat_id=$chat_id&text=$message"