Permalink
Browse files

bitfinex: more madness

  • Loading branch information...
lexszero committed Jun 4, 2015
1 parent 336cc69 commit 05645a7a4199565cbb13fed8de37f3d8b66ebb65
Showing with 116 additions and 4 deletions.
  1. +105 −4 bitfinex.com/bitfinex_utils
  2. +11 −0 utils/define_colors.sh
@@ -2,6 +2,10 @@
BITFINEX_KEY=
BITFINEX_SECRET=
BITFINEX_API_URL="https://api.bitfinex.com/v1"
source "$(dirname $(readlink -f "$0"))/../utils/define_colors.sh"
nonce() {
local nonce=$(cat nonce 2>/dev/null)
nonce=${nonce:-0}
@@ -14,6 +18,10 @@ dbg() {
>&2 echo "$@"
}
api_open() {
curl -s "${BITFINEX_API_URL}${1}"
}
api() {
[[ -z "$BITFINEX_KEY" || -z "$BITFINEX_SECRET" ]] && {
dbg "ERROR: you should set BITFINEX_KEY and BITFINEX_SECRET"
@@ -30,14 +38,107 @@ api() {
-H "X-BFX-PAYLOAD: $payload" \
-H "X-BFX-SIGNATURE: $signature" \
-d "" \
https://api.bitfinex.com/v1${request}
"${BITFINEX_API_URL}${request}"
}
bfx_ticker() {
api_open /pubticker/${1}
}
bfx_stats() {
api_open /stats/${1}
}
bfx_lendbook() {
api_open /lendbook/${1}
}
bfx_orderbook() {
api_open /book/${1}
}
bfx_trades() {
api_open /trades/${1}
}
bfx_lends() {
api_open /lends/${1}
}
get_history() {
bfx_symbols() {
api_open /symbols
}
bfx_symbols_details() {
api_open /symbols_details
}
bfx_history() {
api /history <<<"{\"currency\":\"$1\", \"wallet\":\"$2\"}"
}
bfx_deposit_profit() {
get_history $1 deposit |
bfx_history_movements() {
api /history/movements <<<"{\"currency\":\"$1\"}"
}
bfx_offers() {
api /offers <<<"{}"
}
bfx_credits() {
api /credits <<<"{}"
}
bfx_pretty_profit() {
bfx_history $1 deposit |
jq 'reduce .[] as $item (0; . + if ($item.description | contains("Swap Payment")) then ($item.amount | tonumber) else 0 end)'
}
_bfx_convert_rate() {
#bc -l <<<"e(1/365*l(1+${1}/100))"
echo "$[$1/365]"
}
_bfx_pretty_offers() {
jq -r '.[] | [.currency, .amount, .rate, .timestamp, .period | tostring] | join("\t")' | {
echo -e "${BWhi}Amount ${RCol}${BWhi}Rate ${RCol}${BWhi}Estimated profit ${RCol}${BWhi}Days left ${RCol}${BWhi}Opened at $RCol"
echo -e "═════════════════╪════════════╪══════════════════╪═══════════╪════════════════════"
while read currency amount rate timestamp period; do
timestamp=${timestamp%%.*}
now=$(date +%s)
rate=$(_bfx_convert_rate $rate)
printf "%-3s ${Gre}%12f${RCol} │ %8.4f %% │ %-3s ${BIGre}%12f${RCol} │ %-9d │ %-15s\n" \
"$currency" \
"$amount" \
"$[rate]" \
"$currency" \
"$[amount*rate]" \
"$[period - (now-timestamp)/86400]" \
"$(date --date="@$timestamp" "+%X %x")"
done
echo -e "─────────────────┴────────────┴──────────────────┴───────────┴────────────────────"
}
echo -ne "$RCol"
}
bfx_pretty_swap_state() {
echo
echo "${BGre}Swaps currently provided${RCol}"
echo
bfx_credits | _bfx_pretty_offers
echo
echo "${BYel}Unfilled swap offers${RCol}"
echo
bfx_offers | _bfx_pretty_offers
echo
echo "${BCya}Lendbook (USD)${RCol}"
echo
echo "${BWhi} --=={ ${BRed}Bids ${BWhi}}==-- --=={ ${BGre}Asks ${BWhi}}==--"
bfx_lendbook USD | \
jq -r '[.bids, .asks] | transpose | map({"bid": .[0], "ask": .[1]}) | .[] | [.bid.amount, ((.bid.rate | tonumber)/365|tostring), .ask.amount, ((.ask.rate|tonumber)/365|tostring)] | join("\t")' | head -n 10 |
while read bamnt bid aamnt ask; do
printf "${BWhi}%6.4f ${Whi}[%10.2f] ${BWhi}%6.4f ${Whi}[%10.2f]\n" "$bid" "$bamnt" "$ask" "$aamnt"
done
echo -ne "${RCol}"
}
@@ -0,0 +1,11 @@
RCol='\e[0m' # Text Reset
# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m';
Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m';
Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m';
Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m';
Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m';
Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m';
Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m';
Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m';

0 comments on commit 05645a7

Please sign in to comment.