Compare commits

..

3 Commits

Author SHA1 Message Date
b79313d1e4 example manage positions 2024-12-12 16:24:19 +01:00
694184a25f update fpr ccxt triggers, stoploss-change 2024-12-12 16:23:47 +01:00
0a6d7264cb functions: cancel order(s) 2024-12-12 16:23:15 +01:00
3 changed files with 120 additions and 37 deletions

View File

@ -26,12 +26,11 @@ function order {
local f_symbol=$1
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 limit order - if 0 do market order
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
local f_takeprofit=$6
local f_params="params={"
local f_type
local f_type f_side_opposite
### validity checks ###
@ -39,19 +38,32 @@ function order {
[[ $f_symbol =~ /$CURRENCY ]] || return 1
# check side
[ "$f_side" = "long" ] && f_side="buy"
[ "$f_side" = "short" ] && f_side="sell"
if [ "$f_side" = "long" ] || [ "$f_side" = "buy" ]
then
f_side="buy"
f_pos_side="Long"
f_side_opposide="sell"
fi
if [ "$f_side" = "short" ] || [ "$f_side" = "sell" ]
then
f_side="sell"
f_pos_side="Short"
f_side_opposide="buy"
fi
[[ $f_side =~ ^buy$|^sell$ ]] || return 1
# check order type limit/market
if [[ "$f_price" = "0" ]]
if [[ $f_price = 0 ]]
then
f_type="market"
#elif [[ $f_price = stoploss ]] || [[ $f_price = takeprofit ]]
#then
# f_price="None"
else
f_type="limit"
fi
### validity checks end###
### validity checks end ###
# check for swap/margin trades
@ -68,7 +80,8 @@ function order {
# set leverage
f_ccxt "$STOCK_EXCHANGE.setLeverage($LEVERAGE, '$f_symbol')" || return 1
# define margibn mode isolated/cross
# define margin mode isolated/cross
#[[ $f_type =~ limit|market ]] &&
f_params="${f_params}'marginMode': '$MARGIN_MODE', "
# calculate amount with leverage
@ -102,7 +115,7 @@ function order {
fi
# Add stoploos and take profit if available
# Add stoploss and take profit if available
if [ -n "$f_stoploss" ]
then
if [[ $f_type = limit ]]
@ -122,7 +135,15 @@ function order {
fi
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_stoploss}))"
f_stoploss=$f_ccxt_result
f_params="${f_params}'stopLossPrice': '$f_stoploss', "
# market or limit order with stoploss
if [[ $f_type =~ limit|market ]]
then
f_params="${f_params}'stopLoss': { 'triggerPrice': $f_stoploss, 'type': 'market' }, "
# stoploss (change) for open position
elif [[ $f_price = "stoploss" ]]
then
f_params="${f_params}'reduceOnly': True, 'triggerPrice': $f_stoploss, 'triggerDirection': 'down', 'type': 'market, '"
fi
fi
if [ -n "$f_takeprofit" ]
then
@ -143,7 +164,8 @@ function order {
fi
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_takeprofit}))"
f_takeprofit=$f_ccxt_result
f_params="${f_params}'takeProfitPrice': '$f_takeprofit', "
[[ $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', "
fi
# end up params syntax with "}"
@ -161,6 +183,7 @@ function order {
# do the order
[[ $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}"
echo "$f_order" | notify.sh -s "ORDER"
f_ccxt "print($STOCK_EXCHANGE.createOrder(${f_order}))" || return 1

View File

@ -41,17 +41,55 @@ function order_cancel {
g_echo_note "No orders for $f_symbol/$f_asset found"
return 0
fi
# for f_order in "${f_get_orders_array[@]}"
# do
# get_order_line_vars "$f_order"
# if [[ $f_symbol = $f_order_symbol ]]
# then
# f_ccxt "print(${STOCK_EXCHANGE}.cancelAllOrders('$f_symbol'))"
# get_orders "$f_symbol"
# get_orders_array
# fi
# done
}
function order_cancel_all {
# Info for log
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_order
get_symbols_ticker
get_orders_array
for f_order in "${f_get_orders_array[@]}"
do
get_order_line_vars "$f_order"
if [[ $f_symbol = $f_order_symbol ]]
then
f_ccxt "print(${STOCK_EXCHANGE}.cancelAllOrders('$f_symbol'))"
get_orders "$f_symbol"
get_orders_array
fi
done
}
function order_cancel_id {
# Info for log
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
local f_symbol=$1
local f_id=$2
local f_order
get_symbols_ticker
get_orders "$f_symbol"
get_orders_array
local f_asset=${f_symbol//:$CURRENCY/}
f_asset=${f_asset//\//}
if grep -q "$f_asset.*$f_id" values-orders
then
f_ccxt "print(${STOCK_EXCHANGE}.cancelOrder(id='${f_id}', symbol='${f_symbol}'))"
get_orders "$f_symbol"
get_orders_array
else
g_echo_note "No orders for $f_symbol/$f_asset with id $f_id found"
return 1
fi
}

View File

@ -13,18 +13,40 @@ get_position_array
# go through trading symbols
for symbol in ${f_symbols_array_trade[@]}
do
echo "==== XXXXX $symbol"
asset=${symbol//:$CURRENCY/}
asset=${asset//\//}
# adjust stoploss from percentage profit
from_profit=1
if [ -n "$LEVERAGE" ]
then
g_calc "${from_profit}*${LEVERAGE}"
from_profit=$g_calc_result
fi
# save profit by switching stoploss in profit
if [ -n "${p[${asset}_pnl]}" ] && [[ ${p[${asset}_side]} = long ]]
then
if g_num_is_higher ${p[${asset}_pnl_percentage]} $from_profit
then
# calculate stoploss price with half of current pnl
g_calc "${p[${asset}_current_price]}-((${p[${asset}_current_price]}-${p[${asset}_entry_price]})/2)"
stoploss_price=$g_calc_result
# check for already existing stoploss
if [ -n "${o[${asset}_sl_close_long_id]}" ]
then
# do nothing if current stoploss price is already larger/equal then half of current pnl
g_num_is_higher_equal ${o[${asset}_sl_close_long_stopprice]} $stoploss_price && continue
# cancel existing stoploss order
order_cancel_id "$symbol" "${o[${asset}_sl_close_long_id]}" || continue
fi
# create new stoploss
echo "==== New StopLoss for $asset at $stoploss_price"
order "$symbol" 0 long stoploss "$stoploss_price"
fi
fi
done
##### WARNING! This strategy is only intended as an example and should not be used with real trades. Please develop your own strategy ######
# if you want to use ist remove the next line with return 0
#return 0
# create stoploss order
#print(exchange.createOrder(symbol='XRP/USDT:USDT', type='Stop', side='sell', amount='0', price='None', params={'posSide': 'Long', 'stopPx':0.5}))
#print(exchange.createOrder(symbol='XRP/USDT:USDT', type='Stop', amount='0', side='sell', price='None', params={'posSide': 'Long', 'stopPx': 0.5}))
# edit stoploss
#print(exchange.editOrder(id='debc0df8-10c7-4cfd-95f7-b4de9cc60332', symbol='XRP/USDT:USDT', type='Stop', side='sell', amount='101', params={'stopLossPrice': 0.48}))