This article is a continuation of "Harnessing the Power of Redis for Efficient Trading Operations: A Detailed Look at Redis Pub/Sub and Redis Stream - Part 1".
Let's imagine you're running 2-3 strategies or indicators on TradingView, and you want to send alert messages from TradingView to Interactive Brokers. One method you might consider is using the Order Reference ID to distinguish between strategies or jobs. However, Interactive Brokers API offers a more elegant solution: using the ClientID. The ClientID can be utilized to establish multiple connections to Interactive Brokers. Each ClientID will see its own orders, separate from the master ClientID, providing a clear demarcation for each strategy or indicator.
Examining Redis Pub/Sub, we see that it supports multiple channels. Likewise, with Redis Stream, you can have multiple stream keys.
When making a connection to the IB Insync API, you can pass down the ClientID to
def connect(self) -> bool: """ Connects to TWS/IB Gateway """ ret = False try: logger.info("trying to connect to TWS/IBG") self.ibsyn.connect( shared.ibkr_addr, int(shared.ibkr_port), clientId=int(shared.client_id), ) self.loop = util.getLoop() logger.success("The connection to IBKR done well") ret = True except socket.error: .... except BaseException as err: ... return ret
Upon successful connection, you can view the connection status from the IB Gateway. As shown in the following image, two simultaneous connections have been established from TBOT applications.
Trading Strategy Allocation Between TradingView and TBOT
In TradingView, you have the option to send multiple trading strategies to either multiple TBOT applications or a single TBOT application. If you opt to use multiple TBOTs, your trading strategies' alert messages should include the ClientID property. As shown in the diagram, the alert messages will be routed based on the ClientID.
Alternatively, you can use a single instance of TBOT to deploy different strategies. TBOT will differentiate the strategies using parameters such as ticker, timeframe, order types, and order reference ID. But there's no need to worry about these parameters, as the PineScript TBOT Library (PineTradingbotWebhook) will handle them for you.
You can find the PineScript TBOT on TradingView here.
TradingView's PineScript has the capacity to dispatch multiple alert messages tailored to a variety of trading strategies. Each strategy can be earmarked to a unique Redis Pub/Sub channel or a Redis Stream, designated by a specific stream key. A corresponding TBOT, identified by its ClientID, will then subscribe to a particular Redis Channel or Redis Stream. This will establish a connection to Interactive Brokers, facilitated through the same ClientID.
Each TBOT will function independently, concentrating on its own transactions and utilising its own ClientID. When inspecting the web interface, you will only observe open orders associated with the respective ClientID.
The web interface can be accessed on GitHub at this link: TradingView Webhooks Bot.
The source code for TBOT can also be found on GitHub here: TBOT Trading Boat.
For comprehensive instructions on constructing a swift and straightforward trading robot with Docker and TradingView, please refer to the linked Udemy course: Link to Udemy Course.