Skip to main content
Version: 2.0

No.1: Long and short trading combined

Description

You can find an example for the Custom Expert Advisor on this page. If you still need to become familiar with the MetaTrader strategy tester and the Custom Expert Advisor, please begin with the Getting Started article first.

The detailed explanation of the entire configuration process goes beyond the scope of this article. Therefore, all the necessary configuration files are also available for download. These can be found in the Chart example and profile folder download section. However, a brief explanation and the defining input parameters of each trading module in this system is provided.

In this example, two trading modules are utilized simultaneously. The first module initiates long trades based on the MACD signal, while the second opens short trades. Despite each trading module being fixed to either a long or short direction, creating a trading system that executes both market orders concurrently is straightforward. An ATR indicator determines the initial stop loss for both modules and an ATRS system is employed as a trailing method, meaning the stop loss is adjusted in line with the current average true range of the chart.

Each trading module can only have one open trade at a time. This measure is in place to prevent false signals, such as a rapidly swinging MACD signal line, from opening too many trades due to the same signal. Combining two trading modules allows the system to have a maximum of two open trades at any time.

Please note that this is not a plug-and-play trading system. A trading system needs suitable adjustments, fine-tuning to the timeframe and symbol, and thorough backward and forward testing. Therefore, providing fully working trading strategies as ready-to-use examples is impossible. However, this example could serve as a foundation for your systems.

Individual trading modules

This section provides an overview of how each trading module is configured and what the trading behavior looks like. It can aid in understanding the individual signals generated by each trading module. As previously mentioned, the entire trading system in this example comprises multiple trading modules. Each module operates independently but shares the same resources, ensuring that indicators or oscillators don't need to be recalculated numerous times. This approach conserves the resources required to execute the overall trading system.

EAM_I1.set, MACD - Long trading

When each trading module is executed individually, module no. 1 only performs long operations.

Custom expert advisor example chart 0, Long and short trading combined, MACD - Long trading.png

This trading module was created with the Expert Advisor Builder. The required input parameters for this trading module are as follows.

Input variable within MetaTrader
(Identifier in SET file)
Example valueUser manual chapters
> (OUF) Idea output description (max 63 characters)
(IN_strOufIdeaOutputDescription)
MACD - Long tradingOutput Folder
> (OSA) CONF, Signal in use
(IN_enmOsaSignalInUse)
ONConfiguration
Available Oscillators and Default Values
Example
> (OSA) COND, Relation 1 OSA1 to OSA2
(IN_enmOsaRelation1)
VALUE X CROSS VALUE X
> (OSA1) CONF, Signal in use
(IN_enmOsa1SignalInUse)
ON
> (OSA1) COND, Angle limit condition
(IN_enmOsa1AngleCondition)
GREATER LIMIT 1
> (OSA2) CONF, Signal in use
(IN_enmOsa2SignalInUse)
ON
> (OSA2) COND, Angle limit condition
(IN_enmOsa2AngleCondition)
GREATER LIMIT 1

EAM_I2.set, MACD - Short trading

Module no. 2 only carries out short operations.

Custom expert advisor example chart 2, Long and short trading combined, MACD - Short trading.png

This trading module was created with the Expert Advisor Builder. The required input parameters for this trading module are as follows.

Input variable within MetaTrader
(Identifier in SET file)
Example valueUser manual chapters
> (OUF) Idea output description (max 63 characters)
(IN_strOufIdeaOutputDescription)
MACD - Short tradingOutput Folder
> (ORM) Operation
(IN_enmOrmOperation)
SHORTOrder Manager
> (OSA) CONF, Signal in use
(IN_enmOsaSignalInUse)
ONConfiguration
Available Oscillators and Default Values
Example
> (OSA) COND, Relation 1 OSA1 to OSA2
(IN_enmOsaRelation1)
VALUE X CROSS VALUE X
> (OSA1) CONF, Signal in use
(IN_enmOsa1SignalInUse)
ON
> (OSA1) COND, Angle limit condition
(IN_enmOsa1AngleCondition)
SMALLER LIMIT 1
> (OSA2) CONF, Signal in use
(IN_enmOsa2SignalInUse)
ON
> (OSA2) COND, Angle limit condition
(IN_enmOsa2AngleCondition)
SMALLER LIMIT 1

Chart example and profile folder download

As a result of combining both trading modules, the chart will generate signals and trade, as shown in the following picture.

Custom expert advisor example chart 3, Long and short trading combined, All modules combined.png
tip

To reproduce this example, you can also download the zip package cea_example_1_v_2_0.zip containing the entire profile folder.

In addition to the trading module SET files, the Custom Expert Advisor requires further input information, which defines the trading modules used for the trading system and the current trading stage.

The information about which trading modules are in use is defined in the System.txt file. This standard text file can be edited with any text editor. Please ensure that the existing format is maintained and lines are only added or removed. A helpful feature is that this can be done even when the Custom Expert Advisor is active.

/cea_example_1/System/System.txt
Please ensure data is in format <MODULE_NAME>.
EAM_I1
EAM_I2

Since there may be a need to turn off trading modules, a second text file-based input, which defines the trading state, is required. The code block below shows what this file (Condition.txt) looks like for the example provided in this article.

/cea_example_1/Condition/Condition.txt
Please ensure data is in format <MODULE_NAME> <MODULE_STATE> where MODULE_STATE can be ENABLED, DISABLED_WITHOUT_CLOSING or DISABLED_WITH_CLOSING for each module!
EAM_I1 ENABLED
EAM_I2 ENABLED

Please remember not to alter the basic file formatting. Like the system file, the condition file can also be adapted while the Custom Expert Advisor is active. The trading system will adjust accordingly whenever a file change is detected. This allows for modifying active trading systems while preserving previous inspector values.