Incorporating trader inputs in algorithmic systems – MQL4 for Complete Beginners Tutorial Part 14

Welcome back! In this tutorial we will take one of the strategies we discussed last time and turn into into an MQL4 program.

Also, another important aspect of today’s lesson is – incorporating trader input. We will work with extern variables, which will allow us to create parameters for the trader. In that way the trader can control the script directly from the MetaTrader 4 Forex Platform.

Extern variables will come in very handy when we start coding our expert advisor. So pay attention today! 🙂

Get the full course here: https://www.forexboat.com/learn-mql4

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial14.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

TakeProfitLevel = Bid + TakeProfit*Point; //0.0001
StopLossLevel = Bid - StopLoss*Point;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

}
//+------------------------------------------------------------------+

 

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial14.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

//here we are assuming that the TakeProfit and StopLoss are entered in Pips
TakeProfitLevel = Bid + TakeProfit*Point*10; //0.00001 * 10 = 0.0001
StopLossLevel = Bid - StopLoss*Point*10;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

}
//+------------------------------------------------------------------+

 

What are you waiting for?

START LEARNING FOREX TODAY!

share This:
Muhammad Awais

7 Responses to “Incorporating trader inputs in algorithmic systems – MQL4 for Complete Beginners Tutorial Part 14”

March 17, 2015 at 10:28 am, Nick Haystead said:

Hi Kirill
My point size in tutorial 14 is 0.00001 (see attached), yours is 0.0001, so I only get 1 pip when I multiply by 10, instead of the expected 10 pips. Any idea why my point size is 1/10th of yours?
I am accessing your excellent course through Udemy.

Reply

March 17, 2015 at 10:32 am, Kirill said:

Hi Nick,

That’s because you are using a 5-digit broker, in the example I used a 4-digit broker. Not a worry, it just means that you have to specify your takeprofit and stoploss input parameters 10x greater than usual.

Does that make sense?

Let me know if you still have any questions.

Cheers,

Kirill

Reply

March 17, 2015 at 10:44 am, nickhstd2 said:

So point size isn’t necessarily the same as pip size? I could see this would be a problem if your algo was released to ‘Joe public’ as you would not know whether they were using a 5-digit broker or a 4-digit broker? The user interface part would be confusing if you had to specify 100 for a 10 pip stop-loss for example, how would you get around that issue?

Reply

March 17, 2015 at 11:13 am, Kirill said:

That’s right points and pips are conceptually different. The most common terminology is as follows: a point is always equal to 0.0001 (for non-JPY currencies). A pip, on the other hand, is the minimal possible change in quotation – with a 4-digit account a point is equal to 0.0001, with a 5-digit account a point is equal to 0.00001.

There are different schools as to how to account for this in you EA. You can either expect the user to specify 10x values for input parameters in the case of a 5-digit account. Alternatively, you can assume this responsibility within your code – check the type of account and if it is 5-digits then multiply all inputs by 10. In the latter case you are assuming that the user will always input parameters as if it is a 5 digit account.

Both ways are valid. However, in either case you have to let the user know what they need to do, because otherwise they have no way of telling which approach you have adopted.

Hope that helps,

Kirill

Reply

March 17, 2015 at 11:34 am, nickhstd2 said:

OK thanks Kirill very helpful – on with the programming!
By the way do you intend to do a more advanced course, to build on this one?

March 17, 2015 at 11:52 am, Kirill said:

Glad I could help out!

Sure thing – it’s just a question of ‘when’ that course will come out 🙂 Perhaps, sometime this year.

Cheers,
Kirill

May 26, 2015 at 11:24 am, Kirill said:

Hi Nick,

Hope you have progressed with MQL4! Yes, there will be an advanced course – sometime later this year.

Cheers,

Kirill

Leave a Reply

Your email address will not be published. Required fields are marked *

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: