Waiting for set number of bars before placing an order? Please help? Video Inside. - page 2

 

I have posted the .mq4 file in my first post.

 I have edited the post above. Sorry for repeating myself.

 

"Find the oldest bar that has all MAs in the right order. - that is step one of my previous post." 

 I want to know HOW to write that code. I don't know how to write the code to find the bar AT the MA cross in conjunction with the code I have pasted above? After all, those "if" lines are correct for finding a cross of the Moving Averages, but are not necessarily correct based upon what RaptorUK said above:

 

"MAs don't always cross on a bar so it's not enough to check for when MA values are equal . . . you need to determine that one was above the other at one bar and then for the following bar they have reversed positions.  Of course they may also change in line with a bar . . .  so ideally you should check in 3 bar blocks to find both possibilities."

 

Again, I do not know how to do this either.

 Sorry if I've not made myself clear! 

 
DomGilberto:

I have posted the .mq4 file in my first post.

 I have edited the post above. Sorry for repeating myself.

 

"Find the oldest bar that has all MAs in the right order. - that is step one of my previous post." 

 I want to know HOW to write that code. I don't know how to write the code to find the bar AT the MA cross in conjunction with the code I have pasted above? After all, those "if" lines are correct for finding a cross of the Moving Averages, but are not necessarily correct based upon what RaptorUK said above:

How would you do it manually ?  figure that out,  write it down if you need to,  then you will be able to figure out how to code it . . .
 

I have tried that RaptorUK...

I'd really just appreciate some guidance from someone? (after-all, that is why I am on this forum... for help?) If I am still being vague, I will happily elaborate? I wouldn't ask for help if I were able to translate what I do manually into code?

 

All I am wanting is:

 MA's crossed up (which I have at the moment) and then price to come back down to touch a 21 ema and then the order is placed above the high of all the bars from the touch of the 21 ema and the first bar of when the MA's first turned up? The order would then be removed if price then closed beneath the 60 EMA... Rinsed and repeated until a new trigger? 

   if(PreviousBigFish1<CurrentBigFish2 && CurrentBigFish1>PreviousBigFish2)
               if(PreviousBigFish2<CurrentBigFish3 && CurrentBigFish2>PreviousBigFish3)
                  if(PreviousBigFish3<CurrentBigFish4 && CurrentBigFish3>PreviousBigFish4)
                     if(PreviousBigFish4<CurrentBigFish5 && CurrentBigFish4>PreviousBigFish5)
                        if(PreviousBigFish5<CurrentBigFish6 && CurrentBigFish5>PreviousBigFish6)  
                           
                           if(PreviousSmallFish1<CurrentSmallFish2 && CurrentSmallFish1>PreviousSmallFish2)
                        if(PreviousSmallFish2<CurrentSmallFish3 && CurrentSmallFish2>PreviousSmallFish3)
                     if(PreviousSmallFish3<CurrentSmallFish4 && CurrentSmallFish3>PreviousSmallFish4)
                  if(PreviousSmallFish4<CurrentSmallFish5 && CurrentSmallFish4>PreviousSmallFish5)
                if(PreviousSmallFish5<CurrentSmallFish6 && CurrentSmallFish5>PreviousSmallFish6)LongSignal(0);
                
     if(PreviousBigFish1>CurrentBigFish2 && CurrentBigFish1<PreviousBigFish2)
      if(PreviousBigFish2>CurrentBigFish3 && CurrentBigFish2<PreviousBigFish3)
         if(PreviousBigFish3>CurrentBigFish4 && CurrentBigFish3<PreviousBigFish4)
            if(PreviousBigFish4>CurrentBigFish5 && CurrentBigFish4<PreviousBigFish5)
               if(PreviousBigFish5>CurrentBigFish6 && CurrentBigFish5<PreviousBigFish6)
               
               if(PreviousSmallFish1>CurrentSmallFish2 && CurrentSmallFish1<PreviousSmallFish2)
            if(PreviousSmallFish2>CurrentSmallFish3 && CurrentSmallFish2<PreviousSmallFish3)
         if(PreviousSmallFish3>CurrentSmallFish4 && CurrentSmallFish3<PreviousSmallFish4)
       if(PreviousSmallFish4>CurrentSmallFish5 && CurrentSmallFish4<PreviousSmallFish5)
     if(PreviousSmallFish5>CurrentSmallFish6 && CurrentSmallFish5<PreviousSmallFish6)ShortSignal(1);
   
   
}
  
void LongSignal (int Direction)
{
   if (Direction==0)
      {
      int LongSignal = iBarShift(NULL,60,LongSignal,0);
      int MA_Cross_Long = iHighest(NULL,60,2,MA_Cross_Long,1);
      OrderEntry(0);
      }   
   
}   
void ShortSignal (int Direction)   
{   
   if (Direction==1)
      {
      int ShortSignal = iBarShift(NULL,60,ShortSignal,0);
      int MA_Cross_Short = iLowest(NULL,60,2,MA_Cross_Short,1);
      OrderEntry(1);
      }   			// This code here is clearly wrong, as it simply just spits out buys and sells on the cross of the Moving Average?

 
DomGilberto:

I have tried that RaptorUK...

I'd really just appreciate some guidance from someone? (after-all, that is why I am on this forum... for help?) If I am still being vague, I will happily elaborate? I wouldn't ask for help if I were able to translate what I do manually into code?

 

All I am wanting is:

 MA's crossed up (which I have at the moment) and then price to come back down to touch a 21 ema and then the order is placed above the high of all the bars from the touch of the 21 ema and the first bar of when the MA's first turned up? The order would then be removed if price then closed beneath the 60 EMA... Rinsed and repeated until a new trigger? 

OK,  but if you want someone to code for you then you have this option:  Jobs

If you want help then you are in the right place . . .

I'm not the best person to give help, I'm happy to try but I don't work with Technical Indicators, I've never coded anything for myself using a MA. You talked about the MA crosses happening 8 bars ago . . .  so check if the MAs crossed 8 bars ago . . . how ?  well check the values of the MAs 9, 8 and 7 bars ago and find if they crossed, how ?  well if the value for the H1 MA30 for bar 9 > the value for the H1 MA35 for bar 9  and the value for the H1 MA30 for bar 7 < the value for the H1 MA35 for bar 7 . . .  well then they have crossed somewhere around bar 8 . . .  yes ?   If they did then check if price has since come back down to "touch" a 21 ema . . . ( define touch ?  is a penetration also a touch ? )

 
in the right order would be
for (i=0; i < Bars; i++){
   double i1 = iMA(... ,i),
          i2 = iMA(... ,i),
          :
   if (i1 > i2) if (i2 > i3) ... continue; // ok
   break;
}
return(i-1);
 
Stop comparing to the previous, you're not looking for all to cross at one bar.
 

Spot on WHRoeder!

 Thank you very much. I was working with something completely different.

 

Just out of interest, the "return" at the end, what's the purpose of this "function"? (As I said I am learning this as I am going along!)

 

RaptorUK, I was under the impression I could ask for small bits of help on areas I'd get stuck from someone who may not find it much to help, and too little a task to charge? I wasnt expecting anyone to write me the entire code, it was just that one small part above^

Appreciate everyones help! 

 
DomGilberto: the "return" at the end, what's the purpose of this "function"?
What I previously posted:
WHRoeder: Find the oldest bar that has all MAs in the right order. - that is step one
 

Last thing I promise :)

 Is this right? I sat and stared at your code above to understand it's logic, and It gave me great satisfaction once I figured out its purpose and how it made sense! (remember, I am knew to all this. I do intend on learning MQL4 through-out!)

 

Thanks for your time and patience. It's greatly appreciated, and I feel i've actually learnt a great deal today! (thanks to your time!) 

void CheckForMaTrade()
{

   for (i=0; i < Bars; i++) // Whats the ++ do?
   {
   double i1  = iMA(NULL,60,3,0,1,0,i); 
   double i2  = iMA(NULL,60,5,0,1,0,i);
   double i3  = iMA(NULL,60,8,0,1,0,i);
   double i4  = iMA(NULL,60,10,0,1,0,i);  
   double i5  = iMA(NULL,60,12,0,1,0,i);
   double i6  = iMA(NULL,60,15,0,1,0,i);  
   double i7  = iMA(NULL,60,30,0,1,0,i);
   double i8  = iMA(NULL,60,35,0,1,0,i);
   double i9  = iMA(NULL,60,40,0,1,0,i);
   double i10 = iMA(NULL,60,45,0,1,0,i);
   double i11 = iMA(NULL,60,50,0,1,0,i);
   double i12 = iMA(NULL,60,60,0,1,0,i); 
      
       if(i1>i2)
        if(i2>i3)
          if(i3>i4)
            if(i4>i5)
              if(i5>i6)
                if(i6>i7)
                  if(i7>i8)
                    if(i8>i9)
                      if(i9>i10)
                        if(i10>i11)
                          if(i11>i12)continue; // Not sure about the continue? Or was that for me?
                           break;
      }
      return(i-1);
 }
 
DomGilberto:

 I want the moving averages to cross up (12 of them) BUT not place a LONG position as soon as this happens,

So this isn't what you want any more ?
 

No that is what I want? I dont want to buy on the cross of the 12 EMA's. I already had that coded (first step of learning the ropes and getting something down.)

 What I want is price to be above all the EMA's (12 of them) and then price to pull back to a pre-defined EMA and then an order gets placed above the highs. The order then cancels should price close beneath the 60 ema (which in the case of a long would be the furthest EMA from price.)

 

Why? Am I missing something? 

Reason: