"static" -unexpected token, 'iMA' -some operator expected

 
#include  <Trade/Trade.mqh>

CTrade trade;
int rsiHandle;
ulong posTicket;
int handleSlowMA;
int handleFastMA;
int OnInit()
  {
   int rsiHandle = iRSI(_Symbol,PERIOD_CURRENT,6,PRICE_CLOSE);
   int handleSlowMA = iMA(_Symbol,PERIOD_H1,200,0,MODE_EMA,PRICE_CLOSE);
   int handleFastMA = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE); 
   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {

  }


void OnTick(){

 double posPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      double posSl = PositionGetDouble(POSITION_SL);
      double posTP = PositionGetDouble(POSITION_TP);
      int posType = PositionGetInteger(POSITION_TYPE)

            static int handleSlowMA = iMA(_Symbol,PERIOD_H1,200,0,MODE_EMA,PRICE_CLOSE);
            double slowMaArray[];
            CopyBuffer(handleSlowMA,0,1,2,slowMaArray);
            ArraySetAsSeries(slowMaArray,true);


            static int handleFastMA = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE);
            double fastMaArray[];
            CopyBuffer(handleFastMA,0,1,2,fastMaArray);
            ArraySetAsSeries(fastMaArray,true);
   
           double rsi[];
   CopyBuffer(rsiHandle,0,1,1,rsi);

   if(rsi[0] > 80)
     {
      if(posTicket <=0)
        trade.Sell(0.01,_Symbol);
         posTicket = trade.ResultOrder();
     }
   else if(rsi[0] < 20){
         if(posTicket <=0)
           trade.Buy(0.01,_Symbol);
            posTicket = trade.ResultOrder();
            }

   if(PositionSelectByTicket(posTicket)){
      double posPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      double posSl = PositionGetDouble(POSITION_SL);
      double posTP = PositionGetDouble(POSITION_TP);
      int posType = PositionGetInteger(POSITION_TYPE);

      if(posType == POSITION_TYPE_BUY){
         if(posSl == 0)           
            double sl = posPrice - 0.00500;
            double tp = posPrice + 0.01000;
            trade.PositionModify(posTicket,posSl,tp);
           }
         else if(posType == POSITION_TYPE_SELL){
               if(posSl == 0)
                  double sl = posPrice + 0.01000;
                  double tp = posPrice - 0.00500;
                  trade.PositionModify(posTicket,posSl,posTP);
                 }
   }
         static datetime timestamp;
         datetime time = iTime(_Symbol,PERIOD_CURRENT,0);
         if(timestamp != time){
            timestamp = time;

            static int handleSlowMa = iMA(_Symbol,PERIOD_H1,200,0,MODE_EMA,PRICE_CLOSE);
            double slowMaArray[];
            CopyBuffer(handleSlowMa,0,1,2,slowMaArray);
            ArraySetAsSeries(slowMaArray,true);


            static int handleFastMa = iMA(_Symbol,PERIOD_H1,50,0,MODE_EMA,PRICE_CLOSE);
            double fastMaArray[];
            CopyBuffer(handleFastMa,0,1,2,fastMaArray);
            ArraySetAsSeries(fastMaArray,true);
           }
         if(fastMaArray [0] > slowMaArray [0] && fastMaArray [1] < slowMaArray [1])
           {
            Print("Fast Ma is now > than slow ma");
            double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
            double sl = ask - 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
            double tp = ask + 200 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
            trade.Buy(0.01,_Symbol,ask,sl,tp,"This is a buy");
           }

         if(fastMaArray [0] < slowMaArray [0] && fastMaArray [1] > slowMaArray [1])
           {
            Print("Fast Ma is now < than slow ma");
            double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
            double sl = bid + 100 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
            double tp = bid - 200 * SymbolInfoDouble(_Symbol,SYMBOL_POINT);
            trade.Sell(0.01,_Symbol,bid,sl,tp,"This is a sell");
           }
}  

Hi All,


I'm new to coding and MQL5 and this is my first ea.

The problem I have is " static - unexpected token"," iMa - some operator expected", " = - unexpected token", "handleslowMA - unexpected token" for my buy conditions, all errors are in the same line highlighted below

I tried to google search and was not able to find any answer, so I was hoping you guys can help me with the issue.





Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
67192569:

I'm new to coding and MQL5 and this is my first ea.

The problem I have is " static - unexpected token"," iMa - some operator expected", " = - unexpected token", "handleslowMA - unexpected token" for my buy conditions, all errors are in the same line highlighted below

I tried to google search and was not able to find any answer, so I was hoping you guys can help me with the issue.

You forgot the semicolon...
PositionGetInteger(POSITION_TYPE)
 
Dominik Egert #: You forgot the semicolon...
Thank you Dominik
 
            static int handleSlowMA = iMA(_Symbol,PERIOD_H1,200,0,MODE_EMA,PRICE_CLOSE);

That is not an assignment; it's initialization of a common (globally declared), or static variable(s) 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)

 
William Roeder #:

That is not an assignment; it's initialization of a common (globally declared), or static variable(s) 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)

Thank you William I will keep that in mind.
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893