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

139 lines
4.7 KiB
Bash

#!/bin/bash
function get_levels_all {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_levelsfile f_level f_symbol f_symbol_in_array f_price
get_symbols_ticker
for f_symbol in "${f_symbols_array_trade[@]}"
do
f_symbol=${f_symbol%%:*}
f_symbol=${f_symbol//\/}
# check for data files
for f_levelsfile in "asset-histories/${f_symbol}.history.1d.csv" "asset-histories/${f_symbol}.history.4h.csv" "asset-histories/${f_symbol}.history.15m.csv"
do
[ -s "$f_levelsfile" ] || continue 2
done
# define target file
f_levelsfile="asset-histories/${f_symbol}.history.csv"
printf '%(%Y-%m-%d %H:%M:%S)T' >"${f_levelsfile}.levels-calculating"
g_echo_note "Estimating relevant levels of $f_symbol"
# get current price to reduce the range, save cpu-power and time
f_symbol_in_array=${f_symbol/ /}
f_price=${f_tickers_array[$f_symbol_in_array]}
# get relevant data max 30000 (+-15000) prices from current price
#mapfile -t f_prices < <((cut -d, -f3,4,5 "asset-histories/${f_symbol}.history.1d.csv" "asset-histories/${f_symbol}.history.4h.csv" "asset-histories/${f_symbol}.history.15m.csv" ; echo $f_price) | sed 's/,/\n/g' | sort -rnu | grep -C 15000 "^${f_price}$")
mapfile -t f_prices < <((cut -d, -f5 "asset-histories/${f_symbol}.history.1d.csv" "asset-histories/${f_symbol}.history.4h.csv" "asset-histories/${f_symbol}.history.15m.csv" ; echo $f_price) | sed 's/,/\n/g' | sort -rnu | grep -C 15000 "^${f_price}$")
# calculate levels
get_levels && printf '%(%Y-%m-%d %H:%M:%S)T' >"${f_levelsfile}.levels-calculated"
rm -f "${f_levelsfile}.levels-calculating"
# write new levels file
for f_level in "${f_levels[@]}"
do
echo $f_level
done >"${f_levelsfile}.levels.new"
mv "${f_levelsfile}.levels.new" "${f_levelsfile}.levels"
done
}
function get_levels {
# estimates the relevant levels from price list from array f_prices and put then in array f_levels
# - needs array $f_prices with prices sorted from low to high and no duplicates (sort -nru) to analyze
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
# if there is not enough or no price data
[ -z "${f_prices[100]}" ] && return 1
# reset old levels var
unset f_levels
local f_min_occurrences i j f_level f_level_count f_level_prices f_level_first_price f_baseprice f_threshold_test
# some key points
local f_lowest_price=${f_prices[-1]}
local f_highest_price=${f_prices[1]}
local f_number_of_prices=${#f_prices[@]}
# calc percentual price range
g_percentage-diff $f_highest_price $f_lowest_price
# calc threshold (avarage of percentual price difference)
local f_price_range_percentage=${g_percentage_diff_result//-/}
g_calc "$f_price_range_percentage / $f_number_of_prices / 100" || return 1
local f_threshold=$g_calc_result
# calc threshold in range (1/100 of percentual range)
g_calc "$f_price_range_percentage / 100" || return 1
local f_threshold_in_range=$g_calc_result
#echo "$f_threshold_in_range - $f_threshold - $f_price_range_percentage"
#return 0
# how much occurencies / same prices have so show up for a defined level - percentage from number of prices
#local f_min_occurrences=3
g_calc "$f_number_of_prices / 100 * 0.2" || return 1
local f_min_occurrences=$g_calc_result
# Loop through the f_prices and compare each number with the next
for ((i=0; i<${#f_prices[@]}-1; i++))
do
#echo "$i of ${#f_prices[@]}"
# pair this and next elemtn
j=$((i+1))
f_threshold_test=$f_threshold
f_baseprice=${f_prices[i]}
# if we are in a level use first price of level
if [ -n "$f_level_count" ]
then
f_baseprice=$f_level_first_price
f_threshold_test=$f_threshold_in_range
fi
# pair similiar?
if g_num_is_approx ${f_prices[j]} $f_baseprice $f_threshold_test $f_threshold_test
then
# first number of similars?
if [ -z "$f_level_count" ]
then
# new level
f_level_count=2
f_level_prices="${f_prices[i]}+${f_prices[j]}"
f_level_first_price=${f_prices[i]}
else
# add values to level
f_level_count=$((f_level_count+1))
f_level_prices="$f_level_prices+${f_prices[j]}"
fi
#echo "level ($f_level_count): $f_level_prices"
else
if [ -n "$f_level_count" ]
then
# end of level
#if [ "$f_level_count" -ge "$f_min_occurrences" ]
if g_num_is_higher_equal $f_level_count $f_min_occurrences
then
g_calc "($f_level_prices)/$f_level_count"
f_levels+=("$g_calc_result")
g_echo_note "adding significant level at $g_calc_result after reaching $f_level_count times"
fi
f_level_prices=""
f_level_count=""
f_level_first_price=""
fi
fi
done
}