From 6b51e610ff2387d848e8a9a15f39f39995c67d41 Mon Sep 17 00:00:00 2001 From: olli Date: Sun, 15 Dec 2024 15:26:03 +0100 Subject: [PATCH] sl/tp --- dabo/functions/order.sh | 32 ++++++++++++------- .../example_manage_positions.strategy.sh | 2 +- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/dabo/functions/order.sh b/dabo/functions/order.sh index 4b17a93..5666457 100644 --- a/dabo/functions/order.sh +++ b/dabo/functions/order.sh @@ -24,7 +24,7 @@ function order { # needed vars local f_symbol=$1 - local f_amount=$2 # amount in $CURRENCY / if crypto_amount:XXX then amount in crypto + local f_amount=$2 # amount in $CURRENCY / if asset_amount:XXX then amount in invested asset local f_side=$3 # buy/sell long/short local f_price=$4 # price for limit order - if "0" do market order - "stoploss" for pure StopLoss Order and "takeprofit" for pure TakeProfit Order local f_stoploss=$5 @@ -56,18 +56,22 @@ function order { if [[ $f_price = 0 ]] then f_type="market" - #elif [[ $f_price = stoploss ]] || [[ $f_price = takeprofit ]] - #then - # f_price="None" + elif [[ $f_price = stoploss ]] + then + f_type="stoploss" + f_price="None" + elif [[ $f_price = takeprofit ]] + then + f_type="takeprofit" + f_price="None" else f_type="limit" fi ### validity checks end ### - # check for swap/margin trades - if [ -n "$LEVERAGE" ] + if [ -n "$LEVERAGE" ] && [[ $f_price != stoploss ]] && [[ $f_price != takeprofit ]] then # do some margin things @@ -94,10 +98,10 @@ function order { # get amount in crypto asset - if [[ $f_amount =~ ^crypto_amount: ]] + if [[ $f_amount =~ ^asset_amount: ]] then # if given in crypto - f_amount=${f_amount//crypto_amount:} + f_amount=${f_amount//asset_amount:} else # on market order use current price if [[ $f_type = market ]] @@ -140,7 +144,7 @@ function order { then f_params="${f_params}'stopLoss': { 'triggerPrice': $f_stoploss, 'type': 'market' }, " # stoploss (change) for open position - elif [[ $f_price = "stoploss" ]] + elif [[ $f_type = "stoploss" ]] then f_params="${f_params}'reduceOnly': True, 'triggerPrice': $f_stoploss, 'triggerDirection': 'down', 'type': 'market, '" fi @@ -165,7 +169,10 @@ function order { f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_takeprofit}))" f_takeprofit=$f_ccxt_result [[ $f_type =~ limit|market ]] && f_params="${f_params}'takeProfit': { 'triggerPrice': $f_takeprofit, 'type': 'limit', 'price': $f_takeprofit, }, " - [[ $f_price = "takeprofit" ]] && f_params="${f_params}'stopPx': '$f_takeprofit', " + if [[ $f_type = "takeprofit" ]] + then + f_params="${f_params}'reduceOnly': True, 'triggerPrice': $f_takeprofit, 'triggerDirection': 'up', 'type': 'limit, '" + fi fi # end up params syntax with "}" @@ -181,9 +188,12 @@ function order { fi # do the order + # market order with or without stoploss/takeprofit [[ $f_type = limit ]] && local f_order="symbol='${f_symbol}', type='$f_type', price=$f_price, amount=${f_amount}, side='${f_side}', ${f_params}" [[ $f_type = market ]] && local f_order="symbol='${f_symbol}', type='$f_type', amount=${f_amount}, side='${f_side}', ${f_params}" - [[ $f_type = Stop ]] && local f_order="symbol='${f_symbol}', type='$f_type', amount='0', side='${f_side_opposide}', price='None', ${f_params}" + + # takeprofit/stoploss only + [[ $f_price = None ]] && local f_order="symbol='${f_symbol}', type='$f_type', amount=${f_amount}, side='${f_side_opposide}', price='None', ${f_params}" echo "$f_order" | notify.sh -s "ORDER" f_ccxt "print($STOCK_EXCHANGE.createOrder(${f_order}))" || return 1 diff --git a/strategies/example_manage_positions.strategy.sh b/strategies/example_manage_positions.strategy.sh index 0039eff..bd5f1c0 100644 --- a/strategies/example_manage_positions.strategy.sh +++ b/strategies/example_manage_positions.strategy.sh @@ -44,7 +44,7 @@ do # create new stoploss echo "==== New StopLoss for $asset at $stoploss_price" - order "$symbol" 0 long stoploss "$stoploss_price" + order "$symbol" "${p[${asset}_asset_amount]}" long stoploss "$stoploss_price" fi fi