Unbalanced parentheses & unexpected end of program showing although i cant find a fault?

EMcgeown71
15

This is a simple MA cross EA im working on but have ran into a problem when compiling? If anyone can help that would be great! 

Lines: 15 and 45 are incorrect.

#include <Trade/TRADE.mqh>

CTrade trade;

int OnInit() {

 return(INIT_SUCCEEDED); 
 }

  void OnDeinit(const int reason) {
  
  }
  

void OnTick() {
 
static datetime timestamp;
 datetime time = iTime(_Symbol, PERIOD_CURRENT, 0);
 if (timestamp !=time){
 timestamp = time;

static int handleSlowMa= iMA(_Symbol, PERIOD_CURRENT, 18, 0, MODE_SMA, PRICE_CLOSE);
double slowMaArray [];
CopyBuffer(handleslowma, 0,1,2,slowmaarray);
ArraySetAsSeries(slowmaarray, true);

static int handlefastma = iMA(_Symbol, PERIOD_CURRENT,1,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_ASK);
double tp= ask + 100 * SymbolInfoDouble(_symbol, SYMBOL_ASK);
trade.Buy(3, _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 ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double sl= bid + 100 * SymbolInfoDouble(_Symbol, SYMBOL_BID);
double tp= ask - 100 * SymbolInfoDouble(_Symbol, SYMBOL_BID);
trade.Sell(3, _Symbol,bid,sl,tp, "This is a sell");
}
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
William Roeder
25873
William Roeder  
  1. static int handleSlowMa= iMA(_Symbol, PERIOD_CURRENT, 18, 0, MODE_SMA, PRICE_CLOSE);
    1. 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 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. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    2. 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.03.08)
                How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
                How to call indicators in MQL5 - MQL5 Articles (12 March 2010

  2. Does this look correct to you?

    static int handlefastma = iMA(_Symbol, PERIOD_CURRENT,1,0 MODE_EMA, PRICE_CLOSE)double fastmaarray[];
  3. double sl= ask - 100 * SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    double tp= ask + 100 * SymbolInfoDouble(_symbol, SYMBOL_ASK);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

EMcgeown71
15
EMcgeown71  
William Roeder:
    1. 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 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. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
      4. 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.03.08)
                  How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
                  How to call indicators in MQL5 - MQL5 Articles (12 March 2010

    2. Does this look correct to you?

    3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

      1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

      2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                  MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

      3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
        Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


Okay.... im new to this so i was just trying to test and figure some bits out. Clearly i have a lot to learn, so are you saying what i have coded is basically useless?

William Roeder
25873
William Roeder  
EMcgeown71:, so are you saying what i have coded is basically useless?

#1.1.1 says that will not work.

#1.1.2 says how to fix it.

#1.2 answers your original question. fix it.

#1.3 states a problem you might not find or correct for months.

MAYUR LADKAT
9
MAYUR LADKAT  

if (timestamp !=time){ 


Error as 'if' - expressions are not allowed on a global scope Moving Average.mq5