preloader
post-thumb

Last Update: February 2, 2023


BY eric


author-thumb


Keywords

Introduction

As a part of a trading system, a gateway is a crucial component that connects a trader's algorithmic strategies to the broker's trading infrastructure. It acts as a bridge, facilitating the seamless transmission of buy and sell orders, real-time market data, and other essential information between the trader's system and the broker's platform.

The connection between a trading system and broker's trading platform could be one-to-many, as there are the diverse array of brokers available in the market. With having multiple and flexiable gateways, it is possible to trade across multiple brokers possible.

For institutional traders, they build their own trading infrastructure and be able to access diffent gateways and resources, and their proprietary APIs are not generally available or too expensive to the retail traders. Some professional data providers also provide realtime or historical data, but you still have go through your brokers to make orders. So in this article we focus on building a gateway using what are available (free) to the retail traders, and making getting data and sending orders all in one place.

Understanding the Need for a Gateway

A trading system can be 1) an algorithmic trading program that can execute orders automatically, or 2) a copy trading service that can simply copy trades from one broker to another. So the gateway should handle at least one of the following three tasks:

  • Transmitting buy and sell orders to the broker
  • Receiving real-time market data from the broker
  • Retrieving active positions (optionally account information) from the broker
  1. Diverse Broker Ecosystem:

    • Traders often engage with multiple brokers to leverage specific features, instruments, or market access. Creating a gateway allows for the consolidation of these connections into a unified and manageable interface.
  2. Customization and Strategy Integration:

    • Every trader employs unique strategies and algorithms. A well-designed gateway provides the flexibility to seamlessly integrate these strategies into the trading process, allowing for efficient execution and strategy management.
  3. Real-time Data Feed Integration:

    • Timely and accurate market data is critical for informed decision-making. A gateway enables the integration of real-time data feeds, ensuring traders have access to the latest market information to execute strategies effectively.

Building Your Own Gateway

So based on above, a complete trading system requires at least three things: data, orders, and positions, and those are what is expected from a broker. Without real-time data, trading strategies woouldn't be able to generate buy / sell signals. Without the ability to send orders, the strategies wouldn't be able to execute trades. Without the ability to retrieve positions, the strategies wouldn't be able to manage the trades. The gateway should be constructed to handle these three tasks.

Gateway Architecture

Below are the key considerations for building an efficient gateway:

  1. Understanding Broker APIs:

    • Thoroughly research and comprehend the APIs provided by the brokers you intend to connect with. Each API may have specific functionalities, data formats, and authentication requirements.
  2. Programming Language and Framework:

    • Choose a programming language and framework that aligns with both your trading system and the broker's API. Common languages include Python, Java, or C++, depending on the broker's specifications.
  3. Testing and Simulation:

    • Before deploying your gateway in a live trading environment, conduct extensive testing and simulation. This ensures that the gateway behaves as expected, minimizing the risk of errors that could lead to financial losses.

Broker's Trading platforms

Some brokers build their own trading platforms and provide their own APIs. Some brokers use third-party trading platforms and provide APIs for those platforms. Some brokers use FIX protocol, which is a standard protocol for financial transactions. Some brokers use RESTful APIs, which are more common in web development. Some brokers use proprietary protocols, which are unique to the broker.

But the most common platforms provided by the brokers are:

  • Meta Trader 4 (MT4)
  • Meta Trader 5 (MT5)
  • cTrader

Particularly, for different platforms, there are different set of assets to trade. For example, most stock CFDs are only avaible on Meta Trader platform and not avaible on cTrader. So if the gateway can connect to diffent platforms, it can connect to a wide range of brokers and traders are able to trade a wide range of securities / financial instruments as well. The essence of this discussion is that with a compatible and flexible gateway, we can freely select our preferred broker without the concern of being restricted to limited tradable assets.

Meta Trader

Unfortunately, Meta Trader 4 and 5 do not provide a direct API for trading. However, there are third-party libraries that can be used to connect to the Meta Trader terminals/ servers. An easy-to-use and popular library is MetaTrader5 for Python. It can be used to send orders, retrieve market data, and retrieve positions. But the downside of these libraries is that you still need Meta Trader 4 or 5 installed on your computer, and you need to keep it running all the time. There is also a cloud API for Meta Trader, but it is not free and not generall accessible, so we will not discuss it here.

For the gateway it should be able to be daemonized, run as a headless service, and can be run on varioius types of operating systems (e.g. Linux). So it is not a good idea to use these libraries for the gateway.

However, there is one particular libary worthy of mention is that it can be run as a headless service and can be run on Linux, Windows or MacOS, multiple accounts are supported. But it is not free and not officially supported, stablebility and reliability could be issues. This library requires constant updates and maintenance as well as Meta Trader could block the API or reject connection version at any time.

cTrader

cTrader provides a FIX API for receiving live market data and perform trading operations but not historical data. FIX API is a standard API that can be used to send orders, retrieve market data, and get positions information. It is a good choice for the gateway, as it is free and officially supported by the broker. However, in order to retrieve historical data and getting account information, we need to use the Open API provided by Spotware along with the FIX API.

FIX stands for Financial Information eXchange, and FIX API is a standardized protocol for financial transactions, and it is widely used in the financial industry. So there are lots of existing libraries (free or non-free) that can talk to cTrader's FIX server. Most brokers provide FIX API for trading through cTrader.

RESTful APIs

Some brokers provide their own RESTful APIs for providing realtime market data and trading. RESTful APIs are more common in web development, and they are easy to use and understand. They are also stable and reliable. However, each broker could have their own standard, so it is not easy to build a single gateway that can connect to multiple brokers without customization. Unlike FIX API, a single interface can be used to connect to multiple brokers, with broker's own RESTful API, we need to build a separate gateway for each broker.

Generally speaking, FIX API is a better choice for building a gateway, as it is a standard protocol and it is widely used in the financial industry. It is also stable and reliable, and it is not likely to be blocked or rejected by the broker. It can be run as a headless service and can be run on Linux, Windows or MacOS. Multiple accounts are also supported.

An important note is that brokers support different number of symbols on different platforms. For example, a broker may support around 1000 symbols on their Meta Trader 4 and 5 platforms, but only a small set of symbols, which could be less than 100, are avaible for trading on cTrader. For some reasons, you may want to trade some symbols that are not avaible on cTrader, then you would have to get your gateway to talk to Meta Trader.

Conclusion

Building a gateway that seamlessly connects your trading system to brokers is a strategic move towards enhancing trading efficiency and strategy execution. By understanding the intricacies of broker APIs, implementing robust security measures, and incorporating essential features, traders can create a reliable and customized gateway tailored to their unique trading needs. As technology continues to shape the financial landscape, a well-constructed gateway becomes a valuable asset for staying competitive in the dynamic world of algorithmic trading.

See Also

For detailed information about implementation with the FIX protocol using C#, please refer to this article.

Contact

For further information, I can be reach via:

Credits

Photo by Lukasz Radziejewski on Unsplash

agico

We transform visions into reality. We specializes in crafting digital experiences that captivate, engage, and innovate. With a fusion of creativity and expertise, we bring your ideas to life, one pixel at a time. Let's build the future together.

Copyright ©  2024  TYO Lab