[USEFUL] Cryptocurrency exchange arbitrage script source code [BASH]

in #linux7 years ago

Greetings, guys! Recently I'd tried cryptoarbitrage stuff and I was interested are there any crypto-pairs that may show an interesting difference for exchange of this type.

I've written two scripts. The first is showing you only cryproarbitrage pairs (buy and transfer crypto), the second also works for fiat (but I wouldn't recommend it: high commissions, unpredictable delays, verification, limits etc.)

I won't tell a lot about this script (see it for yourself) - it just calculates difference between Yobit, WEX, Exmo exchanges and also I'm adopting it for several others now. For your comfort, I'm showing the price difference in percent %. The rates are shown too, along with trade volumes (be sure to check volume - high-differenced crypto often has too low volume to make a buy/sell fast).

So the arbitrage goes like this: you look where the coin is cheaper and where it's expensive, if the % is high, you buy, than transfer and sell. Then repeat! Sometimes you can make 10-20 or even 25-30% on one transfer of this kind. Some of you might like to transfer money back with earnings, too (currently my script cannot find pair correlations so you have to do it manually).

The first METARBITRAGE script was created to make it easier for you to track the price difference. It works only with cryptocurrencies (also it shows bitcoin futures price):

MetarbitrageCrypto

Here is the second crypto-fiat arbitrage script which has more pairs:

Meta2

Of course, you could go on without this arbitrage linux/bash script, but looking for 4-10 pairs & 3-4 exchanges manually is not so straightforward and comfortable.

Crypto.sh

#!/bin/bash

clear

tickerswex=( ltc_btc nmc_btc nvc_btc ppc_btc dsh_btc dsh_ltc dsh_eth eth_btc eth_ltc bch_btc bch_ltc bch_eth bch_dsh zec_btc )
tickersyob=( ltc_btc nmc_btc nvc_btc ppc_btc dash_btc dash_ltc dash_eth eth_btc eth_ltc bcc_btc bcc_ltc bcc_eth bcc_dash zec_btc )
tickersexm=( LTC_BTC NMC_BTC NVC_BTC PPC_BTC DASH_BTC DASH_LTC DASH_ETH ETH_BTC ETH_ltc BCH_BTC BCH_LTC BCH_ETH BCH_DASH ZEC_BTC )
tickersol=( LTC_BTC NMC_BTC NVC_BTC PPC_BTC DASH_BTC DASH_LTC DASH_ETH ETH_BTC ETH_ltc BCH_BTC BCH_LTC BCH_ETH BCH_DASH ZEC_BTC )

end=$(echo ${tickerswex[@]} | wc -w)

