Trading Router Library Introduction
You can use Trading Router Libarary to develop your own MT4/5 EA to receive messages from Tradingview. Trading Router Libarary maintains the connection between your EA and Trading Router server and receives incoming messages from Trading Router server. When Trading Router Libarary receives a new message from Trading Router server, it put the message in the receive message buffer, and signals a chart event to EA immediately, the EA then immediately retrieves the message from receive message buffer and process this message. The time duration between Trading Router server sending a message and your own EA receiving the message is very quick, it is less than 0.1 second.
1. Trading Router MT4/5 API
You can import TradingRouterLibsMt4.ex4 (for MT4) or TradingRouterLibsMt5.ex5 (for MT5) for using Trading Router Library functions.
There are 7 functions in Trading Router Library
1.1 TR_API_OnInit
Function Name | TR_API_OnInit | ||||||||
Arguments |
|
||||||||
Description | This function is called in OnInit of EA. It initializes Trading Router Library and send registration message to connect to Trading Router server by using tr_client_token. | ||||||||
Return Value |
|
1.2 TR_API_OnDeinit
Function Name | TR_API_OnDeinit |
Arguments | No |
Description | This function is called in OnDeinit of EA. It deinitializes Trading Router Library. |
Return Value | No |
1.3 TR_API_OnTimer
Function Name | TR_API_OnTimer | ||||||||||||||||
Arguments | No | ||||||||||||||||
Description |
This function is called in OnTimer of EA.
It maintains the connection between Trading Router Library and Trading Router server.
It also receives incoming message.
After you call TR_API_OnTimer, no matter what return value you get, you should call TR_API_HasNextMessage to check if there are new incomming messages, and call TR_API_GetNextMessage to retrieve new incoming messages. |
||||||||||||||||
Return Value |
|
1.4 TR_API_OnChartEvent
Function Name | TR_API_OnChartEvent | ||||
Arguments |
|
||||
Description |
This function is called in OnChartEvent of EA. When a new incoming message comes, a chart event will be triggered,
and OnChartEvent of EA will be called,
then we call TR_API_OnChartEvent in OnChartEvent. TR_API_OnChartEvent will receive the new incoming message and put
the message in receive message buffer(the buffer size is defined by argument message_buffer_len of TR_API_OnInit function).
After TR_API_OnChartEvent is called and the return value is true, you should call TR_API_HasNextMessage to check if there is new incomming messages, and call TR_API_GetNextMessage to receive new incoming messages. |
||||
Return Value |
|
1.5 TR_API_HasNextMessage
Function Name | TR_API_HasNextMessage | ||||
Arguments | No | ||||
Description | This function checks if there is new message in receive message buffer. It should be called after TR_API_OnTimer or TR_API_OnChartEvent is called. | ||||
Return Value |
|
1.6 TR_API_GetNextMessage
Function Name | TR_API_GetNextMessage | ||||
Arguments | No | ||||
Description | This function returns the first message in receive message buffer, or returns empty string if there is no message in receive message buffer. | ||||
Return Value |
|
1.7 TR_API_CheckAndRestoreMessageLost
Function Name | TR_API_CheckAndRestoreMessageLost | ||||
Arguments | No | ||||
Description |
This function checks if there are lost messages since last call of it.
If your EA does not call TR_API_GetNextMessage after calling TR_API_OnTimer or TR_API_OnChartEvent,
the new message will keep in the receive message buffer.
If there are many messages kept in the receive message buffer, and your EA has not called TR_API_GetNextMessage,
the receive message buffer will be full, and new incoming message will be lost.
So when you call TR_API_CheckAndRestoreMessageLost, you can know if there are messages lost.
TR_API_CheckAndRestoreMessageLost only checks if there are messages lost since last call of TR_API_CheckAndRestoreMessageLost.
For example, the receive message buffer is full, you call TR_API_CheckAndRestoreMessageLost twice, and there are no new messages come between the two calls of TR_API_CheckAndRestoreMessageLost, then the first call TR_API_CheckAndRestoreMessageLost returns true which means there are lost messages, but the second call TR_API_CheckAndRestoreMessageLost returns false, because there is no lost messages since last call(the first call). Another example, the receive message buffer is full, you call TR_API_CheckAndRestoreMessageLost once, it returns true which means there are lost messages, then a new message comes and is lost because the receive message buffer is full, you call TR_API_CheckAndRestoreMessageLost again, it also returns true because there are lost messages since last call(the first call). If you always call TR_API_HasNextMessage and TR_API_GetNextMessage after calling TR_API_OnTimer or TR_API_OnChartEvent, no message should be lost unless messages come very frequently (100 messages come in less then 0.1 second), which is impossible. |
||||
Return Value |
|
2. Retrieve Messages
Both TR_API_OnTimer and TR_API_OnChartEvent can put new incoming messages in receive message buffer. So you should call TR_API_HasNextMessage and TR_API_GetNextMessage after calling TR_API_OnTimer or TR_API_OnChartEvent.
When a new message comes, it can be put to receive message buffer by either TR_API_OnTimer or TR_API_OnChartEvent. Consider the following two cases.
Case One: A new incoming message comes, a chart event is triggered and timer out event happens at the same time. The TR_API_OnTimer is called first followed by TR_API_OnChartEvent being called, the new incoming message is put to receive message buffer by TR_API_OnTimer
Case Two: A new incoming message comes, a chart event is triggered, there is no timer out event. Only TR_API_OnChartEvent is called, and the new incoming message is put to receive message buffer by TR_API_OnChartEvent.
In both cases, the message from Trading Router server to receive message buffer is very quick, it should be less than 0.1 second. So if your EA calls TR_API_HasNextMessage and TR_API_GetNextMessage after calling TR_API_OnTimer or TR_API_OnChartEvent, your EA can process the message in less than 0.1 second after the message is sent out from Trading Router server
You can set the size of receive message buffer. The default miminum size is 100, which means the receive message buffer can save 100 messages which is enough. If the buffer is full, new incoming messages will be lost. If you always call TR_API_HasNextMessage and TR_API_GetNextMessage after calling TR_API_OnTimer or TR_API_OnChartEvent, no message should be lost unless messages come very frequently (100 messages come in less then 0.1 second), which is impossible.
3. Message Syntax
You can define your own message syntax as long as the following syntax requirements are met.
- Message should be english characters
- A message is composed by fields
- A field has name and value which is connected by '=', like name=value
- Fields are seperated by ','
- The first field is master token
- The second field is signal
- Field name and value cannot contain ',' or '|'
- The message length is less than 2000 characters
When your EA receives the message, the incoming message is in the following format
- The token field is removed.
- The fist field is signal field.
- Field is seperated by '|', and there is an ending '|' at the end of the message.
- Field name is converted to upper case, field value is the same case as what you send
- Signal value is converted to upper case.
- The field order may be different as what you send. For example if you send "token=xxxxxx,signal=ok,field1=1,field2=2", the received message could be "SIGNAL=OK|FIELD1=1|FIELD2=2|" or it could also be "SIGNAL=OK|FIELD2=2|FIELD1=1|"
You can use any signal value, any field name and any field value, as long as they follow the syntax requirements.
-
token=master12849930099332245,signal=stopit,stopvalue=ok,comment=It is Good
The received message could be
SIGNAL=STOPIT|COMMENT=It is Good|STOPVALUE=ok| -
token=master12849930099332245,signal=mysignal1,field1=ok,field2=This is field two,field3=3
The received message could be
SIGNAL=MYSIGNAL1|FIELD3=3|FIELD2=This is field two|FIELD1=ok|
-
token=master12849930099332245,signal=ok|ok,field1=ok
there is a '|' in the signal vale ok|ok, which is not allowed -
token=master12849930099332245,signal=ok,field1:1
the field1 and value 1 should be connected by '=' like this field1=1