'}' - unexpected end of program test.mq5 164 1 and '{' - unbalanced parentheses test.mq5 41 1

 

Hi everyone,

I am unable to correct these two errors when compiling. Can anyone please help me to solve these two errors, thanks. Also i am new to MQl5 programming (Still learning and practicing), here is my code i would appreciated if anyone can correct if there is any error with a bit of explanation and showed me if there is better way to write it. It's a simple EA to take buy position when last candle open and close above moving average and sell position when last candle open and close below moving average with TakeProfit and StopLoss and TrailingStop.

thanks in advance

//+------------------------------------------------------------------+
//|                                                       test.mq5 |
//|                        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"

#include<Trade\Trade.mqh>
CTrade trade;

//--- input parameters
input int      iMA = 21;
input int      TakeProfit = 250;
input int      SL = 250;
input int      TrailingStop = 10;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()

{  //------------(line 41)
  
  
double myMovingAverageArray[];
int movingAverageDefinition = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
ArraySetAsSeries(myMovingAverageArray,true);
CopyBuffer(movingAverageDefinition,0,0,21,myMovingAverageArray);
double myMovingAverageValue=myMovingAverageArray[0];

double bullish = iClose(_Symbol,PERIOD_CURRENT,0) > iOpen(_Symbol,PERIOD_CURRENT,0);
double bearish = iClose(_Symbol,PERIOD_CURRENT,0) <  iOpen(_Symbol,PERIOD_CURRENT,0)

 // -------------Trailing stop and exit------------------  
   void CheckTrailingStop(double Ask)
   
   
   double SL=NormalizeDouble(Ask-250*_Point,Digits);
   
   
   
   if (PositionsTotal()==0) 
   
{


for (int i=PositionsTotal()-1; i >=0; i--)

}
 
 
{

string symbol=PositionGetSymbol(i);

if (_Symbol==symbol)

if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY)

}

{

ulong PositionTicket=PositionGetInteger(POSITION_TICKET);

double TakeProfit=NormalizeDouble(Ask+250*_Point,Digits);

double CurrentStopLoss==PositionGetDouble(POSITION_SL);
 
 if (CurrentStopLoss<SL)
 
 trade.PositionModify(PositionTicket,(currentStopLoss+10*_Point),0);
 
 }
 
  
   
   void CheckTrailingStop(double Bid)
   
   
   double Sl=NormalizeDouble(Bid+250*_Point,Digits);
 
  if (PositionsTotal()==0) 
 

{


for (int i=PositionsTotal()-1; i >=0; i--)


{

string symbol=PositionGetSymbol(i);

if (_Symbol==symbol)


if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
}

{

ulong PositionTicket=PositionGetInteger(POSITION_TICKET);

double TakeProfit=NormalizeDouble(Bid-250*_Point,Digits);

double CurrentStopLoss==PositionGetDouble(POSITION_SL);


if (CurrentStopLost>SL)


trade.PositionModify(PositionTicket,(currentStopLost-10*_Point),0);

}

// ----------------buy------------------------------------
if (iClose(_Symbol,PERIOD_CURRENT,0) > iOpen(_Symbol,PERIOD_CURRENT,0)) > (myMovingAverageArray[0])  

{
   
  double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits;
   trade.Buy(0.10,NULL,Ask,(Ask-1000*_Point),0,NULL);
   CheckTrailingStop(Ask);
}

else return(false);

//--------------------sell------------------------------------
   else if (iClose(_Symbol,PERIOD_CURRENT,0) < iOpen(_Symbol,PERIOD_CURRENT,0)) < (myMovingaverageArray[0])

{
   
  double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits;
  trade.Sell(0.10,NULL,Bid,(Bid+1000*_Point),0,NULL);
   CheckTrailingStop(Bid);
 
 
 else return(false);

    }       


} //------------------------------ (line 164)
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.03.07
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Please indent your code properly. You can do this with the auto-indentation in your code editor (Tools - Styler).
 

This line

double bearish = iClose(_Symbol,PERIOD_CURRENT,0) <  iOpen(_Symbol,PERIOD_CURRENT,0)

is missing a closing semi-colon;


Here

for (int i=PositionsTotal()-1; i >=0; i--)

}
 
 
{

You have a closing bracket followed by opening, should be reversed, but also there is no code in the loop then.

Same thing here

if (PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL)
}

{
 
double bullish = iClose(_Symbol,PERIOD_CURRENT,0) > iOpen(_Symbol,PERIOD_CURRENT,0);
double bearish = iClose(_Symbol,PERIOD_CURRENT,0) <  iOpen(_Symbol,PERIOD_CURRENT,0);

The results are bools but you are assigning them to double variables????

 
lippmaje:
Please indent your code properly. You can do this with the auto-indentation in your code editor (Tools - Styler).

hi lippmaje,


thanks for the help.

 
Arthur Joseph Mcalister:

This line

is missing a closing semi-colon;


Here

You have a closing bracket followed by opening, should be reversed, but also there is no code in the loop then.

Same thing here

HI Arthur,

thanks for this great help, i am going to correct the code as you say.

 
Keith Watford:

The results are bools but you are assigning them to double variables????

HI Keith,

Thank you for correcting me. Infact i was a bit confused about bool variables, thanks to clarify.

 
spike thunder :


You make one and the same gross mistake: you create a headlining indicator AT EVERY TICK!

Remember: in MQL5, the indicator handle is CREATED ONCE !!! And this is done in OnInit ()!

 
Vladimir Karputov:

You make one and the same gross mistake: you create a headlining indicator AT EVERY TICK!

Remember: in MQL5, the indicator handle is CREATED ONCE !!! And this is done in OnInit ()!

HI Vladimir,

Thanks for your help. 

double myMovingAverageArray[];
int movingAverageDefinition = iMA(_Symbol,_Period,21,0,MODE_EMA,PRICE_CLOSE);
ArraySetAsSeries(myMovingAverageArray,true);
CopyBuffer(movingAverageDefinition,0,0,21,myMovingAverageArray);
double myMovingAverageValue=myMovingAverageArray[0];

double bullish = iClose(_Symbol,PERIOD_CURRENT,0) > iOpen(_Symbol,PERIOD_CURRENT,0);
double bearish = iClose(_Symbol,PERIOD_CURRENT,0) <  iOpen(_Symbol,PERIOD_CURRENT,0)

so you means this part should be moved to the oninit. Would you please check if the trailing is well coded also.

thank you very much for the help

 
spike thunder :

HI Vladimir,

Thanks for your help. 

so you means this part should be moved to the oninit. Would you please check if the trailing is well coded also.

thank you very much for the help

First, please correct your code - move the creation of the indicator handle to OnInit (please start reading the Documentation - there are lots of examples there). After that show your code. And only then can I watch.

 

The parenthesis are correctly opened at line 41 and close at line 164, but there, you are confused : 

if (PositionsTotal() == 0)
    { <---- OPENED OK
    for (int i = PositionsTotal() - 1; i >= 0; i--) <---- NO ";" ? Looping what ? NOT OK
} <--- CLOSED OK
////////////////////////////
{ <--- Opening what ?  NOT OK

    string symbol = PositionGetSymbol(i);
    if (_Symbol == symbol)
        if (PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) <--- possible to use indentation that way but not to let it opened, NOT OK

} <---- Closing what ? NOT OK
///////////////////////////////
{ <---- Opening what ?  NOT OK

    ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
    double TakeProfit = NormalizeDouble(Ask + 250 * _Point, Digits);
    double CurrentStopLoss == PositionGetDouble(POSITION_SL);
    if (CurrentStopLoss < SL)
        trade.PositionModify(PositionTicket, (currentStopLoss + 10 * _Point), 0); <----- Correct syntax with indentation, it is closed ";" OK
} <--- Closing what ?  NOT OK
Reason: