Trade Code: How to Determine Ethereum Arbitrage Opportunities Using Command Line On Cloud

This tutorial is about determining arbitrage opportunity available between Kraken and Bitstamp, as their prices on BTCManager are shown on the ethereum price section. That being said, you can do this with any exchanges.

Set Up Digital Ocean Account

Arbitrage (“arb”) occurs when there’s a positive price difference between buying price at exchange A and selling price at exchange B. In this case, there’s a potential profit to be made. However, there are many factors which you should think before doing an arbitrage of which include price difference and transaction fees.

The reason for setting up a cloud infra account for this tutorial is because we’ll be doing a follow-up tutorial on getting push notifications based on arb-opportunities and there we will be monitoring arb opportunities 24/7. Thus having a cloud app will come in handy there since it’s a pain to keep your system running all the time.

We will use the cheapest droplet (a node) which will cost roughly $5 per month. First, you should sign up on Digital Ocean. Once you have signed up, you’ll have to pay a small fee for verification (The author picked PayPal, and the minimum was $5).

(Tip: Post verification, use code: DROPLET10 for additional $10 credit).

Now that payment and DigitalOcean account is setup, we will set up a droplet. Use these settings for setting up a droplet (after you click on Create Droplet).



Distributions: Ubuntu,

Size: Memory (1 GB 5$/month),

Datacenter Region: Closest to you.



Leave all other fields default and click on Create. Now, wait for few minutes while it sets up a droplet for you. Once it’s done, it will look like:

Now DigitalOcean will email you your login credentials for entering this machine using SSH.

The author is using a Macbook and SSHing your way to a server is super easy with Macs. The author is not familiar with Windows; however, if you are not able to figure out how to SSH using windows, the author suggests you use a virtual terminal provided on the droplet page itself.

To launch a virtual terminal go to access and click on Launch Console.



Coming back to Macbook, all you have to do is open terminal and type:

ssh [email protected] (replace IP-ADDRESS by IP you would have received an email along with username & password) and then enter a password.

It would then immediately prompt you to enter a password again for resetting) and then enter your new password twice and be done with resetting the password.

Pseudo Code

If sell_price (bitfinex) – buy_price(kraken) >= 0

Tell Arb



if sell_price (kraken) – buy_price(bitfinex) >= 0

Tell Arb

To write logic on top of prices, we first need to fetch prices from Bitfinex and Kraken. We will use their API to get prices in real time.

This is the endpoint of Bitfinex for getting price on ETH-USD pair: https://api.bitfinex.com/v1/pubticker/ethusd

Bitfinex Response looks like:



{“mid”:”762.325″,”bid”:”762.32″,”ask”:”762.33″,”last_price”:”762.33″,”low”:”754.8″,”high”:”838.0″,

“volume”:”272430.45160833″,”timestamp”:”1525609308.7313232″}

This is the endpoint for Kraken for getting price on ETH-USD pair:

https://api.kraken.com/0/public/Ticker?pair=ETHUSD

Kraken Response looks like:



{“error”:[],”result”:{“XETHZUSD”:{“a”:[“759.02000″,”1″,”1.000″],”b”:[“758.58000″,”4″,”4.000″],”c”:[“758.58000″,”1.74000000″],”v”:[“31735.14498170″,”49160.70000929″],”p”:[“787.71288″,”795.50191″],”t”:[12448,22418],”l”:[“751.11000″,”751.11000″],”h”:[“830.00000″,”830.00000″],”o”:”812.50000″}}}

Now we have to decode this Kraken response because the keys of this JSON response are meaningless, after some digging – you can find it on Kraken’s website.

Python Time

Once you have logged in on the machine as root user run this command to install python:

apt install python

Coding Time

When the author was learning to code a few years back, most tutorials would just have the author directly providing final code without much explanation. Very uneducational! That being said, in this tutorial – we’ll be breaking code into multiple snippets for you to understand it carefully.

To add code to the system – use vim editor and save it as <anyname>.py file, for example, the author has saved it as ‘a.py’.

Raw Code

Output:

Command : python <name>.py

Determining Arbitrage:

Reading parameters we want to read:

Kraken Ask Price = dataK[‘result’][‘XETHZUSD’][‘a’][0]

Kraken Bid Price = dataK[‘result’][‘XETHZUSD’][‘b’][0]

Bitfinex Ask Price = dataB[‘ask’]

Bitfinex Bid Price = dataB[‘bid’]

Here’s how the response looks after running the script few times.

Final code:

Raw Code

Conclusion

Our notifier is ready! Feel free to use it, tweak it, add more logic on top of it!

If you are using the code on any website, please give due credit. Stay tuned for the next tutorial, which will be about enabling push notifications periodically when arbitrage opportunities show up!

If you need any help in this process, comment here and the author will try his best to help you out!