Beginner - Error open parenthesis expected

 

Hello,

By reading through this website i am trying to make a (hopefully) very simple indicator. This is for learning purposes as I am a total beginner.


My first step is that I want to store the closing price in a variable at a specific time. Later on I want to compare this closing price to a closing price an x amount later and generate a buy or sell signal.


But I get an error at the first step. There is an openparenthesis expected at the "If" statement. But when I read me MQL5 reference for the "if" statement it seems to match my format.

Can someone help me out?



#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

input int timex = 180;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   

   return(INIT_SUCCEEDED);


  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

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

datetime tm=TimeCurrent();
double Close;

   string str1="Date and time with minutes: "+TimeToString(tm);
   string str2="Date only: "+TimeToString(tm,TIME_DATE);
   string str3="Time with minutes only: "+TimeToString(tm,TIME_MINUTES);
   string str4="Time with seconds only: "+TimeToString(tm,TIME_SECONDS);
   string str5="Date and time with seconds: "+TimeToString(tm,TIME_DATE|TIME_SECONDS);
//--- output results
   Alert(str1);
   Alert(str2);
   Alert(str3);
   Alert(str4);
   Alert(str5);

   datetime timezero = StringToTime(str2) +timex*60;

   if(timezero == TimeCurrent)
   {
   Close = iClose(_Symbol,PERIOD_CURRENT,1);
   Print(Close);
   }

  }

//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---

  }
//+------------------------------------------------------------------+
 
Divania111:

Hello,

But I get an error at the first step. There is an openparenthesis expected at the "If" statement. But when I read me MQL5 reference for the "if" statement it seems to match my format.

Can someone help me out?

You have not used the  TimeCurrent function correctly

   if(timezero == TimeCurrent())
 
Paul Anscombe #:

You have not used the  TimeCurrent function correctly

Thanks! Been trying to figure it out for hours and it so obvious.

again, thanks!

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
 

Hi Everyone,


So I have been trying to make my own indicator based on the above.

-If the close price at a specified time is higher than the close price x-amount of minutes back, then I want to plot a up-arrow on the chart.

- If the close price at a specified time is lower than the close price x-amount of minutes back, then I want to plot a down-arrow on the chart.

The idea is that the arrow has a similar style to that of the fractals indicator.


The code seems not to have any errors. Yet, no arrows are generated on the chart. Can someone point me in the right direction as to what I am missing? (ps. when I attached the indicator the input parameter timex is filled in minutes: for example 1320mins in order to set the time to 22:00hrs of the current day). 

Also any tips on how to get the close price from an array back at a specified time? Currently, I simply use close[24] to get the closing price of "array entry 24". 

#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  Gray
#property indicator_color2  Gray
#property indicator_label1  "Arrow Up"
#property indicator_label2  "Arrow Down"
//--- input parameters
input int      TimeX;
//--- indicator buffers
double ArrowUpBuffer[];
double ArrowDownBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
Plot
   SetIndexBuffer(0,ArrowUpBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ArrowDownBuffer,INDICATOR_DATA);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_ARROW,217);
   PlotIndexSetInteger(1,PLOT_ARROW,218);
//--- arrow shifts when drawing
//--- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- Array Setting as series
ArraySetAsSeries(ArrowDownBuffer,true);
ArraySetAsSeries(ArrowUpBuffer,true);
ArraySetAsSeries(close,true);
ArraySetAsSeries (time,true);

int bars = rates_total - 1;
if(prev_calculated > 0) bars = rates_total - prev_calculated;

//Main Loop

   for(int i=bars;i>rates_total;i--)
     {
      datetime tm =TimeCurrent(); 
      string str2;
      str2=TimeToString(tm,TIME_DATE);
      datetime timenow = StringToTime(str2) + TimeX;      
      if(TimeCurrent() == timenow && close[i] >= close[24])
        {
       ArrowUpBuffer [i] = close[i];
        }
          else
            {
             ArrowDownBuffer [i] = close[i];
            }
         }
 return(rates_total);    
     }
 

