MQL5 Tutorial – Simple External Signals In A File

video
play-sharp-fill

 

Introduction to Using External Signals in MetaTrader (00:00 – 00:15)

  • Introduction to how external signals can be used to create buy or sell signals in MetaTrader. Example of a simple text file influencing trading decisions.

Setting Up the Text File for Signals (00:15 – 00:46)

  • Explanation of the location and content of the text file used for generating signals in Windows. Demonstration of how changing the text file content immediately affects trading actions.

Creating the Expert Advisor in MetaEditor (00:46 – 01:18)

  • Instructions for opening MetaEditor, creating a new Expert Advisor file named ‘Simple External Signal’, and initial code setup.

Importing Trade.mqh and Initializing CTrade (01:18 – 01:43)

  • Steps to import the ‘Trade.mqh’ file and create an instance of the ‘CTrade’ class for opening trades.

Getting Ask Price and Normalizing Digits (01:43 – 02:13)

  • Code for obtaining the ask price for the current symbol and normalizing the number of digits behind the decimal point.

Defining File Path and Opening Text File (02:13 – 03:46)

  • Defining the file path for the signal text file, opening the file using ‘FileOpen’, and setting the appropriate file mode and encoding.

Reading and Closing the External File (03:46 – 04:08)

  • Reading the buy signal from the external text file using ‘FileReadString’ and closing the file with ‘FileClose’.

Checking for Open Positions and Executing Trades (04:08 – 04:50)

  • Checking if there are no open positions and if the text file contains a specific buy signal, then executing a buy trade.

Displaying Current Buy Signal and Positions Total (04:50 – 05:08)

  • Using the ‘Comment’ function to display the current buy signal from the file and the total number of positions.

Compiling and Testing the Expert Advisor (05:08 – 05:37)

  • Instructions for compiling the Expert Advisor and testing it in MetaTrader using the Strategy Tester.

Observing EA Behavior with External Signals (05:37 – 06:56)

  • Observing how the Expert Advisor responds to changes in the external text file, opening and closing trades based on the updated signals.

 

This week I’ve gotten a few requests: how we can use external signals to create buy or sell signals in Metatrader. Right now, our buy signal is “Don’t Buy It Now”, and we have no open positions. And this is a simple text file. It is located in the user’s path in Windows. So that path here might be a little bit different, depending on your name and your operating system, but it should be below MetaQuotes, terminal, common files. Right now, it contains the sentence, “Don’t buy it now”. So let’s see what happens if I remove the Don’t and save the file. Immediately, a new Buy trade appears. So now we have opened a new position by using an external text file. And in this little video, we are going to find out how to do that. In your MetaTrader, please click on the little button here or press the F4 key. And in Metatrader, you want to click on File, New, Expert Advisor from Template, Continue. I will call it simple external signal and finish. And now you can remove everything above the on tick function and the two comment lines here. And to open a trade, the first thing we need is to import the trade.MQH file by using the include function.

 

And now we want to create an instance that will have the name trade. And inside the ontick function, the first thing we do is to get the ask price by using symbol info double for the current symbol. We use the constant symbol underline ask because we need the ask price. And with normalize double and underline digits, we make sure that we have the right number of digits behind the dot, because there are some currency pairs that have only three digits and others have five. Let’s tell MQL5 actually which file we are going to use. Remember, this is the path where my files are located. In your case, it might be a little bit different, depending on your operating system and your name. This is the File handle, it’s an integer value and we use FileOpen for the file with the name signal.txt. We use File_Read, because we only want to read what’s in the file. File_ANSI means it’s a file that is containing strings of ANSI type. Ansi is only using one byte. You could, for example, also use File_Unicode. That would be used for two byte symbols. So if you see any weird characters on your screen, the reason might be the wrong encoding here.

 

And File_Common is used for the file path in the common folder of all client terminals. It ends with terminal, common files, like in this case. This pipe character here is used as a delimiter that is used to separate the different items in a txt file or in a comma separated file. And CP_ACP means we are currently using the current Windows ANSI code page. Now that our file is defined, we use File, Read String for the external file we have created here to read out a buy signal. It will be a text string. And afterwards, we close the file. That is done by using the command File Close for our external file. And if no open positions exist and a buy signal occurs, that would mean positions total equals zero and the text in our file is “Buy it Now”. Well, in that case, we want to open a buy position by using trade.buy for 10 micro lots on the current chart for the current Ask Price. We have no stop loss defined. Our take profit is 100 points above the current Ask Price and we don’t need a comment here, so we use null for the last parameter.

 

The last step is to create a chart output by using the comment function. It will show us the current buy signal that is the text that is contained in our file, followed by the text positions total and the value we have calculated for positions total. And if everything is okay, please click on the compile button. And if you don’t have any errors or any warnings, click on a little button here or press F4 to go back to Metatrader. Now in Metatrader, please click on View, Strategy Tester, or press CTRL and R. And here you need to select the file simple external signals.ex5, enable the visualization and start the test. Now our little expert advisor is running and you see the buy signal right now is: don’t buy it now. The number of open positions is zero. So let’s change the text file here. click on File, Save. And immediately a new trade is opened here. And if you speed up the process, you will see that new trades are opened and closed. But if the external condition is no longer true, or if I change the file content manually and save it, the buy signal will change to stop buying or whatever is in the text file.

 

Let’s save that again. Now it says, Buy signal is stop buying or whatever is in the text file. Okay, Now you know how you can create an Expert Advisor that can read external Txt files. Actually, you could also use Excel files or whatever to use external signals. And you have coded it yourself with a few lines of MQL5 code.