Files
dabo/dabo/functions/get_marketdata.sh
2024-07-27 17:44:29 +02:00

88 lines
4.1 KiB
Bash

function get_marketdata {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
get_marketdata_from_url https://www.investing.com/economic-calendar/unemployment-rate-300/ US-UNEMPLOYMENT-INDEX
get_marketdata_from_url https://www.investing.com/economic-calendar/cpi-733 US-CONSUMER-PRICE-INDEX
get_marketdata_from_url https://www.investing.com/indices/fed-funds-composite-interest-rate-opinion US-FED-FEDERAL-FUNDS-RATE-INVERTED-INDEX
get_marketdata_from_url '"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=BTCUSDT&period=5m" | jq -r .[].longShortRatio | tail -n1' BINANCE-BTC-GlobalLongShortAccountRatio
get_marketdata_from_url '"https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=BTCUSDT&period=5m" | jq -r .[].buySellRatio | tail -n1' BINANCE-BTC-TakerLongShortRatio
get_marketdata_from_url '"https://fapi.binance.com/futures/data/openInterestHist?symbol=BTCUSDT&period=5m" | jq -r .[].sumOpenInterest | tail -n1' BINANCE-BTC-OpenInterest
get_marketdata_from_url '"https://api.alternative.me/fng/?limit=1&format=json" | jq -r .data[].value' CRYPTO_FEAR_AND_GREED-INDEX
# clear old stuff
find asset-histories/*INDEX* -type f -mtime +6 -delete
}
function get_marketdata_from_url {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_url="$1"
local f_name="$2"
## get data for analysis
if find asset-histories/${f_name}.history.csv -mmin -${INTERVAL_MIN} 2>/dev/null | grep -q "asset-histories/${f_name}.history.csv"
then
g_echo_note "asset-histories/${f_name}.history.csv already downloaded in the last ${INTERVAL_MIN} minutes"
f_get_marketdata_price=$(cat MARKET_DATA_CMD_OUT-${f_name})
return 0
fi
# check source platform for parsing parameters, prepare and run wget command
>MARKET_DATA_CMD
if echo "${f_url}" | grep -q "boerse.de"
then
echo "wget ${g_wget_opts} -q -O - ${f_url} | egrep 'itemprop=\"price\" content=\"[0-9]+\.[0-9]+\"' | sed s'#\"#\n#g' | egrep '^[0-9]+\.[0-9]+'" >MARKET_DATA_CMD
elif echo "${f_url}" | egrep -q "investing.com.+economic-calendar"
then
echo "wget ${g_wget_opts} -q -O - ${f_url} | egrep 'Actual.+Forecast.+Previous' | cut -d'>' -f7,11 | cut -d'<' -f1,2 | sed 's#,##g' | sed 's/\%//g' | sed 's#</div>#,#' | grep '[0-9]' | cut -d, -f1" >MARKET_DATA_CMD
elif echo "${f_url}" | egrep -q "investing.com.+indices"
then
echo "wget ${g_wget_opts} -q -O - ${f_url} | sed 's#</div>#\n#g' | grep 'text-5xl.*font-bold.*md:text-' | sed 's#^.*>##; s#,##g' | grep '[0-9]'" >MARKET_DATA_CMD
elif echo "${f_url}" | egrep -q '^"https://'
then
echo "wget -q -O - ${g_wget_opts} ${f_url}" >MARKET_DATA_CMD
else
# default to Yahoo Finace Symbols via API
echo "wget ${g_wget_opts} -q -O - https://query1.finance.yahoo.com/v8/finance/chart/${f_url} | jq -r '.[].result[].meta.regularMarketPrice'" >MARKET_DATA_CMD
fi
g_runcmd g_retrycmd sh MARKET_DATA_CMD >MARKET_DATA_CMD_OUT-${f_name}.tmp 2>MARKET_DATA_CMD_OUT-${f_name}.tmp.err
# check output
local f_get_marketdata_price_tmp=$(cat MARKET_DATA_CMD_OUT-${f_name}.tmp)
if g_num_valid_number ${f_get_marketdata_price_tmp}
then
if egrep -q "^0\.00" MARKET_DATA_CMD_OUT-${f_name}.tmp
then
g_echo_note "Ignoring ${f_name} $(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp) - maybe out of business day"
else
g_echo_note "${f_name}: ${f_get_marketdata_price_tmp}"
mv MARKET_DATA_CMD_OUT-${f_name}.tmp MARKET_DATA_CMD_OUT-${f_name}
fi
else
g_echo_warn "MARKET_DATA_CMD_OUT-${f_name}.tmp has wrong Syntax. - Not updating ${f_name} Index.
CMD:
$(tail -n 10 MARKET_DATA_CMD)
Output:
$(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp | cat -t)
Error:
$(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp.err | cat -t)"
fi
if ! [ -e "MARKET_DATA_CMD_OUT-${f_name}" ]
then
local f_old_value=$(tail -n 1 asset-histories/${f_name}.history.csv | cut -d, -f2)
if echo ${f_old_value} | egrep -q "^[0-9]*\.[0-9]+"
then
echo ${f_old_value} >MARKET_DATA_CMD_OUT-${f_name}
else
echo 0 >MARKET_DATA_CMD_OUT-${f_name}
fi
fi
f_get_marketdata_price=$(cat MARKET_DATA_CMD_OUT-${f_name})
echo "${f_timestamp},${f_get_marketdata_price}" >>asset-histories/${f_name}.history.csv
}