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

 

Hey All!

 Thanks for taking time out to help me :)

I have written a tonne of code (A "tonne" in my terms - Been learning MQL4 through trial and error and experimenting.) and am currently stuck on writing one part...

 

I have trailing stops and moving average cross overs. I want the moving averages to cross up (12 of them) BUT not place a LONG position as soon as this happens, I want there to be code that counts 8 bars (Or what ever I define in the global ext int settings) from this cross over (so price is now above all the MA's) and then after its determined if there are 8, it will place an order above the highs...

This order should be cancelled if the MA's turn back down...

 

I have made this very short video here to hopefully help explain the situation > http://screencast.com/t/G6hOqqvjoNY

Will be incredibly grateful for anyone' help :)  - I have attached the code below - As I've mentioned in video, its all experimenting and pretty straight forward stuff, hence why I've greyed out areas as I go along.

 

Thanks! 

Files:
 
DomGilberto: I want the moving averages to cross up (12 of them) BUT not place a LONG position as soon as this happens, I want there to be code that counts 8 bars 
So remember the Times they crossed Up and Down. iBarShift gives the bar count. Wait until the smallest reaches 8.
 

Hey WHRoeder!

Ah, I think I see. Thing is, I am not used to "Time" or iBarShift function?  

 

If I have labelled the MA cross over for a long trade "H1_Fish_long", would I be using that in conjunction with "Time" to tell me what the time was at that specific point, and then clip on iBarShift to count the bars along?

 As I said, I am new to MQL4 and am currently experiencing a very steep learning curve. I want to learn it myself though of course, I'm not looking to free load off of someone else to get them to write it for me :)

 

Thanks! 

 

Also, would "iHighest" not work with determining the number of bars back from the first in the array? Which I could specify with Time?

 

Thoughts? 

 
DomGilberto:

Also, would "iHighest" not work with determining the number of bars back from the first in the array? Which I could specify with Time?

 

Thoughts? 

Did you read the documentation for iHighest() ?  it doesn't take datetime parameters it takes bar numbers.
 

Sorry I am getting ahead of myself here.

 I will eventually need iHighest() for long positions once I can figure out how to count 8 bars from when the MA's turn up. iHighest will be used to determine what the highest bar was within the 8 bar range, then my order will be placed above this level.

I've been looking at this for past 1 1/2 hours now, and I am finding it hard to get a time from when the MA's cross up (for now I will always talk about long trades. As its easy once I have this to reverse it) and then have a function to count the select number of bars (in this case, 8) before the order is placed above the 8 bars ranges high...

 Thanks for your time! 

 

Any feedback will be appreciated. 

 
DomGilberto:

Sorry I am getting ahead of myself here.

 I will eventually need iHighest() for long positions once I can figure out how to count 8 bars from when the MA's turn up. iHighest will be used to determine what the highest bar was within the 8 bar range, then my order will be placed above this level.

I've been looking at this for past 1 1/2 hours now, and I am finding it hard to get a time from when the MA's cross up (for now I will always talk about long trades. As its easy once I have this to reverse it) and then have a function to count the select number of bars (in this case, 8) before the order is placed above the 8 bars ranges high...

OK,  iHighest() returns the bar number,  then using that bar number you use High[] or iHigh() to get the bar high price.

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. 

 

Just watched your video . . . .  why wait for 8 bars ?  just get the MAs from 8 bars ago and see if you have a Buy/Sell condition ? 

By the way,  change your new candle check to use time,  Bars is not reliable.   And read this thread and check your trading function return values so you can report on any errors:  What are Function return values ? How do I use them ?

 

Nice one RaptorUK thank you!

This is all becoming far too confusing for me at the moment. Really stuck!

 You're so right. I just can't get this down on paper to communicate with all the functions and what I want :s...

I've thrown together another 1 minute video, to make this more simple. I appreciate everyone' time :) 

Video: http://screencast.com/t/IbPuDmiC 

 

Thanks for the thread, very helpful! 

 

Remember when the ma's cross. use iBarShift to find out bar number

test if price touched the blue line since the iBarShift (A loop)

if yes, find the highest bar since iBarShift (not including bar zero) iHighest

if price has gone above that high - open the order.

 

WHRoeder - Thank you!

That's what I am stuck on doing?

 

 

double PreviousSmallFish1  =  iMA(NULL,60,3,0,1,0,2); 

   double CurrentSmallFish1   =  iMA(NULL,60,3,0,1,0,1);

   double PreviousSmallFish2  =  iMA(NULL,60,5,0,1,0,2);

   double CurrentSmallFish2   =  iMA(NULL,60,5,0,1,0,1);

   double PreviousSmallFish3  =  iMA(NULL,60,8,0,1,0,2);

   double CurrentSmallFish3   =  iMA(NULL,60,8,0,1,0,1);

   double PreviousSmallFish4  =  iMA(NULL,60,10,0,1,0,2);

   double CurrentSmallFish4   =  iMA(NULL,60,10,0,1,0,1);  

   double PreviousSmallFish5  =  iMA(NULL,60,12,0,1,0,2);

   double CurrentSmallFish5   =  iMA(NULL,60,12,0,1,0,1);

   double PreviousSmallFish6  =  iMA(NULL,60,15,0,1,0,2);

   double CurrentSmallFish6   =  iMA(NULL,60,15,0,1,0,1);  

   

      double PreviousBigFish1  =  iMA(NULL,60,30,0,1,0,2); 

      double CurrentBigFish1   =  iMA(NULL,60,30,0,1,0,1);

      double PreviousBigFish2  =  iMA(NULL,60,35,0,1,0,2);

      double CurrentBigFish2   =  iMA(NULL,60,35,0,1,0,1);

      double PreviousBigFish3  =  iMA(NULL,60,40,0,1,0,2);

      double CurrentBigFish3   =  iMA(NULL,60,40,0,1,0,1);

      double PreviousBigFish4  =  iMA(NULL,60,45,0,1,0,2);

      double CurrentBigFish4   =  iMA(NULL,60,45,0,1,0,1);

      double PreviousBigFish5  =  iMA(NULL,60,50,0,1,0,2);

      double CurrentBigFish5   =  iMA(NULL,60,50,0,1,0,1);

      double PreviousBigFish6  =  iMA(NULL,60,60,0,1,0,2);

      double CurrentBigFish6   =  iMA(NULL,60,60,0,1,0,1); 

      

            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 I put "Orderentry(0);" here, the EA works and will buy a long position at the cross of all the MA's.

                

     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); //If I put "Orderentry(1);" here, the EA works and will sell a short position at the cross of all the MA's. 

 

EDIT - Ok. I dont know how to do that? I want to know how to write the code to find the oldest bar when the MA's have all crossed in the right direction? 

 
DomGilbertoThat's what I am stuck on doing?


  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Stop posting a question about you being stuck. We have no idea whether you're stuck or not. Especially when you don't post code.
  3. All you posted was your attempt at a cross. That won't work. Not all moving averages will cross at the same time. In any case you don't care about when. Find the oldest bar that has all MAs in the right order. - that is step one of my previous post. Then do the remaining items.