I want to continously execute a function as long as buy or sell remain true

 

I am writing an EA that continually execute a function as long as a buy or sell remain true. For example if buy is true, the function will execute and quite. It will check again if buy is still true, if true it will execute again. It will continue the cycle until buy is false. I tried implementing this with something like this: 

while (buyCondition === true) {

...

}

but its not working. Please kindly advice me on what to do.

 
while (buyCondition === true)

I don't know what happens with === instead of ==


Please edit your post and

use the code button (Alt+S) when pasting code

 
Keith Watford:

I don't know what happens with === instead of ==


Please edit your post and

use the code button (Alt+S) when pasting code

even with == it's still not working

 
pirokele:

even with == it's still not working

Show the code.

Keith Watford:

use the code button (Alt+S) when pasting code

 
Keith Watford:

Show the code.

//+------------------------------------------------------------------+
//|                                                    abbexpert.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

bool buytrue;
bool selltrue;
bool firstbuy;
bool firstsell;


int nowcount;
int counter;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
 
buytrue = true;
selltrue = true;
firstbuy = true;
firstsell = true;

 counter = 1;
 nowcount=0;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   while(Bid > MAsignal()) {
   
      closeSellTrade();
      selltrue = true;
      firstsell = true;
      
      if(buytrue == true && OrdersTotal() == 0 && firstbuy == true) {
         openBuyTrade();
         buytrue = false;
         firstbuy = false;
      }
      if(buytrue == true && OrdersTotal() == 0 && firstbuy == false) {
         if(Bid > closedBuyPrice()) {
            openBuyTrade();
            buytrue = false;
         }
      }
   }


   while(Bid < MAsignal()) {
      
         closeBuyTrade();
         buytrue = true;
         firstbuy = true;
         
         if(selltrue == true && OrdersTotal() == 0 && firstsell == true) {
            openSellTrade();
            selltrue = false;
            firstsell = false;
         }
         if(selltrue == true && OrdersTotal() == 0 && firstsell == false) {
            if(Bid > closedSellPrice()) {
               openSellTrade();
               selltrue = false;
            }
         }
        
      }
   
   }
//+------------------------------------------------------------------+

The MAsignal() is a function that returns moving average values

 
  1.    while(Bid > MAsignal()) {

    Perhaps you should read the manual.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    After Sleep and between server calls the market will have changed. You must update your Predefined Variables or series arrays before using any of them — you must call RefreshRates.

  2. pirokele: I am writing an EA that continually execute a function as long as a buy or sell remain true. 

    but its not working. Please kindly advice me on what to do.

    Stop doing that. Nothing is changing, just return (thus, waiting for the next tick) and then reevaluate using the new information.
  3. No sleep means you will be using 100% CPU doing nothing.
Reason: