Direct arbitrage trading in forex

 

The strategy

In this blog post, I will talk about how I create MT4 EA for direct hedge trading strategy.

This strategy belongs to the mean reversion strategy. We created a new instrument based on the price difference between brokers. There is an assumption that the price difference between brokers is stably fluctuating around a point. Whenever the price difference goes too big, we short the new instrument that we created. We can reverse our action when the price difference goes to another way round.

Here is an example that can help you to understand better — link.

From this page, it purported that their product is also able to execute this kind of hedging strategy.

The idea behind this strategy is simple, and it is quit safe. You got almost no exposure in the market, of course you have exposure in the individual brokers. The direction of the underlying instrument is no longer relevant to you, this is so nice that you can profit without having a crystal ball to reveal the future price of the market. With so many good things about this strategy, I would like to implement it through MQL4 and run it on Metatrader platform over connecting to those ECN brokers. I would also like to know the limitations and the requirements to make this strategy to success.

Research and analysis

Before implementing and running the EA on the market, I will create tools to help me to understand the market environment so that I can know beforehand if there is a chance to execute my strategy. To start with the analysis, I will need to have the tick data captured from different brokers directly from the trading account that I am going to use. I picked a several brokers with different types of accounts including normal live trading account and cent account. The live trading account is used for real trading after I confirmed the strategy is success. The cent trading account is used for testing. Here is the list of brokers I started with:

  • FBS
  • Exness
  • RoboForex
  • Axi
  • PepperStone
  • FXTM

Here is the infrastructure I used to capture the tick data:

I have vps deployed with chocoping running at NY. There are multiple MT4s running on the machine to capture the tick data for me. Once the data is ready, I will collect them and merge the data with another tool created which named as tick_data_merge_engine. This engine will replay the tick data according to the time sequence and fit them into the strategy_simulation_engine and data_visualize_engine. There are 2 reasons to do this. First, I need a way to visualize and proof my strategy is workable in the old data. Second, I need a data feed running side by side with my production EA in the future so that I can debug and reproduce any unknown situations.

Here is an image showing the cross spread and total spread information during a week:

There are 4 lines in the above graph:

We pick RoboForex as the primary broker and Exness as the opposite one. From this graph, it is clear that the total spread covers the cross spread which set a protection shield for the brokers. The strategy now becomes more complicated since we also need to consider the spread before we can get our payday.

An other observation from the above graph is that the cross spread is moving around the average value constantly which proof that there is a possibility to use the reversion strategy.

Strategy formation

After the long preparation of the research phase, we are now ready to simulate and try out the execution strategy part. Let’s see some pseudo code illustration to get the idea.

double entry_value_start = cross_spread_avg - some_value_start;
double entry_value_end = cross_spread_avg - some_value_end
if (entry_value_start < cross_spread_ask && cross_spread_ask < entry_value_end)
    do_long();
if (entry_value_start < cross_spread_bid && cross_spread_bid < entry_value_end)
    do_short();

With the above logic, I tried different entry value and here is the result:

 

The criteria of market exit is simple, whenever the profit is big enough to cover the total spread, that is the exit moment. Obviously, from the data we have, there are multiple chances to enter and exit the market with profit within a day. Now we can go to the next phase to implement the EA on MT4.

EA creation 1

You can only connect to one broker at a time in each MT4 while EAs can only be running over MT4. As a result, we need to have a way to perform inter-process communication IPC between multiple MT4 to allow us to operate the reversion strategy. The first candidate on our list is TCP communication among the others such as: named pipe or shared memory with the help of custom created dll solution.

Here is the first prototype we have:


This is a peer to peer structure, each EA is receiving and sending tick data to each other. Once there is a chance spotted, EAs will execute the order separately and simultaneously. Here is a communication complexity analysis for this structure.

+---------+---------------+
|   Case  | Number of IPC |
+---------+---------------+
| Success |       2       |
+---------+---------------+
|   Fail  |       2       |
+---------+---------------+

If we are focusing on the IPC only, the worst case scenario for P2P is 2. I implemented this structure in the first prototype, but then I realized that the logic of fail handling is not very straight forward especially there is a race condition to handle. To simplify the debugging process, I go to the second attempt — Primary and Secondary pair.

A note for the race condition. One example is that the success confirmation arrived from the opposite EA before the order execution success in the other EA. We need to store the confirmation and pair it up later. This handling is important to confirm the order is canceled if the opposite order is not exist.

EA creation 2

In this structure, we pick a primary EA. This EA will receive tick data from secondary EAs and control them when it finds opportunities while the secondary EA will only send tick data to the primary EA. Here is the IPC complexity table:
+---------+---------------+
|   Case  | Number of IPC |
+---------+---------------+
| Success |       3       |
+---------+---------------+
|   Fail  |       3       |
+---------+---------------+

This structure has a higher complexity comparing with the P2P one, though this is not too bad and the implementation is simpler. This is good for development and debug at the beginning. If we need optimization in the future, we will consider an improvement in this area and also the communication method to reduce the latency.

Strategy result

We deployed the EA on VPS shifted by chocoping. Last Friday night at HKT around 20:30, there was a position created over the symbol EURUSD. It shows that our EA can spot the opportunity which is a good sign. The problem is slippage is too huge that damage the position. The EA spotted a negative cross spread and then it tried to enter the market. With the additional 2x pips introduced by both of the brokers, the position is no longer profitable.

I will try a little bit more but I belief this is why this strategy is not actively used by the retail traders.

Comments