Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

write code of metatrader4 indicator, to for signal in ema 20 and ema 50 corsing sell and…

  1. User

    write code of metatrader4 indicator, to for signal in ema 20 and ema 50 corsing sell and buy

  2. Assistant

    To create a MetaTrader 4 indicator for Buy and Sell signals based on the crossing of the EMA 20 and EMA 50, we need to write a custom indicator script using the MetaQuotes Language 4 (MQL4). We will call this indicator EMA_Cross_Signals.mq4. Here's the sample code for the indicator:

    //+------------------------------------------------------------------+
    //|                                                     EMA_Cross.mq4 |
    //|                     Copyright 2021, MetaQuotes Software Corp.    |
    //|                                              https://www.mql5.com |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2021, MetaQuotes Software Corp."
    #property link      "https://www.mql5.com"
    #property version   "1.00"
    #property strict
    
    #property indicator_chart_window
    #property indicator_buffers 3
    #property indicator_plots 2
    #property indicator_type1 DRAW_ARROW
    #property indicator_color1 Blue
    #property indicator_label1 "Buy"
    #property indicator_type2 DRAW_ARROW
    #property indicator_color2 Red
    #property indicator_label2 "Sell"
    #property indicator_width1 2
    #property indicator_width2 2
    
    double BuyBuffer[];
    double SellBuffer[];
    
    int EMA20;
    int EMA50;
    
    input int EMAPeriod1 = 20; // Length of the first EMA
    input int EMAPeriod2 = 50; // Length of the second EMA
    
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int OnInit()
      {
        SetIndexBuffer(0, BuyBuffer, INDICATOR_DATA);
        PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
    
        SetIndexBuffer(1, SellBuffer, INDICATOR_DATA);
        PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
    
        EMA20 = iMA(Symbol(), 0, EMAPeriod1, 0, MODE_EMA, PRICE_CLOSE);
        EMA50 = iMA(Symbol(), 0, EMAPeriod2, 0, MODE_EMA, PRICE_CLOSE);
    
        IndicatorSetString(INDICATOR_SHORTNAME,"EMA_Cross("+IntegerToString(EMAPeriod1)+","+IntegerToString(EMAPeriod2)+")");
    
        return(INIT_SUCCEEDED);
      }
    
    //+------------------------------------------------------------------+
    //| Custom indicator iteration function                              |
    //+------------------------------------------------------------------+
    int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &Data[])
      {
        int limit;
    
        if(prev_calculated==0)
            limit = 1;
        else
            limit = rates_total - prev_calculated + 1;
    
        for(int i = limit; i < rates_total; i++)
          {
            double priceEMA20 = iMA(Symbol(),0,EMAPeriod1,0,MODE_EMA,PRICE_CLOSE, i);
            double priceEMA50 = iMA(Symbol(),0,EMAPeriod2,0,MODE_EMA,PRICE_CLOSE, i);
            double prevPriceEMA20 = iMA(Symbol(),0,EMAPeriod1,0,MODE_EMA,PRICE_CLOSE, i+1);
            double prevPriceEMA50 = iMA(Symbol(),0,EMAPeriod2,0,MODE_EMA,PRICE_CLOSE, i+1);
    
            if(priceEMA20 > priceEMA50 && prevPriceEMA20 <= prevPriceEMA50)
                BuyBuffer[i] = Low[i] - 4 * Point;
            else
                BuyBuffer[i] = 0.0;
    
            if(priceEMA20 < priceEMA50 && prevPriceEMA20 >= prevPriceEMA50)
                SellBuffer[i] = High[i] + 4 * Point;
            else
                SellBuffer[i] = 0.0;
          }
    
        return(rates_total);
      }
    //+------------------------------------------------------------------+
    

    Save this code as 'EMA_Cross_Signals.mq4' and compile it in MetaEditor. Once the compilation is successful, the indicator will be available in MetaTrader 4 under the custom indicators category. This indicator will display Buy (Blue) and Sell (Red) arrows on the chart whenever the EMA 20 and EMA 50 cross.