1. Welcome back! Thank you for being a part of this Traders Community. Let's discuss and share :)
    Selamat datang kembali! Trimakasih telah menjadi bagian dari Komunitas Trader ini. Mari berdiskusi dan berbagi :)
    Dismiss Notice

Tutorial Learn MQL5 – Putting Indicators In The MT5 Program

Discussion in 'Belajar MQL dari Nol' started by Niguru, 14 Aug 2022.

  1. Niguru

    Niguru Member Credit Hunter

    Equity
    Credit
    Ref Point
    In MT4 the content of an indicator can be directly placed in a variable with the data type double, as in the following example:

    Code:
    double ma = iMA(Symbol(),Period(),12,0,MODE_SMA,PRICE_CLOSE,1);
    
    And then these variables can be directly used to determine the signal (a position to open or close a trade), as in the example of MA in trend follower mode:

    Code:
    SignalBUY=ma>Ask;
    
    SignalSELL=ma<Bid;
    
    In MT5 the content of an indicator must be assigned to a variable with an integer data type, which is called a "handling variable". As in the following example:

    Code:
    int handle_iMA=iMA(NULL,_Period,12,0, MODE_SMA, PRICE_CLOSE);
    
    Placement on this handling variable can be checked with the instruction:

    Code:
    if(handle_iMA==INVALID_HANDLE) Print("Handling MA file");
    
    When the indicator will be used to determine the signal, the contents of the handling variable must first be moved to the buffer. Here's an example of the transfer:

    Code:
    double ma[]; CopyBuffer(handle_iMA,0,0,3,ma);
    
    The first instruction is an array variable declaration with the name "ma" with data type = double.
    The second instruction is CopyBuffer, with the following order of parameters:

    Code:
    CopyBuffer(indicator_handle,buffer_num, ,start_pos,count, buffer);
    
    Indicator_handle is the name of the handling variable.

    Buffer_num is the buffer number. Please note that each data displayed on the indicator must be stored in a buffer, and each data has its own buffer number.

    Start_pos is a data position, also known as a candle position. 0 for the last candle, 1 for the previous candle, and so on.

    Count is the amount of data retrieved.

    Buffer is an array variable that holds indicator data.

    Distinguish between variables that are used as buffers and handling variables.
    The handling variable is just a pointer or address that shows the location of the indicator, while the buffer holds data from the indicator.

    After the data is in the buffer, then the instruction to determine the signal can be written.
    For example:

    Code:
    SignalBUY=ma[0]>Ask;
    
    SignalSELL=ma[0]<Bid;
    
    Or if you want to take data from the previous candle:

    Code:
    SignalBUY=ma[1]>Ask;
    
    SignalSELL=ma[1]<Bid;
    
    Because the placement of indicators on handling variables only needs to be done once, it will be more efficient if handling variables are placed in the OnInit area.

    Hopefully this short post can add insight and knowledge of fellow traders.
    Happy trading.. Have a nice day :)
     

Share This Page