From 0094d806e8822db021c6f17d64e8c4d5f06e6f00 Mon Sep 17 00:00:00 2001 From: olli Date: Fri, 14 Jun 2024 22:45:01 +0200 Subject: [PATCH] ccxt switch --- README.md | 2 ++ dabo/functions/check_buy_conditions.sh | 37 +------------------------ dabo/functions/check_for_sell.sh | 4 +-- dabo/functions/check_sell_conditions.sh | 9 +----- dabo/functions/get_orders.sh | 2 +- dabo/functions/get_positions.sh | 13 +++++---- dabo/functions/get_symbols.sh | 2 +- dabo/functions/order.sh | 25 ++++++++++------- 8 files changed, 31 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 1247bf4..abce7f5 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ I thought this fits quite well to the cryptotrading world and that's why I chose - Runnable in a non-root docker containe - multiple different buy and sell strategies possible at the same time +- Hedge mode (long and short positions the same time) not supported + ## Buy conditions - definable RSI Indicator signals min/max (RSI5, 14 and 21) - definable MACD signals min/max diff --git a/dabo/functions/check_buy_conditions.sh b/dabo/functions/check_buy_conditions.sh index 5a1b078..c54e846 100644 --- a/dabo/functions/check_buy_conditions.sh +++ b/dabo/functions/check_buy_conditions.sh @@ -66,42 +66,7 @@ function check_buy_conditions { # remove CURRENCY from asset f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//") - if [ ${STOCK_EXCHANGE} = "BINANCE" ] - then - # get stock exchange specific infos for trade (e.g. MIN_NOTIONAL) - ${TOKEN_INFO_CMD} ${f_ASSET} ${CURRENCY} - - # use MIN_NOTIONAL+5% as INVEST_QUANTITY if INVEST_QUANTITY is under MIN_NOTIONAL - # +5% in spite of MIN_NOTIONAL to be able to sell when the price falls a little bit - #[ $(g_calc "${f_INVEST_QUANTITY} < ${f_MIN_NOTIONAL}") -eq 0 ] || f_INVEST_QUANTITY=$(g_calc "scale=2; $f_MIN_NOTIONAL/100*105") - if ! g_num_is_lower.sh ${f_INVEST_QUANTITY} ${f_MIN_NOTIONAL} - then - g_calc "scale=2; ${f_MIN_NOTIONAL}/100*105" - f_INVEST_QUANTITY=${g_calc_result} - fi - - - # if there is not enough balance for buying because ${f_MIN_NOTIONAL} needed for buying to sell (workaround) - g_calc "${CURRENCY_BALANCE} < ${f_MIN_NOTIONAL}*2" - if [ ${g_calc_result} -ne 0 ] - then - g_echo_note "BUY ${f_ASSET} not enough balance ${CURRENCY_BALANCE} for buying because of MIN_NOTIONAL (${f_MIN_NOTIONAL}*2) needed for buying-to-sell (workaround)" - return 1 - fi - - # continue if not balance enough for lowest quantity (MIN_NOTIONAL) - g_calc "${CURRENCY_BALANCE} > ${f_INVEST_QUANTITY}" - if [ ${g_calc_result} -eq 0 ] - then - g_echo_note "BUY ${f_ASSET} not enough balance (${CURRENCY_BALANCE}) for lowest quantity (MIN_NOTIONAL - ${f_INVEST_QUANTITY})" - return 1 - fi - - binance_convert ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY" || \ - do_trade ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY" - else - do_trade ${f_ASSET} ${CURRENCY} ${f_INVEST_QUANTITY} buy "$f_BUY" - fi + order ${f_ASSET}/${CURRENCY} ${f_INVEST_QUANTITY} buy f_BUY="" fi diff --git a/dabo/functions/check_for_sell.sh b/dabo/functions/check_for_sell.sh index 2281d1f..eae279d 100644 --- a/dabo/functions/check_for_sell.sh +++ b/dabo/functions/check_for_sell.sh @@ -19,7 +19,7 @@ function check_for_sell { then local f_msg="ATTENTION! EMERGENCY STOP DUE TO POOR PERFORMANCE: BALANCE (${f_COMPLETE_BALANCE}) LOWER THEN EMERGENCY_STOP-VALUE (${EMERGENCY_STOP})" g_echo_error "${f_msg}" - do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}" + position_close ${f_ASSET}/${CURRENCY} continue fi if tail -n1 $f_ASSET_HIST_FILE | egrep -q "^$(date +%Y-%m-%d)|$(date +%Y-%m-%d -d yesterday)" @@ -28,7 +28,7 @@ function check_for_sell { else local f_msg="SELL $f_ASSET_HIST_FILE no current data of invested asset" g_echo_warn "${f_msg}" - do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}" + position_close ${f_ASSET}/${CURRENCY} fi done } diff --git a/dabo/functions/check_sell_conditions.sh b/dabo/functions/check_sell_conditions.sh index c93d84e..c4c1232 100644 --- a/dabo/functions/check_sell_conditions.sh +++ b/dabo/functions/check_sell_conditions.sh @@ -89,14 +89,7 @@ function check_sell_conditions { g_echo_note "$f_SELL" echo "${f_last_line},${f_ASSET}" >>trade.log f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//") - # binance_convert ${f_ASSET} ${CURRENCY} ${f_QUANTITY} sell "${f_SELL}" - if [ ${STOCK_EXCHANGE} = "BINANCE" ] - then - binance_convert ${f_ASSET} ${CURRENCY} ${f_QUANTITY} sell "${f_SELL}" ${f_QUANTITY_CURRENCY} || \ - do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}" - else - do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_SELL}" - fi + position_close ${f_ASSET}/${CURRENCY} fi # # ANALYZE diff --git a/dabo/functions/get_orders.sh b/dabo/functions/get_orders.sh index dd810d1..0e99886 100644 --- a/dabo/functions/get_orders.sh +++ b/dabo/functions/get_orders.sh @@ -32,7 +32,7 @@ function get_orders { then if [ -z "$f_ccxt_json_out" ] then - rm -f CCXT_OPEN_ORDERS_${f_symbol_file}_RAW CCXT_OPEN_ORDERS_${f_symbol_file} + rm -f "CCXT_OPEN_ORDERS_${f_symbol_file}_RAW" "CCXT_OPEN_ORDERS_${f_symbol_file}" continue fi echo $f_ccxt_result | tee "CCXT_OPEN_ORDERS_${f_symbol_file}_RAW" | jq -r " diff --git a/dabo/functions/get_positions.sh b/dabo/functions/get_positions.sh index 69652e7..928a780 100644 --- a/dabo/functions/get_positions.sh +++ b/dabo/functions/get_positions.sh @@ -21,15 +21,13 @@ function get_positions { jq -r " .[] | select(.entryPrice != 0) | -.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) -" CCXT_POSITIONS_RAW >POSITIONS - - get_position_array +.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.contracts|tostring) + \",\" + (.contractSize|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.unrealizedPnl|tostring) +" CCXT_POSITIONS_RAW >CCXT_POSITIONS } function get_position_array { - g_array POSITIONS f_get_positions_array + g_array CCXT_POSITIONS f_get_positions_array } function get_position_line_vars { @@ -44,6 +42,11 @@ function get_position_line_vars { [ -z "$f_position_side" ] && f_position_side="long" 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]} + 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 diff --git a/dabo/functions/get_symbols.sh b/dabo/functions/get_symbols.sh index 1630ba4..d3648f4 100644 --- a/dabo/functions/get_symbols.sh +++ b/dabo/functions/get_symbols.sh @@ -5,7 +5,7 @@ function get_symbols { local f_symbols="" [ -s CCXT_TICKERS_RAW ] && f_symbols=$(jq -r '.[] | .symbol' CCXT_TICKERS_RAW) f_symbols=${f_symbols//$'\n'/'+'} - + if [ -z "$f_symbols" ] then g_echo_warn "Could not get symbols list - empty" diff --git a/dabo/functions/order.sh b/dabo/functions/order.sh index 2fcbcd6..d5431ab 100644 --- a/dabo/functions/order.sh +++ b/dabo/functions/order.sh @@ -4,9 +4,9 @@ function order { # needed vars local f_symbol=$1 - local f_quote_currency_amount=$2 # amount in $CURRENCY + local f_amount=$2 # amount in $CURRENCY / if crypto_amount:XXX then amount in crypto local f_side=$3 # buy/sell long/short - local f_price=$4 # price for liomit order - if not given do market order + local f_price=$4 # price for limit order - if not given do market order local f_params f_type @@ -15,9 +15,6 @@ function order { # check symbol XXX/$CURRENCY[:$CURRENCY] [[ $f_symbol =~ /$CURRENCY ]] || return 1 - # check amount - g_num_valid_number "$f_quote_currency_amount" || return 1 - # check side [ "$f_side" = "long" ] && f_side="buy" [ "$f_side" = "short" ] && f_side="sell" @@ -34,11 +31,17 @@ function order { ### validity checks end### - - # get amount in krypto asset - local f_asset=${f_symbol///*} - currency_converter $f_quote_currency_amount $CURRENCY $f_asset || return 1 - local f_amount=$f_currency_converter_result + # get amount in crypto asset + if [[ $f_amount =~ ^crypto_amount: ]] + then + # if given in crypto + f_amount=${f_amount//crypto_amount:} + else + # if given in $CURRENCY + local f_asset=${f_symbol///*} + currency_converter $f_amount $CURRENCY $f_asset || return 1 + local f_amount=$f_currency_converter_result + fi # check for swap/margin trades if [ -n "$LEVERAGE" ] @@ -64,5 +67,7 @@ function order { # do the order f_ccxt "print($STOCK_EXCHANGE.createOrder(symbol='${f_symbol}', type='$f_type', price=$f_price, amount='${f_amount}', side='${f_side}', ${f_params}))" || return 1 + # refresh positions + get_positions }