i also get these errors 

'||' - open parenthesis expected Marketstructurebots.mq5 94 56

')' - open parenthesis expected Marketstructurebots.mq5 94 100

'OrderSend' - wrong parameters count Marketstructurebots.mq5 107 18



//+------------------------------------------------------------------+

//|                                                Marketstructure trading bot.mq5|

//|                        Copyright 2024, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property strict


// Input parameters

input int period = 20; // Period for calculating support and demand

input double sensitivity = 0.1; // Sensitivity for identifying support and demand levels

input double lotSize = 0.1; // Lot size for trading


// Global variables

double supportLevel = 0, demandLevel = 0;


//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{

    if (period <= 0 || sensitivity <= 0 || lotSize <= 0)

    {

        Print("Error: Invalid input parameters. Please ensure period, sensitivity, and lot size are greater than 0.");

        return INIT_FAILED;

    }

    

    return INIT_SUCCEEDED;

}

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{


}

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

    if (!CalculateSupportAndDemand())

    {

        Print("Error calculating support and demand levels. Exiting tick function.");

        return;

    }


    // Check for uptrend or if price is near support or demand level

    if (IsUptrend() || IsNearSupportOrDemand())

    {

        Buy();

    }

}

//+------------------------------------------------------------------+

//| Function to calculate support and demand levels                   |

//+------------------------------------------------------------------+

bool CalculateSupportAndDemand()

{

    // Calculate support and demand levels using a simple moving average

    double ma = iMA(_Symbol, 0, period, 0, MODE_SMA, PRICE_CLOSE);

    if (ma == 0)

    {

        Print("Error: Moving average calculation failed. Ensure the period parameter is valid.");

        return false;

    }

    

    supportLevel = ma - (ma * sensitivity);

    demandLevel = ma + (ma * sensitivity);

    

    return true;

}

//+------------------------------------------------------------------+

//| Function to check if the market is in an uptrend                 |

//+------------------------------------------------------------------+

bool IsUptrend()

{

    // Implement your uptrend condition here

    // For example, if current price > moving average, consider it an uptrend

    double ma = iMA(_Symbol, 0, period, 0, MODE_SMA, PRICE_CLOSE);

    if (ma == 0)

    {

        Print("Error: Moving average calculation failed. Ensure the period parameter is valid.");

        return false;

    }

    

    return (SymbolInfoDouble(_Symbol, SYMBOL_BID) > ma);

}

//+------------------------------------------------------------------+

//| Function to check if the price is near support or demand level   |

//+------------------------------------------------------------------+

bool IsNearSupportOrDemand()

{

    // Check if price is near support or demand level

    double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    return (MathAbs(currentBid - supportLevel) < Point || MathAbs(currentBid - demandLevel) < Point);


}

//+------------------------------------------------------------------+

//| Function to execute a buy order                                  |

//+------------------------------------------------------------------+

void Buy()

{

    // Place a market buy order with specified lot size

    double sl = supportLevel - (supportLevel * 0.1); // Set stop loss 10% below support level

    double tp = demandLevel + (demandLevel * 0.1); // Set take profit 10% above demand level

    double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    #define OP_BUY 0

    int ticket = OrderSend(_Symbol, OP_BUY, lotSize, price, 2, sl, tp, "Buy order", 0, 0, clrGreen);

    if (ticket < 0)

    {

        Print("Error in placing buy order: ", GetLastError());

    }

}

//+------------------------------------------------------------------+


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.05.12
  • 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
 
tozybiz #:

i also get these errors 

'||' - open parenthesis expected Marketstructurebots.mq5 94 56

')' - open parenthesis expected Marketstructurebots.mq5 94 100

'OrderSend' - wrong parameters count Marketstructurebots.mq5 107 18

  1. Don't hijack other threads for your off-topic post. Next time, make your own, new, thread.

  2. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  3. Stop using ChatGPT/Copilot. You have a mixture of MT4 and MT5 calls.
              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

  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)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

Reason: