My journal in learning to code EA autodidact
Page 1 of 5 12345 LastLast
Results 1 to 10 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. #2
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Since I am not a programmer, I will use my own words in my style rather than programmers because of my limited ability. So I hope my words here can be understood by all, especially for me personally because I consider this is my personal record.

    For me personally, it is less complete if I can't build my own EA, although it is only a simple EA.
    There may be many sources on the internet that provide an explanation of how to create an EA. At first glance looks like confusing, yup right ... very, very confusing, but I try to learn from scratch, I guess it's not as difficult as what I imagine if I try and try. In essence is our own persistence in learning and practice.

    Okay, I will save a script, I think I will need this script later on.
    Script to buy order :
    Code:
    OrderSend(Symbol(),0,start_lot,Ask,3,Ask-SL*Point,Ask+TP*Point,"+Jum+oke+",Magic,0,Blue)
    or
    Code:
    OrderSend(Symbol(),OP_BUY,start_lot,Ask,slipage,Ask-SL*Point, Ask+TP*Point,"+Jum+oke+",Magic,0,Blue);

    Not allowed!
    Last edited by ara; 04-22-2018 at 05:48 PM. Reason: adding some codes

  3. #3
    Trader
    Join Date
    Aug 2013
    Posts
    11,572
    Thumbs Up
    Received: 70
    Given: 48
    it is a good step for programming of expert advisor and with time you will be able to improve your simple script and make more advanced scripts that can work well and let you trade well with them, making of expert advisors needs much read about programming and so you can find good materials on the web about this subject that help you to learn well.

    Not allowed!

  4. #4
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Quote Originally Posted by sameeh View Post
    it is a good step for programming of expert advisor and with time you will be able to improve your simple script and make more advanced scripts that can work well and let you trade well with them, making of expert advisors needs much read about programming and so you can find good materials on the web about this subject that help you to learn well.
    yups... thanks bro, I will try with my best effort to programming EA.

    Okay, I will immediately try to practice about the basics of coding meta editor. I don't want to talk a lot about theory, I will try to practice it through metaeditor.
    I will launch a meta trader, then press F4 to open meta editor
    2018-04-26_223558.jpg

    then press CTRL + N to open a new project.
    2018-04-26_223613.jpg

    press next, then write the name of the ea to be created, I will use "TEST" for the name of ea that I make.
    2018-04-26_223931.jpg

    Press next,
    2018-04-26_224214.jpg

    press finish,
    2018-04-26_224224.jpg

    Appear as follows:
    2018-04-26_224426.jpg

    Not allowed!

  5. #5
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    I will immediately create a simple EA with the following rule :

    1. EA will open Buy if there is no order
    2. Using Take Profit and Stop Loss


    Okey is quite simple so the input parameters are as follows:
    1. Name of EA = Buy only.
    2. TP.
    3. SL.
    4. Lots
    5. MagicNumber

    In my previous post I have explained how to start a new project, So it will be like this:

    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
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //---
       
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    To create a EA parameters, then every data preceded by a "extern" declaration, and placed as below:


    #property copyright "Copyright 2018, MetaQuotes Software Corp."
    #property link "https://www.mql5.com"
    #property version "1.00"
    #property strict


    placed here

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+


    Well now we input the parameters:

    Parameter 1
    Name EA = Buy only

    For this naming uses the type of data String, the data in the form of text as a comment EA
    this comment in mql = OrderComment ()
    The characteristic of writing this type of data is preceded by a string word and followed by the desired name without spaced, then, equal to (=) and then marked with a " filled as desired, closed with a " and ;
    So for the parameter 1 can be written : extern string Nama_EA = "Buy Only";
    so it will:

    #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";

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+


    Parameter 2
    TP = 11
    TP or Take profit is written in mql = OrderTakeProfit ()
    For TP using int data type (integer), this data feature of the data is a round integer number (unobtrusive)
    This type of data writing feature is preceded by an int word followed by the desired name without comma, then equal (=) and then given a non-generic number with a sign;

    so it is written:

    #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;


    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+


    Parameter 3
    SL. = 100
    SL or stop loss in mql language written OrderStopLoss ()
    SL writing is the same as TP, because the integer data type (int) so that the rules of writing the same
    written as follows:

    #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;


    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+


    Parameter 4
    Lots = 0.1
    Lots in MQL written = OrderLots ()
    Type data types using double type, which is the type of data that counts with commas.
    The sequence is preceded by a double word, followed by the name (lots) desired without comma, then equal (=) and then given numbers, with or without comma, a comma in here written as (.) not (,) and ending with a sign;
    so will be like this :

    #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;


    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+


    Well, we see the results like this:

    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;
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //---
       
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    Well, now click Compile

    2018-04-29_094258.jpg

    if successful will appear writing like this, with no errors and warnings
    2018-04-29_094712.png

    We can see the results in Mt4, we try to look in the expert folder then EA Buy Only already seen, double click it will appear EA parameters like the following picture :
    2018-04-29_095017.png

    Not allowed!

  6. #6
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Well this time I will add one parameter that is often used as the EA order identity is Magic number (magicnumber). in mql4 language => OrderMagicNumber ()

    Magicnumber is as ID, since magicnumber is often used as the order identity of an EA to distinguish orders from other EAs, although it can actually be used more widely than that.
    In order to know the magic number in an order usually point mouse at an order then there will appear order number and magic number.

    2018-05-04_101900.jpg

    Well, in the EA parameters that will we created this time, we will add the magic number
    This magic number is an int data type (integer without comma)
    so it can be written down

    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 OnInit()
      {
    //---
       
    //---
       return(INIT_SUCCEEDED);
      }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    Okay now Compile again and check there is no error then we see in mt4 if successful it will look like the following image after ea Buy Only has been attached to the chart :

    2018-05-04_104516.png

    Ok, Ea parameters are finished, do this ea can be running? Yes of course this ea can already been running, but can't open order.

    Not allowed!

  7. #7
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Well, now I will create a program so that the EA can run properly.

    I will put the program under int start ()

    int start()
    {
    //----

    Put here
    //----
    return(0);
    }


    To start the program according to the rule = if there is no order it will do order Buy
    Programs created using the "if" operation
    The writing begins with the word "if" and at started with "(" ended with ")" and then "{" and in the end closed with "}"

    Conditions:
    if (if any conditions are specified) {the desired command terminates a semicolon; }
    Sometimes also can be written without curly brackets in special conditions (simple commands)
    if (if there is a specified condition) the desired command terminates a semicolon;

    Okay, maybe that is easy to understanding

    Now back to the rules of EA buy only that we will make

    If there is no order then EA will do buy order means there is only 1 buy order
    if (no order) do buy order;

    no order means total order = 0, the provision of writing equals (=) in condition must be written 2 times (==)
    the total order in mt4 is provided as the variable provided and written OrdersTotal ()
    so that we write as a condition

    if(OrdersTotal()==0)
    or can be written as
    if(OrdersTotal()<1)

    Order buy as the desired command we take in one
    OrderSend(Symbol(),OP_BUY,start_lot,Ask,slipage,As k-SL*Point, Ask+TP*Point,"+Jum+oke+",Magic,0,Blue);

    We change according to the parameters that we provide, start_lot replaced with Lots, slipage replace with number 3 (later I will talk this in the next post), "+Jum+oke+" replace with parameter Nama_EA and Magic is appropriate.

    OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);

    We combine
    So if we combined with the conditions in place under int start ()

    if(OrdersTotal()==0) OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue);

    or same as

    if(OrdersTotal()==0){ OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); }

    also same as

    if(OrdersTotal()<0){ OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); }

    Well the result after it is inserted into int start () and entirely is

    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(OrdersTotal()==0){  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-SL*Point, Ask+TP*Point,Nama_EA,Magic,0,Blue); } 
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    Don't forget to click Compile and make sure there is no error
    I try run this ea in demo account
    2018-05-11_070131.jpg

    Now the ea can be open trades according to the rules that I made.

    Not allowed!

  8. #8
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    If it turns out there is an error means there is still something wrong, recheck by double click on the designated part
    example :
    Compile results turned out
    Compiling 'Buy only.mq4' ...
    '}' - semicolon expected C: \ Program Files \ Roboforex-Metatrader4 \ experts \ Buy Only.mq4 (40, 112)
    1 error (s), 0 warning (s)


    Well, double click on this section
    '}' - semicolon expected C: \ Program Files \ Roboforex-Metatrader4 \ experts \ Buy Only.mq4 (40, 112)
    Then the cursor will point to the error or the part related to the designated, so it must be re-examined shortcomings or excess (usually there is a shortage of semicolons (;) or advantages / lack of signs (,), {} etc.

    Should be searched for error section till succeed

    Not allowed!
    Last edited by ara; 05-13-2018 at 06:16 PM. Reason: adding color to the text

  9. #9
    Trader ara's Avatar
    Join Date
    Dec 2013
    Posts
    1,834
    Thumbs Up
    Received: 106
    Given: 184
    Previously, I've tried to make a simple EA where those EA has been made only to do order Buy, and the active order is limited only 1...
    If we've been looked at those EA, there are some weaknesses :
    - Can only be used one pair only (1 pair of currency)
    - Can only buy order
    - There is no trading time limit
    - Not using the indicator as the trigger (the order trigger)

    This time we discuss first about filter pair so that those EA can be used in all pairs.
    Previously we used:
    if (OrdersTotal () == 0)

    OrdersTotal is the sum of all orders in Mt4 of all pairs, including pending orders. So if there is 1 pending buystop in EURUSD and there are 2 buy in GBPUSD so the amount is 3 orders, so because we make condition OrdersTotal () == 0 then total order is counted all order.

    There are several ways to make filter pairs and I will discuss 2 kinds of commonly used ways according to simple language.
    1. Direct way
    2. Using Functions

    Not allowed!

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

    This method is usually made script above order command to replace OrdersTotal().
    This script is stored in the library as a standard scrip that may be used again.

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


    Well the script above is a script that is often used

    int Totalbuy=0;
    int Totalsell=0;


    This is the order counter variable, the placement is stored in the declaration (under existing parameters extern) can also be above the script as above.

    for(int i=0; i<OrdersTotal(); i++)

    The point is to calculate the active orders starting from the beginning to the last. The opposite of this script is usually count down (simple language)

    for(int i=OrdersTotal()-1; i>=0; i--)

    Try to compare it first!


    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

    this is the default script to select the active order

    if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;

    It uses or (||) means selecting only on symbol (pair) and magic numbers in attack so others are ignored
    this condition can also use the sign and (&&), the difference when using the && is then the sign ! = replaced with == so it will

    if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) continue;

    Please compare between the 2 conditions. Only use && or || or both are used

    Then
    if(OrderType()==OP_BUY) Totalbuy++;
    it means to limit the buy order to which it is declared expressed into a variable Totalbuy
    if(OrderType()==OP_SELL) Totalsell++;
    it means to restrict the sell order to which it is declared declared into a variable Totalsell

    Then how do I use?
    Just replace it
    if(OrdersTotal()==0) replaced with if(Totalbuy==0) can also be replaced with if(Jumlahbuy<1)

    Become as follows:
    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()
      {
    //----
    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); } 
    //----
       return(0);
      }
    //+------------------------------------------------------------------+
    Well now this EA can be used in all pairs

    Not allowed!

Page 1 of 5 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
  •