Building a cryptocurrency trading bot using Azure – Part 1

Want to get rich quickly? Want to earn money while you sleep? Even though it sounds amazing these are the kind of things you can achieve simply by building a cryptocurrency trading bot which you can host in Azure!

Ok, I admit. That intro was a bit overzealous. There is absolutely no guarantee that what we’ll be creating in this post is going to make you any money whatsoever.  Surely you can use it to actually automate trading on the cryptocurrency market but this software was primarily created for educational purposes only. Don’t risk money which you are afraid to lose. What you do with this piece of software is your own responsibility.

Getting started with Azure Functions

I’ve talked about Azure Functions before but for those of you that don’t know what they are; an Azure Function is a piece of code that can be run independently in the cloud. It scales automatically depending on how much it gets called and can have a lot of different triggers. You can use an HTTP trigger which makes  a function trigger when you do a GET request to a specific endpoint or a timer trigger to periodically run a piece of code. There’s a lot of flexibility in these functions and they are very useful in creating a cloud-based architecture.

Creating an Azure Function project

We start by creating an Azure Functions project in Visual Studio. You need to make sure you have the latest updates installed for VS2017 so that these templates are available to you. From here we create an Azure Function that’s triggered by a timer.

Adding a Timer Function

This is our main entry point for our bot and it will be running every 5 minutes. To ensure it runs every 5 minutes you need to make sure that you have your CRON expression defined correctly. This is the set of asterisks and numbers at the top of the function. To learn more about CRON expressions you can check out this link. We set it to trigger every 5 minutes at the 2nd second of that minute. This minor delay is to ensure that the exchange has its 5 minute candle data up-to-date.

Wait what? 5 minute candle data?

Let’s elaborate on that for a bit. Cryptocurrency is traded on a number of different exchanges and most of these have some sort of API we can hook our bot up to. This API allows you to query the going rates for cryptocurrencies but also the historic rates which are represented as candlesticks. A candlestick is an object that contains the high, low, open and close of a cryptocurrency over a set time period. This is the data we will be using to analyse trends and decide whether or not it is a good time to buy or sell.

The exchange we will use for this project is called Bittrex. Their API is well documented and simple to use. Other people already made C# libraries for it which makes it easy to include into our little project.

The main trading loop

Our bot runs every 5 minutes and in that timeframe it needs to perform a specific set of tasks. These are the main tasks it should do:

  • Check if we have available trade slots within our bot (we want to be able to do multiple concurrent trades).
  • Analyse trends and check for buy signals if we have slots available.
  • Buy coins when a buy signal is found.
  • Check our current trades to see if they match our sell conditions.
  • Sell coins when the sell conditions are met.

Our main trading loop looks something like this:

This shows the 5 steps I mentioned above. The actual solution has a lot more stuff going on such as saving the trades to the database and keeping a balance. I omitted those parts for now to simply illustrate how our main loop functions. In this post I will only show portions of the bot to illustrate the principles behind it. The complete code for this project is available on Github.


The most interesting and difficult part of a trading bot is the strategy it uses to decide whether to sell or buy. It can process raw data faster than a human ever will but can’t quite interpret real world sentiments such as news (yet). Therefore we need to be able to decide our buy/sell signals based on technical analysis of historic data. Luckily there’s a vast number of technical indicators we can use for that. A library that has all these indicators is called TA-Lib and there are C# libraries available for it!

Our API calls always return a list of Candle objects and it would be great if we could apply technical indicators to those simply by using an extension method. That’s why I wrote some extension methods for a few of the most popular indicators. One of the more popular ones (the Simple Moving Average) looks like this:


With our indicators taken care of we can start combining these to create strategies. You can combine as many indicators as you like to form strategies. Each strategy is implemented using an interface called ITradingStrategy which forces you to return a list of integer values for each Candle object you feed into it. These integer values can be one of 3 values:

  • -1 – This is a sell signal.
  • 1 – This is a buy signal.
  • 0 – This is the signal to do absolutely nothing.

If we feed our strategy a list of e.g. 100 candles the strategy decides for each of these 100 candles if it contains a sell/buy signal based on the historic data of the previous candles. When running our bot we only have to look at the last item in the list to see what we should be doing. A simple strategy using a SMA crossover looks like this:

In this strategy we create 2 Simple Moving Averages with different period variables. When these two lines crossover it is either a buy or a sell signal depending on which line crosses which. There are thousands of readily available strategies out there on the web using all sorts of indicators. To use them in your bot simply implement a class using ITradingStrategy and tell the bot to use that strategy.

When to sell?

Determining when to sell is almost as hard as determining when to buy. We can use our strategy and wait until it tells us to sell but trades could go on for a long time if we do it that way. We can add some additional checks to sell of our trades quicker:

  • Stop-loss percentage – Adding a stop-loss percentage means we sell our trade when it dips below a specific profit percentage. E.g. sell when we have a profit of -3%.
  • Rate of interest – We can add support for ROI. This means that we can set stuff like “If we have a profit percentage of 3% after 10 minutes sell our trade immediately”. We could also add support for multiple ROI items so we can stack them.

By adding these two variables our ShouldSell method could look something like this:


When the amount of strategies you implement grows you want to be able to compare them. We obviously want to improve our strategy to find the best one possible. To support comparing strategies this project also contains a console application that can be used to backtest your strategies. Backtesting uses historic data to compare the performance of all the strategies on the same set of data.

A backtester for our Azure-based trading bot

The backtester uses the 5 minute candle data for 10 popular crypto currencies. This data is distributed over a 20 day period and was gathered using the public Bittrex API. If you want to add more data or want to backtest using additional currencies you can use this API to retrieve it.

This console application contains a few of the same variables (such as stop-loss percentage, rate of interest) as the Azure Function that handles trading so you can also tweak these to change the results of your backtest.


All of the parts mentioned above make up a simple trading bot that uses an Azure Function that triggers on a 5 minute schedule. It uses one simple function to perform all the tasks it needs to do every time it runs and can be completely customised using your own custom strategies. You can also implement additional indicators if need be.

If you want to contribute new strategies or additional indicators you can do so by simply submitting a PR to the Github repo. That way the existing bot can be continuously improved and we can share our strategies.

In the next post in this series we will hook up a Xamarin mobile app which enables us to monitor our active trades. We will also add additional functionality to directly sell off a trade from within our mobile app.

The bot can be found on Github at

  • James Trefry

    Thank you Steven. This is an incredible article, using the very tech that I was looking for – C# and Azure Functions. I really appreciate you sharing your hard work. I will have fun testing out new automated strategies against historical data. 🙂

    • Steven Thewissen

      Great to hear that! 🙂 Let me know if your testing comes up with an interesting strategy 🙂 I will probably be posting a follow-up on this article somewhere this or next week.

      • James Trefry

        If I’m fortunate enough to discover a winning strategy, you’ll be the first one I share it with! I’m really looking forward to your next article. Thanks soo much!

  • Dominik

    Wow, this post is incredible! Great kudos for sharing your work with the public. I’m also curious whether you tried to use it with real money, and if so – did you achieve profit?

    • Steven Thewissen

      I’m still working on finding a profitable strategy to start from. I have some that show some promise, but I need to have them run for a while before I can judge them 🙂

  • Jonas

    Really interessting article, thank you alot for sharing, have you looked anything at Durable functions? It is a very powerful orcestrator of functions, Jeff Holland, a PM of Azure Function have created a simple one that also uses bittrex, he use it to send a text to add or stop a watcher of a certain coin. In your example it could i.e quite alot of more powerful to have every trader as a dynamic created function (even though they might only live for 5minutes. and here are some basic info regarding it:

    • Steven Thewissen

      Thanks! I have not looked at those yet. Will look into them 🙂

  • Raymond Brink

    Very interesting project! I’ve studied your code, but am I correct to assume that the BackTester is more functional than the actual bot? As far as I can tell the bot can only handle one strategy at a time, where the BackTester can handle a different entry and exit strategy. Is this correct or am I missing something? If so, would you accept a pull request for such behaviour?

    • Steven Thewissen

      You might actually be right 🙂 I believe I built it into the backtester to see if there was an advantage to using a different exit strategy to see if it could be optimized further. Probably never looked into it any further in implementing it in the actual bot. Any pull request you have is welcome! I put it on Github to have it out available for improvements from other people 🙂

  • Alessio Leoncini

    Great project! thanks 🙂
    Do you have news about it?
    Are you became rich? 🙂

    • Steven Thewissen

      Haha, I wish 🙂 Finding a working strategy / trading system is like finding a needle in a haystack and I will probably not find one in this lifetime 😉 Smarter people than me have probably tried coming up with a working strategy but so far I haven’t heard of one.

      • Alessio Leoncini

        Well, you have done an amazing work, simple, clear and with a very good code, and I have to thank you for sharing it.

        Crypto trading is not so simple.. every trading rules seems to be violated every time (and cause it we can find more money opportunities..) now I’m thinking.. it makes sense for you test more than one strategies and buy when all of them are profitable?

        Or are you thinking about a sort of machine learning?

        • Steven Thewissen

          Thank you 🙂 Indeed, automated crypto trading is not easy by any means. I have yet to crack it. It is certainly possible to implement some sort of strategy that uses a lot of different other strategies to determine whether or not buying is the smart thing to do. Since you can create your own strategies you can make them as big and crazy as you want.

          Machine learning is not something I’m considering at the moment. I have no experience with it at all and I’m unsure if that’s something I can do on my own.

  • Top 1000 Cryptolist

    Given the current situation with the markets, it is much more efficient to do intra/day trading and analyze the market sentiment over the social networks. You obviously need to analyze lots of reddits, twitter handles. If you decide to go this way, you can find a nice list of all cryptos with their reddit, twitter, telegram URLs at . Basic list is for free, I think that’s all you need.