๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ› ๏ธcode/์ฃผ์‹

[์ฃผ์‹] Yahoo finance์—์„œ ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ๋‚ด๋ ค๋ฐ›๋Š” Bash ์Šคํฌ๋ฆฝํŠธ

์„ค๋ช…

  • 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"