Pages:
Author

Topic: Bitcoin arbitrage on GitHub: ~2% monthly return, market-neutral long/short - page 7. (Read 38256 times)

newbie
Activity: 6
Merit: 0
Hi Butor,

Thanks for all the elaborate answers.
Any updates on the software as of may/june?

Greets,

Lawk
newbie
Activity: 2
Merit: 0
newbie
Activity: 49
Merit: 0
Hi Mattsun,

Thank you for your message.

1. The 'short term' buy/sell, how is that operated? Do you buy BTC from an exchange (e.g. bitstamp), withdraw it from bitstamp then deposit to another exchange (e.g. bitfinex) and sell it? If that's the case, the long confirmation time might reduce the price difference or even switch the price position, how do you deal with that? If that's not the case, what do you mean by 'short sell'?

Again, there is no USD transfer and no BTC transfer between the exchanges. So we don't have to deal with the latency of withdraws and deposits. That's one of the advantages of long/short strategies. But let me explain what short selling means and maybe it will help you and others on this forum to understand how the strategy works. Short selling is not a Bitcoin thing and it is common in the financial market industry. I actually work on Wall Street so my examples will be closer to my actual job than Bitcoin.

  • First the basics: Let's say you think, based on your financial analysis or your favorite crystal ball, that the Apple stock will go up within the next few weeks and you want to profit from that. So today you buy 10 shares of AAPL at $100. Then the stock price goes up (your crystal ball was right!) and after two week AAPL is priced at $120. You decide to sell your 10 shares of AAPL. Your profit: 10x$120 - 10x$100 = $1,200 - $1,000 = $200. Well done! We say that you were long on this position (buy then sell later).

  • Now let's say that you think that the Apple stock will drop. You don't own any AAPL shares but you want to profit from that drop. So this is what you can do: you can ask your broker to borrow from him, say, 10 shares of AAPL and you promise that you will give him back these 10 shares later. Once your broker agrees to lend you the 10 shares (I won't go into the details about margin accounts) you can go to the market and sell these 10 shares, even if you don't actually own them!
    Let's say today AAPL is at $100. You borrow 10 shares from your broker and sell them on the market at $100. You make $1,000 of that selling, but you still have a debt of 10 shares towards your broker. Two weeks later, your crystal ball is right (again!) and the price of AAPL drops to $80. You decide to buy 10 shares on the market at $80 and give them back to your broker. That's it! You cleared your debt towards your broker and this is your profit on the operation: sell 10x$100 then buy back 10x$80 = $1,000 - $800 = $200. Well done again! The whole operation you just did is called short selling and we say you were short on this position (sell then buy later).

The examples I gave are speculative ones where you try to forecast the market. But short selling is also used to protect your investments against market fluctuation, in particular sudden market falls. Here is an oversimplified example to illustrate:

AAPL is at $100 and MSFT (Microsoft) is at $50. To be market-neutral (i.e. protected against market fluctuation) you can be long on AAPL and short on MSFT at the same time. For that you would buy 10 shares of AAPL (long $1,000) and short sell 20 shares of MSFT (short $1,000). Now let's see what happens when the market suddenly drops by 20%!
  • AAPL price goes from $100 to $80 (-20%).
  • MSFT price goes from $50 to $40 (-20%).

You decide to exit the market by closing your two positions:
  • You sell your AAPL shares for $800 (10x$80). You originally bought them $1,000: you lose $200 on that long position.
  • You buy back your MSFT shares for $800 (20x$40). You originally sold them for $1,000: you make $200 on that short position.

As you can see your loss is $0 thanks of the long/short strategy you used (long AAPL, short MSFT).

As you can imagine, if the whole market goes up your profit will be ...$0. So how do we make money in a long/short strategy? By betting on the discrepancy between the positions and this is exactly what we do with Blackbird on Bitcoin: if Bitfinex prices Bitcoin at $240 and Bitstamp prices at $200 we think that Bitfinex is overpriced while Bitstamp is underpriced: we go short on Bitfinex and go long on Bitstamp until the two prices are similar again. You will have more details about how the long/short strategy works with Blackbird on the GitHub page of the project.



2. Still the 'short sell', so far as I know, bitstamp doesn't have an API for 'Instant Transaction', you can only place a buy/ask offer by API. How do you 'short sell'?

You're right: as of today it's not possible to short sell on Bitstamp. However, it's possible to short sell on Bitfinex, OKCoin and others. Note that the short selling operation is usually pretty straightforward and the Bitcoin lending part is automatically done for you by the exchanges.



3. Do you always see the price position switch between the bitcoin exchanges you selected? I found some exchanges price position maintains the same all the time. For example, bitkonan's price are always higher than bitstamp.

Between Bitfinex, OKCoin and Bitstamp we see price discrepancies pretty often. It really depends on the overall Bitcoin volatility: the higher the volatility the higher the spreads between the exchanges. But check the prices right now (08/24/2015 21:55:25 EDT):

   Bitfinex:  204.54 / 204.55
   OKCoin:    201.13 / 201.27
   Bitstamp:  204.29 / 204.48

As you can see there is a good opportunity right now between Bitfinex (short) and OKCoin (long) with a 1.62% spread. A word to the wise...



4. The spreadEntry and spreadExit point, how do you decide the value? Do you have any reference or analysis behind?

Good question and that's all the challenge: SpreadEntry and SpreadExit are set based on the historical relationship between the exchanges. If you use values closer to zero you will get more trades, but less profit on each trade. Also, as explained above if the Bitcoin volatility is very low you will have difficulties to get trades and might need to lower your values. The Bitcoin volatility is now at 3.51%. You can check it here in realtime.

Note that I have some ideas to implement using dynamic spreads based on:
1. the short-term historical volatility
2. the fact that we want to let the profits run.

Stay tuned!
full member
Activity: 173
Merit: 100
btcmy.net
legendary
Activity: 1596
Merit: 1027
newbie
Activity: 2
Merit: 0
Hi, I'm really inspired by your job. But have a couple of questions about details of 'How it works'.
1. The 'short term' buy/sell, how is that operated? Do you buy BTC from an exchange (e.g. bitstamp), withdraw it from bitstamp then deposit to another exchange (e.g. bitfinex) and sell it? If that's the case, the long confirmation time might reduce the price difference or even switch the price position, how do you deal with that? If that's not the case, what do you mean by 'short sell'?
2. Still the 'short sell', so far as I know, bitstamp doesn't have an API for 'Instant Transaction', you can only place a buy/ask offer by API. How do you 'short sell'?
3. Do you always see the price position switch between the bitcoin exchanges you selected? I found some exchanges price position maintains the same all the time. For example, bitkonan's price are always higher than bitstamp.
4. The spreadEntry and spreadExit point, how do you decide the value? Do you have any reference or analysis behind?

Thanks. Smiley
sr. member
Activity: 410
Merit: 257
I'm working on just this, but I'm using java for now. If you are interested in a collab, let me know.
newbie
Activity: 49
Merit: 0
 
Just wanted to show you guys the actual output of an arbitrage trade if you are interested. It might help to understand the mechanism. Note that this is a real example with real money.

When we start the software it will first check the balances on all our Bitcoin exchanges. As you can see we don't have any Bitcoin (except a negligible amount on OKCoin) but only USD:

Blackbird Bitcoin Arbitrage
Version 0.0.2
DISCLAIMER: USE THE SOFTWARE AT YOUR OWN RISK.

[ Targets ]
   Spread to enter: 0.20%
   Spread to exit: -0.30%

[ Current balances ]
   Bitfinex:    1,299.15 USD   0.000000 BTC
   OKCoin:      1,318.32 USD   0.000582 BTC
   Bitstamp:    1,237.33 USD   0.000000 BTC
   Kraken:      0.00 USD       0.000000 BTC

After this initialization, the system starts to analyze all the spreads between Bitfinex, OKCoin, Bitstamp and Kraken every five seconds. It's only possible to short sell on Bitfinex and Kraken. The targets are different because the trading fees can vary depending on the exchange.

[ 07/10/2015 16:15:55 ]
   Bitfinex:    293.54 / 293.57
   OKCoin:      295.40 / 295.61
   Bitstamp:    291.40 / 292.43
   Kraken:      292.20 / 294.11
   ----------------------------
   Bitfinex/Kraken:     -0.47% [target  1.10%, min -0.47%, max -0.47%]
   OKCoin/Bitfinex:     -0.70% [target  1.00%, min -0.70%, max -0.70%]
   OKCoin/Kraken:       -1.15% [target  1.10%, min -1.15%, max -1.15%]
   Bitstamp/Bitfinex:    0.38% [target  1.10%, min  0.38%, max  0.38%]
   Bitstamp/Kraken:     -0.08% [target  1.20%, min -0.08%, max -0.08%]
   Kraken/Bitfinex:     -0.19% [target  1.10%, min -0.19%, max -0.19%]

[ 07/10/2015 16:16:00 ]
   Bitfinex:    293.54 / 293.57
   OKCoin:      294.65 / 295.28
   Bitstamp:    291.40 / 292.43
   Kraken:      292.20 / 294.11
   ----------------------------
   Bitfinex/Kraken:     -0.47% [target  1.10%, min -0.47%, max -0.47%]
   OKCoin/Bitfinex:     -0.59% [target  1.00%, min -0.70%, max -0.59%]
   OKCoin/Kraken:       -1.04% [target  1.10%, min -1.15%, max -1.04%]
   Bitstamp/Bitfinex:    0.38% [target  1.10%, min  0.38%, max  0.38%]
   Bitstamp/Kraken:     -0.08% [target  1.20%, min -0.08%, max -0.08%]
   Kraken/Bitfinex:     -0.19% [target  1.10%, min -0.19%, max -0.19%]

The software will continue to loop every five seconds, until there is an opportunity at 17:46:55: the spread between OKCoin (long) and Bitfinex (short) equals our 1.00% entry target:

[ 07/10/2015 17:46:50 ]
   Bitfinex:    286.67 / 286.87
   OKCoin:      284.24 / 284.25
   Bitstamp:    286.48 / 286.49
   Kraken:      285.16 / 287.24
   ----------------------------
   Bitfinex/Kraken:     -0.60% [target  1.10%, min -0.94%, max  0.29%]
   OKCoin/Bitfinex:      0.85% [target  1.00%, min -1.11%, max  0.92%]
   OKCoin/Kraken:        0.32% [target  1.10%, min -1.45%, max  0.95%]
   Bitstamp/Bitfinex:    0.06% [target  1.10%, min -0.59%, max  0.67%]
   Bitstamp/Kraken:     -0.46% [target  1.20%, min -0.60%, max  0.68%]
   Kraken/Bitfinex:     -0.20% [target  1.10%, min -1.22%, max  0.50%]

[ 07/10/2015 17:46:55 ]
   Bitfinex:    286.67 / 286.87
   OKCoin:      283.80 / 283.82
   Bitstamp:    285.94 / 286.49
   Kraken:      285.16 / 287.24
   ----------------------------
   Bitfinex/Kraken:     -0.60% [target  1.10%, min -0.94%, max  0.29%]
   OKCoin/Bitfinex:      1.00% [target  1.00%, min -1.11%, max  1.00%]

[ ENTRY FOUND ]
   Date & Time:       07/10/2015 17:46:55
   Exchange Long:     OKCoin (id 1)
   Exchange Short:    Bitfinex (id 0)
   Fees:              0.20% / 0.20%
   Price Long:        $283.82 (target)
   Price Short:       $286.67 (target)
   Spread:            1.00%
   Cash used:         $1,286.16 on each exchange

Trying to send a "buy" limit order: 4.53@$284.10...
Done (order ID: xx,xxx,xxx)

Trying to send a "sell" limit order: 4.49@$286.39...
Done (order ID: xxx,xxx,xxx)

Waiting for the two orders to be filled...
Done

Now that the orders are filled and that we are in the market, we are looking every five seconds for the spread to pass under our exit limit (-0.30%):

[ 07/10/2015 17:47:05 IN MARKET: Long OKCoin / Short Bitfinex ]
   Bitfinex:    286.54 / 286.85
   OKCoin:      283.80 / 283.82
   Bitstamp:    285.94 / 286.49
   Kraken:      285.16 / 287.24
   ----------------------------
   OKCoin/Bitfinex:      1.07% [target -0.30%, min  1.07%, max  1.07%]

[ 07/10/2015 17:47:10 IN MARKET: Long OKCoin / Short Bitfinex ]
   Bitfinex:    286.58 / 286.82
   OKCoin:      283.86 / 284.10
   Bitstamp:    286.02 / 286.60
   Kraken:      285.16 / 287.24
   ----------------------------
   OKCoin/Bitfinex:      1.04% [target -0.30%, min  1.04%, max  1.07%]

Then, at 19:04:10 the spread is under the exit limit (-0.32%). We close the two positions and compute the performance:

[ 07/10/2015 19:04:10 IN MARKET: Long OKCoin / Short Bitfinex ]
   Bitfinex:    284.32 / 284.40
   OKCoin:      285.31 / 285.50
   Bitstamp:    284.29 / 285.00
   Kraken:      284.50 / 288.79
   ----------------------------
   OKCoin/Bitfinex:     -0.32% [target -0.30%, min -0.32%, max  1.07%]

[ EXIT FOUND ]
   Date & Time:       07/10/2015 19:04:10
   Duration:          77.25 minutes
   Price Long:        $285.31 (target)
   Price Short:       $284.40 (target)
   Spread:            -0.32%
   ---------------------------
   Target Perf Long:  0.12% (fees incl.)
   Target Perf Short: 0.39% (fees incl.)
   ---------------------------

BTC exposure on OKCoin: 4.522520
BTC exposure on Bitfinex: -4.486550

Trying to send a "sell" limit order: 4.52@$284.40...
Done (order ID: xx,xxx,xxx)

Trying to send a "buy" limit order: 4.49@$284.59...
Done (order ID: xxx,xxx,xxx)

Waiting for the two orders to be filled...
Done

New balance on Bitfinex:        1,303.81 USD (perf $4.66), 0.000000 BTC
New balance on OKCoin:          1,319.06 USD (perf $0.74), 0.000520 BTC
New balance on Bitstamp:        1,237.33 USD (perf $0.00), 0.000000 BTC
New balance on Kraken:          0.00 USD (perf $0.00), 0.000000 BTC

ACTUAL PERFORMANCE: $5.40 (0.21%)

The system can send us an e-mail with our trade:


So we made $5.40 (+0.21%) in about 1 hour and 17 minutes with an exposure of $2,572.32 and no market risk. Note that the $5.40 is the actual profit after fees. To make sure that all the fees are taken into account the $5.40 amount comes from a straight comparison between the Bitcoin accounts (before the trade and after the trade). +0.21% doesn't seem a lot but remember that with this mechanism you don't get any losing trades since the system always waits for the spread to close (which can take days).

We are currently using the parameters enter=0.20% (plus fees, in this example the fees were 0.80%) and exit=-0.30% that are narrower than a few months ago. The reason being that the Bitcoin volatility these weeks is pretty low. The system needs a volatile market so that arbitrage opportunities can occur. As you can see in the following chart the Bitcoin volatility is pretty low these days (~1.86%):


Our hope is that the Bitcoin volatility re-increases so the spreads can be wider again. The volatility is a key factor of success in this kind of strategy.

I hope this post helped you understand the long/short arbitrage mechanism.


sr. member
Activity: 410
Merit: 257
I'm more a java guy, but I'd be interested to discuss strategies and such with you. I also have quite some implementations for other exchanges.

At the moment, I'd like to get rid of the REST APIs and move on to FIX. Should be quicker, I guess.

PM me, if you are interested in any form of a collab.
newbie
Activity: 40
Merit: 0
Hello, thanks for this work

I am trying to install it on my Linux mint 17 box but got this error message at make :

$HOME/blackbird-master > make
g++ -c -g -Wall -O2  base64.cpp -o base64.o
g++ -c -g -Wall -O2  bitcoin.cpp -o bitcoin.o
g++ -c -g -Wall -O2  bitfinex.cpp -o bitfinex.o
bitfinex.cpp: In function ‘bool Bitfinex::isOrderComplete(CURL*, Parameters, int)’:
bitfinex.cpp:91:68: error: ‘json_boolean_value’ was not declared in this scope
   isComplete = !json_boolean_value(json_object_get(root, "is_live"));
                                                                    ^
make: *** [bitfinex.o] Erreur 1


have you got any clues what went wrong

newbie
Activity: 49
Merit: 0

Thats great progress. Do you have a donation address or anything as such?
Thank you Bitcoin Explorer it's always appreciated. Here is my address: 3NmAGEeRmbdeMyaNjUaASr9Dv2NEssmafY


Lawk to answer your three questions:

Why C++? Maybe porting it to a Ruby or Python would give you more success in finding the coder you need?
Because of the speed. C++ is the main language for this kind of automatic trading program in the industry anyway (quantitative trading, HFT, etc). As you probably know compiled languages are usually far more faster than interpreted languages like Python.
For the moment my program analyzes the market every 5 seconds (can be adapted) and simply computes then compares the spreads between the exchanges. But in the future I would like to add more features within these 5-second loops, e.g. check the short-term volatility or save all the data in a database. So it's good to know that speed shouldn't be an issue. But yes, C++ is a pretty challenging language.


Any plans of adding USD/LTC? Since the framework is already there and all the major exchanges support it, why not add it too, it will increase the arbitrage opportunities a lot right?
Yes and that's something I want to do. Doing arbitrage on multiple exchanges between multiple cryptocurrencies is a great project. Note that the cryptocurrencies have to be liquid enough to allow fast order execution.


What other exchanges do you plan on adding? BTC-E? BTER?
The next exchange will probably be Kraken. As for now, the bid/ask information from Kraken is displayed but the system won't trade it. Again, since the orders have to be quickly executed we need to only add exchanges that offer good liquidity.
Also, it's now possible to short sell on OkCoin so I will need to add that too.
newbie
Activity: 6
Merit: 0
Very cool stuff!

Couple of questions:

- Why C++? Maybe porting it to a Ruby or Python would give you more success in finding the coder you need?
- Any plans of adding USD/LTC? Since the framework is already there and all the major exchanges support it, why not add it too, it will increase the arbitrage opportunities a lot right?
- What other exchanges do you plan on adding? BTC-E? BTER?

newbie
Activity: 49
Merit: 0
For information I added a new exchange, Bitstamp, to my arbitrage system.

https://github.com/butor/blackbird
newbie
Activity: 49
Merit: 0
Buying and holding bitcoin should have less risk of exchanges crashing as you can hold your balance off exchange. 

I see your point now and yes you're perfectly right. You can keep your Bitcoin investment off exchange. Now I don't know whether a Bitcoin wallet also has a risk of default...  Tongue
sr. member
Activity: 362
Merit: 262
...
Not true strictly as you can buy limited amount (say $1000) at a time and transfer bitcoin off the exchange. So you are only exposed to $1000 exchange risk but may be holding $10 000 worth of bitcoin in your own wallet.
This is not a buy and hold strategy.
The problem with arbitrage is you are always risking your principal on 2 exchanges.  You need the full amount on exchange to do the arbitrage and to maximise profits that are usually a small % of principal.  So if one of them goes you lose half your principal.

Actually the risk assessment is the same: twice the risk but only of half your investment.

The interesting question here is: in case of an exchange default, do you consider that you're going to lose 1) only your Bitcoins or 2) all your Bitcoins and all your cash on that exchange?

  • case 1: arbitrage is less risky than buy and hold since in arbitrage you spent lots of time in cash.
  • case 2: this is a real problem with arbitrage because if you want to do arbitrage on, say, 8 exchanges with $1000 cash on each then your total exposure to exchange default is $8000 even though your long/short Bitcoin exposure will be only $2000 at a time.
Buying and holding bitcoin should have less risk of exchanges crashing as you can hold your balance off exchange.  It has of course more price movement risk.  Not sure what you mean by buy and hold. 

Exchange risk is about losing both fiat and bitcoin balances.  These may have slightly different risks though.  But at some point an exchange can be completely gone in which case both balances will be gone.
Case 1: Arbitrage is less risky not because you spend time in cash, but because you are not taking views on prices and betting on that.
Case 2: Adding the exchanges distributes the risk.  This is good.  But of course typically you might end up with more total exposure, so it's a bit of a balance, as you point out. 
newbie
Activity: 49
Merit: 0
Very interesting concept.

I hope you don't abandon it.

Thank you. I won't. My goal now is really to find C++ enthusiasts that can help me improve the code quality and build new stuff.


Not true strictly as you can buy limited amount (say $1000) at a time and transfer bitcoin off the exchange. So you are only exposed to $1000 exchange risk but may be holding $10 000 worth of bitcoin in your own wallet.

This is not a buy and hold strategy.


The problem with arbitrage is you are always risking your principal on 2 exchanges.  You need the full amount on exchange to do the arbitrage and to maximise profits that are usually a small % of principal.  So if one of them goes you lose half your principal.

Actually the risk assessment is the same: twice the risk but only of half your investment.

The interesting question here is: in case of an exchange default, do you consider that you're going to lose 1) only your Bitcoins or 2) all your Bitcoins and all your cash on that exchange?

  • case 1: arbitrage is less risky than buy and hold since in arbitrage you spent lots of time in cash.
  • case 2: this is a real problem with arbitrage because if you want to do arbitrage on, say, 8 exchanges with $1000 cash on each then your total exposure to exchange default is $8000 even though your long/short Bitcoin exposure will be only $2000 at a time.


sr. member
Activity: 362
Merit: 262
Even if it's true, the impact of that default risk is pretty much the same whether you are on an arbitrage strategy or a buy and hold strategy.
Not true strictly as you can buy limited amount (say $1000) at a time and transfer bitcoin off the exchange. So you are only exposed to $1000 exchange risk but may be holding $10 000 worth of bitcoin in your own wallet.

The problem with arbitrage is you are always risking your principal on 2 exchanges.  You need the full amount on exchange to do the arbitrage and to maximise profits that are usually a small % of principal.  So if one of them goes you lose half your principal.
hero member
Activity: 490
Merit: 500
thank you, i will take it a look.
full member
Activity: 251
Merit: 100

Very interesting concept.

I hope you don't abandon it.
newbie
Activity: 49
Merit: 0
In that case the yearly return is less than 30% and judging by what happened to bitcoin platforms in previous years it has been a negative return. That is because the risk of default from these places was far greater than the 30% return. And have not most major currencies inflated greatly the past year? It seems a lot of extra work and risk to slightly beat out the most transparent stocks in Western countries.

Are you saying that, according to you, more than 30% of the current Bitcoin exchanges will go bankrupt within one year from now? Did that happen in the past? Note that I'm only considering the large exchanges that offer enough liquidity to do short-term trading.

Even if it's true, the impact of that default risk is pretty much the same whether you are on an arbitrage strategy or a buy and hold strategy.

Pages:
Jump to: