Before we discuss how we can get an MT4 expert advisor (EA) to work with FIX API Trader, I’d like to
answer the four most frequently asked questions:

1. When should an EA be used in a FIX API account instead of an MT4 or MT5 account?
2. Why can’t I just rewrite an EA for FIX API without using FIX API Trader?
3. Why should a forex robot be adapted for use with FIX API Trader?
4. To what extent will trading directly through a black box be different from trading in a FIX API environment?

FIX API Trader  MQL4 EAs recoding


1.1. When should an expert advisor be used in a FIX API account instead of an MT4 or MT5 account?

The answer is simple: always. In the final analysis, trading through FIX API will help you save money and avoid interference on the part of your MT4 broker. Also, be aware that if a broker claims to be an ECN or STP broker, this is not true in the vast majority of cases. Perhaps, one might say that it is never true. Most brokers use a hybrid model. If you’re curious about this topic, you will find more information here: https://iticsoftware.com/en/blog-posts/forex-brokers-arbitrage-trading/

With that in mind, the question should be changed a bit. It is more pertinent to ask: when is it especially important to use an expert advisor in a FIX API account instead of an MT4 or an MT5 account? It is especially important when order execution time (along with any resulting slippage) and spreads have a considerable effect on the performance of the advisor. In practical terms, a FIX API account should be used whenever you’re working with a scalping or high-frequency trading robot. If your advisor works well in an MT4 demo environment but fails to replicate this performance in a real account, you should definitely consider adapting the advisor for FIX API, as it could be a game changer.

Why would an advisor performing well in an MT4 demo environment be unsuccessful in a real account? Well, when the advisor sends an order to the demo server, the MT4 server confirms the order right away without transmitting it to the forex market. Even so, the order execution time will be equal to some 40 milliseconds in this case, and there are several additional disadvantages that come with an MT4 server:

● It is impossible to process quotes that are updated rapidly even if quotes filtering (which, in itself, is detrimental to trading during spikes) is turned off. The number of updated quotes in an MT4 environment fluctuates between 1 and 6. By comparison, the number of updated quotes in the FIX Protocol allows the user to receive up to 1,000 updated quotes in a second, provided the user is using a cross-connect.
● In an MT4 environment, orders are processed sequentially, while FIX API makes it possible to process thousands of orders simultaneously.
● Order execution time on a real MT4 server can be anywhere between 70 milliseconds and a few seconds when the market is volatile. This results in considerable slippage. At the same time, execution times in a FIX API environment can be in the range of 4-40 milliseconds, with minimum slippage.


1.2. Why can’t I just rewrite an EA for FIX API without using FIX API Trader?

You can. However, it will be much more expensive. This is for several reasons. When an MT4 robot works through FIX API Trader, it uses the entire MQL4 functional programming and the built-in indicators. In other words, you don’t need to rewrite the module for receiving and processing quotes, or the built-in or user indicators, in order to make the robot work in FIX API. Also, although the FIX Protocol is standard, different FIX API brokers have different FIX API connections, so the connector for each broker will have to be written separately. Additionally, a confirmation test with the broker will be subsequently required, and that can be a challenge. FIX API Trader has more than 50 built-in connectors, which are adjusted and certified (meaning they don’t need to be tested), for the following brokers: 4FXBROKERS, ADSS, ALPHA, CFH, CIRCLE_MARKETS, CMC, CTRADER, CURRENEX, DUKASCOPY, EXANTE, FIRSTDERIVATIVES, FXBA, FOREXWARE, FORTEX, FXCM, FXPIG, ICM, INTEGRAL, HOTSPOT, I_GOLD, INTERTRADER, INVAST, KEY_TO_MARKETS, LCG, LMAX, MATCHTRADE, MGM, NEXUSPRIME, ONETRADE, ONEZERO, PFD, PRICEMARKETS, PRIMEXM, PROBUSFX, SAXO, SPOTEX, Swissquote, SQUARED_FINANCIAL, THINKFOREX, TRADAIR, TT, VISUALTRADING, XENFIN.

It is possible that you might not find your broker on the list - most brokers use bridges such as PRIMEXM, Onezero, TT, etc. Ask your broker to provide you with a FIX API description, and we will help you determine which connector will work best for you.

1.3. Why should a forex robot be adapted for use with FIX API Trader?

It’s important to understand that the MT4 platform is an artificially designed platform with its own set of conventions that, in some ways, bear little relation to the forex market. For example, there is no “close order” option on the real market: to book a profit or loss on your buy order, you need to sell the same amount of the security. There’s no such option in the FIX Protocol, either. This mirrors real life, where a single unit of a product that you’ve purchased for $1 needs to be sold if the price is now $1.50 and you want to take your $0.50 profit. Additionally, the range of order types in an MT4 environment does not fully correspond to the range of order types on the exchange, since they were also artificially designed.

Let’s compare the range of order types in MT4 and in FIX API.

MT4 order types:
● Market orders. Currently there are two kinds of market orders:
- buy by market
- sell by market
● Pending orders. Currently there are four kinds of pending orders:
- buy stop
- sell stop
- sell limit
- buy limit

FIX API order types (the most popular ones):
● FOK and IOC market orders
● FOK and IOC limit orders
● GTC (Good Till Canceled) limit orders

If your advisor is using market orders, you can replace them with FOK and IOC market orders. If low slippage is important to your trading strategy, we recommend that you replace market orders with FOK or IOC limit orders. In that case, it will be possible for you to control slippage. You will be able to enter a value for the external “Slippage” parameter.

Here’s how it works in practical terms. Let’s say that the current market bid/ask price is 1.41000 / 1.41010. You enter a value of 10 for the “Slippage” parameter, and your advisor generates a buy signal. You then need to send a buy FOK limit order with a price of 1.41020 (i.e., the current ask price + slippage). The order will then be filled at a price of 1.41020 or a lower (better) price, or not filled at all.

It works the same way with a sell signal. You need to send a sell FOK limit order with a price of 1.40990 (the current bid - slippage), and the order is then filled at a price of 1.40990 or higher (better), or not at all.

If you’re using IOC limit orders, the broker can fill a part of the order at the limit price (or better), while the remaining part will be rejected. IOC limit orders are best if your trading involves high volumes.

If your advisor uses buy and sell limit orders, you can replace them with GTC orders - you will effectively be getting the same thing. The situation is more complicated with buy and sell stop orders, since the exchange does not offer suitable substitutes. The only FIX API broker that offers them is LMAX, and even then, LMAX keeps such orders on its server and sends them to the exchange as market orders once the specified price is reached. If your MT4 broker keeps your account in the A-book, it does the same thing, with the difference that you’ll experience greater execution times and, consequently, higher slippage. We believe that the most sensible course of action is to use FOK or IOC limit orders here as well. In other words, instead of sending buy or sell limit orders, your advisor should memorize the level at which the order should be submitted; once the level is reached, the advisor will send an FOK or IOC limit order with the specified slippage. This approach will also help you control slippage.


1.4. To what extent will trading directly through a black box be different from trading in a FIX API environment?

When you are using an advisor that has been adapted for use with FIX API Trader, quotes will be transmitted from the FIX API account into MT4 at the same speed that you’d get if the quotes were sent directly into a black box (i.e., an advisor fully programmed to work directly with a FIX API account). Whenever a signal is generated, the advisor relays it to the FIX API account. The resulting delay caused by Windows timers can be on the order of 1-5 milliseconds, which is not material even if you are using a high-frequency or arbitrage trading strategy.

2. How to adapt an MT4 Expert Advisor (EA) for use with FIX API Trader


