My journal in learning to code EA autodidact - Page 5

# 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.
If you have already a good skill in coding EA, please provide your feedback, suggestions and your knowledge to me here.

2. How to call
Example:

if(totalorders(OP_BUYLIMIT) ==0 )OrderSend(Symbol(), 2, start_lot, Ask- Distance*Point, 3, Ask- Distance*Point-SL*Point,Ask-Distance*Point+TP*Point, "", Magic, 0, Blue);

or

For Selllimit

if(totalorders(OP_SELLLIMIT) ==0 )OrderSend(Symbol(), 3, start_lot, Bid+Distance*Point, 3, Bid +Distance*Point+SL*Point,Bid+Distance*Point-TP*Point, "", Magic, 0, Red);

or

if(totalorders(3)==0)OrderSend(Symbol(), OP_SELLLIMIT, start_lot, Bid+Distance*Point, 3, Bid +Distance*Point+SL*Point,Bid+Distance*Point-TP*Point, "", Magic, 0, Red);

well, I'll make a simple EA

Code:
```//+------------------------------------------------------------------+
//|                                             EA Pending Order.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//-------------------------------------------------------------------+

extern string  EA_Name                = "Ara+Pending";
extern int     Start_Hour               = 6;
extern int     End_Hour               = 20;
extern int     TP                      = 20;
extern int     SL                      = 100;
extern double  Lots                    = 0.01;
extern int     Distance                   = 15;
extern int     Magic                   = 69;

double slb,tpb,sls,tps,pt;
int wt,wk,tiket,ticet;
//+------------------------------------------------------------------+
//| 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()
{
label();

if(totalorders(3)==0)OrderSend(Symbol(), OP_SELLLIMIT,NR(Lots) , Bid+Distance*Point, 3, Bid+ Distance*Point+SL*Point,Bid+Distance*Point-TP*Point, "", Magic, 0, Aqua);
if(totalorders(5)==0)OrderSend(Symbol(), OP_SELLSTOP,NR(Lots) , Bid-Distance*Point, 3, Bid- Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", Magic, 0, Yellow);
}
return(0);
}
//+------------------------------------------------------------------+

{
bool trade = false;
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);
}

void label()
{
Comment("\n ",
"\n ",
"\n ------------------------------------------------",
"\n :: =>+Ara+Pending<=",
"\n :: =>Ara<=",
"\n ------------------------------------------------",
"\n :: Spread                 : ", MarketInfo(Symbol(), MODE_SPREAD),
"\n :: Leverage               : 1 : ", AccountLeverage(),
"\n :: Equity                 : ", AccountEquity(),
"\n :: Jam Server             :", Hour(), ":", Minute(),
"\n ------------------------------------------------",
"\n :: >>By: Ara<<",
"\n ------------------------------------------------");
}```

3. [lang=ar]Your topic is excellent I am interested in this and I have many ideas of my own, but I do not know how to create an expert to implement it so I trade with them manually. So please continue on this subject so that we can learn it from you, I am always trying to develop myself[/lang]

4. Originally Posted by snnaky
[lang=ar]Your topic is excellent I am interested in this and I have many ideas of my own, but I do not know how to create an expert to implement it so I trade with them manually. So please continue on this subject so that we can learn it from you, I am always trying to develop myself[/lang]
Ok dear, to be honest I am also trade forex manually, I don't trade using any ea, but I only just want to know about how to coding EA, besides being a forex trader, I am an online affiliate marketer, publisher/blogger, and seller. I think I can create and modify many EAs that have good quality and then I sell them or I can use my coding skills to make money. Ha ha ha

5. EA Trapping

In this post I will try to discuss about EA trap by using two pending stop order.

The rule is on a certain hour directly put 2 orders, buystop and sellstop and when one pending stop order is touched, then the other pending order directly removed.
Then if both pending orders has not been touched on those day then both pending orders should be removed. and other additions on Monday are not trading because EA is suitable for the trend on that day, if Monday is usually expected sideway.

So every day orders will only order one time only.

Take a look at the picture above on the arrow, so on the server clock about 0: 30 then the stop order will be installed.

Well, I try to make the declaration as follows:

extern string EA_Name = "Ara+Trap";
extern bool Trade_sellstop = true;
extern string Server_hour = "Setting up to you";
extern int Hour = 0;
extern int Minute = 30;
extern bool Tp_in_Money = false;
extern double TP_in_money = 5;
extern int TP = 100;
extern int SL = 20;
extern double Lots = 0.1;
extern int Distance_stop = 25;
extern bool TrailingStop_ = true;
extern int TrailingStop = 12;
extern int Magic = 69;
double slb,tpb,sls,tps,pt,lslb,ltpb;

6. Some parts that I try to prepare are the delet order type script, here is made in the form of the following functions:

void closeall(int m)
{
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic|| OrderType()!=m) continue;
if (OrderType() > 1) OrderDelete(OrderTicket());
if (OrderType() == 0) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
if (OrderType() == 1)OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
}
}

To specify an order at a certain hour, make the following part:

Hour()==Hour && Minute()==Minute

For deleted part when one stop order has been touched is:

if(totalorders(0)==1 && totalorders(1)==0){
closeall(5);
}
if(totalorders(0)==0 && totalorders(1)==1){
closeall(4);
}

while the part to check out the cabinet that is not touched then on the specified hour is deleted
example at 23

if(Hour()==23){
closeall(4);
closeall(5);
}

For Pending stop order accessories:

double as = Ask + Distance_stop*pt;
double bi = Bid - Distance_stop*pt;
if(SL==0)slb=0;else slb=as-SL*pt;
if(SL==0)sls=0;else sls=bi+SL*pt;
if(TP==0)tpb=0;else tpb=as+TP*pt;
if(TP==0)tps=0;else tps=bi-TP*pt;

Another part of making EA off on Monday trading is

DayOfWeek()!=1

Well, EA will be as follows

Code:
```//+-----------------------------------------------------------------------------------------+
//|                                                    EA ARA trapping |
//|                                                aramaharani@gmail.com |
//|                                     Ara |
//+------------------------------------------------------------------+

extern string  EA_name                 = "Ara Trapping";
extern bool    Trade_sellstop          = true;
extern string  Server_hours              = "Set any";
extern int     Hour                     = 0;
extern int     Minute                   = 30;
extern bool    Tp_in_Money             = false;
extern double  TP_in_money             = 5;
extern int     TP                      = 100;
extern int     SL                      = 20;
extern double  Lots                    = 0.1;
extern int     Distance_stop              = 25;
extern bool    TrailingStop_           = true;
extern int     TrailingStop            = 12;
extern int     Magic                   = 69;
double slb,tpb,sls,tps,pt,lslb,ltpb;
//+------------------------------------------------------------------+
//| 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()
{
label();
if(TrailingStop_)dtrailing();
if(Tp_in_Money ){
if(TP_in_money<= money(0)){
closeall(0);
closeall(2);
}
if(TP_in_money<= money(1)){
closeall(1);
closeall(3);
}
}
if(totalorders(0)==1 && totalorders(1)==0){
closeall(5);
}
if(totalorders(0)==0 && totalorders(1)==1){
closeall(4);
}
if(Hour()==23){
closeall(4);
closeall(5);
}
//----
double as = Ask + Distance_stop*pt;
double bi = Bid - Distance_stop*pt;
if(SL==0)slb=0;else slb=as-SL*pt;
if(SL==0)sls=0;else sls=bi+SL*pt;
if(TP==0)tpb=0;else tpb=as+TP*pt;
if(TP==0)tps=0;else tps=bi-TP*pt;
if(DayOfWeek()!=1 && Hour()==Hour && Minute()==Minute && totalorders(0)==0 && totalorders(1)==0){
if(totalorders(5)==0  && Trade_sellstop ) {OrderSend(Symbol(),OP_SELLSTOP,NR(Lots),bi,3,sls, tps,EA_Name,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);
}
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);
}

void dtrailing()
{
for(int i=0; i<OrdersTotal(); i++){
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic ) continue;
if(Bid-OrderOpenPrice()>pt*TrailingStop)  {
if((OrderStopLoss()<Bid-pt*TrailingStop) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-pt*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
if(OrderType()==OP_SELL)  {
return(0);
}
}
}
}

}
void closeall(int m)
{
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic||  OrderType()!=m) continue;
if (OrderType() > 1) OrderDelete(OrderTicket());
if (OrderType() == 0) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
if (OrderType() == 1)OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
}
}
double money(int m)
{
double dp = 0;
int i;
for (i = 0; i < OrdersTotal(); i++) {
if (OrderSymbol() != Symbol()  || OrderMagicNumber()!=Magic || m!=OrderType()) continue;
dp += OrderProfit();
}
return(dp);
}

void label()
{
Comment("\n ",
"\n ",
"\n ------------------------------------------------",
"\n :: =>Ara<=",
"\n :: =>aramaharani@gmail.com<=",
"\n ------------------------------------------------",
"\n :: Spread                 : ", MarketInfo(Symbol(), MODE_SPREAD),
"\n :: Leverage               : 1 : ", AccountLeverage(),
"\n :: Equity                 : ", AccountEquity(),
"\n :: Server Hours             :", Hour(), ":", Minute(),
"\n ------------------------------------------------",
"\n :: Floting buy : ",money(0),
"\n :: Floting sell : ",money(1),
"\n ------------------------------------------------",
"\n :: >>By: Ara<<",
"\n ------------------------------------------------");
}```

7. Martingale Trapping EA

In this post I will add martingale to EA trapping on previous post.
Martingale is also intended to refer to the range of orders and candle changes to prevent the occurrence of several orders when there is a surge in prices and long candle, as in previous post.

Adding the martingale coding:

1. Function for martingale order

if(trad()==1 && wt!=Time[0] && totalorders(1)==0 && totalorders(0)<Max_order ) {OrderSend(Symbol(),OP_BUY,xlot(0),Ask,3,slb, tpb,Nama_EA,Magic,0,Blue); wt=Time[0]; }
if(trad()==2 && wk!=Time[0] && totalorders(0)==0 && totalorders(1)<Max_order ) {OrderSend(Symbol(),OP_SELL,xlot(1),Bid,3,sls, tps,EA_Name,Magic,0,Red); wk=Time[0]; }

2. Function for calculating order distance and order when distance is met

{
int type,dp; double opb,ops,lastlotb,lastlots;
for (int i = 0; i < OrdersTotal(); i++) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
type=OrderType();
if(type==0)opb = OrderOpenPrice();
if(type==1)ops = OrderOpenPrice();
}
double as = opb - Distance_order * pt;
double bi = ops + Distance_order * pt;
if ( Ask <= as && totalorders(0)> 0 ) dp=1;
if ( Bid >= bi && totalorders(1)> 0 ) dp=2;

return(dp);
}

3. Functions for calculating Lots of each order repetition

double xlot(int m)
{
double ylot,dlot;
for (int i = 0; i < OrdersTotal(); i++) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic ||OrderType()!=m) continue;
dlot=OrderLots();
}
if(OrderType()==m)ylot=NR(Lots*MathPow(DiMarti,jum lahorder(m)));
return(ylot);
}

Well the results are:

Code:
```//+------------------------------------------------------------------+
//|                                                    EA ARA |
//|                                                aramaharani@gmail.com |
//|                                    Ara |
//+------------------------------------------------------------------+

extern string  EA_Name                = "Ara";
extern bool    Trade_sellstop          = true;
extern string  Server_hour              = "Set sesuai selera";
extern int     Hour                     = 0;
extern int     Minute                 = 30;
extern bool    Tp_in_Money             = false;
extern double  TP_in_money             = 5;
extern int     Distance_order             = 35;
extern int     Max_order               = 10;
extern double  Marti                 = 1.3;
extern int     TP                      = 100;
extern int     SL                      = 20;
extern double  Lots                    = 0.1;
extern int     Distance_stop              = 25;
extern bool    TrailingStop_           = true;
extern int     TrailingStop            = 12;
extern int     Magic                   = 69;
double slb,tpb,sls,tps,pt,lslb,ltpb;
int wt,wk,tiket,ticet;
//+------------------------------------------------------------------+
//| 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()
{
label();
if(TrailingStop_)dtrailing();
if(Tp_in_Money ){
if(TP_in_money<= money(0)){
closeall(0);
closeall(2);
}
if(TP_in_money<= money(1)){
closeall(1);
closeall(3);
}
}
if(totalorders(0)==1 && totalorders(1)==0){
closeall(5);
}
if(totalorders(0)==0 && totalorders(1)==1){
closeall(4);
}
if(Hour()==23){
closeall(4);
closeall(5);
}
//----
double as = Ask + Distance_stop*pt;
double bi = Bid - Distance_stop*pt;
if(SL==0)slb=0;else slb=as-SL*pt;
if(SL==0)sls=0;else sls=bi+SL*pt;
if(TP==0)tpb=0;else tpb=as+TP*pt;
if(TP==0)tps=0;else tps=bi-TP*pt;
if(DayOfWeek()!=1 && Hour()==Hour && Minute()==Minute && totalorders(0)==0 && totalorders(1)==0){
if(totalorders(5)==0  && Trade_sellstop ) {OrderSend(Symbol(),OP_SELLSTOP,NR(Lots),bi,3,sls, tps,WA_Name,Magic,0,Red);}
}
if(trad()==1 && wt!=Time[0] && totalorders(1)==0 && totalorders(0)< Max_order   ) {OrderSend(Symbol(),OP_BUY,xlot(0),Ask,3,slb, tpb,Nama_EA,Magic,0,Blue); wt=Time[0]; }
if(trad()==2 && wk!=Time[0] && totalorders(0)==0 && totalorders(1)< Max_order   ) {OrderSend(Symbol(),OP_SELL,xlot(1),Bid,3,sls, tps,EA_Name,Magic,0,Red); wk=Time[0]; }

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);
}
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);
}

void dtrailing()
{
for(int i=0; i<OrdersTotal(); i++){
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic ) continue;
if(Bid-OrderOpenPrice()>pt*TrailingStop)  {
if((OrderStopLoss()<Bid-pt*TrailingStop) || (OrderStopLoss()==0)) {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-pt*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
if(OrderType()==OP_SELL)  {
return(0);
}
}
}
}

}
void closeall(int m)
{
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSymbol() != Symbol() || OrderMagicNumber()!=Magic /*|| m!= OrderType()*/) continue;
if (OrderType() == m) OrderDelete(OrderTicket());
if (OrderType() == m) OrderDelete(OrderTicket());
if (OrderType() == m) OrderClose(OrderTicket(), OrderLots(), Bid, 3, CLR_NONE);
if (OrderType() == m)OrderClose(OrderTicket(), OrderLots(), Ask, 3, CLR_NONE);
}
}
double money(int m)
{
double dp = 0;
for (int i = 0; i < OrdersTotal(); i++) {
if (OrderSymbol() != Symbol()  || OrderMagicNumber()!=Magic || m!=OrderType()) continue;
dp += OrderProfit();
}
return(dp);
}

void label()
{
Comment("\n ",
"\n ",
"\n ------------------------------------------------",
"\n :: =>Ara<=",
"\n :: =>aramaharani@gmail.com<=",
"\n ------------------------------------------------",
"\n :: Spread                 : ", MarketInfo(Symbol(), MODE_SPREAD),
"\n :: Leverage               : 1 : ", AccountLeverage(),
"\n :: Equity                 : ", AccountEquity(),
"\n :: Server Hour            :", Hour(), ":", Minute(),
"\n ------------------------------------------------",
"\n :: Floting buy : ",money(0),
"\n :: Floting sell : ",money(1),
"\n ------------------------------------------------",
"\n :: >>By: Jum69<<",
"\n ------------------------------------------------");
}
{
int type,dp; double opb,ops,lastlotb,lastlots;
for (int i = 0; i < OrdersTotal(); i++) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
type=OrderType();
if(type==0)opb = OrderOpenPrice();
if(type==1)ops = OrderOpenPrice();
}
double as = opb - Distance_order * pt;
double bi = ops + Distance_order * pt;
if ( Ask <= as &&  totalorders(0)> 0 ) dp=1;
if ( Bid >= bi &&  totalorders(1)> 0 ) dp=2;

return(dp);
}
double xlot(int m)
{
double ylot,dlot;
for (int i = 0; i < OrdersTotal(); i++) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic ||OrderType()!=m) continue;
dlot=OrderLots();
}
if(OrderType()==m)ylot=NR(Lots*MathPow(DiMarti,totalorders(m)));
return(ylot);
}```

8. it is a decent advance for programming of master counsel and with time you will have the capacity to enhance your basic content and make further developed contents that can function admirably and let you exchange well with them, making of master counselors needs much read about programming thus you can discover great materials on the web about this subject help you to learn well.

9. Originally Posted by ahleyan22
it is a decent advance for programming of master counsel and with time you will have the capacity to enhance your basic content and make further developed contents that can function admirably and let you exchange well with them, making of master counselors needs much read about programming thus you can discover great materials on the web about this subject help you to learn well.
Thank you for your comment, I don't think this will be useful to others, but I hope it will be useful for anyone who wants to learn basic techniques in coding. I made this thread as my record in learning EA coding, I found it difficult to find an easy tutorial in coding EA on the internet for free. So, I think there is no harm I made a note of my journey in learning coding here.

10. I stopped the thread for a while because I had not continued learning about EA for a long time, I did not have much time to study coding for now, but I will continue it later, I apologize for this situation. Learning about EA programs is not easy for me, especially when I learn to build a complex EA, it really takes a lot of time just to create and test a complex ea.

11. Hello my friend It is a good idea how the businessman can learn to create a special index and why is it a good advantage? Because the cursor you are designing is a good idea for you and it is also good. All the indicators are good but not all fit. I want to know for this science, thanks for the thread.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•