Creating an Expert Advisor, which Trades on a Number of Instruments

Introduction

The technical side of implementing the program code in order for a single Expert Advisor, launched on a single chart, to be able to trade with different financial assets at the same time. In general, this was not a problems even in MQL4. But only with the advent of the MetaTrader 5 client terminal, traders finally got the opportunity to perform a full analysis of the work of such automates, using strategy testers.

So now multi-currency automates will become more popular than ever, and we can forecast a surge of interest in the construction of such trading systems. But the main problem of implementation of such robots is in the fact that their dimensions in the program code expand, at best, in an arithmetic progression, and this is not easy to embrace for a typical programmer.

In this article we will write a simple multi-currency Expert Advisor, in which the structure flaws are, if not absent, then at least minimized.

1. Implementing of a simple trend-following system

In fact, we could start with a maximally simple trading system, following the trend on the basis of a built-in terminal of a technical indicator Triple Exponential Moving Average. This is a very simple algorithm, which does not require special commentaries, and which we will now embody in the program code.

But first and foremost, I would like to make the most general conclusions about the Expert Advisor. It makes sense to begin with the block of incoming Expert Advisor parameters, declared on a global level.

So, first of all we must choose the financial assets that we will be working with. This can be done using line input variables, in which the asset symbols can be stored. Now it would be nice to have a trade ban switch for each financial asset, which would allow to disable trading operations by the asset.

Naturally, each asset should be associated with their individual trading parameters of Stop Loss, Take Profit, the volume of the open position, and slippage. And for obvious reasons, the input parameters of the indicator Triple Exponential Moving Average for each trading chip should be individual.

Here is one final block of input variables for just one chip, performed in accordance with these arguments. The remaining blocks differ only by the numbers in the names of input parameters of the Expert Advisor. For this example I limited myself to only twelve financial assets, although ideally there is no software limitations for the number of such blocks.

We only need something to trade on! And most importantly - our PC must have enough resources for solving this problem.

Now that we figured out the variables at the global level, we can proceed to the construction of the code within the function OnTick(). The most rational option here would be the division of the algorithm for receiving trading signals and the actual trading part of the Expert Advisor into two custom functions.

And since the Expert Advisor works with twelve financial assets at the same time, there must also be twelve calls of these functions within the OnTick() block.

Naturally, the first input parameter of these functions should be a unique number, under which these trading assets will be listed. The second input parameter, for obvious reasons, will be the line name of the trading financial asset.

For the role of the third parameter, we will set a logical variable to resolve the trade. Next, for the algorithm of determining trading signals, follow the input indicator signals, and for a trading function - the distance to the pending orders, the volume of position and slippage (allowable slippage of the price of open position).

For transferring the trading signals from one function to another, static arrays should be set as the parameters of the function, which derive their values through a reference. This is the final version of the proposed code for the OnTick() function.

Inside the TradeSignalCounter() function, it is only needed to obtain the handle of the technical indicator Triple Exponential Moving Average once at the start of each chip, and then at each change of the bar to calculate the trading signals.

This relatively simple scheme with the implementation in the code is beginning to overflow with minor details.

The first of these functions returns the true value at the moment of the bar change, based on the selected symbol and timeframe, and the second one, returns the line by the result code of the trading transaction, derived from the field retcode of the trade request structure MqlTradeResult.

The Expert Advisor is ready, it's time to begin with testing! There are no visible serious differences in the testing of the multi-currency Expert Advisor from his fellow single-currency Expert Advisor.

Determine the configurations on the tab "Parameters" of the Strategy Tester:

Figure 1. "Settings" tab of Strategy tester

If necessary, adjust the values of the input parameters on the tab "Input parameters:

Figure 2. "Parameters" tab of Strategy tester

and then click the "Start" button in the Strategy Tester on the tab "Settings":

Figure 3. Running the Expert Advisor test

The passing time of the first test of the Expert Advisor may turn out to be very significant, due to the loading of the history for all twelve symbols. After completing the test in the strategy tester, open the tab "Results":

Figure 4. Testing results

and make an analysis of the data, using the contents of the "Chart" tab:

Figure 5. Chart of balance dynamics and equity

and the "Journal":

Figure 6. Strategy tester Journal

Quite naturally, the very essence of the algorithm's entrances and exits of the market of this Expert Advisor's is too simple, and it would be naïve to expect very significant results, when using the first, random parameters. But our goal here is to demonstrates the fundamental idea of constructing a multi-currency Expert Advisor in the simplest way possible.

With the optimization of this Expert Advisor may arise some inconveniences due to too many input parameters. The genetic algorithm of optimization requires a much smaller amount of these parameters, so the Expert Advisor should be optimized on each chip individually, disabling the remaining chips of inputs parameters TradeN.

Now, when the very essence of the approach has been outlined, you can start working with the more interesting algorithm of decision making for multi-currency robot.

2. Resonances on the financial markets and their application in trading systems

The idea of taking into account the correlations between the different financial assets, in general, is not new, and it would be interesting to implement the algorithm, which would be based precisely on the analysis of such trends. In this article I will implement a multi-currency automate, based on the article of Vasily Yakimkin "Resonances - a New Class of Technical Indicators" published in the journal "Currency Speculator" (in Russian) 04, 05, 2001.

The very essence of this approach in a nutshell looks as follows. For example, for researching the on EUR / USD, we use not only the results of some indicators on the financial asset, but also the results of the same indicator on related to the EUR/USD assets - EUR/JPY and USD/JPY. It is best to use the indicator, the values of which are normalized in the same range of changes for the simplicity and ease of measurements and calculations.

Considering these requirements, a well suited for this classic is the stochastic indicator. Although, in actuality, there is no difference in the use of other indicators. As the trend direction we will consider the difference sign between the value of the stochastic Stoh and its signal line Sign .

Figure 7. Determining the trend direction

For the variable symbol dStoh there is an entire table of possible combinations and their interpretations for the direction of the current trend:

Figure 8. Combinations of the variable symbol dStoh and the trend direction

In a case where two signals of the assets EUR / JPY and USD / JPY have the opposite values, we should determine their sum, and if this sum is greater than zero, consider both signals as positive, otherwise - as negative.

Thus, for the opening up Longs, use the situation when the trend is growing, and to exit, use a downward trend,or a trend when the signals of the indicator of the main asset EUR / USD are negative. Also, exit the long if the main asset has no signals, and if the sum of the variable dStoh for the remaining assets is less than zero. For the shorts, everything is absolutely analogous, only the situation is opposite.

The most rational solution would be to place the entire analytical part of the Expert Advisor in the multicurrency indicator, and for the Expert Advisor from the indicator buffers, take only the ready signals for trade control. The version of this indicator type is presented by the indicator MultiStochastic.mq5, providing a visual analyses of market conditions.

Figure 9. MultiStochastic Indicator

The green bar signal the opening and retaining of longs, and the red bars - of shorts, respectively. Pink and light green points on the upper edge of the chart represent the signals of exiting the long and short positions.

This indicator can be directly used for receiving signals in the Expert Advisor, but still it would be better to ease its work and remove all of the unnecessary buffers and elements of visualization, leaving only what is directly involved in the supply of trading signals. This is precisely what was done in the MultiStochastic_Exp.mq5 indicator.

In this Expert Advisor, I traded with only three chips, so the code of the OnTick() function became exceedingly simple:

However, the code of the TradeSignalCounter() function is a little more complex: The fact is that a multi-currency indicator works directly with three timeseries of different financial assets, and therefore, we implement a more subtle verification of bars for the adequacy of the minimum number of them in one of the three timeseries, using the Rates_Total() function.

In addition, an additional verification on the synchronization of timeseries is made, using the SynchroCheck() function, to guarantee the accuracy of determining the moment when a bar change occurs in all timeseries simultaneously.

There are no other radical ideological differences of the code of this Expert Advisor (Exp_ResonanceHunter.mq5) due to the fact that it is compiled on the basis of the same functional components. Therefore I don't think that it will be necessary to spend any more time on its internal structure.

Conclusion

In my opinion, the code of the multi-currency Expert Advisor in MQL5 is absolutely analogous to the code of a regular Expert Advisor.

The MQL5 programming language primarily targets the creation of automated trading systems and complex instruments of technical analyses. But aside from this, it allows us to create interesting information systems for tracking market situations, and provides a return connection with the trader. The article describes the MQL5 Standard Library components, and shows examples of their use in practice for reaching these objectives. It also demonstrates an example of using Google Chart API for the creation of charts.

In this article the author talks about evolutionary calculations with the use of a personally developed genetic algorithm. He demonstrates the functioning of the algorithm, using examples, and provides practical recommendations for its usage.

The development of trading strategies primarily focuses on searching for patterns for entering and exiting the market, as well as maintaining positions. If we are able to formalize some patterns into rules for automated trading, then the trader faces the question of calculating the volume of positions, the size of the margins, as well as maintaining a safe level of mortgage funds for assuring open positions in an automated mode. In this article we will use the MQL5 language to construct simple examples of conducting these calculations.

This article focuses on the object oriented approach to doing what we did in the article "Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners" - creating a simple Expert Advisor. Most people think this is difficult, but I want to assure you that by the time you finish reading this article, you will be able to write your own Expert Advisor which is object oriented based.