Cryptocurrency trading bot in ~40 lines of JavaScript
github.com
github.com
40 lines of JS definitely scare me.
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.
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.
As first rule, you shouldn't entrust anyone's life savings with a single trader.
Forty lines of Javascript to me is far less scary than 40,000 lines. At least with forty lines, I can easily tell what the hell it is trying to do.
In this case, it's just example code on how to use the API. Its "strategy" is to attempt to buy low and sell high. It doesn't really get any simpler than that.
This is basically a minimum viable product for others to play around with.
If that playing leads to bot trading competitions, fine. If it leads to someone building a 40k line monster and thinking it might be a good idea to entrust it with a meaningful proportion of anyone's savings.... No.
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.
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.
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
I made it pull from a third party global average because an individual market can shift a lot depending on how volatile it is.
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?
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
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.
The part that is hard are trading costs and slippage. But assuming that retail investor’s capital is relatively small and no trading costs (Alpaca provides this), an individual can do pretty well.
The parts that are hard for hedge funds is slippage, illiquidity of assets, trading costs, and client demands. As an individual you really don’t have to worry about those things and consequentially things become much easier.
The only downside is that it’s harder to get as much cheap leverage as the big guys. Also some (HFT) strategies are off limits to individuals, but that’s fine, because HFT really isn’t a good way to make money in the first place.
Data is available since 2019-03-30 for majority of the supported exchanges. Yes it's all from public APIs.
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.
ie 0.2 * 0.1
parseFloat((0.2 * 0.1).toFixed(10))
Shameless plug: use https://www.npmjs.com/package/bigint-money
Just believing really hard that the law doesn't apply to you doesn't make it true.
> 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).
We will most likely bring it back in as our fiat support matures.
Hell, I don't even know why you'd want to explicitly avoid KYC unless your going out of your way to create a money laundering service.
Also, there's some typos in those terms, ex: no space before email in "contact us atsupport@blockbid.io".
I can tell you why you want to avoid KYC: It's really annoying for the users and it's labor-intensive for the company.
One of the fundamental rules of money laundering is that anything that can be used for money laundering will be used for money laundering. Allowing anonymous users to shift from one crypto to another could be part of a money laundering routine. It does not have to include any fiat currency in the flow.
> I can tell you why you want to avoid KYC: It's really annoying for the users and it's labor-intensive for the company.
Ha! It's annoying, labor intensive, and a cost center for non-crypto companies as well. I can assure you that the entire non-crypto financial services industry would avoid it if they could as well.
What's the point? That's like exchanging cash for cash. I guess you could use it to "launder" crypto in the sense that you can make transfers off-chain. The exchange still has those records though, they don't need KYC to provide them, if necessary.
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.
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).
Furthermore, the sheer number of quite profitable high speed trading platforms seems to totally rebuke your assertion.
Luckily, there are now tools such as Accointing or CryptoTax that take care of the 30,000 transactions.
Your point is perfectly valid and is a useful addition to my prior post.
Otherwise, they could be an indication of future performance.
Just being visible in the OB will cause other people to act differently from if you weren't. This of course has a profound effect on strategies relying on passive orders.
I will give a shout to these guys if anyone is interested in back testing -> https://www.coinapi.io/
I'd be happy to answer any questions to the best of my ability.
This is a very naive bot, it will rarely ever execute at that margin but you will never lose either. You are just betting on the fact that hopefully someone buys out the orderbook by accident or because they really want that asset.
A more realistic margin is 0.0000001% but when you start doing that, you will start losing money so you will want a much better algorithmic trading strategy.
I will probably write a hedging tutorial next.
>trading money
>in javascript
Am I alone in thinking this is insane?
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.