diff --git a/dabo/functions/order.sh b/dabo/functions/order.sh new file mode 100644 index 0000000..2fcbcd6 --- /dev/null +++ b/dabo/functions/order.sh @@ -0,0 +1,68 @@ +function order { + # Info for log + g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@" + + # needed vars + local f_symbol=$1 + local f_quote_currency_amount=$2 # amount in $CURRENCY + local f_side=$3 # buy/sell long/short + local f_price=$4 # price for liomit order - if not given do market order + local f_params f_type + + + ### validity checks ### + + # 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" + [[ $f_side =~ ^buy$|^sell$ ]] || return 1 + + # check order type limit/market + if [ -z "$f_price" ] + then + f_type="market" + f_price=0 + else + f_type="limit" + fi + + ### 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 + + # check for swap/margin trades + if [ -n "$LEVERAGE" ] + then + # do some margin things + + # check for CCXT swap symbol :$CURRENCY + [[ $f_symbol =~ : ]] || f_symbol="$f_symbol:$CURRENCY" + + # set position mode + f_ccxt "$STOCK_EXCHANGE.setPositionMode(hedged=False, symbol='$f_symbol')" || return 1 + + # set leverage + f_ccxt "$STOCK_EXCHANGE.setLeverage($LEVERAGE, '$f_symbol')" || return 1 + + # define margibn mode isolated/cross + f_params="params={'marginMode': '$MARGIN_MODE'}" + else + # short/sell not possible in spot market + [[ $f_side =~ ^sell$ ]] || return 1 + fi + + # 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 + +} +