assoziative array

This commit is contained in:
olli 2024-09-04 18:03:28 +02:00
parent 61c805ebfd
commit ff46ae8e8a

View File

@ -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
}