Questions from Beginners MQL5 MT5 MetaTrader 5 - page 441

 

Can you please tell me what I'm doing wrong?

I need to calculate how many bars have passed since the price last crossed the MA - excluding the zero bar.

   if(Bars>1400)
     {
      int Stop;
      double MATcycle;
      for(int i=0;i!=1000 || Stop!=1; i++)
        {
         MATcycle=iMA(Symbol(),TFT,pMAT,shiftMAT,typeMAT,priceMAT,i+1);
         if(MATcycle<High[i+1] && MATcycle>Low[i+1])
           {
            Stop=1;
            Print("MATcycle=",MATcycle," i=",i);
           }
         else return (0);
        }

 
-Aleks-:

Can you please tell me what I'm doing wrong?

You need to - count how many bars have passed since the price last crossed the MA - not including the zero bar.

   if(Bars>1400)
     {
      int Stop;
      double MATcycle;
      for(int i=0;i<1000 || Stop==1; i++)
        {
         MATcycle=iMA(Symbol(),TFT,pMAT,shiftMAT,typeMAT,priceMAT,i+1);
         if(MATcycle<High[i+1] && MATcycle>Low[i+1])
           {
            Stop=1;
            Print("MATcycle=",MATcycle," i=",i);
           }
         else return (0);
        }

As far as I understand, once you have identified the crossing and set stop=1, there is an immediate exit from the cycle. Ok, then you need to go further and remember the number of the candle where the crossing occurred. I.e.

It's easier to write it yourself:

for( int i = 1; i < 1000; i++ )
{
 iMaValue = ...;   // Значение МА на i свече
 if( iMaValue < High[ i ] && iMaValue > low[ i ] )
 {
  num = i;   // Запоминаем номер свечи, на котором произошло пересечение
  break;     // Выходим из цикла
 }
}

This is you have identified the number of the candle. If the number = 2 and do not take into account the current one, then from the moment of crossing one candle was formed (or interpret it as you want).

 
Tapochun:

As far as I understand, once you have identified the crossover and set stop=1, there is an immediate exit from the cycle. OK, then you need to go further and remember the number of the candle where the crossover occurred. I.e.

It's easier to write it yourself:

This is you have identified the number of the candle. If the number = 2 and ignore the current one, then one candle has formed since the crossing (well, or interpret it as you like).

Thank you, that's how it worked for me


int Stop=0;
   if(Bars>1400)
     {
     
      double MATcycle;
      for(int i=1;(i!=1000 || Stop==0); i++)
        {
         MATcycle=iMA(Symbol(),TFT,pMAT,shiftMAT,typeMAT,priceMAT,i);
         if(MATcycle<=High[i] && MATcycle>=Low[i])
           {
            Print("MATcycle=",MATcycle," i=",i);
            Stop=i;
            break;
           }
        }
     }

 
alph:
Can you tell me if this is a realistic tester figure? And is it a good or bad result for a year with a $3,000 depo?
The answer is simple: the tester's performance is NOT REAL if you are testing an EA. Strategy Tester is used by traders to check the performance of the ATC, or to test manual strategies online.
 
-Aleks-:

Thank you, that's how it worked out for me


int Stop=0;
   if(Bars>1400)
     {
     
      double MATcycle;
      for(int i=1;(i!=1000 || Stop==0); i++)
        {
         MATcycle=iMA(Symbol(),TFT,pMAT,shiftMAT,typeMAT,priceMAT,i);
         if(MATcycle<=High[i] && MATcycle>=Low[i])
           {
            Print("MATcycle=",MATcycle," i=",i);
            Stop=i;
            break;
           }
        }
     }

You shouldn't write it that way, because if there was no crossover during the last 1000 bars, first, the loop will continue, and second, it may loop/error, because if the story ends and there is no crossover, there will be no exit from the loop, because Stop = 0. It's better to write it the way I mentioned above.

 
Tapochun:

You shouldn't write it that way, because if there was no crossover during the last 1000 bars, firstly, the cycle will continue, and secondly, it may loop/error, because if the story ends and no crossover occurs, there will be no exit from the cycle, because Stop = 0. It's better to write it the way I mentioned above.

Doesn't "or" sign work - according to the expression, the loop will either be over for 1000 bars or it will terminate as soon as the required result has been found. Or it will be over before 1000 bars if the required result, i.e. MA crossing?
 
-Aleks-:
Doesn't the "or" sign work - the condition says that it either reaches 1000 bars or finishes as soon as the desired result is found. Or it will be over before 1000 bars, if the desired result, i.e. crossing of the MA?
|| means that if at least one of the conditions in the brackets is true, the cycle will repeat itself. Therefore, even when i >= 1000, but stop = 0, the cycle will continue and i will keep incrementing, which will cause incorrect MA values (in case it goes outside the history). And break operator is responsible for termination of the loop when the desired result is found;
 
Tapochun:
|| means that if at least one of the conditions in brackets is true, the loop will be repeated, hence, even when i >= 1000, but stop = 0, the loop will continue, i will continue to increment, which will cause the wrong MA value (in case there is an outlier in the history). And break operator is responsible for termination of the loop upon finding the desired result;
Got it, thanks for the clarification! I thought that one of the conditions would be wrong and then the loop would stop...
 

Hello 2015.09.19_02:13AM MSC. In ArrayResize() function anyway the compiler writes

opposite the size of the array -- comma expected, whether you write int or not. If you don't write int, it says: "-.

I've changed it without type. And I removed & reference and square brackets - it worked!
Warnings: when it was written normally, -- compiler wrote: hides identifier

global-level declaration. And when I removed identifier declaration on a global level, - wrote

it writes: "Error, undeclared identifier. And I changed arrays High[] and Low[] to HighP[] and LowP[]. Not

helped. Compiler writes the same thing. 02:27 MSC. I'm attaching a screenshot file.

 
Николай Никитюк:

Hello 2015.09.19_02:13AM MSC. In ArrayResize() function anyway the compiler writes

opposite the size of the array -- comma expected, whether you write int or not. If you don't write int, it says: "-.

"without type. And I removed & reference and square brackets - it helped!
Warnings: when it was written normally, -- compiler wrote: hides identifier

global-level declaration. But when I removed identifier declaration on a global level, - wrote

it writes: "Error, undeclared identifier. And I changed arrays High[] and Low[] to HighP[] and LowP[]. Not

helped. Compiler writes the same thing. 02:27 MSC. I am attaching the screenshot file.

  1. Images should be inserted like this:Forum: how to insert an image
  2. Don't you read at all what you're being advised?

    Forum on trading, automated trading systems and strategy testing

    Questions from Beginners

    Karputov Vladimir, 2015.09.17 18:46

    1. Wrong spelling of ArrayResize. It should be so:
      //--- устанавливаю размеры массивов с запасом (reserve)
      ArrayResize(mrate,16,9);
      ArrayResize(maVal,16,9);
      ArrayResize(fVal,3,2);
      ArrayResize(zVal,3);
    2. And setting the timeseries flag for the array will look like this:
      //--- массив максимальных цен баров
      ArraySetAsSeries(High,true);
      //---  
      ArraySetAsSeries(Low,true);