My journal in learning to code EA autodidact - Page 2
Page 2 of 5 FirstFirst 12345 LastLast
Results 11 to 20 of 50

Thread: My journal in learning to code EA autodidact

  1. #1
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184

    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.

    Not allowed!

  2. #11
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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++)
    {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    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_BUY) is the number of buy
    totalorders (OP_SELL) is the number of sell
    totalorders (OP_BUYSTOP) is the number of Buystop pending orders
    etc ....

    The order type can also be expressed in numbers
    OP_BUY = 0
    OP_SELL = 1
    OP_BUYLIMIT = 2
    OP_SELLLIMIT = 3
    OP_BUYSTOP = 4
    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:
    //+------------------------------------------------------------------+
    //|                                                     Buy only.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "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(0)==0) {  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); } 
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
      
    int totalorders( int tipe)
    {
    int total=0;
    for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
         total++;
      }
    return(total) ;
    }
    //+------------------------------------------------------------------+

    Not allowed!

  3. #12
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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);

    Compare with buy order scriot!
    There is a difference need to noted that the buy order refers to the current Ask price while the sell order uses the Bid.
    The difference between Ask and bid is often called as spread.

    On buy order
    Ask-SL*Point,
    Ask+TP*Point,


    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:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "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 Totalbuy=0;
      int Totalsell=0;
      for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
          if(OrderType()==OP_BUY)   Totalbuy++;
          if(OrderType()==OP_SELL)  Totalsell++;
      }
      if(Totalbuy==0){  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); } 
    if(Totalsell==0){OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
    //----
       return(0);
      }
    //+------------------------------------------------------------------+

    Not allowed!
    Last edited by ara; 05-24-2018 at 10:21 AM. Reason: edit some codes

  4. #13
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "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(0)==0) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
      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++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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 * /

    Not allowed!
    Last edited by ara; 05-24-2018 at 10:20 AM. Reason: edit some codes

  5. #14
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Quote Originally Posted by letsdo View Post
    thank you ara for creating this helpful content I learn about coding with your post help
    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.

    Not allowed!
    Last edited by ara; 05-21-2018 at 06:05 PM. Reason: remove active link

  6. #15
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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
    extern bool Trade_buy= true;
    extern bool Trade_sell= true;


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

    Trade_buy== true can also be written Trade_buy== 1 or only Trade_buy
    While Trade_buy==false can also be written Trade_buy== 0 or !Trade_buy

    We try to enter into the EA

    EA 1

    Code:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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 Totalbuy=0;
      int Totalsell=0;
      for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
          if(OrderType()==OP_BUY)   Totalbuy++;
          if(OrderType()==OP_SELL)  Totalsell++;
      }
      if(Totalbuy==0 && Trade_buy== true){  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); } 
    if(Totalsell==0 && Trade_sell==true){OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    EA 2

    Code:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
      if(totalorders(1)==0 && Trade_sell) {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++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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

    Not allowed!
    Last edited by ara; 05-24-2018 at 10:21 AM. Reason: adding a picture

  7. #16
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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.\

    Now we will discuss about adding trading time

    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(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
    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(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
    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,,,,

    Not allowed!

  8. #17
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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
    Previously we made declaration start time and ended of trading.

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


    then the function of the time is as follows:

    int Trade_hour()
    {
    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;
    return (trade);
    }


    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:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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()
      {
    //----
    
      if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
      if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*Point, Bid-TP*Point,Nama_EA,Magic,0,Red);}
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
     
    int Trade_hour()
    {
    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;
    return (trade);
    }
    
    int totalorders( int tipe)
    {
    int total=0;
    for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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.

    Not allowed!

  9. #18
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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(Trade_hour()==1){
    if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
    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" :

    if(Trade_hour()==1){
    if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*pt, Ask+TP*pt,Nama_EA,Magic,0,Blue);}
    if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*pt, Bid-TP*pt,Nama_EA,Magic,0,Red);}
    }


    So when we enter into the EA as follows

    Code:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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()
      {
    //----
    
      if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*pt, Ask+TP*pt,Nama_EA,Magic,0,Blue);}
      if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*pt, Bid-TP*pt,Nama_EA,Magic,0,Red);}
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
     
    int Trade_hour()
    {
    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;
    return (trade);
    }
    
    int totalorders( int tipe)
    {
    int total=0;
    for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
         total++;
      }
    
    return(total);
    } 
    
    //+------------------------------------------------------------------+

    Not allowed!

  10. #19
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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()
      {
    //----
    
      if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*dpt, Ask+TP*dpt,Nama_EA,Magic,0,Blue);}
      if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+SL*dpt, Bid-TP*dpt,Nama_EA,Magic,0,Red);}
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    double dpt()
    
    {
    double pt=0;
     if(Digits==3 || Digits==5) pt=10*Point;   else   pt=Point;
     return(pt);
    
    } 
    
    int Trade_hour()
    {
    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;
    return (trade);
    }
    
    int totalorders( int tipe)
    {
    int total=0;
    for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic || OrderType()!=tipe) continue;
         total++;
      }
    
    return(total);
    } 
    
    //+------------------------------------------------------------------+
    THis is not difficult.

    Not allowed!

  11. #20
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    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)

    Please look:
    Before
    if(Trade_hour()==1){
    if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
    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(Trade_hour()==1){
    if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,NR(Lots),Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);}
    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:
    //+------------------------------------------------------------------+
    //|                                                 Buy and Sell.mq4 |
    //|                        Copyright 2018, MetaQuotes Software Corp. |
    //|                                             https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    extern string Nama_EA = "Buy and Sell";
    extern bool    Trade_buy               = true;
    extern bool    Trade_sell              = true;
    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()
      {
    //----
    
    if(Trade_hour()==1){
    if(totalorders(0)==0 && Trade_buy) {OrderSend(Symbol(),OP_BUY,NR(Lots),Ask,3,Ask-SL*pt, Ask+TP*pt,Nama_EA,Magic,0,Blue);}
    if(totalorders(1)==0 && Trade_sell) {OrderSend(Symbol(),OP_SELL,NR(Lots),Bid,3,Bid+SL*pt, Bid-TP*pt,Nama_EA,Magic,0,Red);}
    }
       return(0);
      }
    //+------------------------------------------------------------------+
     
    int Trade_hour()
    {
    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;
    return (trade);
    }
    
    int totalorders( int tipe)
    {
    int total=0;
    for(int i=0; i<OrdersTotal(); i++)
      {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          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);
    }
    //+------------------------------------------------------------------+

    Not allowed!
    Last edited by ara; 06-02-2018 at 07:13 AM. Reason: edit some codes

Page 2 of 5 FirstFirst 12345 LastLast

Tags for this 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
  •