Cryptocurrency Trading Bot


A bazillion years ago, when I worked at Morgan Stanley. There was a big sign above the entrance and exit to the trading floor:  “DID YOU MAKE MONEY TODAY?”  If you are in finance that is the only metric that matters.

This summer I wrote a paper trading bot to arbitrage bid/ask spreads across different crypto exchanges. Here is the git repo – it is in progress and there is one glaring bug so if you run it you will lose $$.

So lets talk a little bit about what is going on here and how these exchanges work.

Well someone offers to buy 3 bitcoin for $100, someone else offers to buy .5 bitcoin for $105 and so forth. This builds one side of the order book, and the different quantities and prices represent the depth of the order book.

Then there is the other side of the order book, someone offers to sell 2 bitcoin for $200, another person offers to sell .5 bitcoin for $195.

The trade is made when someone else offers to take the other side. If I come in and say yes! I will buy 2 bitcoin from you for $200. Bam the trade is made and that is removed from the order book.

Here is an example json of the order book from hitbtc, an exchange where you can trade crypto.

Now the bid/ask arbitrage is when you can lock in a profit. When on one exchange you can buy 2 bitcoin for $200 and immediately sell it for $205. This will not happen on the same exchange but it can happen on different exchanges, so if you have a script that is constantly monitoring a bunch of currencies and a bunch of exchanges.

This does not happen in regular exchanges for normal people like me, and probably you if you are reading this blog instead of something on your bloomberg terminal. These types of trades happen very quickly. For traditional exchanges, you want to be as close as physically possible to the exchange, and so forth.

For the crypto exchanges, there is no physical exchange, except perhaps for Gemini, which I think has a physical exchange. So there is no direct pipe to the exchange. The reason why you can arb in this way is because of the Risk.

So this is the other big idea in trading RISK! What kind of risk are you taking, and what is your risk versus your reward. Theoretically if you are executing the trade instantaneously then what is the risk?

As Milton Friedman said – There aint no such thing as a free lunch.

Aint that the truth! When you are making all this money on this supposedly risk free arbitrage you are still taking on risk but what is that risk?

Well in the case of these crypto exchanges you have exchange risk. The exchange may shut down, may be hacked, you can lose all your money. That is the big risk and you are being compensated by it with the arbitrage trade.

There is also currency risk. If you trade your crypto into some sovereign currency (USD, EUR, etc), there is a high transaction cost and you will lose money. In order to make money you have to keep your money in crypto and maybe exchange into USD once a day. This means you have exposure to the directionality of the crypto. You may make money on each trade, but if crypto goes down one day it does not matter because you will have lost money relative to the dollar.  You can probably mitigate this with options, but it is something to consider.

Other things to consider are: the percentage the exchange takes on each trade, slippage, that is if you are unable to execute at the price you want, if the market moves and you can only execute one side of the trade and so you have to unwind the trade of keep the position on. But these are run of the mill trading issues. There is nothing special about it being in crypto.

This is interesting. I have been looking into deep learning algorithms for predicting crypto prices but it is difficult because time series data is autocorrelated. Each tick or trade is based on the previous trade. I want to look into this aspect in greater detail. I am not interested in numerical methods.

Decentralized Protocols


This week I worked on some scuttlebutt with Tenor and Ben. Although I building the prayer project on Ethereum, itself a decentralized protocol, I never looked too much at the general landscape of decentralized protocols… until now.  To get myself up to speed I listen to the DAT podcast, a third web podcast on scuttlebutt. This second on was an interesting podcast where the founder of scuttlebutt (sea slang for rumor), distinguishes between cyberspace (the space of communication) and cypherspace (the space of algorithms).

I downloaded Patchwork (a social network built on scuttlebutt) then I ran a patchwork  server. I thought maybe I should build a prayer sb tool, and so I looked at this chat code.  After listening to  Francis‘ talk at Recurse and read a bunch of papers and visited sites. I started going through the proto school exercises which I did not know were made by protocol labs that includes things like ipfs, Nomad and raft . I downloaded beaker, which uses dat, not scuttlebutt. AND I saw great fantastic animation of how raft works – I suggest you watch it if you are interested.

The decentralized tech I was familiar with before was blockstack. They have a great git repo, but looking at all these other initiatives has really opened by eyes.

A gossip protocol is a way to describe how peer to peer nodes share information.  Nodes have a rule  that determines what messages the believe. This rule is the consensus algorithm (I think). There is also no guarantee that every node will receive every message.

So the question is, do we think that bitcoin and ethereum are gossip protocols???  According to the internet it is unclear. Gavin refers bitcoin as using a gossip protocol. Other people do not. The bitcoin and ethereum protocols are heavier than just a gossip protocol – they consensus built in. It seems that gossip is not about consensus.  Scuttlebutt is not looking for universal consensus. Each node controls what truth is. But that consensus is layered on top of a gossip protocol. Gossip is not interested in agreeing with other nodes, but it making a decision for itself.  But I am new to this so I may be off base.  The difference appears to be with the topology of the network. Everyeone

There is an idea of a blockchain like system. Scuttlebutt uses Kappa Architecture, where all activity is appended immutably to a log file. However it is not the log file that all nodes must agree on but the message … I think…. I could prob

After going through scuttlebutt, it seems like a useful architecture for lightweight decentralized communication and decision making but not right for the prayer project. One of the things I am interested in with the prayer project is the notion of action or execution of a prayer. Scuttlebutt is about communication and sure we can build something on top to execute, but I like coopting the logic of exchange built into currency for another purpose.