Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 242

 
Youri Lazurenko:

I can't solve a seemingly simple problem. Somewhere on the chart there was a MA crossing. We find a bar after the crossing. How do I calculate the number of bars from that bar to the current bar?

The number of that particular bar. No?
 
Youri Lazurenko:

I can't solve a seemingly simple problem. Somewhere on the chart there was a MA crossing. We find a bar after the crossing. How do I calculate the number of bars from this bar to the current one?


It seems to be done (at least the comment on the screen is correct). Thanks to"number of bars" I think I've found the solution. I use the iTime function to determine the datetime of the needed bar. The sketch looks like this:

void OnTick()

{

int nBarsUp, nBarsDn;

datetime date_dn = iTime(NULL, 0, IntersectionDN());

datetime date_up = iTime(NULL, 0, IntersectionUp());

datetime date_1 = iTime(NULL, 0, 1);

nBarsUp = Bars(Symbol(), 0, date_up, date_1);

nBarsDn = Bars(Symbol(), 0, date_dn, date_1);

Comment("Number of long bars: " + nBarsUp + "\n "+ "Number of short bars: " + nBarsUp + "\n".

"Number of short bars:" + nBarsDn);

}

//+------------------------------------------------------------------+

int IntersectionDN()

{

for(int i = 0; i < 36; i++)

{

if(iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i) > iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i)

&& iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i-1) < iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i-1))

return(i-1);

}

return(-1);

}

int IntersectionUp()

{

for(int i = 0; i < 36; i++)

{

if(iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i) < iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i)

&& iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i-1) > iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i-1))

return(i-1);

}

return(-1);

}

 
Youri Lazurenko:

Seems to have worked (at least the comment on the screen is correct). Thanks to"number of bars", I think I've found a solution. I used iTime function to determine the datetime of the desired bar. The sketch looks like this:

void OnTick()

{
   int nBarsUp, nBarsDn;
   datetime date_dn = iTime(NULL, 0, IntersectionDN());
   datetime date_up = iTime(NULL, 0, IntersectionUp());
   datetime date_1  = iTime(NULL, 0, 1);
  
   nBarsUp = Bars(Symbol(), 0, date_up, date_1);
   nBarsDn = Bars(Symbol(), 0, date_dn, date_1); 
   
   Comment("Количество длинных баров: " + nBarsUp + "\n"+
           "Количество коротких баров: " + nBarsDn);     
}

//+------------------------------------------------------------------+

int IntersectionDN()

{
   for(int i = 0; i < 36; i++)
   {
      if(iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i) > iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i)
         && iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i-1) < iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i-1))
         return(i-1);
   }  
   return(-1);
}

int IntersectionUp()
{
   for(int i = 0; i < 36; i++)
   {
      if(iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i) < iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i)
         && iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_CLOSE, i-1) > iMA(Symbol(), 0, 21, 0, MODE_EMA, PRICE_CLOSE, i-1))
         return(i-1);
   } 
   return(-1);  

}

You are looking for a crossover in the loop. And from zero bar deep into history. So your function IntersectionXX() returns the bar number.

What's all this for?

int nBarsUp, nBarsDn;
   datetime date_dn = iTime(NULL, 0, IntersectionDN());
   datetime date_up = iTime(NULL, 0, IntersectionUp());
   datetime date_1  = iTime(NULL, 0, 1);
  
   nBarsUp = Bars(Symbol(), 0, date_up, date_1);
   nBarsDn = Bars(Symbol(), 0, date_dn, date_1); 
 
Artyom Trishkin:

You are looking for a crossover in the loop. And from bar zero to the bottom of the story. So IntersectionXX() returns the bar number.

What's all this for?


The bar number, yes. But I need to know the number of bars - from this bar deep in the history to the zero bar. In short, I need the angle of the bar slope from the MA. But since it cannot be calculated as it seems to me (price on the vertical line and bars on the horizontal line placed at some distance from each other - they are incompatible values). Once upon a time there was a suggestion on the forum to introduce a coefficient - the difference in price divided by the number of bars. The idea seems reasonable to me. I want to put it into practice, but because I am a dummy myself, I try to find something and try it myself by trial and error.

P.S. How to insert code in MQL4 (like yours, not like mine)?
 

Can anyone advise how to deal with this problem?


After changing the TF, the panel is warped

 
Youri Lazurenko:

The number of bars, yes. But I need to know the number of bars - from this some bar in the back of the story to the 0th bar. In short, I need the angle of the bar from the MA. But since it cannot be calculated, it seems to me (on the vertical - the price, on the horizontal - the bars placed at some distance from each other - the values are incompatible). Once upon a time there was a suggestion on the forum to introduce a coefficient - the difference in price divided by the number of bars. The idea seems reasonable to me. I want to put it into practice, but as a dummies myself, I try to find something and try myself by trial and error.

P.S. How to insert code in MQL4 (the way you have it, not the way I have it)?
If this is the tenth bar, how do I know the difference between the tenth and zero bar? 10-0=10
And what is the subtraction for?

 

Hello! The two minusnumbers q and ware compared incorrectly, when they are equal, the if operator thinks one is greater than the other.What is the error? When q = -0.0002 and w is also -0.0002, res12=false, why?

 

Hello! The two minusnumbers q and ware compared incorrectly, when they are equal, the if operator thinks one is greater than the other.What is the error? When q = -0.0002 and w is also -0.0002, res12=false, why?

static bool res12=true;

start()
{   

double SPREAD=MarketInfo(Symb,MODE_SPREAD);// Спред

   double q=High[0]-3.0*Point-High[1];
   double w=-SPREAD*Point;
      
   if (New_Bar==true&&ticket1<=0)
     {
      if(q>w)
       {
        res12=false; 
        Alert("res12=false");
        Alert (q);
        Alert (w);
       }
     }
      if (New_Bar==true&&ticket1<=0)
     {
        if(q<=w)
        {
         res12=true;
         Alert("res12=true");
         Alert (q);
         Alert (w);
        }
      }





}
 
Jenya77769:

Hello! The two minusnumbers q and ware compared incorrectly, when they are equal, the if operator thinks one is greater than the other.What is the error? When q = -0.0002 and w is also -0.0002, res12=false, why?


I think we need to normalize and that's it, you can't compare doubles with each other without normalization.

if NormalizeDouble (q<=w)
 
Vyacheslav Kornev:

I think I need to normalise and that's it, as it's not possible to compare the double between them without normalisation.


Thanks, it worked, but I normalized q and w separately!

if(NormalizeDouble(q,4)> NormalizeDouble(q,4))
Reason: