Follow Me As I Create An AI Trading Bot For Bitcoin / Crypto

The Doctor

Senior Member
Joined
Dec 18, 2010
Messages
1,109
Reaction score
474
Before crypto got big, my software engineering efforts were mainly focused on marketing. A couple years ago, I got to work for an ICO which propelled me into the world of crypto and fintech. I then got into trading and since I was already algorithmically inclined, went deep into trading maths.

I have made several backtests using mostly straight-forward algebra. I've created several forecasters and advanced machine learning models for timeseries data. What haven't I done? I haven't taken the most advanced artificial intelligence models I know of and applied them to the problem of trading. Crypto is highly volatile. It is for this reason that a bot should be able to make a killing, if so expertly engineered.

I do have some proprietary efforts going on but I thought to myself "Someone has to put some source out worth a damn. Why not me? Who would I share it with?" The answer (Now that BHW has this section), is here, with you.

Initial thoughts:
  • CCXT for OHLCV data & trade actions.
  • Websockets? Will look into it.
  • Most volume is probably at Bitmex exchange. Let's use it.
  • This will be done in Python of course.
  • Let's start with some LSTM variants. Differentiable Neural Computer maybe? hmm...
  • What about the optimizer agent? Q Learner? Genetic? Bayesian? Let's try some.
  • Get a Github repo going. That will come first.
  • Google Colaboratory can help everyone follow along. Not everyone has a Titan GPU like me :p
Let's do this thing.

Github Link: https://github.com/TheDoctorAI/AI-Trader
 
Last edited:
1st! I am interested in this journey even I am not into tranding and crypto,. ;) Goodluck op
 
Before creating an automated tool, do you have a working trading stategy? Seems like you would need at least to know how to make money trading before automating anything.

I do! I have many actually that I have already backtested. Some of those backtests are really, really good. However, They don't generalize well. So they tend to work good when the market is ranging but they don't respond well to big market changes - when volatility goes sick-house. Of course that's not the end of the world. You can still make a ton of money, even if the market goes against you during times of big change. I have some backtests with between 80% and 100% success rates tested over the past year of trading data. I don't like that it doesn't generalize though. A good trading agent should be able to respond to whatever happens. Moreover, a good trading agent should be an intelligent one. Not just following some algebraic indicators and if/then rules.

Machine learning can do this. To your point, I don't need to take a trading methodology and then have machine learning execute it. Rather, the AI will learn its own methodology, far better than I could teach it.
 
Where did you learn machine learning to accomplish this? Recommend any starting point?

Same place everyone starts a learning experience. youTube. So much good info all you have to do is search, sit back, watch, and learn.

youtube.com/watch?v=ukzFI9rgwfU
 
Good luck to your journey. I'm a trader. I will try your project.
 
Thank you everyone for your interest. I have some extra time this evening so let's move our Github repo forward a bit. How shall we begin? When I last left off, I had already coded a downloader for OHLCV data. It uses the CCXT library to fetch market data from any arbitrary cryptocurrency exchange/symbol/timeframe. That script is here: https://github.com/TheDoctorAI/AI-Trader/blob/master/download_market.ipynb

Running These Scripts

Machine learning has seen amazing innovations recently. One such innovation from Google is called Colaboratory. When I first started learning AI, I bought a super expensive GPU so that I could train my models quickly. Then, Google went and released a free service that allows anyone/everyone to use Google's GPUs & TPUs (Tensor Processing Unit) for free! That's just... Wow. What a lucky time for people getting into AI. It also means that I can easily share these models with you all and you can run them on expensive hardware in a few clicks at zero cost. If you visit any of the .ipynb files in the Github I linked here, you'll see an "Open in Colab" button at the top:

gfCtZtS


Note: If you're going to run my AI models, don't forget to (In Colab, for each ipynb example) click Runtime -> Change runtime type and then select GPU under hardware accelerator. No need for TPU at this stage (It won't make it faster). GPU will. Also, I have coded these so that they save and load data from Google drive. You will probably have to authorize the script to load the .csv file. Google will step you through that automatically when you run the notebooks (It's easy).

Our First Model: Multidimensional LSTM Bitcoin Forecast

We have a long way to go but we will start where I imagine everyone invariably does - LSTM (Long/Short-Term Memory) neural networks. The power of LSTMs can't be overstated. An LSTM is a recurrent neural network that incorporates a memory mechanism. If a neural network were a computer, the memory module of an LSTM would be the RAM & memory controller. I'm not going to get into the particulars of gradient descent here but suffice to say, LSTM solves some important problems that makes it useful for this task of forecasting a market.

Most Bitcoin LSTM model examples you will find only forecast the close price. Well, that's pretty lame. Any serious effort should attempt to forecast the OHLCV (Open/High/Low/Close/Volume). There are also technical indicators to consider (Moving averages, Ichimoku, Bollinger bands, etc). By training on multiple vectors/dimensions, we can incorporate a relatively arbitrary number of inputs. We won't be adding any technical indicators just yet but just know that we plan to investigate that. I especially have some ideas about using technical indicators and convolution down the line.

I have directed our downloader to grab 1h OHLCV data from the Bitmex exchange and save it to CSV. We then load the CSV into our model and out comes our forecast:

E99YeRf


Very good. We can see that we are fitting to the data nicely. The LSTM has learned the general patterns of Bitcoin and has now forecasted. I had to limit the amount of candles in the dataset to less than 400 due to an annoyance in the model that cropped up when adapting it to BTC/USD. We'll deal with that later. It is important though as we need to see how much data we can benefit training on.

What's exciting here is we can see that not only is our model projecting our fitted line into the future, it's also learning Bitcoin's movement patterns. This forecast has Bitcoin running back up to about $5,550 and completing the classic M pattern we so often see on the chart. This is an impressive intuition on behalf of the neural network. Of course we can argue about the complexity of the model but still, the resulting pattern is undeniable. If I were charting on my own right now with my own intuition, this is the sort of pattern I would project. Again, very impressive. We still need to do some accuracy tests. More to come on that.

Note: Anyone wanting to continuously use this forecaster, all you have to do is run the downloader notebook and then run the forecaster (Both on Colab). The model takes about 5 minutes to train. I have also added some code that prints out the forecast vectors at the end of the script (So you can do what you want with it).

What's Next

Alright, so. We need some more code for evaluating accuracy. We also need some more models. mLSTM is far from the best we can do. Very far. It is however a good start. Also, this is not a trading agent/bot model. We can't backtest it because it doesn't optimize for actual trading. For that, we'll need a different algorithm. Of course models like the one we just used can contribute to an agent's understanding.

Links

The market loader: https://github.com/TheDoctorAI/AI-Trader/blob/master/download_market.ipynb

The resulting csv: https://github.com/TheDoctorAI/AI-Trader/blob/master/data/btcusd-1d.csv

The AI: https://github.com/TheDoctorAI/AI-Trader/blob/master/lstm_forecaster.ipynb



Much more to come. Stay tuned.
 
Last edited:
Following the thread, The Doctor always caught my attention, I know him by his infiniproxy thread, unfortunately that project did not end well, but the mistakes are to learn.
I wish you luck The Doctor, it's a pleasure to have you back in the forum after a while.
 
I have made a quick fix & modification to the Colab notebook. Got rid of adj close from the plots. Will remove completely from the model at some point but will leave the code alone for now for anyone wanting to test it. There are a few more improvements off the top of my head. I'll contribute more code either tomorrow or in a few days.

YbSJaHS



Following the thread, The Doctor always caught my attention, I know him by his infiniproxy thread, unfortunately that project did not end well, but the mistakes are to learn.
I wish you luck The Doctor, it's a pleasure to have you back in the forum after a while.

Thanks! Infiniproxy was good for a while but once it grew to many VPS servers and users, the code that pooled them all through the website (Not the underlying proxy server code) had trouble scaling. I wound up putting it to rest and going on to work in crypto. I actually have a new thread pending approval to sell Infiniproxy's underlying technology. It's a simple (Very stable) IPv6 admin panel you can install on any server/VPS as I'm selling the source-code. I will use the extra $ to fund more AI.
 
That looks like it is something good enough to trade on, LOL!

It is, sort of. It's good as a technical indicator and indeed it's probably more accurate than 99% of technical indicators you will find on a charting platform. However, it's not good enough for the sort of bot we're trying to make here. Not that it isn't a decent forecast but it's not a trading agent. This code only says "I think the price will be X," not "I think a trade at price X and exiting at Y will be profitable." There is a difference between the two. We could use this model to make some rule like "If price is forecasted to be greater than P% movement or maybe greater than S standard deviations, place a trade in the corresponding direction of movement." As reasonable as that sounds, an agent that actually leans how to trade on its own should be vastly more powerful.
 
May i know why it sometimes gives way different results like if you compare your last post to the one before it?
 
Just be careful not to do curve fitting. You might want to optimize it a little but not to the point, where you start getting nearly perfect results.
 
May i know why it sometimes gives way different results like if you compare your last post to the one before it?

I haven't run this one enough times to be sure what you're asking but I think what you're seeing was the result of another hour passing and then removing the adj. close vector. So it's either more accurate now (The last figure) or it doesn't much matter.

crypto is the most dangerous asset to trade, take care

Thanks. It is. Of course it's also the most profitable.

Just be careful not to do curve fitting. You might want to optimize it a little but not to the point, where you start getting nearly perfect results.

Yes, this model shouldn't be overfitting the way I have it setup. Of course accuracy will always drop off further out in the forecast. We do the best we can.

@The Doctor - which languages you know?

Python, PHP, Nim, MySQL, Javascript, AutoIt, Pine, and a bit of C.

I really want to see what some other models can do for us. Working on that now.
 
Back
Top