How does it work?
In this page we will have an overview of the way MKR Platform works. Following, we show a diagram to understand in a global view how the platform interacts with other elements. After the diagram will explain every element of it
There are different ways to explain this diagram. We will start by the origin of the information. There are also different ways to use MKR trading platform, so for this example will use the case of running an automatic trading bot in real time. We suppose that the user already have connected to the trading platform and started running the bot on a real or paper trading account of IBKR platform
-
IBKR terminal, aka TWS (Trader Work Station) queries quotes from the global exchange, in this example, the Chicago Mercantile Exchange CME, which is a reliable and regulated financial markets exchange. Information of the quotes of the selected instrument we chose for trading is sent to the data farms of IBKR, and from there to our computer, which is running the TWS Terminal.
-
At the same time, Market Robots (MKR) terminal instance is running is our computer (not necessary has to be the same computer). MKR uses the Socket API from IBKR. It means both terminals communicate by the TCP/IP protocol, which allows them to communicate over public networks, private networks or in the same computer, which is the most common case.
-
Now that the information of a new quote is on the MKR platform instance, the event "QuoteUpdate" will be fired. This event is fired every time a new quote has arrived to the platform, this covers a candle value update or a new candle arrival.
-
Every robot has to implement the QuoteUpdate event, so every time this event is fired, the code programmed in the robot will be executed. Robots analyze the data provided on the last quote and previous ones, besides information in the indicators and portfolio. The robot will make necessary calculations with the final objective to determine:
-
If a new order must be sent to the terminal. Order can be used to open a new position, close an existing position, or both at the same time
-
Calculate the parameters of the new order, if it's the case
-
Optionally update internal variables of the robot
-
When a robot creates and sends a new order, it calls the functions of an interface, called "TerminalBase". Depending on the terminal we connected (in this case is TWS, but could be another one like MetaTrader4), order will be sent to the instantiated terminal. Developer of Robots doesn't have to mind about the details of the terminal, as robot is programmed to run on any terminal with the same code.
-
The details of the connection to the terminal are implemented in the source code of the specific terminal, which implements the interface called "TerminalBase". In this example, the order will be actually sent to TWS using IBKR sockets API. Before starting the automatic trading, the user is connected to the TWS using the platform.
-
Once the order is sent to the terminal, the order will be live in the trading account. If it's a limit order, it will be in "open" state until filled. If it's a market order, it will be filled in matter of seconds. In any case, we will receive a confirmation of the execution via another event (OnExecution)
-
Terminal exposes "OnExecution" event, so the robot can use this event to run any code according to its purpose.
-
Robot also can access the portfolio of the terminal to get how many "futures" or "stocks" has of the current trading instrument.
-
MKR Platform can show/update the chart of the current session, so user or developer can check in real time what's going on with the operations. On the chart, these elements will be visualized:
-
Quotes of selected instrument, in form of the well-known Japanese candles. User can move throw time using mouse or keyboard. User can also adapt zoom level or move chart vertically
-
Indicators: Developer can program his own indicators on an easy way. They can be added to the chart at any time. Robots can also add and use indicators. Once indicators are added, user or robots can set visibility on/off
-
Signals: In some situations, robots want to alert the user of some event, but it does not have to imply a new order. In this case, robot can use signals in the chart. Signals include a text description, a color and a shape. They point to a specific candle of the chart
-
Orders: As in any trading platform, executed or open orders will be displayed in the chart. Robots can attach a text to the order, which will be also displayed on the chart and orders record. This is useful, for example, if a robot implements different strategies, the user may want to know which strategy triggered every order in the chart. If an order implies closing a position, the profit or loss will be also drawn in the chart or red or green color.
-
MKR Platform hides all the details of connection to the trading terminal, for example:
-
Will fire the OnQuote event every time a quote is updated or inserted, so the robot can run its program
-
Will handle the order execution event to keep the portfolio updated. So the robot can also fire its code in the event and anytime can query the portfolio
-
Will keep the chart updated. It will be available to the user or robot all the time. And also can be used to send reports via email or save the chart on the hard disk drive.
-
If connection to the terminal is lost, MKR will try to connect again after some minutes to keep the robot working without need for human intervention.
-
MKR will automatically update the values of all the indicators attached to the chart. Indicators can be set by the user or by the robot. Indicators are always visible by the robot, even if they are hidden in the chart
We already have seen the normal flow of a robot running in real time, getting quotes and making orders. But there are some parts of the diagram still unexplored:
On the right side of the diagram, we can see some entities of the database
-
Robot parameters: Every robot can have an unlimited number of parameters. Parameters values are always a decimal or integer number. Better to understand what a parameter is with some examples: -"take profit percentage", "stop loss percentage" "maximum minutes in market"... In most cases, values of parameters are different on every instrument, so a group of parameters are always attached to the selected instrument. Developer can also set different groups of parameters and save each one with a given name. Before starting trading with a selected robot, the user has to select the group of parameters to use. As parameters can be optimized. It's possible to get different values of parameters on different periods of optimization. User may want to store, for example, optimized parameters for the whole year, optimized parameters for last 6 months, etc
-
Orders: Executed orders are stored in database. An order may come from different sources, for example: real time manual trading, real time automatic trading, simulated trading etc. The purpose of storing orders in database is for future querying, analyze and make statistics from them. Having all orders stored, allows the platform to generate basic statics of trading and also generate the net liquidity over time chart.
-
Quotes: Quotes can be downloaded from the TWS, but there are limits of the amount of quotes and sometimes it can be a slow process, so it's not convenient to get the quotes from TWS every time we want to make a backtesting/simulation. Query quotes from our own database is the most convenient and fast way to do it. MKR Platform can use the quotes to draw charts and do backtesting
-
Instruments: As in any other platform, we store all the instruments available for trading, including future contracts, stocks, etc.
-
Financial information: TWS API offer methods to query financial information from stocks. These are some examples of information we can get from any stock:
-
Total revenues record, for every quarter of year, or trailing twelve months
-
Earnings per share records
-
Net Income available to common
-
Price to sales ratio TTM
-
Dividends per share
-
Cash flow per share
-
Shares Outstanding
-
Market capital
-
Dividend dates and records
-
Expert analysts mean consensus
-
News: news can be query by API services or be scrapped from public websites. Once stored in the database, can be query by date and time to get the most relevant for trading.