help logic script!

 
Hello! I'm new here. I have this code, where I receive a signal from an indicator and based on it I open operations. but here it opens operations as soon as I connect it, and also opens infinite operations.

double Lots = 0.01;
double TakeProfit = 600;
double StopLoss = 200;
input int MagicNumber;
double TakeProfitPoint = TakeProfit * Point;
double StopLossPoint = StopLoss * Point;
double ValorCustom(){
         return (iCustom(NULL, 0, "..//Indicators//Market//Nihilist and ForexAlien Indicator.ex4", 0, 1));};
          // return (iCustom(NULL, 0, "Nihilist and ForexAlien Indicator", 0, 1));};
           


int currentSignal = ValorCustom();

void OnTick()
{
   bool orderOpened = false;
   if (currentSignal != 0 && !orderOpened)
   {
      if (currentSignal > 0 && !orderOpened)
      {
         Alert("Señal de compra");
         OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLossPoint, Ask + TakeProfitPoint, "My EA", MagicNumber, 0, Green);
         orderOpened = true;
      }
      else if (currentSignal < 0 && !orderOpened)
      {
         Alert("Señal de venta");
         OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLossPoint, Bid - TakeProfitPoint, "My EA", MagicNumber, 0, Red);
         orderOpened = true;
      }
   }
   else if (currentSignal == 0)
   {
      orderOpened = false;
   }
}


edit: I found out that the indicator returns the number 2147483647 when there is no signal. That's why I always opened operations (> 0)
 
leonardo vazquez:
// substitute this 
      if (currentSignal > 0 && !orderOpened)
      if (currentSignal > 0 && !orderOpened && currentSignal != EMPTY_VALUE)

2147483647  means EMPTY_VALUE!

 
  1. leonardo vazquez: I'm new here

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  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 (2019)
              Messages Editor

  3. int currentSignal = ValorCustom();
    
    void OnTick() 

    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 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)

Reason: