My journal in learning to code EA autodidact - Page 2

# Thread: My journal in learning to code EA autodidact

1. ## My journal in learning to code EA autodidact

First of all, thanks to the admin has given me a chance to create this thread. Basically I don't need any automatic robots at all for my trading activities. And I am not a programmer. But I feel that having the ability to coding EA is an interesting thing especially for me personally. That's why I wanted to learn myself about EA coding, I want to create a journal for learning how to code Expert advisors.

I will learn coding from zero through many sources on the internet, at least I want have an ability to create a simple EA. But I hope to be more than that. That's why I have to keep this journal as a note or diary to remember what I've learned, because I'm easy to forget.

If you want to learn together with me here, please give a contribution here.

2. 2. Using Functions

So in addition to using the direct way on the part 1 can also be used functions.
This function specifies a value that is usually placed outside int start ().
Because the number of these orders is an integer is not bloated then used integer (int).

look at it!

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}
return(total) ;
}

This is a script for filter order using function.
How to use it

totalorders (OP_SELL) is the number of sell
etc ....

The order type can also be expressed in numbers
OP_SELL = 1
OP_SELLLIMIT = 3
OP_SELLSTOP = 5

So

totalorders (OP_BUY) can also be written totalorders (0)
as well as totalorders (OP_SELL) can be written totalorders (1)

So the above EA can be replaced
if(totalbuy == 0) becomes if(totalorders (OP_BUY) == 0) or if(totalorders(0) == 0)

And if inserted into the EA
Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy Only";
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}
return(total) ;
}
//+------------------------------------------------------------------+

3. My EA can already be used in all pair and have used fliter pair.
This time we will add rule for order sell, so the EA also can do sell order, while the previous EA just do order buy only.

To add a sell order rules then we need a sell order script, as follows:
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point , Bid-TP*Point,Nama_EA,Magic,0,Red);

There is a difference need to noted that the buy order refers to the current Ask price while the sell order uses the Bid.

On sell order
Bid+SL*Point,
Bid-TP*Point

And to distinguish the order color here I use blue for buy and red for sell.

Well now how to enter the sell order?
In EA 1 in the previous post using a direct pair filter, for that has already provided variable sell order that is TotalSell, so it can be made condition: if(Totalsell==0) or if(Totalsell<1)
Become as follows:
if(Totalsell==0){OrderSend(Symbol(),OP_SELL,Lots,B id,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}

THen we put into the EA 1.

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy Only";
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
int Totalsell=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
if(OrderType()==OP_SELL)  Totalsell++;
}
if(Totalsell==0){OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
//----
return(0);
}
//+------------------------------------------------------------------+

4. In EA 2 that uses a filter pair using the function is if(totalorders(1)==0) can also if(totalorders(OP_SELL)==0) or if(totalorders(1)<1) can also if(totalorders(1)<1)

Become as follows:

if(totalorders(OP_SELL)==0){OrderSend(Symbol(),OP_ SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}

can also be used

if(totalorders(1)<1){OrderSend(Symbol(),OP_SELL,Lo ts,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}

Then we input into the EA 2

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy Only";
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

if(totalorders(1)==0) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
//----
return(0);
}
//+------------------------------------------------------------------+

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}

//+------------------------------------------------------------------+
Another thing to know is that in the mql4 to make information where the description is not read as normal program is used the sign // for one line but if it consists of more lines used the sign / * description and terminated * /

5. Originally Posted by letsdo
are you code writer for meta trader 4 or are you, a beginner?
what is the best way to learning code writing?
I also want to spread the world with forex expectation
as you know a code can also do freelancing on mql 5
Hello letsdo my mate..., I'am a beginner in coding, I am still learning about how to make EAs for MT4. As far as I know that all indicators and Expert Advisor (EA) that exist on Mt4 can't be used on Mt5. But the programmers mainly for the Mql4 script and mql5 try hard to create the mql4 converter tool into mql5. There are many scripts that we can use for it. One of them is using this online converter. You can find them on google.

6. We've been discussing strings, int and doubles. There is one more often used as an option, ie bool.
This Bool ignores value 1 if true and 0 if false.
So this type of data is in a declaration at the beginning of the word bool and added the word without spaces then = (equal to) and given false or true choice and ended with; (semicolon)
Example:
We would like to add an option to EA above so there are options to buy or sell only

We make a declaration first

Then where to put it?
To add this then the thing to look for is the triger part of the order, ie
if(totalorders(1)==0) become if(totalorders(1)==0 && Trade_sell== true )

We try to enter into the EA

EA 1

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----
int Totalsell=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
if(OrderType()==OP_SELL)  Totalsell++;
}
//----
return(0);
}
//+------------------------------------------------------------------+
EA 2

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}

//+------------------------------------------------------------------+
Well the result will be like this... (when we attached the EA to the chart)
2018-05-24_141708.png

7. We have made EA that can buy and sell with additional option parameter to continue trading buy or sell by using boolian so it can be used true or false option.\

We will discuss trading time using broker trading time (time broker) by using variable Hour() for hour and Minute() for minute.

Quite simple example
if(Hour()== 7 && Minute()==5)
This means that what conditions that we want at 07:05

We try to apply in our EA command
if(Hour()== 7 && Minute()==5){
if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Poin t, Bid-TP*Point,Nama_EA,Magic,0,Red);}
}

In addition above, the EA will only buy and sell orders when the clock broker has shown at 7 over 5 minutes.
So how can the desired time parameter be easily displayed in EA?
Then we can make the hour and minute time declaration (this time is an integer data type / no comma)
example

extern int Order_Hour = 7;
extern int Minute = 5;
so if this is inserted into EA as follows:

if(Hour()== Order_Hour && Minute()==Minute){
if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Poin t, Bid-TP*Point,Nama_EA,Magic,0,Red);}
}

It's so easy,,,,

8. So what if we want to add trading time from a certain hour to a certain hour?

Well to make this time limit I will share a script so it is commonly used in the form of function

extern int Start_Hour = 22;
exter int End_Hour = 4;

then the function of the time is as follows:

{
if(Start_Hour > End_Hour){
if (Hour() >= Start_Hour || Hour() < End_Hour) trade = true;
} else
if (Hour() >= Start_Hour && Hour() < End_Hour) trade = true;
}

Because the script is in the form of a function then its placement outside int start()
Overview of the script using bool in applied trading time and must be worth 1.

for its application in EA if(Trade_hour()==1){.....}

If we enter into EA 2 as follows:

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int Start_Hour = 22;
exter int End_Hour = 4;
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+

{
if(Start_Hour > End_Hour){
if (Hour() >= Start_Hour || Hour() < End_Hour) trade = true;
} else
if (Hour() >= Start_Hour && Hour() < End_Hour) trade = true;
}

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}

//+------------------------------------------------------------------+
Okay, EA is done, just compile and EA is ready to be used for trade. Parameter is complete already.

9. EA that I made can make buy and sell order (hedge) continuously if type order 0 and with the addition of filter trading hours. The weakness on the EA that I've created before is not support for 5 digits, I will add a 5 digits filter.
The difference between a 4 digits and 5 digits lies in Points

There are many ways to make EA to be used in 5 digits but I will give 2 simple ways:

1. Using a script

we make the declaration first, given the point value is a semicolon, it includes double data type,
double pt=0; // it put in the declaration.

we create the script as follows:

if(Digits==3 || Digits==5) pt=10*Point; else pt=Point;

The point is that if digits = 3 or digits = 5 then pt = 10 x point else (meaning if not) then pt = point
inserted under int init ()

so

int init()
{
//----
if(Digits==3 || Digits==5) pt=10*Point; else pt=Point;
//----
return(0);
}

What does it mean to be placed there?

int init() stores data when EA is being attached, actually it should not be placed inside init() can also be placed inside int start().

So what's the difference when it put in init() and in start()?

When placed in init() then the pt (pont) value will be read both inside int start() and outside but if it placed at int start() then outside int start() is not readable.

how to change it?

we see the previous EA section associated with the point

if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Poin t, Bid-TP*Point,Nama_EA,Magic,0,Red);}
}

Replate "point" with "pt" :

}

So when we enter into the EA as follows

Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int Start_Hour = 22;
exter int End_Hour = 4;
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;
double pt=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+

{
if(Start_Hour > End_Hour){
if (Hour() >= Start_Hour || Hour() < End_Hour) trade = true;
} else
if (Hour() >= Start_Hour && Hour() < End_Hour) trade = true;
}

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}

//+------------------------------------------------------------------+

10. 2. Using a function

point is a data type of a semicolon then we start with a double.

double dpt()

{

if(Digits==3 || Digits==5) pt=10*Point; else pt=Point;
return(pt);

}

because this is a function, it is placed outside int start(), and for its application just replace point with dpt()

So it goes as follows
Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int Start_Hour = 22;
exter int End_Hour = 4;
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
double dpt()

{
double pt=0;
if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
return(pt);

}

{
if(Start_Hour > End_Hour){
if (Hour() >= Start_Hour || Hour() < End_Hour) trade = true;
} else
if (Hour() >= Start_Hour && Hour() < End_Hour) trade = true;
}

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}

//+------------------------------------------------------------------+
THis is not difficult.

11. This time we will discuss about filter lots.
Usefulness is to prevent errors caused by the parameter lots that are not appropriate.

In the previous discussion we have a lot to discuss also about the use of scrip directly and also can use the function. So on the basis of every trader in pouring the program code it has his own style, and not necessarily the same as everyone.

Well to add this filter lots I will share the script in the form of function, as follows:
double NR(double thelot)
{
double maxlots = MarketInfo(Symbol(), MODE_MAXLOT),
minilot = MarketInfo(Symbol(), MODE_MINLOT),
lstep = MarketInfo(Symbol(), MODE_LOTSTEP);
double lots = lstep * NormalizeDouble(thelot / lstep, 0);
lots = MathMax(MathMin(maxlots, lots), minilot);
return (lots);
}

How is it used?

In the EA that I have created in the previous part we take part that has to do with the regulation of lots that is Lots and we add become NR(Lots)

Before
if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Poin t, Bid-TP*Point,Nama_EA,Magic,0,Red);}
}

After
if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,NR(Lots),Bid,3,Bid+SL* Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
}

From the addition of the script above then if there is mistaken filling parameter of lots then EA will still execute order and not error.
If the filling of the lots is false and too small then EA keeps order by using the smallest lot which is permitted by the broker, and also when used on the wrong use of lots will still order according to the broker's condition, in addition to this filter it will avoid the error due to the use of lots.

We put it into the previous EA
Code:
//+------------------------------------------------------------------+
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

extern string Nama_EA = "Buy and Sell";
extern int Start_Hour = 22;
exter int End_Hour = 4;
extern int     TP                      = 11;
extern int     SL                      = 100;
extern double  Lots                    = 0.1;
extern int     Magic                 = 69;
double pt=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
//----

}
return(0);
}
//+------------------------------------------------------------------+

{
if(Start_Hour > End_Hour){
if (Hour() >= Start_Hour || Hour() < End_Hour) trade = true;
} else
if (Hour() >= Start_Hour && Hour() < End_Hour) trade = true;
}

int totalorders( int tipe)
{
int total=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
total++;
}

return(total);
}
double NR(double thelot)
{
double maxlots = MarketInfo(Symbol(), MODE_MAXLOT),
minilot = MarketInfo(Symbol(), MODE_MINLOT),
lstep = MarketInfo(Symbol(), MODE_LOTSTEP);
double lots = lstep * NormalizeDouble(thelot / lstep, 0);
lots = MathMax(MathMin(maxlots, lots), minilot);
return (lots);
}
//+------------------------------------------------------------------+