d=$(curl -s http://cfe.cboe.com/cfe-products/xbt-cboe-bitcoin-futures | html2text | grep -A5 "Cboe XBT Bitcoin Futures Trading Data")

echo "\/------------------------------------------------------------------------------\/"
echo "|| M E T A R B I T R A G E                                    Handle with care! ||"
echo "|| This script helps you to arbitrage cryptocurrencies between exchanges.       ||"
echo "|| Please wait, scanning $end crypocurrency pairs on two exchanges.               ||"
echo "||                                        Written by Security XIII on 8.12.2017 ||"
echo "\/------------------------------------------------------------------------------\/"
echo ""
echo "$d"
echo ""

text=( "PAIR NAME" "YOBIT.NET" "Y volume" "WEX.NZ" "W volume" "EXMO.ME" "E Volume" "OpenLedger" "O Volume" "DIFF. Y/W" "DIFF. Y/E" "DIFF. W/E" )
printf "%-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-30s\n" " ${text[0]}" "${text[1]}" "${text[2]}" "${text[3]}" "${text[4]}" "${text[5]}" "${text[6]}" "${text[7]}" "${text[8]}" "${text[9]}" "${text[10]}" "${text[11]}"
echo "-------------------------------------------------------------------------------------------------------------------------------"

for i in `seq 0 $(($end-1))`; do

    w=""; y=""; e=""; r=""; r2=""; r3="";

    curl -f -s https://wex.nz/api/3/ticker/"${tickerswex[i]}" > wfile
    w=$(cat wfile | jq '.'"${tickerswex[i]}"'.sell')
    wv=$(cat wfile | jq '.'"${tickerswex[i]}"'.vol')
    rm wfile

    curl -f -s https://yobit.net/api/3/ticker/"${tickersyob[i]}" > yfile
    y=$(cat yfile | jq '.'"${tickersyob[i]}"'.sell')
    yv=$(cat yfile | jq '.'"${tickersyob[i]}"'.vol')
    rm yfile

    curl -f -s https://api.exmo.com/v1/ticker > efile
    e=$(cat efile | jq '.'"${tickersexm[i]}"'.sell_price' | awk -F'"' '{ print $2 }')
    ev=$(cat efile | jq '.'"${tickersexm[i]}"'.vol' | awk -F'"' '{ print $2 }')
    rm efile

    curl -f -s https://stats.openledger.info/api/asset/pairs > ofile
    o=$(cat ofile | jq '.'"${tickersol[i]}"'.last')
    ov=$(cat ofile | jq '.'"${tickersol[i]}"'.baseVolume')
    rm ofile

    [[ $w == null ]] && w=""; [[ $y == null ]] && y=""; [[ $e == null ]] && e=""; 
    [[ $wv == null ]] && wv=""; [[ $yv == null ]] && yv=""; [[ $ev == null ]] && ev="";
    [[ $o == null ]] && o=""; [[ $ov == null ]] && ov="";

    [[ $w =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $y =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r=$(echo "scale=8;(($y/$w)-1)*100" | bc)
    [[ $y =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $e =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r2=$(echo "scale=8;(($y/$e)-1)*100" | bc)
    [[ $e =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $w =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r3=$(echo "scale=8;(($w/$e)-1)*100" | bc)


    printf "%-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-20s\n" "${tickersyob[$i]}" "${y:0:10}" "${yv:0:10}" "${w:0:10}" "${wv:0:10}" "${e:0:10}" "${ev:0:10}" "${o:0:10}" "${ov:0:10}" "${r:0:10}" "${r2:0:10}" "${r3:0:10}"

done

echo "-------------------------------------------------------------------------------------------------------------------------------"

Cryptofiat.sh

#!/bin/bash

clear

tickerswex=( btc_usd btc_eur btc_rur ltc_btc ltc_usd ltc_eur ltc_rur nmc_btc nmc_usd nvc_btc nvc_usd usd_rur eur_rur ppc_btc ppc_usd dsh_btc dsh_rur dsh_usd dsh_eth eth_btc eth_usd eth_eur eth_rur bch_usd bch_btc bch_rur bch_eth zec_btc zec_usd )
tickersyob=( btc_usd btc_eur btc_rur ltc_btc ltc_usd ltc_eur ltc_rur nmc_btc nmc_usd nvc_btc nvc_usd usd_rur eur_rur ppc_btc ppc_usd dash_btc dash_rur dash_usd dash_eth eth_btc eth_usd eth_eur eth_rur bcc_usd bcc_btc bcc_rur bcc_eth zec_btc zec_usd )
tickersexm=( BTC_USD BTC_EUR BTC_RUB LTC_BTC LTC_USD LTC_EUR LTC_RUB NMC_BTC NMC_USD NVC_BTC NVC_USD USD_RUB EUR_RUB PPC_BTC PPC_USD DASH_BTS DASH_RUB DASH_USD DASH_ETH ETH_BTC ETH_USD ETH_EUR ETH_RUB BCH_USD BCH_BTC BCH_RUB BCH_ETH ZEC_BTC ZEC_USD )

end=$(echo ${tickerswex[@]} | wc -w)

echo "\/------------------------------------------------------------------------------\/"
echo "|| M E T A R B I T R A G E                                    Handle with care! ||"
echo "|| This script helps you to arbitrage cryptocurrencies between exchanges.       ||"
echo "|| Please wait, scanning $end crypocurrency pairs on two exchanges.               ||"
echo "||                                        Written by Security XIII on 8.12.2017 ||"
echo "\/------------------------------------------------------------------------------\/"
echo ""
text=( "PAIR NAME" "YOBIT.NET" "Y vol" "WEX.NZ" "W vol" "EXMO.ME" "E Vol" "DIFF. Y/W" "DIFF. Y/E" "DIFF. W/E" )
printf "%-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-30s\n" " ${text[0]}" "${text[1]}" "${text[2]}" "${text[3]}" "${text[4]}" "${text[5]}" "${text[6]}" "${text[7]}" "${text[8]}" "${text[9]}"
echo "-------------------------------------------------------------------------------------------------------------------------------"

for i in `seq 0 $(($end-1))`; do

    w=""; y=""; e=""; r=""; r2=""; r3="";

    curl -f -s https://wex.nz/api/3/ticker/"${tickerswex[i]}" > wfile
    w=$(cat wfile | jq '.'"${tickerswex[i]}"'.sell')
    wv=$(cat wfile | jq '.'"${tickerswex[i]}"'.vol')
    rm wfile

    curl -f -s https://yobit.net/api/3/ticker/"${tickersyob[i]}" > yfile
    y=$(cat yfile | jq '.'"${tickersyob[i]}"'.sell')
    yv=$(cat yfile | jq '.'"${tickersyob[i]}"'.vol')
    rm yfile

    curl -f -s https://api.exmo.com/v1/ticker > efile
    e=$(cat efile | jq '.'"${tickersexm[i]}"'.sell_price' | awk -F'"' '{ print $2 }')
    ev=$(cat efile | jq '.'"${tickersexm[i]}"'.vol' | awk -F'"' '{ print $2 }')
    rm efile

    [[ $w == null ]] && w=""; [[ $y == null ]] && y=""; [[ $e == null ]] && e=""; 
    [[ $wv == null ]] && wv=""; [[ $yv == null ]] && yv=""; [[ $ev == null ]] && ev="";
 
    [[ $w =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $y =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r=$(echo "scale=8;(($y/$w)-1)*100" | bc)
    [[ $y =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $e =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r2=$(echo "scale=8;(($y/$e)-1)*100" | bc)
    [[ $e =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && [[ $w =~ ^[+-]?[0-9]+\.?[0-9]*$ ]] && r3=$(echo "scale=8;(($w/$e)-1)*100" | bc)


    printf "%-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-10s | %-20s\n" "${tickersyob[$i]}" "${y:0:10}" "${yv:0:10}" "${w:0:10}" "${wv:0:10}" "${e:0:10}" "${ev:0:10}" "${r:0:10}" "${r2:0:10}" "${r3:0:10}"

done

echo "-------------------------------------------------------------------------------------------------------------------------------"
#sleep 60

Also, you can take the script from my pastebin:

https://pastebin.com/u/sxiii

If you d'like to withdraw your cryptocurrency to fiat money, please take a look at exchange list service, which even has pairs that gives you bonus on withdrawal: https://www.bestchange.ru

Have a good investing mood and financial smiles ;)
Den Ivanov aka @SXIII from Random City


Sort:  

Nice piece of code mate and thank you for sharing it. There aren't many people that would share such information, probable afraid from other doing it and them not to be able to do it anymore. While it doesn't have big gains, arbitrage is a way of increasing your balance through exchanges.

Thank you @cryptorg for your comment. I also found more developed product on github here: https://github.com/manu354/cryptocurrency-arbitrage

@fifelue is it still so after Chinese bought etherdelta?

Whoa, cool script! And very well/cleanly written.

Why did you use hard coded file names instead of something like WFILE=$(mktemp)
YFILE=$(mktemp)
etc?

A small note for Ubuntu users, you'll also need the html2text and jq packages installed.

Thank you very much for your comment :) I'm afraid I don't know how to work with mktemp files in the bash. I will definitely look this topic & learn it for my future use, thanks @not-a-bird !

NP.

Essentially, you just invoke mktemp and it creates a file and returns that file's name. So you can use it anywhere you'd use a hard-coded file name.

So by doing WFILE=$(mktemp) you get something like /tmp/tmp.CqOZSJZb5y in your WFILE variable. So where you deal with wfile it would look something like this:

WFILE=$(mktemp)
curl -f -s https://wex.nz/api/3/ticker/"${tickerswex[i]}" > "${WFILE}"
w=$(cat "${WFILE}" | jq '.'"${tickerswex[i]}"'.sell')
wv=$(cat "${WFILE}" | jq '.'"${tickerswex[i]}"'.vol')
rm "${WFILE}"

You could do it for wfile, yfile, efile...

Sorry if I over-explained that.

If you want to sell this file for Bitcoins, use https://SatoshiDisk.com/
Don't need to create account or pass KYC. Upload, Share, Profit!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 60526.80
ETH 2335.69
USDT 1.00
SBD 2.53