8.5 KiB
| fecha |
|---|
| 2022-06-21 |
Me reuno con Dani, que ha estado echándole un ojo al tema del arbitraje en Bisq.
Definitions
ArbitrageCombo
An ArbritrageCombo is a series of buys and sells in one or several BTC-XXX markets executed with the hope that TotalPortfolioValue is larger at the end of the ArbitrageCombo.
If markets were perfect, it would be impossible to run a profitable ArbitrageCombo. The expected gain of any ArbitrageCombo would be 0 (ignoring fees. If fees are taken into account, it would be negative). Given that we know that markets are definetely not perfect, there should be a certain degree of hope in finding profitable. Whether market imperfection is enough to make a nice amount of money out of it is uncertain.
Balances
A balance is the available amount of a certain currency to execute trades. The bunch of different currencies at play compose the CurrencyPortfolio. Given that ArbitrageCombos consist on making several trades in different ExchangePairs and different ways, but...
- A perfect match of amounts will almost never available.
- bisq requires initial BTC deposits to execute operations ..., a certain amount of all the used currencies must always be available. The existing balances limit the amount of ArbitrageCombos that can be executed.
Assuming that the only goal of arbitrage is to maximize value, there should be a target Balance allocation for the CurrencyPortfolio. This means, there should be a conscious decision on how the the total wealth of the operation should be distributed across the different currencies. Otherwise, the allcoation may drift into one or other currency, possibly reaching a situation where no more arbitrage can be done because there is a shortage of one of the involved currencies. Furthermore, there are other motivations to have a target allocation in the CurrencyPortfolio (such as risk willing to be assumed or preference towards holding certain currencies).
For example, an arbitrage agent could have a target of having 0.1BTC and the rest of the value split as 50% € and 50% $. Whenever this balance gets broken, different strategies can be used to recover it. Mainly:
- Only executing ArbitrageCombos that, besides generating profit, tip the balance favourably towards the allocation target.
- Executing neutral trades (neutral as in, profit = 0)
The TotalPortfolioValue is the value, measured in €, of the CurrencyPortfolio. For example, balances in €, $ and BTC are converted to € because of its role as unit of account to measure value.
The end goal of the whole arbitrage operation is to increase the TotalPortfolioValue of the CurrencyPortfolio with no or very little risk.
Exchange Pair
An ExchangePair is the combination of two currencies which can be traded in Bisq. Trades can happen in both ways (BTC -> Other currency / Other currency -> BTC).
TradeOffer
A TradeOffer is a public commitment from a bisq participant (the maker) to engage in a trade with certain conditions. The trade is binding, meaning that once someone takes it (the taker), the maker is forced into executing it. You can picture the maker's offer as a contract with his signature on it. Anyone can sign the taker spot and start the binding transaction.
A trade offer contains, at least, the following details:
- A BTC amount.
- A secondary currency amount.
- A price (which is obviously the proportion between the previous two)
- The role for each currency (what is the maker giving away and what is taker giving away)
- A payment method for the secondary currency
- A taker security deposit in BTC, which gets returned after the trade is succesful
- A bisq trading fee, which can be paid in either BSQ or BTC
- A mining fee, which can only be paid in BTC
An offer is profitable, in the sense that it increases the Total
Logic
ArbitrageSeeker
An ArbitrageSeeker is a little script that monitors all available TradeOffers and tries to compose a profitable ArbitrageComboPlan. An ArbitrageComboPlan is simply a possible ArbitrageCombo that hasn't been committed yet.
The ArbitrageComboPlan is mostly a bunch of TradeOffers tied together. The execution of all of the included TradeOffers would result in a change in the Balances. Hence, we introduce the concept of PreArbitragePortfolio and PostArbitragePortfolio. Two metrics are used to measure the difference in the Pre and Post Balances:
- The TotalPortfolioValue. An ArbitrageComboPlan where the difference in TotalPortfolioValue is <= 0 is a stupidity, since no profit is gained.
- The weights of the different currencies in the CurrencyPortfolio change.
How to compose such a ArbitrageComboPlan, or the best one? A few intuitions:
- The only way any TradeOffer can generate profit is when the price of it is below market price. So, any ArbitrageCombo should have at least one TradeOffer below market rates.
- To balance things out so that the weights of the CurrencyPortfolio don't drift too hard, we should ideally find other TradeOffers with the same currency pair, but in the other way. The additional TradeOffers can improve or reduce profitability, depending on their distance to market prices.
- This poses the existence of a trade-off: when building an ArbitrageComboPlan, there might be the need to choose between profit and CurrencyPortfolio balancedness. Some rule needs to be specified to decide how to weight both.
With the previous ideas in mind, a pseudo-codish approach could be as follows:
I started trying to build a small logic to get this done, but once you get into it you end up realising how it's actually a pretty dense optimization problem. A few options on how to deal with it reasonably:
- Build a nasty, not so elegant heuristic with a few hardcodes in there to approximate how someone would try to build an ArbitrageComboPlan manually. Not too bad, probably the first thing that should be tried.
- Given that the volume of profitable trades should be relatively small (<50) a basic combinatorial heuristic could also go places easily I guess.
- In dreamland, a Genetic Algorithm could be a fun idea to try. My intuition is that, for small amounts of candidate TradeOffers, the combinatorial heuristic would be better and faster, but as the number increases, the Genetic Algorithm would end up being more efficient at some point.
Other random thoughts
Agile approach
- We discussed that the first step towards bringing this live would be to simply implement something roughly like the ArbitrageSeeker described above and let it run constantly for some time to assess how frequently opportunities arise. A new idea came to my mind. While the previous still holds true, I realised there is a possible additional step which might be very interesting and not take that much work: implementing a fake CurrencyPortfolio and simulating that offers are actually taken. Something similar to webpages that let you play on the real stock market with fake money. We would assign an initial balance to the agent and let it "take offers" in a fakish way. After running it for some time, we can actually see how well it did and have data to answer the question: if we had actually used this, how much would we have earned?
Risks
- The larger the set of available currencies and payment methods is, the bigger the chance of securing profitable ArbitrageCombos.
- Small, illiquid currencies are probably where the heftiest opportunities will come from. I'm thinking about markets which have smallish volumes, but are not completely dead, such as the Brazilian Real or the Swiss Frank. The problem here is that both come with two risks: one is that exchange rates from-to € might chew up the profitability from the arbitrage (since these are not really currencies we want to hold mid-long term). The second one is that these exchange rates might not be so easy to include real-time into the calculation of the profitability of an ArbitrageComboPlan. My only idea on how this could be executed practically is using Revolut, which easily enables payments in many currencies with low fess, but AFAIK those guys don't provide an API service to simulate currency exchange rates and fees.
- Taking multiple offers is not an atomic operation. When we decide to execute an ArbitrageCombo, there is a risk that some offers are available at the time of taking and others stop being available or change conditions, making them unprofitable. This is very unlikely if the lag between designing an ArbitrageComboPlan and moving forward with it is short (seconds), but it is nevertheless possible. To optimize for this kind of issue, it might be a smart idea to take offers in a predefined sequence that tries to minimize the negative impact of one of them failing.