MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

How to add customizable variables into an Expert Advisor in MQL4

You might be wondering what it means exactly, to add in customizable variables, in an Expert Advisor, since doing coding makes every Expert Advisor customizable anyway.

However, what this means is that, when you go back into your MetaTrader4 terminal, to attach this Expert Advisor to a chart or to test it in the strategy tester, you may want to be able to tweak it, without having to tweak the code itself.

For Example: Let's pretend you have an Expert Advisor based on the RSI indicator, such as the "Template Ea", provided in the "How to use MetaEditor" chapter.

Now what if you hadn't ran it through the strategy tester yet?

What if it is set to Sell when the RSI goes higher than 70 and Buy when the RSI goes less than 30?

After you have ran it, you may find that it is unsuccessful and needs some tweaking, as to where it buys and sells.

So, now you have to go back to your code and change that... OR DO YOU!???

You don't need to, if you have made it customizable from outside the MetaEditor.

And this is what you will learn how to do in this section.

This was covered briefly in the last chapter, however, for anyone that didn't read that chapter, we will go over it again and in a little more detail.

Step 1: Decide what you want to be customizable

If you have already copy and pasted the code from the template EA, and saved it as a new file name, you will see some the first parts of code at the very top.

It is the code within the red square, in the picture below.

How to create custom inputs for expert advisor autotraders using extern int


In the picture above you can see everything that says "extern" in front of it. In the picture below, you can see where they can be changed and used, outside of the MetaEditor

They can be changed from a chart, when the Expert Advisor is attached to it OR they can be changed from within the strategy tester.

It is recommended though to change these variables in the strategy tester, simply to make the backtesting process faster. After you are satisfied with your EA, then going into you code and editing these extern variables, to match your backtesting values, is recommended. It will save you from forgetting the changes.

What if you made the changes to your extern variables while an Expert Advisor was on the chart... Then accidentally removed that EA from the chart?

When you go to reattach it, any changes made will be lost and you will have to try and remember them. If you just change them on the strategy tester and then change them permanently, in your code, you will not have this worry.

Please observe below...

The above pictures shows how the extern variables will appear when opened on an expert advisor. They open automatically, upon attaching an EA to a chart and through the "Expert Properties" tab, when a chart is Right Clicked.

It also opens from the strategy tester when that Expert Advisor is in use, in the strategy tester. Simply click "Expert Properties" to open.

What should you make externally editable?

There are many things you may want to edit while strategy testing, without having to go back into MetaEditor. Here are a few things you might consider using as external inputs:

  • TakeProfit/StopLoss
  • Indicator Settings
  • Amount of Lots for investment
  • Slippage

All of the above are things, that you may need to tweak a fair bit, while backtesting your strategies. Once backtesting results are perfect, you just go and input those values into your mql4 code, as the permanent default values.

Step 1: How to code in extern variables

Since we are talking basics here we will just stick to "Extern int" and "Extern double".

The difference between these two are only in the type of numbers they can handle.

An "Extern int" can handle whole numbers, positive and negative, but without decimals.

An "extern double" is needed, for handling decimals, including positive or negative numbers.

Pretend you are adding in an RSI indicator, for use in your EA.

So here is our code, placed in the indicator section of our template EA...

The above is the code needed to use an RSI INDICATOR within our Expert Advisor. As you can see there are a few different parts to it.

There is the name we gave it, its call to use via "iRSI"and then all of its variables within the brackets

NOTE:MetaEditor has an auto-suggest feature that will show you what needs to go where in any indicator. Try it out for yourself...

So looking at the values, we see that the period time is set to 14 and the barshift is set to 3.

Now, what if we want to try running the strategy tester with an RSI period of 10 or 20 or 50 even? What if we want to experiment with different barshift times?

We could jump back and forth from the strategy tester, in the MT4 terminal, to the MetaEditor every time... OR we could use the flexibility of an "extern int" or "extern double".

Question: What should we use for the time period? An "extern int" or an "extern double"?

Do we need decimals? No we do not, so we use an "Extern int". We do not need decimals for barshift values either, so we use an "extern int", as well.

Next, we go back up to the top of our Expert Advisors code and anywhere before "int start(){", we add in our externs.

We simply decide on a name first. We will use "period" for the RSI INDICATORS period and "barshift" for the shift...

Now, we type in: extern int period=20;

Below that, we write: extern int barshift=1;

The MetaEditor and MT4 terminal knows that whatever comes after the term "extern int", is the name given to it. Whatever comes after the "=" is the value than can be edited externally. Then just cap it off with a semi-colon ";".

It's that easy and should look like this:

Now that we have named the above "period" and "barshift" and gave them both a value, they are ready to be used anywhere throughout our code.

Let us now go back to our RSI indicator in the indicator section of our code. We can now simply replace the values for period with the name given to our "extern int". Which by no coincidence, is named "period". Also replace the shift value, with "barshift". It will look like the following image.

We now have an RSI INDICATOR that has values for period and shift that can be altered outside of the MetaEditor, during backtesting! Voila!

You can use this in many different ways. Any time you need something externally editable, just do as shown above.

HINT: You can also use the "int" or "double", without the "extern" if you are needing to call on something only inside your code, without having to write its full function call every time. These must be used within the rest of your code, after the "int start(){"

For example; "double MYRSI=iRSI(NULL,0,14,0,1);

In this example you see the use of "double". As you know that means it will handle decimals.

That is because the indicator, when in use, is always returning a value. That value is precise and uses decimals.

We used the name "MYRSI", so now if we want to include it in an opening or closing condition, we simply call it by the name we gave it. The program then refers back to the code defined after "MYRSI", which is the RSI INDICATOR.

Our closing condition may look like this for a sell order:

if(MYRSI>75)

{

int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slip,0,0,"TEMPLATE",MagicNumber1,0,Green);

}

If you noticed the "int opensell=", you should be familiar with it. The OrderSend function sends back a value that does not need decimals. It returns the "Order Ticket Number". So, we use "int", instead of "double".

NEXT UP --->

How to add buying and selling conditions into an Expert Advisor