Running more than one EA at a time - page 2

 

First, make sure that you initialized all the variables with some meaningful defaults. For example, you are using BuyTicket before it is initialized, so it can be any value on the first run.

Excerpt from the MQL4 reference file (MQL4 Reference / Language Basics / Variables / Initialization of Variables):

If a variable is not initialized explicitly, the value stored in this variable can be any. Implicit initialization is not used.

So, in the following part add defaults to the variables:

//Global Variables
int BuyTicket;
int SellTicket;
double InternalStopLoss;
double CalcDigits;
double CalcPoint;
bool MABuyFanning;
bool MASellFanning;
int SelectedOrder;
bool Closed;
int ErrorCode;
string ErrLog;
double BuyStopLoss;
double SellStopLoss;
bool NewBar;
double ThisBarOpen;
double SmallMA;
double MediumMA;
double LargeMA; 
 

And yeah, MQL4 has bugs and quirks, but most of the errors could be avoided just by reading the basics in the reference manual.

Also, there is an article on common bugs in MQL4 programs. Check top of the forum, you'll find the link.

 
drazen64:

First, make sure that you initialized all the variables with some meaningful defaults. For example, you are using BuyTicket before it is initialized, so it can be any value on the first run.

Excerpt from the MQL4 reference file (MQL4 Reference / Language Basics / Variables / Initialization of Variables):

So, in the following part add defaults to the variables:



That is true, however, it depends on the usage. Sometimes you just want to declare it because you know where the value will be taken from, and sometimes you do need to initialize it with something since it may not works correctly if it is not initialize with specific default values.
 
deysmacro:

That is true, however, it depends on the usage. Sometimes you just want to declare it because you know where the value will be taken from, and sometimes you do need to initialize it with something since it may not works correctly if it is not initialize with specific default values.


He does not need to initialize variables at the place of the definition, but he definitely should initialize variables to some value before using them.

If he uses them before initialization, value is unpredictable. So are his errors. Before any deeper code analysis he should take care of this problem.

Modern compilers raise errors if you try to use uninitialized variables. MQL4 unfortunately does not, so the coder must be sure that everything is properly initialized.

 
You could point out to him 1 thing that he uses the var without proper initializations.
 
deysmacro:
You could point out to him 1 thing that he uses the var without proper initializations.


Read the second sentence in the first line of https://www.mql5.com/en/forum/151167/page2#954612

You could read the answer before criticizing it.

BTW, how are you helping here? "Smart" remarks do not help very much.

 
for(Counter = 0; Counter <=OrdersTotal()-1; Counter++)
               {
               SelectedOrder = OrderSelect(Counter,SELECT_BY_POS);
               if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_BUY)
                  {
                 // while(IsTradeContextBusy()) Sleep(10);
                  Closed = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),Slippage,Red);
                  if(Closed == true) BuyTicket = 0;
                  else Alert("Symbol: ",Symbol()," Ticket: ",BuyTicket," unable to close buy order(s): buy ma convergence close routine");                  
                  }
            Counter--;               
                }
 

I did read before I post mine. I mean, show him one thing that relates to the var and that var is not initialized properly.


Hehe. GumRai beats it. XD

 

If I understand you correctly, you are saying that the fact that he uses uninitialized variables is not something to worry about just because I did not point out exactly what could happen because BuyTicket and some other variables contain some unknown values when the program starts?

Well, so be it. Let him use uninitialized variables. What could go possibly wrong?

 
I have initialised all the variables as suggested as well as removing "==true" from the booleans. Also I have removed all the bank space from inside the lines of code. I have tested it using the debugger on two different symbols - one on the laptop and one on the PC for approximately 2 hours and they both worked fine, selling, buying and closing at the correct times with no errors at all. However then I added the EA to the other 3 symbols on both the PC and the laptop so that all 4 were working on both terminals and all began to miss entries and exits from then onwards. At the end of the test, on one terminal I removed the EAs before closing the open positions and on the other terminal I removed the EAs after closing the open positions. The terminal which still had open positions took a very long time to remove the EAs but the terminal with no open positions removed all 4 EAs immediately. Revised code is posted separately below.