BJF has developed tools and technology that adapt an MT4 EA for use in a FIX API Trader environment, ensuring that the code of the original MT4 EA remains unchanged and that only a certain recompilation of the original MT4 EA is needed. This is achieved through the use of BJF’s cFIXFunctions base class and BJF’s eFIX_EA template.

2.1. BJF’s base class of cFIXFunctions for work in a FIX API Trader environment


The base class contains all the methods and variables required for work in a FIX API Trader environment. The ability to inherit the base class ensures that the standard functions of the MQ4 language can be mapped to  the functions of FIX API Trader.
For example, the MT4 trading function OrderSend(……) will be transformed into a trading function in a FIX API Trader environment, which will then send the FIX broker a FIX Protocol sequence with a request to open a corresponding order. Again, note that the MT4 OrderSend(……) function and all the other functions do not need to be modified.
cFIXFunctions is the name of the base class. The definition and implementation details for the base class can be found in the following files:
#include <BJF\cFIXFunctions.mqh>
#include <BJF\cFIXFunctions.mq4>

It is important to note that the base class is responsible for the display of MT4 order/position lines and stop-loss/take-profit levels that are live on the FIX broker’s server.

2.2. The eFIX_EA template

The eFIX_EA template represents the MT4 EA. This is where the base class is inherited, and this is where you can find the MT4 code that requires the MT4 EA code to be entered in order to adapt the EA for use with FIX API Trader. The full code of the EA can be found in the eFIX_EA.mq4 file. Here it will suffice to mention the key elements of the code.

1. We launch the base class:
#include <BJF\cFIXFunctions.mqh>
#include <BJF\cFIXFunctions.mq4>
2. We inherit the base class:
class cExpert : public cFIXFunctions
{
public:
cExpert();
~cExpert();
int OnInitEA(); //MT4 EA OnInit()
void OnDeinitEA(const int reason);//MT4 EA OnDeinit(const int reason)
void OnTickEA(); //MT4 EA OnTick()
//--- Place here all MT4 EA functions ---

//--- ---
void Run();
};
3. We create a copy of the new cExpert class:
cExpert * p_cExpert = NULL;
//+------------------------------------------------------------------+
int OnInit()
{
p_cExpert = new cExpert();
if (!p_cExpert.mb_InitSucceeded)
{
Print("mb_InitSucceeded = false");
return(INIT_FAILED);
}
if (p_cExpert.OnInitEA() != INIT_SUCCEEDED)
{
Print("OnInitEA() return INIT_FAILED");
return(INIT_FAILED);
}
EventSetTimer(1);
Print(p_cExpert.ms_EA_Name + " FIX Started Fine...");
return(INIT_SUCCEEDED);
}

4. We launch the MT4 EA that we need to adapt for use in a FIX API Trader environment:
void OnTimer()
{
EventKillTimer();
p_cExpert.Run();
}
//+------------------------------------------------------------------+
void cExpert::Run()
{
while(!IsStopped())
{
Sleep(pi_LoadCPU);
if (!IsFixBrokerOnline())
{
Sleep(1000);
continue;
}
FIX_Run();
OnTickEA();
}
}
//+------------------------------------------------------------------+
void cExpert::OnTickEA()
{

}
//+------------------------------------------------------------------+

2.3. A step-by-step example of how to adapt the MT4 EA Moving Average.mq4 for use with FIX API Trader


Here is a brief description of the MT4 EA code that needs to be adapted for use in FIX API Trader:

input int pi_Magic = 20131111;
input double Lots = 0.1;
input double MaximumRisk = 0.02;
input double DecreaseFactor = 3;
input int MovingPeriod = 12;
input int MovingShift = 6;
//+------------------------------------------------------------------+
void OnTick()
{
if(Bars < 100 || !IsTradeAllowed())
return;

if(CalculateCurrentOrders(Symbol()) == 0)
CheckForOpen();
else
CheckForClose();
}
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
...
}
//+------------------------------------------------------------------+
double GetLotSize(double Lot)
{
...
}
//+------------------------------------------------------------------+
void CheckForOpen()
{
...
}
//+------------------------------------------------------------------+
void CheckForClose()
{
...
}
//+------------------------------------------------------------------+

