Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1242

 
Sayberix:

I'm reading it. Just haven't gotten there yet, I guess. I don't think it's easy to get through a 28 MB pdf reference. Or to put it another way - not all parishioners know the whole Bible, even though they are devout believers.

I have another question, please advise - if it's not difficult:

when executing code in the strategy visualiser:

1. the program goes into an infinite loop;

2. the results of execution are different:

without breakpoints:

With breakpoint on Print:


All done on debugging historical data.

You are constantly increasing the variable. What do you think: when will the size of the variable go beyond its limits?

 
Vladimir Karputov:

You are constantly increasing the size of the variable. What do you think: when will the size of the variable go beyond its limits?

I think that 10 iterations of the ulong should have been enough. The question then is:

1. why doesn't the loop break? and

2. why aren't the results of the iteration time counter itself displayed?


Is it because debugging was done on historical data?

 
Sayberix:

I think that for 10 iterations the ulong should have been enough. The question then is:

1. why doesn't the loop break? and

2. why aren't the results of the iteration time counter itself displayed?


Is it because of debugging on historical data?

You have a variable declared at global program level - you are constantly increasing it.

 
Oh, I see... Iteration takes less than a millisecond...
 
Vladimir Karputov:

You have a variable declared at the global program level - you are constantly increasing it.

I still don't understand what the global variable has to do with it. The program itself is limited to a loop: "for(int i=1; i<=10; i++)" ?

Or maybe I don't understand something, please explain.

 
Sayberix:

I still don't understand what the global variable has to do with it. The program itself is limited to a loop: "for(int i=1; i<=10; i++)" ?

Or maybe I don't understand something, please explain.

Remove variables

ulong count, raschet = 1;

out of global scope.


On each tick, you repeatedly increment the raschet variable. And on the next tick, you CONTINUE to increment it several times. A few ticks and theraschet variableis out of range. Eventually you print out the variable's values, but don't look at the printout for some reason.

 
Vladimir Karputov:

Remove the variables

from the global scope.


On each tick, you increment the raschet variable repeatedly. And on the next tick, you CONTINUE to increment it repeatedly. A few ticks and theraschet variableis out of range. Finally you print out the variable's values, but don't look at the printout for some reason.

Thanks, I get it. I just got confused, thinking that return stops the program at all, whereas it just interrupts its execution on the current tick. Instead of "return;" I put"ExpertRemove();".

 

Can you tell me in this code:

//+------------------------------------------------------------------+
//|                                                          env.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property indicator_buffers 2

int envHandle;
double upperEnv[], lowerEnv[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   envHandle = iCustom(_Symbol,_Period,"Examples\\Envelopes",3,0,MODE_LWMA,PRICE_OPEN,0.03);
   if(envHandle<0)
     {
      Alert("Ошибка при создании индикаторов - номер ошибки: ",GetLastError(),"!!");
      return(INIT_FAILED);
     }
   SetIndexBuffer(0,upperEnv,INDICATOR_DATA);
   SetIndexBuffer(1,lowerEnv,INDICATOR_DATA);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(envHandle);
  }
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {

   ArraySetAsSeries(upperEnv, true);
   ArraySetAsSeries(lowerEnv, true);

   if((CopyBuffer(envHandle,0,0,3,upperEnv)<0) || CopyBuffer(envHandle,1,0,3,lowerEnv)<0)
      Alert("Ошибка копирования буферов индикатора Envelopes - номер ошибки:",GetLastError(),"!!");

   double   upEnv = upperEnv[0];
   double   lowEnv = lowerEnv[0];
  }
//+------------------------------------------------------------------+

How can I get constantly updated price values of Envelopes lines in variables upEnv and lowEnv? It draws the indicator, but the indicator values in the variables are updated 2 times and that's it.

 
Sayberix:

Can you please tell me in this code:

How can I get constantly updated price values of Envelopes lines in variables upEnv and lowEnv? It draws the indicator, but the indicator values in the variables are updated 2 times and that's it.

First of all, decide: do you want to write an Expert Advisor or an indicator? (You have a mixed code now - indicator and Expert Advisor are mixed together) - i.e. the code will not work.

Hint: Use MQL Wizard to create a template.

 
Vladimir Karputov:

First make up your mind: do you want to write an EA or an indicator? (Right now your code is a mess - both indicator and Expert Advisor are mixed together) - that is, the code is unworkable.

Hint: Use MQL Wizard to create a template.

I already tried it. But it generates classes in Wizard. I'd like to at least understand the code in a simple way - the code I understand at least a little. Could you please advise how to correct the code in order to get indicator values in the EA?

Reason: