From e991042eaa82c041bf69207a54197ae0bfebe969 Mon Sep 17 00:00:00 2001 From: olli Date: Thu, 5 Sep 2024 18:02:00 +0200 Subject: [PATCH] stoploss and takeprofit --- dabo/functions/get_orders.sh | 5 ++- dabo/functions/get_positions.sh | 60 ++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/dabo/functions/get_orders.sh b/dabo/functions/get_orders.sh index 01015b9..1fba19a 100644 --- a/dabo/functions/get_orders.sh +++ b/dabo/functions/get_orders.sh @@ -53,7 +53,7 @@ function get_orders { echo $f_ccxt_result | tee "CCXT_ORDERS_${f_symbol_file}_RAW" | jq -r " .[] | select(.status==\"open\") | -.symbol + \",\" + .type + \",\" + .side + \",\" + (.price|tostring) + \",\" + (.amount|tostring) + \",\" + .id + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) +.symbol + \",\" + .type + \",\" + .side + \",\" + (.price|tostring) + \",\" + (.amount|tostring) + \",\" + .id + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) + \",\" + (.stopPrice|tostring) " >"CCXT_ORDERS_${f_symbol_file}" else rm -f "CCXT_ORDERS_${f_symbol_file}_RAW" "CCXT_ORDERS_${f_symbol_file}" @@ -117,6 +117,9 @@ function get_order_line_vars { o[${f_asset}_takeprofitprice]=${f_order_array[7]} f_order_takeprofitprice=${f_order_array[7]} + o[${f_asset}_stopprice]=${f_order_array[8]} + f_order_stopprice=${f_order_array[8]} + } diff --git a/dabo/functions/get_positions.sh b/dabo/functions/get_positions.sh index 8913abe..465e97e 100644 --- a/dabo/functions/get_positions.sh +++ b/dabo/functions/get_positions.sh @@ -22,7 +22,7 @@ function get_positions { g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" - local f_symbol f_symbols + local f_symbol f_symbols f_asset f_stoploss f_takeprofit get_symbols_ticker @@ -39,13 +39,40 @@ function get_positions { [ -z "$f_symbols" ] && return 1 f_ccxt "print($STOCK_EXCHANGE.fetchPositions(symbols=[${f_symbols}]))" && echo $f_ccxt_result >CCXT_POSITIONS_RAW - jq -r " .[] | select(.entryPrice != 0) | -.symbol + \",\" + (.collateral|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) +.symbol + \",\" + (.collateral|tostring) + \",\" + (.entryPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) " CCXT_POSITIONS_RAW >CCXT_POSITIONS + # check for takeprofit/stoploss orders if not in CCXT output (needed for phememx and maybe more exchanges) + get_position_array + for f_symbol in ${f_symbols_array_trade[@]} + do + f_asset=${f_symbol//:$CURRENCY/} + f_asset=${f_asset//\//} + # only continue if position for symbol exists and stoploss or takeprofit is empty + [ -z "${p[${f_asset}_entry_price]}" ] && continue + [ -n "${p[${f_asset}_stoploss_price]}" ] && continue + [ -n "${p[${f_asset}_takeprofit_price]}" ] && continue + + # check for position side + [ "${p[${f_asset}_side]}" = "long" ] && f_action=sell + [ "${p[${f_asset}_side]}" = "short" ] && f_action=buy + if [[ ${p[${f_asset}_side]} =~ long|short ]] + then + # search fpr stoploss and takeprofit + f_stoploss=$(egrep "^$f_symbol,Stop,$f_action,null,0," CCXT_ORDERS | cut -d , -f9) + f_takeprofit=$(egrep "^$f_symbol,MarketIfTouched,$f_action,null,0," CCXT_ORDERS | cut -d , -f9) + # escape : and / for sed and edit CCXT_POSITIONS if stoploss or takeprofit order found + f_symbol=${f_symbol//\//\\\/} + f_symbol=${f_symbol//:/\\:} + [ -n "$f_stoploss" ] && sed -i "/^$f_symbol,.*,${p[${f_asset}_side]},/s/^\(\([^,]*,\)\{6\}\)[^,]*/\1$f_stoploss/" CCXT_POSITIONS + [ -n "$f_takeprofit" ] && sed -i "/^$f_symbol,.*,${p[${f_asset}_side]},/s/^\(\([^,]*,\)\{7\}\)[^,]*/\1$f_takeprofit/" CCXT_POSITIONS + fi + + done + } function get_position_array { @@ -54,7 +81,7 @@ function get_position_array { # clear/create assoziative array p unset p declare -Ag p - + get_symbols_ticker # build array from lines in CCXT_POSITIONS g_array CCXT_POSITIONS f_get_positions_array for f_position in ${f_get_positions_array[@]} @@ -85,36 +112,37 @@ function get_position_line_vars { 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 + # mark price seems not lates price in very case so take the ticker + p[${f_asset}_current_price]=${v[${f_asset}_price]} + f_position_current_price=${v[${f_asset}_price]} - f_position_side=${f_position_array[4]} + f_position_side=${f_position_array[3]} [ -z "$f_position_side" ] && f_position_side="long" p[${f_asset}_side]=$f_position_side - f_position_leverage=${f_position_array[5]} + f_position_leverage=${f_position_array[4]} [[ $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]} + p[${f_asset}_liquidation_price]=${f_position_array[5]} + f_position_liquidation_price=${f_position_array[5]} - if [[ ${f_position_array[7]} = null ]] + if [[ ${f_position_array[6]} = null ]] then 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]} + p[${f_asset}_stoploss_price]=${f_position_array[6]} + f_position_stoploss_price=${f_position_array[6]} fi - if [[ ${f_position_array[8]} = null ]] + if [[ ${f_position_array[7]} = 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]} + p[${f_asset}_takeprofit_price]=${f_position_array[7]} + f_position_takeprofit_price=${f_position_array[7]} fi # calc pnl percentage