How to find the time of the bar where the moving averages crossed? (code inside) - page 3

 

Yea I need it specifically for PERIOD_H1 chart. As I may change time frames and want that hard coded for that TF.

 

(Im worried that I am doing this wrong - especially what you've written to me twice now WHRoeder regarding MA's and the if statement... - So i'm bracing for some slapping, but I think that would tell me the bar where the MA's crossed (below). So this would surely work by applying it to iBarShift...?) 

if(i1>i2 && i2>i3 && i3>i4 && i4>i5 && i5>i6 && i6>i7 && i7>i8 && i8>i9 && i9>i10 && i10>i11 && i11>i12)triggerBarTime = Time[1];
 
WHRoeder:

Multiple moving averages will not cross at the same time.

Why are you double posting? Your question was previously answered.

Find the earliest bar where they were all in the proper order.


int DIR = 0;
for(int iBar = 0; iBar < Bar; iBar++){
   double MA20 = ... iBar), 
           MA5 = ... iBar);
          :
   if(     MA20 > MA5 && MA5 > ...) DIR = -1;
   else if(MA20 < MA5 && MA5 < ...) DIR = +1; // All fanning up.
   else if (DIR != 0){ iBar--; break; }       // Last one was cross.
}
Can I ask what the Bar variables purpose is? Also, what does "--" mean? The two "--" and "!" marks? (if you'd be so kind? I have looked through the help section, but haven't come across what they're purposes are.)
 
DomGilberto:

Can I ask what the Bar variables purpose is? Also, what does "--" mean? The two "--" and "!" marks? (if you'd be so kind? I have looked through the help section, but haven't come across what they're purposes are.)


And can I ask you why basic from book is still unknown ??

https://book.mql4.com/basics/expressions 

 
Your question is like you saying "what is this little metal thingy (a key) and why is one pressey thing a rectangle (gas) and the others are squarish?"
learn to code.
 

But I know what a gas key is... - that's more simple than coding...

 You lot are right though - I do need to read it through.

 It's because I learn best from trial and error (im a hands on guy when it comes to retaining knowledge). It helps me understand logic when I see it. Plus the MQL4 site doesn't necessarily explain things clearly on how they all work together.

 So to answer your question deVries, that is why I know some basics and some more advance stuff, but it comes patchy because I like to play around and learn that way.

Sorry if I am becoming a pain!

EDIT - I still don't understand "Bar" part of that code you wrote WHRoeder - I'm not sure that's right? 

 
DomGilberto: But I know what a gas key is... - that's more simple than coding...

But would you be trying to drive without knowing what it was? That would be moronic.

You are trying to code without knowing what "--" and "!" are. What's the difference?

 

!= True, if x is not equal to y - (DIR != 0) - If DIR (which is 0) is not equal to "0" then the Break operator will return control to the for operator. And start the loop again.

-- Subtraction of 1 from the value of the variable. So "iBar--;" = iBar = 0, so subtracting 1 = -1?

 

Hey - I'm really trying here. I'm sorry if I don't learn as quick as what some people do (and don't you think I'd rather be that way! I would much rather learn it by myself and get it, without having to ask for someone' help!) but I am trying my best. I may not be the brightest spark in the world... So forgive me for that at least... 

 

 
DomGilberto:

!= True, if x is not equal to y - (DIR != 0) - If DIR (which is 0) is not equal to "0" then the Break operator will return control to the for operator. And start the loop again.

-- Subtraction of 1 from the value of the variable. So "iBar--;" = iBar = 0, so subtracting 1 = -1?

 

Hey - I'm really trying here. I'm sorry if I don't learn as quick as what some people do (and don't you think I'd rather be that way! I would much rather learn it by myself and get it, without having to ask for someone' help!) but I am trying my best. I may not be the brightest spark in the world... So forgive me for that at least... 

 


In this loop

for(int iBar = 0; iBar < Bar; iBar++){//....do some stuff and come back}

iBar has here different values as long it is smaller as Bar it will become value 1 higher

iBar--;

 means....

iBar = iBar - 1;
 
int DIR = 0;
for(int iBar = 0; iBar < Bars; iBar++)
   {
   double i1  = iMA(NULL,60,3,0,1,0,1); 
   double i2  = iMA(NULL,60,5,0,1,0,1);
   double i3  = iMA(NULL,60,8,0,1,0,1);
   double i4  = iMA(NULL,60,10,0,1,0,1);  
   double i5  = iMA(NULL,60,12,0,1,0,1);
   double i6  = iMA(NULL,60,15,0,1,0,1);  
   double i7  = iMA(NULL,60,30,0,1,0,1);
   double i8  = iMA(NULL,60,35,0,1,0,1);
   double i9  = iMA(NULL,60,40,0,1,0,1);
   double i10 = iMA(NULL,60,45,0,1,0,1);
   double i11 = iMA(NULL,60,50,0,1,0,1);
   double i12 = iMA(NULL,60,60,0,1,0,1); 
   double ema21 = iMA(NULL,60,21,0,1,0,1);

   if(i1<i2 && i2<i3 && i3<i4 && i4<i5 && i5<i6 && i6<i7 && i7<i8 && i8<i9 && i9<i10 && i10<i11 && i11<i12) DIR = -1;
      triggerBarTime = Time[1];
      if(Low[1]<ema21)
      OrderEntry(0);
  else if(i1>i2 && i2>i3 && i3>i4 && i4>i5 && i5>i6 && i6>i7 && i7>i8 && i8>i9 && i9>i10 && i10>i11 && i11>i12) DIR = +1; // All fanning up.
      triggerBarTime1 = Time[1];
      if(High[1]>ema21)
      OrderEntry(1);
   
   if (DIR != 0)
   { 
   iBar--; break; 
   }       // Last one was cross.

Ah - I think I got it working now - Just staring at Bar for a while and then realised :) - I am starting to realise that what I am trying to code is slightly more complex than I first thought. I need the MA's to turn and fan up or down (depending if its a buy or sell). If its fanned up (for a long) They dont always needs to be in the right order - it's just a case of first identifying when they're all stacked up and fanned apart in the right order - from this point all I am wanting is price to check the 21 ema and then place an order above the highs. I'll have a play around of course! NOT expecting anyone to write me the code - just merely writing my thoughts aloud.

 Thank you! 

 
Fix your indenting or missing braces
Posted code
   if(i1<i2 && i2<i3 && i3<i4 && i4<i5 && i5<i6 && i6<i7 && 
      i7<i8 && i8<i9 && i9<i10 && i10<i11 && i11<i12) DIR = -1;
      triggerBarTime = Time[1];
      if(Low[1]<ema21)
      OrderEntry(0);
  else if(i1>i2 && i2>i3 && i3>i4 && i4>i5 && i5>i6 && i6>i7 && 
          i7>i8 && i8>i9 && i9>i10 && i10>i11 && i11>i12) DIR = +1; // All fanning up.
      triggerBarTime1 = Time[1];
      if(High[1]>ema21)
      OrderEntry(1);
Properly indented
   if(i1<i2 && i2<i3 && i3<i4 && i4<i5 && i5<i6 && i6<i7 && 
      i7<i8 && i8<i9 && i9<i10 && i10<i11 && i11<i12) DIR = -1;
   triggerBarTime = Time[1];
   if(Low[1]<ema21) OrderEntry(0);
   else if(i1>i2 && i2>i3 && i3>i4 && i4>i5 && i5>i6 && i6>i7 && 
          i7>i8 && i8>i9 && i9>i10 && i10>i11 && i11>i12) DIR = +1; // All fanning up.
   triggerBarTime1 = Time[1];
   if(High[1]>ema21)  OrderEntry(1);
Properly braced
   if(i1<i2 && i2<i3 && i3<i4 && i4<i5 && i5<i6 && i6<i7 && i7<i8 && 
      i8<i9 && i9<i10 && i10<i11 && i11<i12){
      DIR = -1;
      triggerBarTime = Time[1];
      if(Low[1]<ema21) OrderEntry(0);
   }
   else if(i1>i2 && i2>i3 && i3>i4 && i4>i5 && i5>i6 && i6>i7 && i7>i8 && 
           i8>i9 && i9>i10 && i10>i11 && i11>i12){
      DIR = +1; // All fanning up.
      triggerBarTime1 = Time[1];
      if(High[1]>ema21)  OrderEntry(1);
   }