it keeps placing trades, while its not suppose to. Please help the newbie.......

 
//+------------------------------------------------------------------+
//| Input variables                                                  |
//+------------------------------------------------------------------+
input int Stoploss = 50, Takeprofit = 100;
input double Tradevol = 0.1;
//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
bool glBuyPlaced, glSellPlaced, openPosition = PositionSelect(_Symbol);
long positionType = PositionGetInteger(POSITION_TYPE);

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   glBuyPlaced = false;
   glSellPlaced = false;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
   {
//--- Declare the trade classes
   MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request);

//--- Define the array to hold the close and open prices
   MqlRates rates[]; ArraySetAsSeries(rates,true);
   CopyRates(_Symbol,_Period,0,10,rates);
//+------------------------------------------------------------------+
//| Buy order                                                        |
//+------------------------------------------------------------------+
   if(glBuyPlaced == false && (openPosition == false || positionType != POSITION_TYPE_BUY))
      {
         if(rates[2].close > rates[2].open)
            {
               if(rates[1].close > rates[2].open)
                 {
                     if(rates[0].close > rates[1].open)
                        {
                           request.action = TRADE_ACTION_DEAL;
                           request.type = ORDER_TYPE_BUY;
                           request.symbol = _Symbol;
                           request.volume = Tradevol;
                           request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
                           request.deviation = 9;
                           request.sl = SymbolInfoDouble(_Symbol,SYMBOL_BID) - (Stoploss * _Point);
                           request.tp = SymbolInfoDouble(_Symbol,SYMBOL_ASK) + (Takeprofit * _Point);
                           request.type_filling = ORDER_FILLING_FOK;

                           if(!OrderSend(request,result))
                           Print("Error");
                         }
                  }
             }
               glBuyPlaced = true; glSellPlaced = false;
       }
//+------------------------------------------------------------------+
//| Sell market order                                                |
//+------------------------------------------------------------------+
   if(glSellPlaced == false && (openPosition == false || positionType != POSITION_TYPE_SELL))
      {
         if(rates[2].close > rates[2].open)
            {
               if(rates[1].close < rates[2].open)
                  {
                     if(rates[0].close < rates[1].close)
                        {
                           request.action = TRADE_ACTION_DEAL;
                           request.type = ORDER_TYPE_SELL;
                           request.symbol = _Symbol;
                           request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
                           request.volume = Tradevol;
                           request.deviation = 9;
                           request.sl = SymbolInfoDouble(_Symbol,SYMBOL_ASK) + (Stoploss * _Point);
                           request.tp = SymbolInfoDouble(_Symbol,SYMBOL_BID) - (Takeprofit * _Point);
                           request.type_filling = ORDER_FILLING_FOK;

                           if(!OrderSend(request,result))
                           Print("Error");
                        }
                  }
            }
         glBuyPlaced = false; glSellPlaced = true;
      }

   }

Hey guys. can u help out. My EA keeps placing trade while i just want it to place an order once, and the next one follows after the former one has been closed.

[Deleted]  
udoh.jeremiah.emem:

Hey guys. can u help out. My EA keeps placing trade while i just want it to place an order once, and the next one follows after the former one has been closed.

Re-evaluate your trading conditions. Make sure they are not true always

 
ok. thanks bro for then tip
 

Don't keep opening other threads One/two. You have this one open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum

bool glBuyPlaced, glSellPlaced, openPosition = PositionSelect(_Symbol);
long positionType = PositionGetInteger(POSITION_TYPE);
That is not an assignment; it's initialization of a static with a constant. Global and static variables 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,) 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. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum