Want to get rich quickly? Want to earn money while you sleep? Even however it sounds amazing thesis are the kleuter of things you can achieve simply by building a cryptocurrency trading bot which you can host ter Azure!
Ok, I admit. That intro wasgoed a bit overzealous. There is absolutely no assure that what wij’ll be creating ter this postbode is going to make you any money whatsoever. Surely you can use it to actually automate trading on the cryptocurrency market but this software wasgoed primarily created for educational purposes only. Don’t risk money which you are afraid to lose. What you do with this lump of software is your own responsibility.
Getting commenced 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 chunk of code that can be run independently te the cloud. It scales automatically depending on how much it gets called and can have a lotsbestemming 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 lump of code. There’s a loterijlot of plasticity ter thesis functions and they are very useful ter creating a cloud-based architecture.
Wij embark by creating an Azure Functions project te Visual Studio. You need to make sure you have the latest updates installed for VS2018 so that thesis templates are available to you. From here wij create an Azure Function that’s triggered by a timer.
This is our main entry point for our bot and it will be running every Five minutes. To ensure it runs every Five 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 verbinding. Wij set it to trigger every Five minutes at the 2nd 2nd of that minute. This minor delay is to ensure that the exchange has its Five minute candle gegevens up-to-date.
Wait what? Five minute candle gegevens?
Let’s elaborate on that for a bit. Cryptocurrency is traded on a number of different exchanges and most of thesis have some sort of API wij can hook our bot up to. This API permits you to query the going rates for cryptocurrencies but also the historic rates which are represented spil candlesticks. A candlestick is an object that contains the high, low, open and close of a cryptocurrency overheen a set time period. This is the gegevens wij will be using to ontleden trends and determine whether or not it is a good time to buy or sell.
The exchange wij will use for this project is called Bittrex. Their API is well documented and elementary to use. Other people already made C# libraries for it which makes it effortless to include into our little project.
The main trading loop
Our bot runs every Five minutes and ter that timeframe it needs to perform a specific set of tasks. Thesis are the main tasks it should do:
- Check if wij have available trade slots within our bot (wij want to be able to do numerous mededinger trades).
- Anatomiseren trends and check for buy signals if wij 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 Five steps I mentioned above. The actual solution has a lotsbestemming more stuff going on such spil saving the trades to the database and keeping a balance. I omitted those parts for now to simply illustrate how our main loop functions. Te this postbode I will only vertoning portions of the bot to illustrate the principles behind it. The accomplish code for this project is available on Github.
The most interesting and difficult part of a trading bot is the strategy it uses to determine whether to sell or buy. It can process raw gegevens quicker than a human everzwijn will but can’t fairly interpret real world sentiments such spil news (yet). Therefore wij need to be able to determine our buy/sell signals based on technical analysis of historic gegevens. Fortunately there’s a vast number of technical indicators wij can use for that. A library that has all thesis indicators is called TA-Lib and there are C# libraries available for it!
Our API calls always come back a list of Candle objects and it would be excellent if wij 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 Elementary Moving Average) looks like this:
With our indicators taken care of wij can commence combining thesis to create strategies. You can combine spil many indicators spil you like to form strategies. Each strategy is implemented using an interface called ITradingStrategy which compels you to comeback a list of oprecht values for each Candle object you feed into it. Thesis oprecht values can be one of Trio values:
- -1 – This is a sell signal.
- 1 – This is a buy signal.
- 0 – This is the signal to do absolutely nothing.
If wij feed our strategy a list of e.g. 100 candles the strategy determines for each of thesis 100 candles if it contains a sell/buy signal based on the historic gegevens of the previous candles. When running our bot wij only have to look at the last voorwerp te the list to see what wij should be doing. A elementary strategy using a SMA crossover looks like this:
Ter this strategy wij create Two Ordinary Moving Averages with different period variables. When thesis 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 ter 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 spil hard spil determining when to buy. Wij can use our strategy and wait until it tells us to sell but trades could go on for a long time if wij do it that way. Wij can add some extra checks to sell of our trades quicker:
- Stop-loss percentage – Adding a stop-loss percentage means wij sell our trade when it dips below a specific profit percentage. E.g. sell when wij have a profit of -3%.
- Rate of rente – Wij can add support for ROI. This means that wij can set stuff like “If wij have a profit percentage of 3% after Ten minutes sell our trade immediately”. Wij could also add support for numerous ROI items so wij can stack them.
By adding thesis 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. Wij 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 gegevens to compare the spectacle of all the strategies on the same set of gegevens.
The backtester uses the Five minute candle gegevens for Ten popular crypto currencies. This gegevens is distributed overheen a 20 day period and wasgoed gathered using the public Bittrex API. If you want to add more gegevens or want to backtest using extra currencies you can use this API to retrieve it.
This console application contains a few of the same variables (such spil stop-loss percentage, rate of rente) spil the Azure Function that treats trading so you can also tweak thesis to switch the results of your backtest.
All of the parts mentioned above make up a ordinary trading bot that uses an Azure Function that triggers on a Five minute schedule. It uses one plain function to perform all the tasks it needs to do every time it runs and can be downright customised using your own custom-made strategies. You can also implement extra indicators if need be.
If you want to contribute fresh strategies or extra indicators you can do so by simply submitting a PR to the Github repo. That way the existing bot can be continuously improved and wij can share our strategies.
Ter the next postbode te this series wij will meet up a Xamarin mobile app which enables us to monitor our active trades. Wij will also add extra functionality to directly sell off a trade from within our mobile app.
Thank you Steven. This is an incredible article, using the very tech that I wasgoed looking for – C# and Azure Functions. I indeed appreciate you sharing your hard work. I will have joy testing out fresh automated strategies against historical gegevens. ??
Fine to hear that! ?? Let mij know if your testing comes up with an interesting strategy ?? I will most likely be posting a follow-up on this article somewhere this or next week.
If I’m fortunate enough to detect a winning strategy, you’ll be the very first one I share it with! I’m indeed looking forward to your next article. Thanks soo much!
Wow, this postbode is incredible! Good kudos for sharing your work with the public. I’m also nosey whether you attempted to use it with real money, and if so – did you achieve profit?
I’m still working on finding a profitable strategy to embark from. I have some that display some promise, but I need to have them run for a while before I can judge them ??
Indeed interessting article, thank you alot for sharing, have you looked anything at Durable functions? It is a very powerful orcestrator of functions, Jeff Nederland, a PM of Azure Function have created a ordinary one that also uses bittrex, he use it to send a text to add or zekering a watcher of a certain coin. Ter your example it could i.e fairly alot of more powerful to have every trader spil a dynamic created function (even however they might only live for 5minutes. https://medium.com/@jeffhollan/serverless-and-bitcoin-creating-price-watchers-dynamically-beea36ef194e and here are some basic informatie regarding it: https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-overview
Thanks! I have not looked at those yet. Will look into them ??
Very interesting project! I’ve studied your code, but am I keurig to assume that the BackTester is more functional than the actual bot? Spil far spil I can tell the bot can only treat one strategy at a time, where the BackTester can treat a different entry and uitgang strategy. Is this onberispelijk or am I missing something? If so, would you accept a pull request for such behaviour?
You might actually be right ?? I believe I built it into the backtester to see if there wasgoed an advantage to using a different uitgang strategy to see if it could be optimized further. Most likely never looked into it any further ter implementing it te the actual bot. Any pull request you have is welcome! I waterput it on Github to have it out available for improvements from other people ??
Superb project! thanks ??
Do you have news about it?
Are you became rich? ??
Haha, I wish ?? Finding a working strategy / trading system is like finding a needle te a haystack and I will most likely not find one te this lifetime ?? Smarter people than mij have most likely attempted coming up with a working strategy but so far I toevluchthaven’t heard of one.
Well, you have done an amazing work, plain, clear and with a very good code, and I have to thank you for sharing it.
Crypto trading is not so elementary.. every trading rules seems to be violated every time (and cause it wij 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?
Thank you ?? Indeed, automated crypto trading is not effortless by any means. I have yet to crack it. It is certainly possible to implement some sort of strategy that uses a lotsbestemming of different other strategies to determine whether or not buying is the wise thing to do. Since you can create your own strategies you can make them spil big and crazy spil you want.
Machine learning is not something I’m considering at the ogenblik. I have no practice with it at all and I’m unassured if that’s something I can do on my own.
Given the current situation with the markets, it is much more efficient to do intra/day trading and analyze the market sentiment overheen the social networks. You obviously need to analyze lots of reddits, twitter treats. If you determine to go this way, you can find a nice list of all cryptos with their reddit, twitter, telegram URLs at https://www.cryptowasabi.com . Basic list is for free, I think that’s all you need.
My name is Steven Thewissen. I create mobile apps with Xamarin and love getting creative with Photoshop. I’m also a gamer on Xbox One, play soccer, love cycling and love my daily dose of superheroes. Am still studying to be one myself. Oh, and I’m also Dutch.
Related movie: How to build a Aluminum 6 GPU Ethereum, Monero, Litecoin, LBRY and Other Cryptocurrency Mining Equipment