[JAVA] I made a virtual currency arbitrage bot and tried to make money

Introduction

Aiming for interest and small money earning, I tried to develop a Bitcoin inter-exchange arbitrage (arbitrage) system. It was interesting to get various knowledge, so I will leave it as an output.

The content is quite old (around 2017-2018), so there may be various changes, but I'm not sure.

What is arbitrage?

It is also known as "without sheath". It is a financially historic trading method, and it seems that it has been certified as one of the world's three major profit-making laws.

What is the world's three major profit-making law "Arbitrage"? Explain the features, mechanism, and how to earn!|ABEYUTO (Yuto Abe) Official Media|ABEYUTO (Yuto Abe) Official Media

It is a method that can surely make a profit by taking an order by piercing the distortion at the moment when some distortion occurs in the market.

Distortion has quite a variety of patterns --Price difference between futures and spots --Price difference between currencies --Price difference between exchanges

There is something like that.

This time, I made a program aiming at the distortion of the price difference between exchanges.

Compared to stocks and historic financial products, virtual currencies have fewer functions on the exchange itself (such as not being able to sell). There is also a reason that it can only be done between exchanges in reality because the API of the exchange must also be published for execution.

Below, arbitrage is referred to as Abitra.

Where did the exchange use and what is the specific method?

I used. The specific method is as follows.

Pattern 1: Send money and sell after purchase

This is the pattern I tried first, buying at a cheap exchange, sending money and selling at a high exchange. ** This pattern didn't work for the reasons described below. ** **

Deposit the same amount of Japanese Yen to the exchange to be used as a preparation.

image.png

The program monitors price information and places buy orders at the cheaper price the moment a difference is created between exchanges.

Example)

1 BTC is 1,000,000 yen on Exchange A 1 BTC on Exchange B is 1,010,000 yen (price difference +10,000 yen)

Buy 0.1 BTC on Exchange A, transfer to Exchange B and sell

image.png

** This will instantly generate a profit of 1000 yen. ** **

I thought that if I continued this, I would be able to earn fully automatically, but ...

It was discovered that Bitcoin remittance was slower than I expected ... I thought that it would be sent in an instant after reading a book, but it took me 30 minutes or a few hours to spare. It is meaningless unless the transaction is completed during the momentary distortion. If this is XRP or a currency with fast remittance speed, this may be solved.

I've tried various things since then, but there are fundamental problems when incorporating remittance into the flow.

1. 1. The amount of money between the accounts will be out of balance, so you have to rebalance regularly.

Since the money is sent from the account you bought, it happens that there is neither Japanese Yen nor BTC in a specific account. Since it is difficult to deposit Japanese Yen automatically, it is necessary to regularly transfer BTC from the surplus exchange, but the logic there has become complicated.

2. Remittance fees are quite high and put pressure on profits

Depending on the exchange, the remittance fee is quite high, isn't it? (I was reading a Bitcoin book and thought that virtual currencies are characterized by low and fast transaction costs, but ...)

Originally, the arbitrage itself is a model that accumulates small profits steadily, so it is quite painful to raise the transaction cost for each transaction.

If you try to do it, you will only trade under conditions that make a profit even if you include the remittance fee, but there was a problem that the number of transactions would be greatly reduced.

3. 3. Not many exchanges can use the remittance API with this mechanism

In the first place, the remittance API is fairly secure (it is natural because it leads to illegal remittance).

The first few exchanges could be implemented in Gabagaba, but in increasing the number of exchanges, ――The money will be transferred one business day after you hit the remittance API and succeed. --The API parameter requires a 6-digit number for Google Auth two-step verification (what to do)

Something was found and it was really tough.

Pattern 2: Rebalance without sending money

I gave up because it seems to be difficult to incorporate remittance.

Looking at the price information of the exchange while making it, I noticed that ** basically cheap exchanges and high exchanges are mostly fixed, but there are moments of reversal several times a day. .. ** **

Therefore, put the same amount of Japanese Yen and BTC in all accounts.

--Orders to go for profit as arbitrage --Order to rebalance

