From 1e8a95bac77581fd3ae2f6be7aa80cd412b20f8b Mon Sep 17 00:00:00 2001 From: olli Date: Fri, 20 Oct 2023 17:39:19 +0200 Subject: [PATCH] new chart data and global timestamp --- dabo/functions/get_asset.sh | 197 ++++++++++++++++++++---------------- 1 file changed, 110 insertions(+), 87 deletions(-) diff --git a/dabo/functions/get_asset.sh b/dabo/functions/get_asset.sh index 646038c..7d19b57 100644 --- a/dabo/functions/get_asset.sh +++ b/dabo/functions/get_asset.sh @@ -3,94 +3,117 @@ function get_asset { g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" local f_ASSET="$1" - # write asset hist file with macd and rsi - local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}.history-raw.csv" - [ -f "${f_ASSET_HIST_FILE}" ] || echo "Date and Time,Price" >"${f_ASSET_HIST_FILE}" - if ! grep -q "^$(echo "${f_timestamp}" | cut -d: -f1,2)" "${f_ASSET_HIST_FILE}" + # write asset hist file with macd and rsi + local f_ASSET_HIST_FILE="asset-histories/${f_ASSET}.history-raw.csv" + + if find "${f_ASSET_HIST_FILE}" -mmin -${INTERVAL_MIN} | grep -q "${f_ASSET_HIST_FILE}" + then + g_echo_note "${f_ASSET_HIST_FILE} already downloaded in the last ${INTERVAL_MIN} minutes" + return 0 + fi + + + + [ -f "${f_ASSET_HIST_FILE}" ] || echo "Date and Time,Price" >"${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)" + echo "${f_line}" >>${f_ASSET_HIST_FILE} + + local f_linecount=0 + local f_last_price=0 + local f_lines="$(tail -n 51 "${f_ASSET_HIST_FILE}" | wc -l)" + + for f_price in $(tail -n 50 "${f_ASSET_HIST_FILE}" | grep "^[0-9]" | cut -d, -f2) + do + if [ "$f_last_price" == "$f_price" ] + then + continue + fi + let "f_linecount+=1" + f_last_price=$f_price + done + + if [ ${f_linecount} -le 3 ] && [ ${f_lines} -ge 50 ] then - local f_line="${f_timestamp},$(egrep "^${f_ASSET}," EXCHANGE_GET_ASSETS_CMD_OUT | cut -d, -f2)" - echo "${f_line}" >>${f_ASSET_HIST_FILE} - - local f_linecount=0 - local f_last_price=0 - local f_lines="$(tail -n 51 "${f_ASSET_HIST_FILE}" | wc -l)" - - for f_price in $(tail -n 50 "${f_ASSET_HIST_FILE}" | grep "^[0-9]" | cut -d, -f2) - do - if [ "$f_last_price" == "$f_price" ] - then - continue - fi - let "f_linecount+=1" - f_last_price=$f_price - done - - if [ ${f_linecount} -le 3 ] && [ ${f_lines} -ge 50 ] - then - g_echo_note "${f_ASSET_HIST_FILE}: price seems not to change - ignoring" - return 0 - fi - - f_ASSET_HIST_FILE="asset-histories/${f_ASSET}.history.csv" - if [ ${f_linecount} -lt 30 ] && [ ${f_lines} -ge 50 ] && [ ${INTERVAL} -lt 300 ] - then - g_echo_note "${f_ASSET_HIST_FILE}: set price to 5 minute period" - if ! echo "${f_timestamp}" | egrep -q ":[0-5]0:|:[0-5]5:" - then - return 0 - fi - fi - - # headline - [ -s "${f_ASSET_HIST_FILE}" ] || echo "${csv_headline}" >"${f_ASSET_HIST_FILE}" - - - # date and price - echo -n "${f_line}" >>${f_ASSET_HIST_FILE} - - # calculate price change percentage - f_last_price=$(tail -n2 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2) - if echo $f_last_price | grep -q "^[0-9]" - then - f_price=$(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f2) - local f_price_change=$(g_percentage-diff ${f_last_price} ${f_price}) - else - local f_price_change="" - fi - echo -n ",${f_price_change}" >>"${f_ASSET_HIST_FILE}" - - # calculate macd and rsi - get_macd_indicator ${f_ASSET_HIST_FILE} - get_rsi_indicator ${f_ASSET_HIST_FILE} 5 - get_rsi_indicator ${f_ASSET_HIST_FILE} 14 - get_rsi_indicator ${f_ASSET_HIST_FILE} 21 - get_rsi_indicator ${f_ASSET_HIST_FILE} 720 - get_rsi_indicator ${f_ASSET_HIST_FILE} 60 - get_rsi_indicator ${f_ASSET_HIST_FILE} 120 - get_rsi_indicator ${f_ASSET_HIST_FILE} 240 - get_rsi_indicator ${f_ASSET_HIST_FILE} 480 - - # get coingecko price change - local f_asset=$(echo ${f_ASSET} | sed "s/${CURRENCY}\$//" | tr '[:upper:]' '[:lower:]') - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_24h_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_7d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_14d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_30d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_1y_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.market_cap_change_percentage_24h)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} - - # Calculate Fibonacci-Retracement - # last 60 timeframes (a day on hourly) - get_fibonacci_indicator ${f_ASSET_HIST_FILE} 60 - # last 168 timeframes (a week on hourly) - get_fibonacci_indicator ${f_ASSET_HIST_FILE} 168 - # last 672 timeframes (a month on hourly) - get_fibonacci_indicator ${f_ASSET_HIST_FILE} 672 - # last 8064 timeframes (a year on hourly) - get_fibonacci_indicator ${f_ASSET_HIST_FILE} 8064 - - # end with newline - echo "" >>${f_ASSET_HIST_FILE} + g_echo_note "${f_ASSET_HIST_FILE}: price seems not to change - ignoring" + return 0 fi + + f_ASSET_HIST_FILE="asset-histories/${f_ASSET}.history.csv" + #if [ ${f_linecount} -lt 30 ] && [ ${f_lines} -ge 50 ] && [ ${INTERVAL} -lt 300 ] + #then + # g_echo_note "${f_ASSET_HIST_FILE}: set price to 5 minute period" + # if ! echo "${f_timestamp}" | egrep -q ":[0-5]0:|:[0-5]5:" + # then + # return 0 + # fi + #fi + + # headline + #[ -s "${f_ASSET_HIST_FILE}" ] || echo "${csv_headline}" >"${f_ASSET_HIST_FILE}" + if [ -s "${f_ASSET_HIST_FILE}" ] + then + sed -i -e 1c"$csv_headline" "${f_ASSET_HIST_FILE}" + else + echo "$csv_headline" >"${f_ASSET_HIST_FILE}" + fi + + # date and price + echo -n "${f_line}" >>${f_ASSET_HIST_FILE} + + # calculate price change percentage + f_last_price=$(tail -n2 ${f_ASSET_HIST_FILE} | head -n1 | cut -d, -f2) + if echo $f_last_price | grep -q "^[0-9]" + then + f_price=$(tail -n1 ${f_ASSET_HIST_FILE} | cut -d, -f2) + local f_price_change=$(g_percentage-diff ${f_last_price} ${f_price}) + else + local f_price_change="" + fi + echo -n ",${f_price_change}" >>"${f_ASSET_HIST_FILE}" + + # calculate macd and rsi + get_macd_indicator ${f_ASSET_HIST_FILE} + get_rsi_indicator ${f_ASSET_HIST_FILE} 5 + get_rsi_indicator ${f_ASSET_HIST_FILE} 14 + get_rsi_indicator ${f_ASSET_HIST_FILE} 21 + get_rsi_indicator ${f_ASSET_HIST_FILE} 720 + get_rsi_indicator ${f_ASSET_HIST_FILE} 60 + get_rsi_indicator ${f_ASSET_HIST_FILE} 120 + get_rsi_indicator ${f_ASSET_HIST_FILE} 240 + get_rsi_indicator ${f_ASSET_HIST_FILE} 480 + + # get coingecko price change + local f_asset=$(echo ${f_ASSET} | sed "s/${CURRENCY}\$//" | tr '[:upper:]' '[:lower:]') + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_24h_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_7d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_14d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_30d_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.price_change_percentage_1y_in_currency)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + echo -n ,$(jq -r ".[] |select(.symbol==\"${f_asset}\")|\"\\(.market_cap_change_percentage_24h)\"" COINGECKO_GET_ASSETS_CMD_OUT) >>${f_ASSET_HIST_FILE} + + # range and fibonacci + get_range ${f_ASSET_HIST_FILE} + + # Calculate EMA 50:36 100:37 200:38 800:39 + local f_calcemanumcolumn + for f_calcemanumcolumn in 50:36 100:37 200:38 800:39 + do + local f_calcema=$(echo ${f_calcemanumcolumn} | cut -d: -f1) + local f_caclemacolumn=$(echo ${f_calcemanumcolumn} | cut -d: -f2) + get_ema "${f_ASSET_HIST_FILE}" 2 ${f_calcema} "$(tail -n2 "${f_ASSET_HIST_FILE}" | head -n1 | grep ^2 | cut -d, -f${f_caclemacolumn})" "${f_price}" + if [ -z "${f_ema}" ] + then + g_echo_note "${FUNCNAME} $@: Not enough data for calculating EMA - waiting for more values" + echo -n "," >>"${f_ASSET_HIST_FILE}" + else + echo -n ",${f_ema}" >>"${f_ASSET_HIST_FILE}" + fi + done + + # end with newline + echo "" >>${f_ASSET_HIST_FILE} + fi }