ccxt switch

This commit is contained in:
olli 2024-06-14 22:45:01 +02:00
parent f6c358d807
commit 0094d806e8
8 changed files with 31 additions and 63 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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
# 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_quote_currency_amount $CURRENCY $f_asset || return 1
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
}