I thought that it would go around if I rebalanced within the range that does not lose while taking profits by dividing it into

    1. initial state

image.png

  1. Price difference occurs 1 BTC is 1,000,000 yen on Exchange A 1 BTC on Exchange B is 1,010,000 yen (price difference +10,000 yen)

Buy 0.1 BTC on Exchange A, transfer to Exchange B and sell

image.png

    1. Rebalance

At this rate, BTC cannot be purchased at A, and BTC cannot be sold at B, resulting in lost opportunities. Therefore, when the prices of A and B are reversed, the opposite order is placed (transaction fees must also be taken into consideration).

image.png

Unlike the model, you can buy at a cheap price and sell at a high price at the moment when the price difference occurs. Buying and selling = The balance is lost and you cannot buy or sell on a specific exchange for a while.

So, to maximize profits

--- If there is a yen difference, place an order to go for profit --- If there is a yen difference, place an order to rebalance.

This parameter needs to be tuned diligently.

Since it was a time when the volatility of Bitcoin itself was high, I was doing frequent maintenance while looking at the data.

Also, since it holds a certain amount of BTC, there is always a risk of price fluctuations.

Was it profitable after all?

Due to the rise in the price of BTC, the comparison from the principal has increased considerably. (Actually, it was much less than the figure, so it's about the tears of a sparrow, but ...)

However, unexpectedly, it became difficult to tune the parameters so as not to lose the opportunity and the operating cost, and I was not motivated and I was disappointed.

Also, there are many behaviors that I do not understand well at each exchange. (Some exchanges have an amount that is far from the market price for a moment at 4 am ...)

I wanted to think that optimizing parameters would be an interesting area if combined with machine learning, but that was not the case.

If I have a chance, I would like to try again.

It's rough, but I'll upload the sauce as well. https://github.com/haruhiko-tano/arbitrage-bot

Recommended Posts

I made a virtual currency arbitrage bot and tried to make money
[LINE @] I tried to make a Japanese calendar Western calendar conversion BOT [Messaging API]
I tried to make a login function in Java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried JAX-RS and made a note of the procedure
I tried to make Java Optional and guard clause coexist
I tried to make a client of RESAS-API in Java
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
[Java] I tried to make a maze by the digging method ♪
I tried to make a group function (bulletin board) with Rails
How to make a Discord bot (Java)
I tried to make a parent class of a value object in Ruby
[iOS] I tried to make a processing application like Instagram with Swift
I tried to make a Web API that connects to DB with Quarkus
I tried to make my own transfer guide using OpenTripPlanner and GTFS
I tried to make a talk application in Java using AI "A3RT"
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make Basic authentication with Java
[LINE @] I tried to make a Japanese calendar Western calendar conversion BOT [Messaging API]
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried to develop a DUO3.0 study website.
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to create a LINE clone app
I made a Docker container to run Maven
I tried to link JavaFX and Spring Framework.
[Android] I tried to make a material list screen with ListView + Bottom Sheet
I made a LINE bot with Rails + heroku
[Small story] I tried to make the java ArrayList a little more convenient
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
Introducing what I made when I wanted to add a header and footer to RecyclerView
I tried to make an app that allows you to post and chat by genre ~ Where I had a hard time ~
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to make a new sorting algorithm, but I don't know if it's really new
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
I tried to read and output CSV with Outsystems
I tried to develop a ramen shop sharing website.
I tried to decorate the simple calendar a little
I tried to integrate AWS I oT button and Slack
[Ruby] I made a crawler with anemone and nokogiri.
I tried to create a Clova skill in Java
I made a Restful server and client in Spring.
I tried to make FizzBuzz that is uselessly flexible
I tried to chew C # (reading and writing files)
I tried to make a reply function of Rails Tutorial extension (Part 3): Corrected a misunderstanding of specifications
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I tried to develop a web application from a month and a half of programming learning history
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
I tried to make an app that allows you to post and chat by genre ~ App overview ~
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
I tried to make an application in 3 months from inexperienced