This commit is contained in:
olli 2024-09-23 10:05:49 +02:00
parent 62ad7ce2da
commit cab4d7e0df
3 changed files with 82 additions and 14 deletions

View File

@ -49,14 +49,14 @@ function calc_ema {
local f_last_value=${v_csv_array_associative[${f_column}_${f_position}]} local f_last_value=${v_csv_array_associative[${f_column}_${f_position}]}
[ -z "$f_target_column" ] && return 4 [ -z "$f_target_column" ] && return 4
local v local f_v
# reset old ema var # reset old ema var
unset f_ema unset f_ema
# find last EMA # find last EMA
local f_last_ema_position=$((f_position-1)) local f_last_ema_position=$((f_position-1))
local f_last_ema=${v_csv_array_associative[${f_target_column}_${f_last_ema_pos}]} local f_last_ema=${v_csv_array_associative[${f_target_column}_${f_last_ema_position}]}
# check if last EMA is given # check if last EMA is given
if [ -n "$f_last_ema" ] if [ -n "$f_last_ema" ]
@ -66,15 +66,17 @@ function calc_ema {
else else
## calc SMA if previous EMA is not given (only needed on first EMA calc) ## calc SMA if previous EMA is not given (only needed on first EMA calc)
# get last $f_period values # get last $f_period values
g_echo_note "calc SMA - previous EMA is not given"
local f_last_period_values_from=$((f_position-$f_period+1)) local f_last_period_values_from=$((f_position-$f_period+1))
local f_last_period_values local f_last_period_values
for ((v=$f_last_period_values_from; v<=${f_position}; v++)) for ((f_v=$f_last_period_values_from; f_v<=${f_position}; f_v++))
do do
if [ -z ${f_last_period_values} ] if [ -z ${f_last_period_values} ]
then then
f_last_period_values=${v_csv_array_associative[${f_column}_${v}]} f_last_period_values=${v_csv_array_associative[${f_column}_${f_v}]}
else else
f_last_period_values="$f_last_period_values+${v_csv_array_associative[${f_column}_${v}]}" g_calc "${f_last_period_values}+${v_csv_array_associative[${f_column}_${f_v}]}"
f_last_period_values=$g_calc_result
fi fi
done done
# calc SMA (EMA=SMA in this special first case) # calc SMA (EMA=SMA in this special first case)
@ -82,6 +84,14 @@ function calc_ema {
fi fi
# write back EMA # write back EMA
if [[ $g_calc_result =~ ^- ]]
then
if ! [[ $f_period = 9 ]]
then
g_echo_warn "${FUNCNAME} $@: EMA can not be negative ($g_calc_result)"
return 1
fi
fi
v_csv_array_associative[${f_target_column}_${f_position}]=$g_calc_result v_csv_array_associative[${f_target_column}_${f_position}]=$g_calc_result
f_ema=$g_calc_result f_ema=$g_calc_result

View File

@ -22,7 +22,7 @@ function get_ohlcv-candles {
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_histfile f_symbol f_timeframe f_1h_histfile local f_histfile f_symbol f_timeframe f_1h_histfile f_1d_histfile
local f_timeframes="1w 1d 4h 1h 15m 5m" local f_timeframes="1w 1d 4h 1h 15m 5m"
[ -n $1 ] && f_timeframes=$1 [ -n $1 ] && f_timeframes=$1
@ -40,12 +40,16 @@ function get_ohlcv-candles {
then then
f_1h_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1h.csv" f_1h_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1h.csv"
[ -s "$f_1h_histfile" ] && convert_ohlcv_1h_to_4h "$f_1h_histfile" "$f_histfile" [ -s "$f_1h_histfile" ] && convert_ohlcv_1h_to_4h "$f_1h_histfile" "$f_histfile"
#f_add_missing_ohlcv_intervals "$f_histfile" 4h f_add_missing_ohlcv_intervals "$f_histfile" 4h
elif [ "$f_timeframe" = "1d" ] elif [ "$f_timeframe" = "1d" ]
then then
f_1h_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1h.csv" f_1h_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1h.csv"
[ -s "$f_1h_histfile" ] && convert_ohlcv_1h_to_1d "$f_1h_histfile" "$f_histfile" [ -s "$f_1h_histfile" ] && convert_ohlcv_1h_to_1d "$f_1h_histfile" "$f_histfile"
#f_add_missing_ohlcv_intervals "$f_histfile" 1d f_add_missing_ohlcv_intervals "$f_histfile" 1d
elif [ "$f_timeframe" = "1w" ]
then
f_1d_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1d.csv"
[ -s "$f_1d_histfile" ] && convert_ohlcv_1d_to_1w "$f_1d_histfile" "$f_histfile"
else else
get_ohlcv-candle "${f_eco_asset}" ${f_timeframe} "${f_histfile}" "ECONOMY-${f_eco_asset}" get_ohlcv-candle "${f_eco_asset}" ${f_timeframe} "${f_histfile}" "ECONOMY-${f_eco_asset}"
fi fi
@ -379,8 +383,8 @@ function convert_ohlcv_1h_to_1d {
f_latestdate=$(TZ="$f_target_timezone" date -d "$f_latestdate $f_mytimezone" "+%Y-%m-%d") f_latestdate=$(TZ="$f_target_timezone" date -d "$f_latestdate $f_mytimezone" "+%Y-%m-%d")
f_nextdate=$(date -d "$f_latestdate +1day" "+%Y-%m-%d") f_nextdate=$(date -d "$f_latestdate +1day" "+%Y-%m-%d")
echo $f_latestdate #echo $f_latestdate
echo $f_nextdate #echo $f_nextdate
# mytimezone, respecting summer/winter time # mytimezone, respecting summer/winter time
f_mytimezone=$(date -d "$_latestdate" +%Z) f_mytimezone=$(date -d "$_latestdate" +%Z)
@ -451,6 +455,60 @@ function convert_ohlcv_1h_to_1d {
} }
function convert_ohlcv_1d_to_1w {
local f_input_file=$1
local f_output_file=$2
local f_week_date f_day f_month f_year f_other f_line f_data
local -A f_open_prices f_high_prices f_low_prices f_close_prices f_volume_prices
# get lastest date to continue from here and create output file if not exists
if [ -s "$f_output_file" ]
then
f_latestdate=$(tail -n1 "$f_output_file" | cut -d, -f1)
else
touch "$f_output_file"
fi
# if not exists use first date as latest date
[ -z "$f_latestdate" ] && f_latestdate=$(date -d "$(head -n1 "$f_input_file" | cut -d, -f1)" +%Y-%m-%d)
# go through lines
for f_line in $(grep -A9999 -B9 "^$f_latestdate" "$f_input_file")
do
IFS=',' read -r f_date f_open f_high f_low f_close f_volume f_other <<< "$f_line"
IFS='-' read -r f_year f_month f_day <<< "$f_date"
# use week-number to sort day data in weeks
f_week_number=$(date -d "$f_year-$f_month-$f_day" +%U)
f_week_number=${f_week_number##0}
f_week_year=$f_year$f_week_number
# calculate week ohlcv and write to arrays sortet by f_week_year
g_calc "${f_open_prices[$f_week_year]:-$f_open}"
f_open_prices[$f_week_year]=$g_calc_result
g_num_is_higher "$f_high" "${f_high_prices[$f_week_year]:-0}" && f_high_prices[$f_week_year]=$f_high
[ -z "${f_low_prices[$f_week_year]}" ] && f_low_prices[$f_week_year]=$f_low
g_num_is_lower "$f_low" "${f_low_prices[$f_week_year]:-0}" && f_low_prices[$f_week_year]=$f_low
f_close_prices[$f_week_year]=$f_close
g_calc "${f_volume_prices[$f_week_year]:-0}+$f_volume"
f_volume_prices[$f_week_year]=$g_calc_result
done
# go through array(s) and write down missing week data
for f_week_year in "${!f_open_prices[@]}"
do
f_week_date=$(date -d "${f_week_year:0:4}-01-01 +$((${f_week_year:4}-1)) week" +%F)
# ignore if date alerady exists
grep -q ^$f_week_date, "$f_output_file" && continue
echo "$f_week_date,${f_open_prices[$f_week_year]},${f_high_prices[$f_week_year]},${f_low_prices[$f_week_year]},${f_close_prices[$f_week_year]},${f_volume_prices[$f_week_year]}"
done | sort >>"$f_output_file"
}
function f_add_missing_ohlcv_intervals { function f_add_missing_ohlcv_intervals {
@ -485,7 +543,7 @@ function f_add_missing_ohlcv_intervals {
while IFS=',' read -r f_curr_date f_open f_high f_low f_close f_volume f_percent f_curr_vals while IFS=',' read -r f_curr_date f_open f_high f_low f_close f_volume f_percent f_curr_vals
do do
echo "$f_curr_date" 1>&2 #echo "$f_curr_date" 1>&2
# if prev date is not empty # if prev date is not empty
if [ -z "$f_prev_date" ] if [ -z "$f_prev_date" ]
@ -495,7 +553,7 @@ function f_add_missing_ohlcv_intervals {
continue continue
fi fi
echo "$f_curr_date x" 1>&2 #echo "$f_curr_date x" 1>&2
# only 10 interations to prevelt endless loop # only 10 interations to prevelt endless loop
f_counter=0 f_counter=0
@ -504,7 +562,7 @@ function f_add_missing_ohlcv_intervals {
do do
((f_counter++)) ((f_counter++))
echo "$f_curr_date xx $f_counter" 1>&2 #echo "$f_curr_date xx $f_counter" 1>&2
# get second timestamps # get second timestamps
f_prev_date_in_seconds=$(date -d"$f_prev_date" +%s) f_prev_date_in_seconds=$(date -d"$f_prev_date" +%s)

View File

@ -58,7 +58,7 @@ function get_values {
f_histfile="asset-histories/${f_asset}.history.${f_time}.csv" f_histfile="asset-histories/${f_asset}.history.${f_time}.csv"
if ! [ -s "$f_histfile" ] if ! [ -s "$f_histfile" ]
then then
g_echo_warn "file $f_histfile empty or does not exist" [ "$f_time" = "1w" ] || g_echo_warn "file $f_histfile empty or does not exist"
f_return=1 f_return=1
continue continue
fi fi