From ff46ae8e8a756b28d3c864cfb7a3f19edb8e6e6e Mon Sep 17 00:00:00 2001 From: olli Date: Wed, 4 Sep 2024 18:03:28 +0200 Subject: [PATCH] assoziative array --- dabo/functions/get_positions.sh | 90 +++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/dabo/functions/get_positions.sh b/dabo/functions/get_positions.sh index f5dc4e2..8913abe 100644 --- a/dabo/functions/get_positions.sh +++ b/dabo/functions/get_positions.sh @@ -43,46 +43,96 @@ function get_positions { jq -r " .[] | select(.entryPrice != 0) | -.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.contracts|tostring) + \",\" + (.contractSize|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.unrealizedPnl|tostring) +.symbol + \",\" + (.collateral|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) " CCXT_POSITIONS_RAW >CCXT_POSITIONS } function get_position_array { + local f_position + + # clear/create assoziative array p + unset p + declare -Ag p + + # build array from lines in CCXT_POSITIONS g_array CCXT_POSITIONS f_get_positions_array + for f_position in ${f_get_positions_array[@]} + do + get_position_line_vars "$f_position" + done + + # write values to file + for i in "${!p[@]}" + do + echo "\${p[$i]}=${p[$i]}" + done | sort >values-positions.new + mv values-positions.new values-positions + } function get_position_line_vars { local f_pos_line=$1 - g_array $f_pos_line f_position_array , + f_position_symbol=${f_position_array[0]} - f_position_currency_amount=${f_position_array[1]} + local f_asset=${f_position_symbol//:$CURRENCY/} + f_asset=${f_asset//\//} + + printf -v f_position_currency_amount %.2f ${f_position_array[1]} + p[${f_asset}_currency_amount]=$f_position_currency_amount + f_position_entry_price=${f_position_array[2]} + p[${f_asset}_entry_price]=$f_position_entry_price + f_position_current_price=${f_position_array[3]} + p[${f_asset}_current_price]=$f_position_current_price + f_position_side=${f_position_array[4]} [ -z "$f_position_side" ] && f_position_side="long" + p[${f_asset}_side]=$f_position_side + f_position_leverage=${f_position_array[5]} - [ -z "$f_position_leverage" ] && f_position_leverage="1" - f_position_contracts=${f_position_array[6]} - f_position_contract_size=${f_position_array[7]} - f_position_liquidation_price=${f_position_array[8]} - f_position_unrealized_pnl=${f_position_array[9]} + [[ $f_position_leverage = null ]] && f_position_leverage="1" + p[${f_asset}_leverage]=$f_position_leverage + + p[${f_asset}_liquidation_price]=${f_position_array[6]} + f_position_liquidation_price=${f_position_array[6]} - g_percentage-diff $f_position_entry_price $f_position_current_price - [ "$f_position_side" = short ] && g_percentage-diff $f_position_current_price $f_position_entry_price - f_position_pnl_percentage=$g_percentage_diff_result - if [ -n $f_position_leverage ] + if [[ ${f_position_array[7]} = null ]] then - g_calc "$f_position_pnl_percentage*$f_position_leverage" - f_position_pnl_percentage=$g_calc_result - - g_calc "$f_position_currency_amount/$f_position_leverage" - f_position_currency_amount=$g_calc_result - - g_calc "$f_position_currency_amount/100*$f_position_pnl_percentage" - f_position_pnl=$g_calc_result + unset p[${f_asset}_stoploss_price] + unset f_position_stoploss_price + else + p[${f_asset}_stoploss_price]=${f_position_array[7]} + f_position_stoploss_price=${f_position_array[7]} fi + + if [[ ${f_position_array[8]} = null ]] + then + unset p[${f_asset}_takeprofit_price] + unset f_position_takeprofit_price + else + p[${f_asset}_takeprofit_price]=${f_position_array[8]} + f_position_takeprofit_price=${f_position_array[8]} + fi + + # calc pnl percentage + if [[ $f_position_side = long ]] + then + g_percentage-diff $f_position_entry_price $f_position_current_price + else + g_percentage-diff $f_position_current_price $f_position_entry_price + fi + g_calc "$g_percentage_diff_result*$f_position_leverage" + f_position_pnl_percentage=$g_calc_result + p[${f_asset}_pnl_percentage]=$g_calc_result + + # calc pnl + g_calc "$f_position_currency_amount/100*$f_position_pnl_percentage" + printf -v f_position_pnl %.2f $g_calc_result + p[${f_asset}_pnl]=$f_position_pnl + }