Demystifying Algorithmic Trading — Part 1
Welcome to this multi-part series on algorithmic trading from Tantra Labs! If you’re reading this, you probably already have some familiarity with crypto and trading crypto or other assets, but like many people, you probably think algorithmic trading is scary! But fear not!
Yes, there is quite a bit to learn before you will be proficient, but algorithms themselves are conceptually rather simple. At Tantra Labs we specialize in algorithmic trading, particularly algorithmic trading of Bitcoin, and we wanted to do our part to help educate the wider community. To that end, various Tantra Labs team members and advisors will contribute their expertise to this series, and hopefully, we can achieve our goal of demystifying algorithmic trading, once and for all.
Also, please check out this episode of Tantra Talks Podcast, a companion listening experience to this article that you can enjoy before or after reading.
And now without any further ado: Part 1
Let’s Make Some Bread
The purpose of this article is to educate and inform you as to what algorithms are and why they are ultimately the greatest thing since sliced bread, even though algorithms are actually older than sliced bread! Technically speaking, slicing bread is an algorithm of sorts. Not a sorting algorithm, but an algorithm, of sorts…
So what makes slicing bread an algorithm? A quick search on Google, which is one of the greatest algorithms ever created, reveals the definition of an algorithm as “a process or set of rules … to be followed in calculations or other problem-solving operations, especially by a computer.” But we are just going to focus on the first part. An algorithm is a process or a set of rules. Specifically, in slicing bread, these rules are very loosely defined so that a bread-slicing algorithm might look something like this.
N (Slicing Action) / L (loaf of bread ) = S (Slices of Bread Created)
So a little basic algebra refresher — N is the variable I am using to define the number of times the slicing of bread will be done, and L is the number of loaves that I will slice. This is a very simple example and not meant to be exact, but I think you get the point. N/L = S. It’s that easy! Now that we understand how to slice bread in a formulaic way, let’s talk about why this is useful.
Humans = Machines
What if I told you that you are actually an algorithm? Well more like an ensemble of algorithms — the way you walk, how you speak, and even the dopamine your brain gives you after you’ve accomplished something are all algorithms that you have accumulated over time to help you navigate the reality in which you exist. Even the way you see, taste, touch, and hear are algorithms that came preprogrammed in your body via DNA.
Remember when you started your first job and they made you read all those instruction manuals? In those manuals were tons of algorithms that detailed how to do your day to day job. For example here is an algorithm that defines a cashier’s job:
Now the human programming language is much more complex and dynamic than say, Python or C++, but nonetheless Language is Code.
If we want someone to do something, then we have to define what we want that person to do and how we want that person to do it. If the person is really smart and we trust him or her, then we can hopefully just define what we want the person to do and he or she will figure out how to do it. In programming this is called Machine Learning, which is itself a subset of Artificial Intelligence or AI. We aren’t going to talk about that today, but it is important to understand that most algorithms and even AI today require rules.
At the very core, an algorithm is a way to view, understand, and/or respond to any event, which makes the human being one of the greatest algorithms in all of existence.
Teaching the Machine to Trade
Getting a machine to do anything is no easy feat. Even with human beings it took billions of years for us to evolve to the point where we are today, and we still do a lot of silly things. So if we want to get a machine to trade for us the best place to look is in nature. We need to look at how life has developed over the eons and not assume that we are smarter than whatever celestial fart that/who created all of us!
For the sake of understanding, it’s helpful to become ignorant. In this way we can start at the most basic level and define what it will take to make a machine understand the market. Ignorance should guide your decisions in some way for this process. For instance, it may seem intuitive to buy when the price is going up and sell when it is going down. But what if, historically speaking, that has not actually been the case, and in reality you should be using “mean reversion” to sell as the price is going up and buy as the price is going down.
Forgive the oversimplification but the point is… DON’T ASSUME ANYTHING. Let the machine tell you the answer, but first we need to give the machine the ability to do that…
1) Create The Universe
So what did life do first? Well first there was an environment for existence. The Christian bible says it was the word and the word was God! So I guess for the algorithm, the code is God?
Really what we need is to code an environment for the algorithm. This seems simple enough, but what kind of an environment does a trading algorithm need? For the sake of oversimplification we are going to say the universe that we are creating only has 1 exchange and 1 asset. This will help us from straying too far off the path and over-complicating everything.
First off, let’s set some basic rules for our environment, just like the universe set rules for us, like (unfortunately) how I can’t teleport…
1) You can’t see the future.
2) You can perform 3 actions; buy, sell, or hold.
3) If you run out of money, you lose.
3 rules is all we need to get up and running. The first rule is the most important. Maybe you have heard the term overfitting? Basically, it’s really easy to write an algorithm that buys and holds in 2017. Because we all know the price basically went straight up, we want to avoid ever letting our algorithm know the future of its environment. We’ll talk more about this in a later article in this series, but just be sure to avoid adding your own bias of the past.
So what does the timeline of our universe look like? And how does the algorithm understand where it is in that timeline? The simplest way to build a timeline for our universe is to use historical data of trading bars, so we will download some sweet sweet 1 minute XBTUSD data from Bitmex, that will contain a historical record from 2015 till now of the open, high, low, close, and volume or OHLCV of each minute. This “timeline” is the historical dataset we will use to “teach” our algorithm.
2) Understand The Universe
Now that we have created a universe, we need to give our algorithm a way to interpret this data because honestly, WTF does 7543, 7544, 7534, 7540, 83490 mean? We need to give the algorithm a way to interpret this information. Any suggestions?
I suggest we start with the basics — more historical information!
If the algorithm is just seeing the current point in time as the price, it’s pretty hard to make a decision, so we are going to follow in the footsteps the genius’s over on the forums of Trading View that make a ton of money by drawing lines on pictures of lines. We are going to give our algorithm the ability to interpret historical data as a “Moving Average,” a reference point to today’s current price.
One simple way to interpret a moving average of the price is to ask “Are people in the market paying more for Bitcoin right now than they have in the past X amount of time?” Now for those of you who don’t know what a moving average is, I’m really impressed you are still reading this! Hi mom!
So a moving average is exactly what the name suggests. It is the “moving,” meaning it updates dynamically based on new data presented, “average,” as in the sum of all numbers divided by the number of numbers.
This is mathematically defined as:
3) Respond to the Universe
The algorithm can now step through time in its universe and begin to understand if one moving average is over another. That’s pretty much it at this point, and it took a lot of writing just to get here… and it’s all theoretical. It takes even more code to make that a reality! But I digress… It is actually enough to give you and the algorithm an understanding of the algorithm’s universe.
So for the sake of this simplified example, we are going to tell our hypothetical algorithm to look at the 50 minute moving average crossing the 200 minute moving average. We want the algorithm to buy when the 50 is over the 200 and sell when the 50 is below the 200, and just buy as much as it can!
So here is the outcome of that algorithm:
Yikes!
But this is only a starting point… Once you are confident that your algorithm is following the rules of its historical universe then you can let it venture into alternate realities, AKA the real world or another asset’s historical data.
Once you build an algorithm capable of handling this reality then your live performance might start looking like this:
If it does already, or you would like to learn more about how we got ours to perform like this, then please reach out!
That’s all for now! We hope you enjoyed Part 1 of “Demystifying Algorithmic Trading.” Please stay tuned as we release more from this series.
For more about Tantra Labs, check out our introductory post here.
To listen to our podcast, Tantra Talks, click HERE.
For a list of beginner Bitcoin recommendations, check out this article.
For the latest updates from Tantra Labs, follow us on Twitter @Tantra_Labs.
Don’t forget to give us your 👏
Author’s opinion only. The views and opinions expressed in this article are those of the author and do not necessarily reflect the position of Tantra Labs Inc. or any other company. Examples of analysis performed within this article are only examples. They should not be utilized in real-world investment decisions as they are based only on very limited information. Assumptions made within the analysis are not reflective of the position of Tantra or any company.
Non-reliance. The information set forth herein is for information purposes only and should not be relied on or construed as investment advice, counsel, or solicitation for investment in Tantra or any other company. Interested investors should seek appropriate independent professional legal, investment, and tax advice prior to relying on any of the material contained in this article.
Forward-looking statements. Certain information set forth herein contains forward-looking statements that give a reader the opportunity to understand the author’s beliefs and opinions with respect to the future. These statements are not guarantees of future performance of Tantra or any other company and undue reliance should not be placed on them, as they necessarily involve known and unknown risks and uncertainties.
Not a securities offer. This article does not constitute an offer of securities by Tantra or any other company.