top | item 20523119

Cryptocurrency trading bot in ~40 lines of JavaScript

180 points| thomasfromcdnjs | 6 years ago |github.com

77 comments

order
[+] gpderetta|6 years ago|reply
In in experience the vast majority of the code of a trading application is dedicated to safety: pre/post trade checks, price checks, open notional, pnl delta limits, order rate, fill rate etc. And of course error handling and getting out of unexpected states safely and quickly.

40 lines of JS definitely scare me.

[+] dcolkitt|6 years ago|reply
FWIW, I just took a look at our HFT trading system. We specialize in colocated low latency market making in an equity market in a mid-sized country. The following line counts are only for the C++ code directly used in the production system. Doesn't count research, model fitting, ETL pipelines, build systems, etc.

75 kloc - Total line count

10 kloc - Common libraries, utilities, data structures, etc.

10 kloc - Reconstitutes the limit order book from the data feed

10 kloc - General framework (order management, margin tracking, logging, runtime control hooks, etc.)

7 kloc - Safety checks (pre/post trade, price, circuit breakers, etc.)

13k - Adapter for the exchange's datafeed protocol

5k - Adapter for the exchange's order gateway protocol

3k - System for correlating our orders within the datafeed

10k - Signals/alphas

8k - Trading logic

Not claiming that other people's systems will necessarily look anything like ours, but my experience jibes with this too. Trading code is only about 10% of the total codebase for a production-ready trading system. Overall strategy-specific code (signals and trading logic) only make up 25% of the codebase, with the rest being all general-purpose "plumbing".

Even a tiny inconsequential function that most people never think about- reliably identifying which orders in the data feed belong to us- takes nearly half the amount of code as nearly all the trading logic put together.

[+] thomasfromcdnjs|6 years ago|reply
It definitely should scare you =D

The script is a tutorial to help complete beginners have an understanding of how to write a bot.

Though you're right, I should have really made it more clear because it is real money.

[+] flyGuyOnTheSly|6 years ago|reply
Agreed.

The bot I am currently working on is pushing 6,000 lines of code, and I feel like it might double in size before it's perfect.

[+] benj111|6 years ago|reply
"40 lines of JS definitely scare me"

Me too.

I suppose it depends how you want to treat this though.

As a bot programming challenge this seems fair enough. As something to entrust your or others life savings with? No.

[+] gildas|6 years ago|reply
You might want to replace the `openOrders.forEach ...` call with a call to `await Promise.all(openOrders.map ...)` if you want to catch errors and stop the program when cancelling previous orders, and more importantly make sure you cancel orders before doing anything else.
[+] thomasfromcdnjs|6 years ago|reply
Thanks for the tip, I didn't actually know that technique.
[+] dcsan|6 years ago|reply
This is dangerous haha!

It seems the price coming back from cryptonator doesn't update, TUSD is a stable coin but surely the bitcoin rate against it should be updating every 30s ? https://api.cryptonator.com/api/ticker/btc-tusd

https://www.cryptonator.com/api/ > Prices are updated every 30 seconds

[+] joosters|6 years ago|reply
It also seems exceptionally risky to grab a price off some random website and then trade blindly on another. Shouldn't you be getting the prices from the market you are actually trading in?
[+] dcsan|6 years ago|reply
my point is the API above doesn't seem to update much at all. Maybe every 30 minutes?

https://apiv2.bitcoinaverage.com/#price-data this seems like a better free API, wondering what others use? is there anything with tickdata not just 15s?

[+] rolltiide|6 years ago|reply
One redeeming quality of cryptocurrencies existing is that all the exchanges have REST compliant APIs and real time data available for free

The finance sector’s biggest meme for the last decade and a half was that PhD quants were required to program trading algorithms when really a single exchange cost $12,000 a month for real time data, it came with horrible documentation, support, antiquated protocols, FIX, and then you had to figure out how to co-locate your own server to connect to the exchange fast enough to prevent front running

The gatekeeping is what kept this specialized for the most part

[+] gpderetta|6 years ago|reply
> FIX

Those that do not understand FIX are doomed to reinvent it badly. I only got an appreciation for the protocol when I implemented a FIX server from scratch. Even the wierd text+field delimiter encoding is not bad (except for performance of course).

Session management, order entry and recovery in FIX is well designed and well documented (although it takes a few readings of the spec to put the pieces together). The biggest issue is the plethora of underdocumented extensions that have been bolted on to satisfy niche needs. If you are lucky you can ignore them most of the time.

Another big issue is a lot of exchanges treat FIX as more of a guideline than an hard spec and you have to workaround the lossage.

The best exchange binary protocols I have worked with (at least here in EU, borrow heavily from FIX.

[+] short_sells_poo|6 years ago|reply
> The gatekeeping is what kept this specialized for the most part.

It certainly kept the vast majority of retail out, but I disagree that this was the main cause of specialization. I think that the number of lone geniuses who could've beat the market using algo trading is a number approaching zero.

It's true that you don't need a PhD, or even higher education, as the math you need can be entirely self taught. However, you need plenty of specialized knowledge to avoid the accidental discharge of the vast number of very expensive footguns that litter the landscape. This knowledge can be gained from literature, but without having someone streetwise as a guide, it's going to be very difficult to discern genuine insight from wishful thinking.

[+] tardis_thad|6 years ago|reply
It's also one of the reasons why I've built https://tardis.dev (historical tick-level cryptocurrency market data replay API).
[+] Severian|6 years ago|reply
After a cursory glance, is it me or is floating point being used? I thought this was something you wanted to avoid because of precision errors.

ie 0.2 * 0.1

[+] uberneo|6 years ago|reply
This will work!!

parseFloat((0.2 * 0.1).toFixed(10))

[+] JustFinishedBSG|6 years ago|reply
> no KYC required

Just believing really hard that the law doesn't apply to you doesn't make it true.

[+] gridlockd|6 years ago|reply
> Just believing really hard that the law doesn't apply to you doesn't make it true.

If you just want to trade crypto/crypto, many exchanges don't require a KYC process. If you want to deposit/withdraw actual dollars, that's a different story.

In any event, KYC is something that exchanges are responsible for, not its customers.

[+] koolba|6 years ago|reply
I'm scratching my head wondering what the hell that actually that's supposed to mean. Their terms when you sign up do mention a KYC process:

> Blockbid is registered with the Australian Transaction Reports and Analysis Centre (AUSTRAC) which is the regulatory body that deals with the Anti-Money Laundering and Counter-Terrorism Financing Act 2006 (AML/CTF Act).

> [... truncated ...]

> Blockbid operates a Know Your Customer (KYC) and AML/CTF Program that meets the requirements of the AML/CTF Act (and associated laws).

[+] cdiddy2|6 years ago|reply
Ya, that probably wont last long if the exchange is centralized. Would have to re implement on bisq/kyber/uniswap or another decentralized exchange for this to last without KYC. Then of course you have to deal with massively increased latency/network speed to settle trades.
[+] mruts|6 years ago|reply
If any wants to try their hand at quant trading, Quantopian is a great place to get started. Great data sources and you can get allocated capital and make a little bit of money. Also the competitions are fun and you get paid $50/day for having (essentially) the best Sharpe ratio.

If you want to trade your own money, Alpaca is really awesome. Pretty easy to import an algorithm from Quantopian and use it on Alpaca.

I’ve been managing about 15% of my portfolio through Alpaca and it’s been great. On my account, I can get 4x intraday leverage, which has been absolutely awesome. Also there’s no trading costs, so I don’t have any problem with daily rebalancing (I actually rebalance to 4x leverage in the morning and deleverage at the end of the day).

Highly recommended! There’s a lot of alpha in the market for retail investors (with leverage, no transaction costs, and the ability to short).

[+] DennisP|6 years ago|reply
Quantopian looks amazing, I hadn't seen that. How much historical data and fundamentals do they have for equities?
[+] edf13|6 years ago|reply
Sounds suspiciously like an ad...
[+] logiclogic|6 years ago|reply
Advertisement for their API? Trading is gambling unless you have some insider info to increase your odds. Only rich or stupid people gamble.
[+] cubano|6 years ago|reply
Actually, it's not irrational for non-rich smart people to gamble ie poker is a game where skill almost always wins the day, and betting baseball parlays can, with a good deal of study, land in a positive expectation.

Furthermore, the sheer number of quite profitable high speed trading platforms seems to totally rebuke your assertion.

[+] Can_K|6 years ago|reply
Cool, until the tax declaration comes up.

Luckily, there are now tools such as Accointing or CryptoTax that take care of the 30,000 transactions.

[+] mercutio88|6 years ago|reply
won't tax declaration only apply to executed orders? those will be much lower.
[+] throwaway77384|6 years ago|reply
I understand that back-testing is a foolish way to work out whether a bot works, but it'd be interesting to see how this performs on arbitrary lengths of past history. Like, is this enough to beat a lazy index fund calculated over the same time, with interest reinvested?
[+] gentaro|6 years ago|reply
Why is backtesting foolish? As long as you haven't used the data you're using to backtest in any way, it seems like a reasonable way to test your strategy.
[+] thomasfromcdnjs|6 years ago|reply
Great to see people are interested in the tutorial.

I'd be happy to answer any questions to the best of my ability.

[+] uberneo|6 years ago|reply
I can see that there is no variable of "fees" which usually applies even on a LIMIT order. For example Coinbase Pro has Maker fees of 0.15%. Ideally this fees should be added to BUY order and subtracted from SELL order, as that will actually cost you.
[+] mercutio88|6 years ago|reply
Cool stuff. 20% Deviation from the market price is quite high though. I suppose orders will never get a chance to execute. What would be a more realistic percentage? Could it be based on the current volatility for example? Or am I missing something.
[+] snvzz|6 years ago|reply
>trading programmatically

>trading money

>in javascript

Am I alone in thinking this is insane?

[+] rubbingalcohol|6 years ago|reply
No, it is indeed fairly insane. It's a good proof-of-concept of API-based trading, but I am far too timid to trust this sort of thing to a bot, especially considering that other people are running better bots.
[+] notahacker|6 years ago|reply
tbf, you ought to be getting alarm bells from "Australian crypto exchange, no KYC required" long before we start looking at whether the coding approaches are remotely sane.

Considering the well established likelihood of a flash crash in the markets you're trading in, the exchange's own code being stuck together with duct tape or your/their assets being frozen, you probably shouldn't be trading anything you're not willing to lose to buggy javascript anyway.

[+] unnouinceput|6 years ago|reply
40 lines, yeah...I can do it in 2 lines even, if I use external libraries. body onload="mybot();" and boom, I'm done.
[+] alexeiz|6 years ago|reply
When did Blockbid exchange launch? Looks like just yesterday. The btc/usd order book is just 5 levels deep.
[+] throwaway857384|6 years ago|reply
It seems to be for a specific exchange that has $240 24 hour volume...
[+] epscylonb|6 years ago|reply
This looks great. Only 40 lines, that's some amazing golf right there.
[+] analpaper|6 years ago|reply
an included general purpose library with 40 lines of custom business logic? that software can't be very smart lol
[+] miroshnik|6 years ago|reply
Is it profitable?