dabo/functions/get_assets.sh

151 lines
6.1 KiB
Bash
Raw Normal View History

2023-04-28 17:09:15 +02:00
function get_assets {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
## determine assets with prices
local f_filename="EXCHANGE_GET_ASSETS_CMD"
# Try to collect assets
# get data from API
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
then
binance-api-call GET /api/v3/ticker/price || return 1
# parse API output
cat ${g_tmp}/API_CMD_OUT | jq -r '.[] | .symbol + "," + .price' | grep "${CURRENCY}," | egrep -v "${TRANSFER_CURRENCY},|,0[\.][0]*$" >${f_filename}_OUT.tmp
fi
# timestamp for data
f_timestamp=$(g_date_print)
# check output
if [ -s ${f_filename}_OUT.tmp ] && egrep -q "^[A-Z]+${CURRENCY},[0-9]*\.[0-9]+$" ${f_filename}_OUT.tmp
then
mv ${f_filename}_OUT.tmp ${f_filename}_OUT
else
g_echo_warn "${f_filename}_OUT.tmp has wrong Syntax. - Not updating ${f_filename}_OUT $(tail -n 10 ${f_filename}_OUT.tmp)"
return 2
fi
## write assets list and filter by marketcap and sort by price
# get krypto symbol list sorted by marketcap
echo "curl -s -X 'GET' \"https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false&price_change_percentage=1h,24h,7d,14d,30d\" -H 'accept: application/json'" >COINGECKO_GET_ASSETS_CMD
g_runcmd g_retrycmd sh COINGECKO_GET_ASSETS_CMD >COINGECKO_GET_ASSETS_CMD_OUT || return 1
#ASSET_MARKETCAP_OUT.tmp || return 1
if [ -s COINGECKO_GET_ASSETS_CMD_OUT ] && grep -q "market_cap_rank" COINGECKO_GET_ASSETS_CMD_OUT
then
# get marketcap sort
jq -r '.[].symbol' COINGECKO_GET_ASSETS_CMD_OUT | tr [:lower:] [:upper:] | head -n $LARGEST_MARKETCAP >ASSET_MARKETCAP_OUT.tmp-sort
if [ -s ASSET_MARKETCAP_OUT.tmp-sort ] && egrep -q "^[A-Z0-9]+$" ASSET_MARKETCAP_OUT.tmp-sort
then
mv ASSET_MARKETCAP_OUT.tmp-sort SORTMARKETCAP
else
g_echo_warn "ASSET_MARKETCAP_OUT.tmp-sort has wrong Syntax. - Not updating ASSET_MARKETCAP_OUT.tmp-sort $(tail -n 10 ASSET_MARKETCAP_OUT.tmp-sort)"
return 2
fi
# write down 24h pricechange
cat COINGECKO_GET_ASSETS_CMD_OUT | jq -r '.[] | .symbol + "," + (.price_change_percentage_24h|tostring)' | tr [:lower:] [:upper:] >ASSET_PRICE_CHANGE_PERCENTAGE_24H
# store coingecko symbolids for coingecko info URLs
jq -r '.[] | .symbol + "," + .id' COINGECKO_GET_ASSETS_CMD_OUT >COINGECKO_IDS.tmp
if [ -s COINGECKO_IDS.tmp ]
then
mv COINGECKO_IDS.tmp COINGECKO_IDS
else
g_echo_warn "COINGECKO_IDS.tmp has wrong Syntax. - Not updating COINGECKO_IDS $(tail -n 10 COINGECKO_IDS.tmp)"
return 2
fi
fi
# Write file with asset list and ignore marketcap under LARGEST_MARKETCAP
cat EXCHANGE_GET_ASSETS_CMD_OUT | cut -d"," -f1 | while read f_ASSET
do
# ignore marketcap under LARGEST_MARKETCAP
f_assetwocurrency=$(echo ${f_ASSET} | sed "s/$CURRENCY$//")
if ! egrep -q "^${f_assetwocurrency}$" SORTMARKETCAP
then
#g_echo_note "Ignoring $f_ASSET because of lower marketcap then Top ${LARGEST_MARKETCAP}"
continue
fi
echo ${f_ASSET} >>ASSETS.tmp
done
mv ASSETS.tmp ASSETS
## write histfiles by having a look on marketcap
#>ASSETS_HIGHEST.tmp
# get alread invested assets
#f_investedassets_regex=$(cat EXCHANGE_GET_BALANCES_CMD_OUT | cut -d, -f1 | perl -pe 's/^/\^/; s/\n/\|/' | perl -pe 's/\|$//')
local f_ASSET
local f_parallel_arg
echo -n "parallel -j3 bash -c --" >/tmp/parallel
for f_ASSET in $(cat ASSETS | egrep -v "${BLACKLIST}")
do
#get_asset "${f_ASSET}"
echo -n " \"get_asset ${f_ASSET}\"" >>/tmp/parallel
done
export -f get_asset
export f_timestamp
. /tmp/parallel
# cleanup trashlines (possibly generated by kill further of this progress)
sed -i "/[0-9]$(date +%Y)-/d" asset-histories/*
#cat ASSETS | while read f_ASSET
#do
# # write asset hist file with macd and rsi
# local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}.history.csv"
# [ -f "${f_ASSET_HIST_FILE}" ] || touch "${f_ASSET_HIST_FILE}"
# if ! grep -q "^$(echo "${f_timestamp}" | cut -d: -f1,2)" "${f_ASSET_HIST_FILE}"
# then
# local f_line="${f_timestamp},$(egrep "^${f_ASSET}," EXCHANGE_GET_ASSETS_CMD_OUT | cut -d, -f2)"
# #g_echo_note "Writing new line (${f_line}) to history ${ASSET_HIST_FILE}"
# echo -n "${f_line}" >>${f_ASSET_HIST_FILE}
# get_macd_indicator ${f_ASSET_HIST_FILE}
# get_rsi_indicator ${f_ASSET_HIST_FILE} 30
# echo ",${f_macd},${f_rsi}" >>${f_ASSET_HIST_FILE}
# fi
# cat ASSETS | while read f_ASSET
# do
# # ignore already invested asset
# if echo "$f_ASSET" | egrep -q "$f_investedassets_regex"
# then
# g_echo_note "Already invested in $f_ASSET - ignoring for more diversification"
# continue
# fi
#
# # sort latest assets prices by highest in last timeframe (ignore - or 0)
# get_rate_percentage_min_before_and_now $(echo ${f_ASSET} | sed "s/${CURRENCY}\$//") ${CURRENCY} 1 || continue
# local f_ASSET_PERCENTAGE=${f_exchange_rate_diff_percentage}
# echo "$f_ASSET_PERCENTAGE" | egrep -q '^-|^0.0' && continue
# g_echo "adding ${f_ASSET}({$f_ASSET_PERCENTAGE}) to ASSETS_HIGHEST because marketcap in Top ${LARGEST_MARKETCAP}"
# echo "${f_ASSET_PERCENTAGE},${f_ASSET}" >>ASSETS_HIGHEST.tmp
# done
# cat ASSETS_HIGHEST.tmp | sort -r >ASSETS_HIGHEST.tmp2
# mv ASSETS_HIGHEST.tmp2 ASSETS_HIGHEST
# rm ASSETS_HIGHEST.tmp
# get MSCI World Index for analysis
echo "wget -q -O - https://www.boerse.de/realtime-kurse/MSCI-World/XC0009692739 | egrep 'itemprop=\"price\" content=\"[0-9]+\.[0-9]+\"' | cut -d\\\" -f6" >MSCI_WORLD_CMD
g_runcmd g_retrycmd sh MSCI_WORLD_CMD >MSCI_WORLD_CMD_OUT.tmp
# check output
if [ -s MSCI_WORLD_CMD_OUT.tmp ] && egrep -q "^[0-9]*\.[0-9]+$" MSCI_WORLD_CMD_OUT.tmp
then
if egrep -q "^0\.00$" MSCI_WORLD_CMD_OUT.tmp
then
g_echo_note "Ignoring MSCI World $(tail -n 10 MSCI_WORLD_CMD_OUT.tmp) - maybe out of business day"
else
mv MSCI_WORLD_CMD_OUT.tmp MSCI_WORLD_CMD_OUT
fi
else
g_echo_warn "MSCI_WORLD_CMD_OUT.tmp has wrong Syntax. - Not updating MSCI WORLD Index $(tail -n 10 MSCI_WORLD_CMD_OUT.tmp)"
fi
echo "${f_timestamp},$(cat MSCI_WORLD_CMD_OUT)" >>asset-histories/MSCI-WORLD-INDEX.history.csv
}