Determining how to build an automated trading system may seem like a difficult task, but it’s something that any investor can do with some dedication and perseverance. Unfortunately, what most people don’t realise is that building algorithms isn’t just about getting a computer to follow a set of rules. You – yourself need to adopt a systematic mindset if you are to build an automated trading system.
Algorithmic trading is based around creating and following a set of rules; a framework. While the methods of executing or acting upon the framework may differ from trader to trader, there’s one thing that never changes; it takes a system to build a trading system.
Much like the rules the help govern your trades, there is a framework that we like to use to explain how you can get started building a trading algorithm.
Check out this article if you are not sure on how to use algorithms for trading.
Step 1: Determine Your Trading Strategy
It’s important for you to at least have some sort of trading strategy ready before you set out developing an algorithm. Moreover, your strategy should be tested at least to the extent that you know that you are able to trade the securities in question in the way that you want.
For example: don’t make a strategy for trading Gold if you have no experience trading futures, or are ineligible to do so.
You can read about some of the basic trading strategies in this useful article from “Investopedia”.
Believe it or not, it doesn’t matter yet if your strategy is effective or not, you can always alter it in the future (more on this point later).
Write down a list of points that define your trading strategy:
- What assets are you trading?
- What frequency are you trading at?
- What information does your strategy depend on?
- What are the rules of your strategy?
You might find it useful to sketch out a decision tree diagram that allows you to visualise how your algorithm would behave. Save this diagram for later when you start to code.
Step 2: Choosing the Right Broker
Now that you have your strategy, it’s important for you to gather the means by which you will ultimately execute it. In this step, your goal is to focus on two crucial elements that will ultimately define your trading algorithm’s efficacy: your broker and your data.
Choosing the right broker is one of the most critical parts of developing a trading system. Take into account:
- Trading fees: If your strategy demands lots of trading, fees will create a lot of friction that can ultimately affect your performance.
- Execution/Routing: If you’re trading at a high frequency, then you want to make sure to use a broker that will route your trades as quickly as possible.
- Order Types and Securities: Make sure your broker allows you to trade the securities you want, the way your strategy demands.
- API: The broker you choose must have an application programming interface (API) that they will grant you access to. This will be used in communicating your orders directly to your broker’s server computers, skipping all menu clicking and manual operations.
Step 3: Data
Once you have selected your broker, you must shift your attention towards fulfilling your data requirements. Your trading strategy is likely based on a series of rules that involve interpreting the prices of securities. So, where do you get your prices? Your broker may supply you with some sort of pricing information, but it may not be enough. It’s common practice for many retail brokers to provide unreliable data because good data can be expensive.
When choosing a data vendor, as yourself the following questions:
- Do you need real-time prices, and how much delay can you tolerate?
- Does your strategy demand exact data, or are aggregations sufficient?
- Do you need historical data?
- How often will your algorithm be accessing this data? How many data requests do you expect in a trading day?
- Do you need to exchange specific data?
- Do you need order book depth and precise volume data?
- What is the largest granularity you are willing to accept?
- Will you be cleaning your own data?
- How reliable is your data vendor?
- How flexible are plans and pricing with your vendor? How easily can you downgrade or upgrade?
Odds are you won’t get exactly what you want. It helps if you understand the extent to which your strategy’s performance is (or isn’t) affected by the compromise. For example; if you’re keeping your trades open for more extended periods of time, do you really need real-time data, because you might as well settle for delayed data at a fraction of the cost.
Keep in mind, your algorithm may rely on more than just prices, and you must take just as much care with alternative data sources. Make sure to research APIs, consult reviews, and don’t be afraid to speak with sales departments – they are more flexible with pricing than you may think.
Step 4: Creating a Management Policy
Before you can start building an automated trading system, you need to think about risk management and account management. While your strategy may include risk management components, it most likely only does so to the extent of position sizing, entrance, and exit signals. There is no sense in approaching risk management casually, as that can result in your algorithm running itself into the ground. This is especially true if your algorithm runs autonomously.
Taking into account your own risk aversion and financial goals, ask yourself the following questions:
- How much capital is reinvested into the strategy, and how much is saved?
- How do you define success within the framework of your strategy? How do you define failure?
- What is the maximum downside that you are willing to accept before pulling the brakes?
- How do you, personally, cope with volatility?
Many people employ algorithmic trading to get away from their emotions, only to find that they are even more uncomfortable watching an algorithm trade on their behalf. And, finally;
- How many strategies do you plan to use?
We briefly touched upon this topic in this guide’s first step; your original strategy may not be as successful as you may think. This is why, when developing a trading algorithm, it’s vital to adopt a systematic and modular approach. By creating a solid management policy, you will be able to swap out different strategies in the future while ensuring that the rest of your trading system doesn’t deprecate with incoming changes.
Step 5: Coding
Armed with a plan, you are now ready to start coding your algorithm. Depending on your technical expertise, this may be the hardest or the easiest step. The best place to start is with your broker’s API. Familiarise yourself with its documentation, what languages are supported, and test your functions within a development environment. Once you’re comfortable with the syntax, start building your strategy.
Translate your written strategy into code through operating clauses such as if, elif, else, while, and where statements. Use flow control to determine the conditions prerequisite to placing an order. Building an algorithm can be a great project in furthering your understanding of programming. Some of the most common programming languages used by retail investors are Python, C, and Java, with derivatives unique to brokers and trading platforms like Pine Script (TradingView), and MQL5 (MetaQuotes).
Next, you will need to develop your management policy. Keep in mind that this module should be separate and independent of your strategy – while remaining reactive to it. Some of the variables you should use within your management policy are:
- Account endpoints to balance, equity, and portfolio assets
- Trade history
- Strategy logs
- Performance history
- Credit status
If you’re building your very first algorithm, you may find that you draw little value from building a complex management policy in the beginning. That’s ok, you can always revisit the management policy later when you become more familiar with just what kind of trader you are.
Step 6: Testing Your Trading Algorithm
Testing is a crucial component in developing an algorithm, and you should expect to devote a lot of time to going back between testing and coding. In fact, at Remi Technologies, we allot a mandatory testing period for every phase of development. Testing can be broken down into three different parts; functional testing and debugging, backtesting, and forward testing.
Functional Tests and Debugging
When running your algorithm for the first time, prepare for errors. There might be syntax errors within your code, or incorrect uses of API requests, or merely poor date formatting – all of which are endemic to any software development project. Testing (within a safe environment) and debugging, is a natural part of the development process.
Backtesting
Backtesting should seldom be used to determine the efficacy of your algorithm but serves an essential function in ensuring that your code functions as expected. Most importantly, you should use backtesting to double-check the logic behind your strategy. Here are some crucial questions that backtesting should help you answer:
- Is your algorithm trading more or less than you expected?
- How does your algorithm react in a bull/bear market?
- Does your algorithm behave, functionally, as expected?
You may find that there is still a lot of room for improvements in your strategy and reiterate through the coding-testing-debugging process multiple times. Remember, while examining the performance of the algorithm can shed some light on its expected return, it should not be considered indicative. Past performance does not guarantee future results.
Forward Testing
Now that you’ve perfected your code, strategy, and management policy, it’s time to conduct a forward test. A forward test involves allowing your algorithm to paper-trade using real prices for a certain period of time. Forward testing will show you how your algorithm reacts to new information, slippage, and shifts in market liquidity. You will also gain a better understanding of how your data vendor performs and the quality of live data. If management policy is part of your automated trading system, forward testing will help you improve it as well.
Third-Party Testing
Asking someone else to review your code, and even your trading strategy can help you identify hidden problems. However, this should only be done through a reputable firm, complete with contracts and non-disclosure agreements that protect your intellectual property. Companies with greater computational resources will often be able to stress-test your automated trading system through many more conditions than you ever could, and also bring into question the philosophy behind parts of your strategy. Remi Technologies offers such services.
Step 7: Going Live
Once you’re comfortable with your trading system and have developed at least one trading strategy that you are satisfied with, it’s time to go live. A lot of work has brought you to this step, but don’t relax yet. Your job isn’t over once your algorithm is trading, you still have to monitor it, conduct maintenance, and reflect on its actions – all while developing the next version of your strategy.
It’s difficult to say how much time will take between your decision to begin developing a trading algorithm, and actually launching it. Some traders prefer to conceptualise a strategy and run existing algorithms while leaving development to the professionals. Others view building a trading algorithm as a personal challenge and focus on learning the technology first, before perfecting a strategy. Ultimately, it will boil down to your individual algorithmic trading goals, your ability to manage your time effectively, and technological skill. However, regardless of your ambitions, the outcome will always be a rewarding learning experience that will help you better understand yourself and the market that surrounds you.
If you need help getting started as an algorithmic trader, or want to develop your own algorithm with the help of one of our teams, use the contact form below.