ccxt switch
This commit is contained in:
parent
f6c358d807
commit
0094d806e8
@ -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
|
- Runnable in a non-root docker containe
|
||||||
- multiple different buy and sell strategies possible at the same time
|
- multiple different buy and sell strategies possible at the same time
|
||||||
|
|
||||||
|
- Hedge mode (long and short positions the same time) not supported
|
||||||
|
|
||||||
## Buy conditions
|
## Buy conditions
|
||||||
- definable RSI Indicator signals min/max (RSI5, 14 and 21)
|
- definable RSI Indicator signals min/max (RSI5, 14 and 21)
|
||||||
- definable MACD signals min/max
|
- definable MACD signals min/max
|
||||||
|
@ -66,42 +66,7 @@ function check_buy_conditions {
|
|||||||
# remove CURRENCY from asset
|
# remove CURRENCY from asset
|
||||||
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
|
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
|
||||||
|
|
||||||
if [ ${STOCK_EXCHANGE} = "BINANCE" ]
|
order ${f_ASSET}/${CURRENCY} ${f_INVEST_QUANTITY} buy
|
||||||
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
|
|
||||||
f_BUY=""
|
f_BUY=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ function check_for_sell {
|
|||||||
then
|
then
|
||||||
local f_msg="ATTENTION! EMERGENCY STOP DUE TO POOR PERFORMANCE: BALANCE (${f_COMPLETE_BALANCE}) LOWER THEN EMERGENCY_STOP-VALUE (${EMERGENCY_STOP})"
|
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}"
|
g_echo_error "${f_msg}"
|
||||||
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
|
position_close ${f_ASSET}/${CURRENCY}
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
if tail -n1 $f_ASSET_HIST_FILE | egrep -q "^$(date +%Y-%m-%d)|$(date +%Y-%m-%d -d yesterday)"
|
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
|
else
|
||||||
local f_msg="SELL $f_ASSET_HIST_FILE no current data of invested asset"
|
local f_msg="SELL $f_ASSET_HIST_FILE no current data of invested asset"
|
||||||
g_echo_warn "${f_msg}"
|
g_echo_warn "${f_msg}"
|
||||||
do_trade ${f_ASSET} ${CURRENCY} ${f_QUANTITY_CURRENCY} sell "${f_msg}"
|
position_close ${f_ASSET}/${CURRENCY}
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
@ -89,14 +89,7 @@ function check_sell_conditions {
|
|||||||
g_echo_note "$f_SELL"
|
g_echo_note "$f_SELL"
|
||||||
echo "${f_last_line},${f_ASSET}" >>trade.log
|
echo "${f_last_line},${f_ASSET}" >>trade.log
|
||||||
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
|
f_ASSET=$(echo ${f_ASSET} | sed "s/${CURRENCY}//")
|
||||||
# binance_convert ${f_ASSET} ${CURRENCY} ${f_QUANTITY} sell "${f_SELL}"
|
position_close ${f_ASSET}/${CURRENCY}
|
||||||
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
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# # ANALYZE
|
# # ANALYZE
|
||||||
|
@ -32,7 +32,7 @@ function get_orders {
|
|||||||
then
|
then
|
||||||
if [ -z "$f_ccxt_json_out" ]
|
if [ -z "$f_ccxt_json_out" ]
|
||||||
then
|
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
|
continue
|
||||||
fi
|
fi
|
||||||
echo $f_ccxt_result | tee "CCXT_OPEN_ORDERS_${f_symbol_file}_RAW" | jq -r "
|
echo $f_ccxt_result | tee "CCXT_OPEN_ORDERS_${f_symbol_file}_RAW" | jq -r "
|
||||||
|
@ -21,15 +21,13 @@ function get_positions {
|
|||||||
jq -r "
|
jq -r "
|
||||||
.[] |
|
.[] |
|
||||||
select(.entryPrice != 0) |
|
select(.entryPrice != 0) |
|
||||||
.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring)
|
.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.contracts|tostring) + \",\" + (.contractSize|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.unrealizedPnl|tostring)
|
||||||
" CCXT_POSITIONS_RAW >POSITIONS
|
" CCXT_POSITIONS_RAW >CCXT_POSITIONS
|
||||||
|
|
||||||
get_position_array
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_position_array {
|
function get_position_array {
|
||||||
g_array POSITIONS f_get_positions_array
|
g_array CCXT_POSITIONS f_get_positions_array
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_position_line_vars {
|
function get_position_line_vars {
|
||||||
@ -44,6 +42,11 @@ function get_position_line_vars {
|
|||||||
[ -z "$f_position_side" ] && f_position_side="long"
|
[ -z "$f_position_side" ] && f_position_side="long"
|
||||||
f_position_leverage=${f_position_array[5]}
|
f_position_leverage=${f_position_array[5]}
|
||||||
[ -z "$f_position_leverage" ] && f_position_leverage="1"
|
[ -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
|
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_side" = short ] && g_percentage-diff $f_position_current_price $f_position_entry_price
|
||||||
f_position_pnl_percentage=$g_percentage_diff_result
|
f_position_pnl_percentage=$g_percentage_diff_result
|
||||||
|
@ -5,7 +5,7 @@ function get_symbols {
|
|||||||
local f_symbols=""
|
local f_symbols=""
|
||||||
[ -s CCXT_TICKERS_RAW ] && f_symbols=$(jq -r '.[] | .symbol' CCXT_TICKERS_RAW)
|
[ -s CCXT_TICKERS_RAW ] && f_symbols=$(jq -r '.[] | .symbol' CCXT_TICKERS_RAW)
|
||||||
f_symbols=${f_symbols//$'\n'/'+'}
|
f_symbols=${f_symbols//$'\n'/'+'}
|
||||||
|
|
||||||
if [ -z "$f_symbols" ]
|
if [ -z "$f_symbols" ]
|
||||||
then
|
then
|
||||||
g_echo_warn "Could not get symbols list - empty"
|
g_echo_warn "Could not get symbols list - empty"
|
||||||
|
@ -4,9 +4,9 @@ function order {
|
|||||||
|
|
||||||
# needed vars
|
# needed vars
|
||||||
local f_symbol=$1
|
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_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
|
local f_params f_type
|
||||||
|
|
||||||
|
|
||||||
@ -15,9 +15,6 @@ function order {
|
|||||||
# check symbol XXX/$CURRENCY[:$CURRENCY]
|
# check symbol XXX/$CURRENCY[:$CURRENCY]
|
||||||
[[ $f_symbol =~ /$CURRENCY ]] || return 1
|
[[ $f_symbol =~ /$CURRENCY ]] || return 1
|
||||||
|
|
||||||
# check amount
|
|
||||||
g_num_valid_number "$f_quote_currency_amount" || return 1
|
|
||||||
|
|
||||||
# check side
|
# check side
|
||||||
[ "$f_side" = "long" ] && f_side="buy"
|
[ "$f_side" = "long" ] && f_side="buy"
|
||||||
[ "$f_side" = "short" ] && f_side="sell"
|
[ "$f_side" = "short" ] && f_side="sell"
|
||||||
@ -34,11 +31,17 @@ function order {
|
|||||||
|
|
||||||
### validity checks end###
|
### validity checks end###
|
||||||
|
|
||||||
|
# get amount in crypto asset
|
||||||
# get amount in krypto asset
|
if [[ $f_amount =~ ^crypto_amount: ]]
|
||||||
local f_asset=${f_symbol///*}
|
then
|
||||||
currency_converter $f_quote_currency_amount $CURRENCY $f_asset || return 1
|
# if given in crypto
|
||||||
local f_amount=$f_currency_converter_result
|
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
|
# check for swap/margin trades
|
||||||
if [ -n "$LEVERAGE" ]
|
if [ -n "$LEVERAGE" ]
|
||||||
@ -64,5 +67,7 @@ function order {
|
|||||||
# do the 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
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user