Please help me

 
//+------------------------------------------------------------------+
//|                                                     My first.mq5 |
//|                                              Chioma J. Obunadike |
//|                                     https://wa.me/+2349124641304 |
//+------------------------------------------------------------------+
#property copyright "Chioma J. Obunadike"
#property link      "https://wa.me/+2349124641304"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
extern int EMA = 10
  EMA=iMA(Symbol,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE)
void OnInit()
  {
//--- check for input value
   if (EMA = PRICE_CLOSE)
     {
      Print("Found one ");
     }
   else
      Print("Not here");
      }

This is my first EA. I'm trying to make an EA that will notify me when price touches the 10 EMA on the daily timeframe. I have tried to code this but it gives me errors and I can't figure out why. Please help me make it work.  

Files:
errors.jpg  50 kb
 
  1. extern int EMA = 10  
      EMA=iMA(Symbol,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE)  

    Do not post code that will not even compile.

  2. MT5 does not have an extern keyword.

  3. That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  4. void OnInit()
      {
    //--- check for input value
       if (EMA = PRICE_CLOSE)

    EMA is a handle. It is not the constant zero.

  5. You are assigning zero to the variable then testing it. Zero is false. See Operations of Relation

  6. Perhaps you should read the manual, especially the examples.

       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

  7. iMA(Symbol,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE)

    On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
              Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
              SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 

Thanks for the tips. I started from scratch and I am finally getting a hang of it. Spent the whole day brushing us on an MQL5 online course. I created this script and it works great but I unfortunately have not been able to figure out how to triger the alert only when the close price is above or below the 10EMA by 0.10 points. Can you give me pointers for this please?

Also, I tried to make the script timeframe specific but it still runs on H1 timeframes or maybe I'm wrong but I think it does. I attached a screenshot of the output on mt5.

Thanks in advance 

//+------------------------------------------------------------------+
//|                                                     A script.mq5 |
//|                                              Chioma J. Obunadike |
//|                                     https://wa.me/+2349124641304 |
//+------------------------------------------------------------------+
#property copyright "Chioma J. Obunadike"
#property link      "https://wa.me/+2349124641304"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
// I'm working on a script that will print "found it" when price closes next to the 10EMA (plus or minus 0.10 pips) on D1
   double cont = iMA(Symbol(),PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE); // Asssign variable to 10EMA
   
   MqlRates PriceInformation [];        // defines closePrice. Source: https://www.youtube.com/watch?v=H10QFIZoUYA
       ArraySetAsSeries(PriceInformation,true);
          int Data=CopyRates(Symbol(),PERIOD_D1,0,Bars(Symbol(),PERIOD_D1),PriceInformation);
             double closePrice = PriceInformation[0] .close;
   
               if (cont<=closePrice) Alert("found one!");
   
  }
//+------------------------------------------------------------------+
 
Chioma Obunadike #: Thanks for the tips. I started from scratch and I am finally getting a hang of it. Spent the whole day brushing us on an MQL5 online course. I created this script and it works great but I unfortunately have not been able to figure out how to triger the alert only when the close price is above or below the 10EMA by 0.10 points. Can you give me pointers for this please? Also, I tried to make the script timeframe specific but it still runs on H1 timeframes or maybe I'm wrong but I think it does. I attached a screenshot of the output on mt5. Thanks in advance 

You have coded a "Script", which executes only once and then terminates. That is the wrong type of program for what you are trying to achieve.

If you plan to make an "Indicator" (only to show chart indications and alerts), then use the OnInit() and OnCalculate() event handlers.

If you plan to write an "Expert Advisor" (for automated trading), then use the OnInit() and OnTick() event handlers.

 
Fernando Carreiro #:

You have coded a "Script", which executes only once and then terminates. That is the wrong type of program for what you are trying to achieve.

If you plan to make an "Indicator" (only to show chart indications and alerts), then use the OnInit() and OnCalculate() event handlers.

If you plan to write an "Expert Advisor" (for automated trading), then use the OnInit() and OnTick() event handlers.

Thanks so much Fernando, I have converted it to an indicator. I have also been able to add lines of code for when EMA is closePrice +/- 0.10  but I find that the EMA function I have assigned is not working.

The line of code is below. I had imagined that the value for "cont" would be a number that would stand for my 10 period EMA. However, the output I have when I try to print (cont) is a compilation error. Could you perhaps please tell me what I am doing wrong?

double cont = iMA(NULL,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE); // Asssign variable to 10EMA


 

 
Chioma Obunadike #: Thanks so much Fernando, I have converted it to an indicator. I have also been able to add lines of code for when EMA is closePrice +/- 0.10  but I find that the EMA function I have assigned is not working. The line of code is below. I had imagined that the value for "cont" would be a number that would stand for my 10 period EMA. However, the output I have when I try to print (cont) is a compilation error. Could you perhaps please tell me what I am doing wrong?
double cont = iMA(NULL,PERIOD_D1,10,0,MODE_EMA,PRICE_CLOSE); // Asssign variable to 10EMA

That is incorrect! iMA() returns a handle in MQL5, not a value. Only in MQL4 did it return a value, but you are coding in MQL5. Please read up on the documentation and have a look at the example code.

In essence, you should obtain the handle in the OnInit() and then use CopyBuffer() in the other event handlers such as OnCalculate().

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
//+------------------------------------------------------------------+
//|                                                The main work.mq5 |
//|                                              Chioma J. Obunadike |
//|                                     https://wa.me/+2349124641304 |
//+------------------------------------------------------------------+
#property copyright "Chioma J. Obunadike"
#property link      "https://wa.me/+2349124641304"
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int EMA_10_h=0; // Asssign variable to 10EMA
  double ema_10[];
  
int OnInit()
  { EMA_10_h=iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE);
  ArraySetAsSeries(ema_10,true);
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//--- 
  // I'm working on an indicator that will print "found it" when price closes next to the 10EMA (plus or minus 0.10 pips) on D1
    CopyBuffer(EMA_10_h,0,1,5,ema_10);
   
   MqlRates PriceInformation [];        // defines closePrice. Source: https://www.youtube.com/watch?v=H10QFIZoUYA
       ArraySetAsSeries(PriceInformation,true);
          int Data=CopyRates(Symbol(),PERIOD_D1,0,Bars(Symbol(),PERIOD_D1),PriceInformation);
             double closePrice = PriceInformation[0] .close;
   
               if ((ema_10[0])>=((closePrice)-0.10) && (ema_10[0])<= ((closePrice)+0.10)) Alert("found one!", closePrice);
                else Print("Not here", closePrice);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


I think I am finally beginning to understand. This is the updated version of the code. I ran the indicator on my MT5 and so far, it works! Cheers! Thanks for the help. I am finally at the last step of creating the indicator. Do you by any chance have an article that can help me automatically run the indicator through my market watch? 

 
This is the output. The alerts come off 4 times in a row but I guess I can manage that
Files:
finally.jpg  254 kb