Basic EA Will Not Compile - What did I miss?

 

Hi all -

After a couple year hiatus from MT4/MQL4, I'm trying to get back into it with a very basic EA. I want the EA to buy or sell after 50% retracement into a bullish engulfing candle with a 1:1 Reward/Risk Ratio.

Unfortunately the EA will not compile and I'm getting frustrated of trying to address the issues mentioned in the description - did I just miss something simple or did I really screw up 37 different issues?

Thanks for any help you can provide!

//+------------------------------------------------------------------+
//|                                     Engulfing Candle Success.mq4 |
//|                      Copyright © 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

int MagicNumber                  =     23894;            // Magic Number
double RetracementPercent        =     50;               // Retracement Value
double SLCushion                 =       2;              // Stop loss cushion beyond engulfing low / high
double RRR                       =        1;             // Risk/Reward ratio for trades
bool New_Bar                    =     false;             // New Bar?
int
   Time_0,                                               // New bar beginning time
   Total,                                                // Amount of orders in window
   points;                                               // 5 digit adjustment for points
double pips;                                             // 5 digit adjustment for pips
string Symb;                                             // Symbol abbreviation

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if (Digits==5 || Digits==3)                              // For 5 digit brokers
   {
   points=10;                                            // Make points = 10 units
   pips=Point*10;                                        // Pips are actually 10 units...
   }
   else                                                  // else...
   {
   points=1;                                             // Points = 1 unit
   pips=Point;                                           // Pips are just 1 unit
   }
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
     //----Order Processing
   
       for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
       {
         if (
         OrderSelect(pos, SELECT_BY_POS)                          // Only my orders w/
         &&  OrderMagicNumber()  == MagicNumber                   // my magic number
         &&  OrderSymbol()       == Symbol() )                    // and my pair.
         {                                                        // Analyzing orders:
                  Total++;                                                 // Counter of market orders
         }
       }
      if(Total>0)                                                  // Only one order at once
      {
      return(0);
      }
   return(0);
  }
//----------------------------------------------------------------------------- New bar? ------
                                                            // First zero out
   if (Time_0 != Time[0])                                   // If the bar beginning time changed...
      {
      New_Bar= true;                                        // Then there is a new bar
      Time_0 = Time[0];                                     // Remember the new bar beginning time
      if (Total>0)
        {
         OrderDelete(ticket);                               // Delete pending orders if there is a new bar
            } 
   Symb = Symbol();
   Total = 0;
      }  
//+----------------------Bullish Engulfing Candle = Buy
double LimitBuyPrice = (Low[1]+High[1])*RetracementPercent/100;                      // Determine Entry Price as Retracement % into previous candle
double BuyStopPrice = Low[1]-SLCushion;                                              // Stop Loss is set below the candle low by cushion amount
double BuyTargetPrice = ((LimitBuyPrice-BuyStopPrice)* RRR)+LimitBuyPrice;              // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]>High[2] && Low[1]<Low[2])                                 // If new Bullish Engulfing Candle
    {
    RefreshRates();                                                                     // Refresh Market Info
    Print ("Limit Buy at ", LimitBuyPrice, " Stop at ", BuyStopPrice, " Target at ", BuyTargetPrice);       //Order Entry
    int ticket = OrderSend (Symb,2,1,LimitBuyPrice,2,BuyStopPrice,BuyTargetPrice,,MagicNumber);
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
   
//+----------------------Bullish Engulfing Candle = Sell    
double LimitSellPrice = (Low[1]+High[1])*RetracementPercent/100;                        // Determine Entry Price as Retracement % into previous candle
double SellStopPrice = High[1]+SLCushion;                                                     // Stop Loss is set below the candle low by cushion amount
double SellTargetPrice = ((LimitSellPrice-SellStopPrice)* RRR)+LimitSellPrice;                  // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]<Low[2] && High[1]>High[2])                                         // If new Bearish Engulfing Candle
    {
    RefreshRates();                                                                               // Refresh Market Info
    Print ("Limit Sell at ", LimitSellPrice, " Stop at ", SellStopPrice, " Target at ", SellTargetPrice);   //Order Entry
    ticket = OrderSend (Symb,2,3,LimitSellPrice,2,SellStopPrice,SellTargetPrice,,MagicNumber);
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
 

The bulk of your code looks like it needs to be within the start() function,  it is currently outside it . . .

 

If yo have any user defined functions they go outside of any other function .. . 

 
int start()
  {
     //----Order Processing
   
       for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
       {
         if (
         OrderSelect(pos, SELECT_BY_POS)                          // Only my orders w/
         &&  OrderMagicNumber()  == MagicNumber                   // my magic number
         &&  OrderSymbol()       == Symbol() )                    // and my pair.
         {                                                        // Analyzing orders:
                  Total++;                                                 // Counter of market orders
         }
       }
      if(Total>0)                                                  // Only one order at once
      {
      return(0);
      }
   return(0);  //<--- place this and 
  }            //<--- and this

//...                 just before deinit()
//...
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

Most of the issues are gone then. The remaining ones are easy to fix. 

 

Thanks for you help and the quick responses - I see now that I was opening and closing the start function without much happening in the middle.

After shifting the end of the start function though, I now get more errors than ever - what am I missing?


//+------------------------------------------------------------------+
//|                                     Engulfing Candle Success.mq4 |
//|                      Copyright © 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

int MagicNumber                  =     23894;            // Magic Number
double RetracementPercent        =     50;               // Retracement Value
double SLCushion                 =       2;              // Stop loss cushion beyond engulfing low / high
double RRR                       =        1;             // Risk/Reward ratio for trades
bool New_Bar                    =     false;             // New Bar?
int
   Time_0,                                               // New bar beginning time
   Total,                                                // Amount of orders in window
   points;                                               // 5 digit adjustment for points
double pips;                                             // 5 digit adjustment for pips
string Symb;                                             // Symbol abbreviation

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if (Digits==5 || Digits==3)                              // For 5 digit brokers
   {
   points=10;                                            // Make points = 10 units
   pips=Point*10;                                        // Pips are actually 10 units...
   }
   else                                                  // else...
   {
   points=1;                                             // Points = 1 unit
   pips=Point;                                           // Pips are just 1 unit
   }
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 //----Order Processing
   
       for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
       {
         if (
         OrderSelect(pos, SELECT_BY_POS)                          // Only my orders w/
         &&  OrderMagicNumber()  == MagicNumber                   // my magic number
         &&  OrderSymbol()       == Symbol() )                    // and my pair.
         {                                                        // Analyzing orders:
                  Total++;                                                 // Counter of market orders
         }
       }
      if(Total>0)                                                  // Only one order at once
      {
      return(0);
      }
//----------------------------------------------------------------------------- New bar? ------
                                                            // First zero out
   if (Time_0 != Time[0])                                   // If the bar beginning time changed...
      {
      New_Bar= true;                                        // Then there is a new bar
      Time_0 = Time[0];                                     // Remember the new bar beginning time
      if (Total>0)
        {
         OrderDelete(ticket);                               // Delete pending orders if there is a new bar
            } 
   Symb = Symbol();
   Total = 0;
      }  
//+----------------------Bullish Engulfing Candle = Buy
double LimitBuyPrice = (Low[1]+High[1])*RetracementPercent/100;                      // Determine Entry Price as Retracement % into previous candle
double BuyStopPrice = Low[1]-SLCushion;                                              // Stop Loss is set below the candle low by cushion amount
double BuyTargetPrice = ((LimitBuyPrice-BuyStopPrice)* RRR)+LimitBuyPrice;              // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]>High[2] && Low[1]<Low[2])                                 // If new Bullish Engulfing Candle
    {
    RefreshRates();                                                                     // Refresh Market Info
    Print ("Limit Buy at ", LimitBuyPrice, " Stop at ", BuyStopPrice, " Target at ", BuyTargetPrice);       //Order Entry
    int ticket = OrderSend (Symb,2,1,LimitBuyPrice,2,BuyStopPrice,BuyTargetPrice,,MagicNumber);
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
   
//+----------------------Bullish Engulfing Candle = Sell    
double LimitSellPrice = (Low[1]+High[1])*RetracementPercent/100;                        // Determine Entry Price as Retracement % into previous candle
double SellStopPrice = High[1]+SLCushion;                                                     // Stop Loss is set below the candle low by cushion amount
double SellTargetPrice = ((LimitSellPrice-SellStopPrice)* RRR)+LimitSellPrice;                  // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]<Low[2] && High[1]>High[2])                                         // If new Bearish Engulfing Candle
    {
    RefreshRates();                                                                               // Refresh Market Info
    Print ("Limit Sell at ", LimitSellPrice, " Stop at ", SellStopPrice, " Target at ", SellTargetPrice);   //Order Entry
    ticket = OrderSend (Symb,2,3,LimitSellPrice,2,SellStopPrice,SellTargetPrice,,MagicNumber);
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
   
return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
 

wnkender: 

After shifting the end of the start function though, I now get more errors than ever - what am I missing?

I dont know. If I copy paste the code you have posted. I get 1 error, if I fixed that one, I get 2 more. Fixed the 2, code compiles.

//+------------------------------------------------------------------+
//|                                     Engulfing Candle Success.mq4 |
//|                      Copyright © 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

int MagicNumber                  =     23894;            // Magic Number
double RetracementPercent        =     50;               // Retracement Value
double SLCushion                 =       2;              // Stop loss cushion beyond engulfing low / high
double RRR                       =        1;             // Risk/Reward ratio for trades
bool New_Bar                    =     false;             // New Bar?
int
   Time_0,                                               // New bar beginning time
   Total,                                                // Amount of orders in window
   points;                                               // 5 digit adjustment for points
double pips;                                             // 5 digit adjustment for pips
string Symb;                                             // Symbol abbreviation
int ticket;                                                                                            //<--- first issue. ticket was not declared.
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  if (Digits==5 || Digits==3)                              // For 5 digit brokers
   {
   points=10;                                            // Make points = 10 units
   pips=Point*10;                                        // Pips are actually 10 units...
   }
   else                                                  // else...
   {
   points=1;                                             // Points = 1 unit
   pips=Point;                                           // Pips are just 1 unit
   }
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 //----Order Processing
   
       for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
       {
         if (
         OrderSelect(pos, SELECT_BY_POS)                          // Only my orders w/
         &&  OrderMagicNumber()  == MagicNumber                   // my magic number
         &&  OrderSymbol()       == Symbol() )                    // and my pair.
         {                                                        // Analyzing orders:
                  Total++;                                                 // Counter of market orders
         }
       }
      if(Total>0)                                                  // Only one order at once
      {
      return(0);
      }
//----------------------------------------------------------------------------- New bar? ------
                                                            // First zero out
   if (Time_0 != Time[0])                                   // If the bar beginning time changed...
      {
      New_Bar= true;                                        // Then there is a new bar
      Time_0 = Time[0];                                     // Remember the new bar beginning time
      if (Total>0)
        {
         OrderDelete(ticket);                               // Delete pending orders if there is a new bar
            } 
   Symb = Symbol();
   Total = 0;
      }  
//+----------------------Bullish Engulfing Candle = Buy
double LimitBuyPrice = (Low[1]+High[1])*RetracementPercent/100;                      // Determine Entry Price as Retracement % into previous candle
double BuyStopPrice = Low[1]-SLCushion;                                              // Stop Loss is set below the candle low by cushion amount
double BuyTargetPrice = ((LimitBuyPrice-BuyStopPrice)* RRR)+LimitBuyPrice;              // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]>High[2] && Low[1]<Low[2])                                 // If new Bullish Engulfing Candle
    {
    RefreshRates();                                                                     // Refresh Market Info
    Print ("Limit Buy at ", LimitBuyPrice, " Stop at ", BuyStopPrice, " Target at ", BuyTargetPrice);       //Order Entry
    int ticket = OrderSend (Symb,2,1,LimitBuyPrice,2,BuyStopPrice,BuyTargetPrice,NULL,MagicNumber);                        // <--- 2.1 issue. there was a paramaeter expected
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
   
//+----------------------Bullish Engulfing Candle = Sell    
double LimitSellPrice = (Low[1]+High[1])*RetracementPercent/100;                        // Determine Entry Price as Retracement % into previous candle
double SellStopPrice = High[1]+SLCushion;                                                     // Stop Loss is set below the candle low by cushion amount
double SellTargetPrice = ((LimitSellPrice-SellStopPrice)* RRR)+LimitSellPrice;                  // Target set to achieve reward/risk ratio

  if (New_Bar && Close[1]<Low[2] && High[1]>High[2])                                         // If new Bearish Engulfing Candle
    {
    RefreshRates();                                                                               // Refresh Market Info
    Print ("Limit Sell at ", LimitSellPrice, " Stop at ", SellStopPrice, " Target at ", SellTargetPrice);   //Order Entry
    ticket = OrderSend (Symb,2,3,LimitSellPrice,2,SellStopPrice,SellTargetPrice,NULL,MagicNumber);                         // <--- 2.2 issue. there was a paramaeter expected
    if (ticket<0) Alert("OrderSend failed: ",GetLastError());
    }
   
return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

 0 error(s), 0 warning(s) 

 

Wierd - now I copy+pasted the code into a new EA from scratch and it compiled perfectly.

Thanks for the help - this is a great community and I really appreciate the support the more experienced programmers here provide for free.