Please help me.

 

I need some help here, I'm a novice at building EAs and I don't know, what I don't know.

I'm using: MetaTrader4 version:4.00 Build 1090 (19May2017)  and  MetaEditor version: 5.00 Build 1601 (19May 2017).


I've built the following, very simple EA, and can't figure out why I keep getting errors when compiling.

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                     FreshCross Alert.mq4                         |
//|                        Stuart Harris                             |
//|                  Copyright 2018, Stuart Harris                   |
//|                  stuart.harris.mail@gmail.com                    |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
#property copyright "Stuart Harris Copyright 2018"
#property link      "stuart.harris.mail@gmail.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
extern int MagicNumber= 777016;
extern double Lots =0.01;
extern double StopLoss=50;
extern double TakeProfit=50;
extern int TrailingStop=0;
extern int Slippage=3
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   PlaySound("Appear-KP-1137861048");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   PlaySound("bye_bye_son-Mike_Koenig-1260922981");
  }  
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0; 
  {
     int  ticket;
     int result=0;
     // List of Buy Entry Rules
 if(((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)<iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,1)))|| // Open Buy Rule #1 (14 cross)
    ((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)<iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,1)))|| // Open Buy Rule #2 (60 cross)
    ((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)<iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,1))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,0)))) // Open Buy Rule #3 (100 cross)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"FreshCross Alert",MagicNumber,0,Blue)
      &&PlaySound("AUD-CHF Golden Cross");
     }
     if(result>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
       if(OrderSelect(result,SELECT_BY_TICKET)==true)      
        {
bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
      if(!res)
           Print("Target/Stop failed, Error#", GetLastError());
        else
           Print("Target/Stop Placed Successfully");   
        }
        return(0);
     }
      // List of Sell Entry Rules
     if(((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)>iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,1)))|| // Open Sell Rule #1 (14 cross)
        ((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)>iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,1)))|| // Open Sell Rule #2 (60 cross)
        ((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,2)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,2))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,1))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_M15,60,0,MODE_SMA,PRICE_CLOSE,0))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_M15,14,0,MODE_SMA,PRICE_CLOSE,0)))) // Open Sell Rule #3 (100 cross)
         {
          ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"FreshCross Alert",MagicNumber,0,Red)
          &&PlaySound("AUD-CHF Death Cross");
         }
        if(result>0)
         {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
       if(OrderSelect(result,SELECT_BY_TICKET)==true)
          {
 bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
       if(!res)
           Print("Target/Stop failed, Error#", GetLastError());
        else
           Print("Target/Stop Placed Successfully");
          }
        return(0);
       } 
      // Manual Close Rules, and Trailing Stop
      
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderType()==OP_BUY)
            {
           if((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,1))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))) //Close Buy Rule

              {
             bool res=false;
                  result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
                   return(0);
                    
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
            bool res=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                     if(!res)//OrderModify has failed and has retured a value of -1
                     Print("TrailingStop failed, Error#",GetLastError());
                     else
                        Print("TrailingStop Executed");  
                    }
                 }
              }
           
         else 
           {
 if((iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,1))&&(iMA(NULL,PERIOD_M15,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_M15,100,0,MODE_SMA,PRICE_CLOSE,0))) // Close Sell Rule

                {
               bool res=false;
                    result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
                  return(0);
                  
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
             bool res=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                     if(!res)//OrderModify has failed and has retured a value of -1
                     Print("TrailingStop failed, Error#",GetLastError());
                     else
                        Print("TrailingStop Executed");
                    }
                 }
              }
           }
        }
     }
   return(0);
        {

  for(int i=0;i<OrdersTotal();i++)
        {
     if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))continue;
     if(OrderMagicNumber()==MagicNumber) result++;
        }
         return (result);
         }
      }
   }
return(0);
}

8 Errors,  0 Warnings:

Line 23, Column 5,     'OnInit' - unexpected token

Line  1,  Column 1,    some operator expected

Line 25, Column37,    ';' - unexpected token

Line 25, Column 4,     'PlaySound' - constant expression required

Line 24, Column 3,     '{' - some operator expected

Line 26, Column 4,     'return' - semicolon expected

Line 26, Column 4,     'return' - expressions are not allowed on a global scope

Line 27, Column 3,     ')' - expressions are not allowed on a global scope

8

 
enoch.ben.yahudah:

I need some help here, I'm a novice at building EAs and I don't know, what I don't know.

I'm using: MetaTrader4 version:4.00 Build 1090 (19May2017)  and  MetaEditor version: 5.00 Build 1601 (19May 2017).


I've built the following, very simple EA, and can't figure out why I keep getting errors when compiling.

8 Errors,  0 Warnings:

Line 23, Column 5,     'OnInit' - unexpected token

Line  1,  Column 1,    some operator expected

Line 25, Column37,    ';' - unexpected token

Line 25, Column 4,     'PlaySound' - constant expression required

Line 24, Column 3,     '{' - some operator expected

Line 26, Column 4,     'return' - semicolon expected

Line 26, Column 4,     'return' - expressions are not allowed on a global scope

Line 27, Column 3,     ')' - expressions are not allowed on a global scope

8

You did not build this EA yourself, at least not entirely and you are not "Stuart Harris", are you? You have copy/pasted sections from other sources and then probably made some changes without actually knowing what you are doing.

You can't just mix old style code with the newer MQL4+ code structures and then hope that it all works.

 
Fernando Carreiro:

You did not build this EA yourself, at least not entirely and you are not "Stuart Harris", are you? You have copy/pasted sections from other sources and then probably made some changes without actually knowing what you are doing.

You can't just mix old style code with the newer MQL4+ code structures and then hope that it all works.

Fernando Carreiro,

First of all I am Stuart Harris,  Enoch is a name I sometimes use online.  Secondly I've already admitted I am a novice and don't know what I am doing.  I learned to write some code from the jimdandy site.  I think that is all old style code.  And I have been studying whatever I find in various forums trying to learn.  I'm not understanding why it won't recongize OnInit, My referrence say that init or OnInit will both work.  I have written this EA as an "audio only" alert and it compiles and runs just fine.  But when I have it send an order suddenly it won't complie.

As for mixing old and new ...  to me everything is new, I am learning.  I'm very coachable and want to learn please help.

 
enoch.ben.yahudah:

Fernando Carreiro,

First of all I am Stuart Harris,  Enoch is a name I sometimes use online.  Secondly I've already admitted I am a novice and don't know what I am doing.  I learned to write some code from the jimdandy site.  I think that is all old style code.  And I have been studying whatever I find in various forums trying to learn.  I'm not understanding why it won't recongize OnInit, My referrence say that init or OnInit will both work.  I have written this EA as an "audio only" alert and it compiles and runs just fine.  But when I have it send an order suddenly it won't complie.

As for mixing old and new ...  to me everything is new, I am learning.  I'm very coachable and want to learn please help.

I will start with the first error:

extern int TrailingStop=0;
extern int Slippage=3; // <-- Missing semicolon
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()

It is not recognising the "OnInit" because you are missing a ";" after the "Slippage" declaration.

However, most of your errors are due to you not understanding the general structure and syntax of the coding language. Unless you learn and understand that first, you are going to have many more errors as you keep adding to your code.

So first, dedicate some time to first learning how the code is supposed to be structured and its syntax. Read the documentation and also look into "C" coding as an alternative to help you understand how it is structured.

PS! Also use "void OnTick()" instead of "int start()" which is the old obsolete code style.

 
Thank you very much.  I will make the changes you have suggested.  Can you recommend a class that will teach me these things?  If not, it is OK.  I will keep searching until I find one.
 
enoch.ben.yahudah: Thank you very much.  I will make the changes you have suggested.  Can you recommend a class that will teach me these things?  If not, it is OK.  I will keep searching until I find one.

The old tutorial that is still available on the MQL4 site is now completely outdated and obsolete.

There are also video tutorials on Udemy, but have no idea if they are any good.

As for books, have a look at these two suggestions:

Something Interesting to Read
Something Interesting to Read
  • 2017.08.03
  • www.mql5.com
This is the thread about books related for stocks, forex, financial market and economics...
 
Thank you