We copy the parameters and global variables of the original EA into the eFIX_EA template:
//--- Place here all MT4 EA parameters and global variables ---
input int pi_Magic = 20131111;
input double Lots = 0.1;
input double MaximumRisk = 0.02;
input double DecreaseFactor = 3;
input int MovingPeriod = 12;
input int MovingShift = 6;
3. We copy the prototypes of all the functions in the original EA in the cExpert class into the eFIX_EA template:
class cExpert : public cFIXFunctions
{
public:
cExpert();
~cExpert();
int OnInitEA(); //MT4 EA OnInit()
void OnDeinitEA(const int reason);//MT4 EA OnDeinit(const int reason)
void OnTickEA(); //MT4 EA OnTick()
//--- Place here all MT4 EA functions ---
int CalculateCurrentOrders(string symbol);
double GetLotSize(double Lot);
void CheckForOpen();
void CheckForClose();
//--- ---
void Run();
};
4. In the eFIX_EA template, we define the functions of the original EA as cExpert methods:
void cExpert::OnTickEA()
{
...
}
//+------------------------------------------------------------------+
int cExpert::CalculateCurrentOrders(string symbol)
{
...
}
//+------------------------------------------------------------------+
double cExpert::GetLotSize(double Lot)
{
...
}
//+------------------------------------------------------------------+
void cExpert::CheckForOpen()
{
...
}
//+------------------------------------------------------------------+
void cExpert::CheckForClose()
{
...
}
//+------------------------------------------------------------------+
5. We launch the MT4 EA that is adapted for use in FIX API Trader:
void OnTimer()
{
EventKillTimer();
p_cExpert.Run();
}
//+------------------------------------------------------------------+
void cExpert::Run()
{
while(!IsStopped())
{
Sleep(pi_LoadCPU);
if (!IsFixBrokerOnline())
{
Sleep(1000);
continue;
}
FIX_Run();
OnTickEA();
}
}
//+------------------------------------------------------------------+

That’s all we need to do. Note that we haven’t changed a single line in the MT4 EA that we are adapting for use in FIX API Trader.

2.4. An example of how to adapt the MACD Sample.mq4 MT4 EA for use with FIX API Trader


The EA MACD Sample.mq4 file that needs to be adapted, as well as the file of the adapted EA (eFIX_MACDSample), is enclosed in this article. In this EA, stop-loss trailing is performed, and the adapted EA performs this task very well.

2.5. An example of the MT4 EA dialogue trading panel for use in a FIX API Trader environment


The high compatibility between a FIX API Trader environment and the standard environment of the MT4 terminal is shown in the illustration below. We can see an MT4 FIX EA displaying the all-too-familiar trading panel that sends trading orders directly to the server of a FIX broker:

FIX API Trader One Click

The EX4 file of this EA (eFIX_TradeByClckNet_01.ех4) is enclosed in this article.

2.6. A list of files enclosed in this article


1. cFIXFunctions.mqh, cFIXFunctions.mq4 , ObjFunctions.mqh - the implementation of the base class
2. eFIX_EA.mq4 - the EA template for adapting the MT4 EA
3. Moving Average.mq4, eFIX_Moving Average – the original MT4 EA and its adapted version
4. MACD Sample.mq4, eFIX_MACD Sample – the original MT4 EA and its adapted version
5. eFIX_TradeByClckNet_01.ех4 - the dialogue trading panel for work in a FIX API Trader environment

2.7. Final notes


It is certainly possible that you might come across an EA whose elements are not included in our base class. In that case, it will be necessary to simply modify our base class.

Subscribe for Email Updates

And learn more about professional forex trading via FIX API protocol . You will receive several educational videos and usefull infoirmation about FIX API.