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

 

Hey All!

I was hoping for some help on my code - Currently stuck on this one damn part!


This is the process I am attempting to write into code below:

1) If all MA's are "fanned" out and crossed so that price is above all the moving averages and they are indicating a long potential.

2) I am then wanting to know the exact bar (using Time) that the moving averages are "fanned apart" at the cross of the moving averages.

3) If 1 and 2 are true, I then wait for any bar to come back down (in the case for a long position) and touch the 21 EMA.

4) From the first Bar (where the MA's crossed and fanned out) to the bar that has now touched the 21 EMA - I want a pending order OR at the market trade placed ABOVE the highs of this range (1 & 3).

5) If price at ANY point CLOSES beneath the 60 EMA, BEFORE triggering the order - then the order is to be cancelled. 

 

 This is obviously not all the code, but this is the main part that I am pulling my hair out with. Please check the code with the "<<<<<<<<<<<<<". These are the points I am confused about.

 I could REALLY do with someone' help - it would really be appreciated!

 

Many thanks in advance! 

datetime          triggerBarTime;   <<<<< Global settings.
datetime          triggerBarTime1;  <<<<< Global settings.	



void CheckForMaTrade()
{

//+-----------------------------------------------------------------------------------------------------------------------+ 
    //Small and Big Fish on the H1 Time Frame.                                                                           |
//+-----------------------------------------------------------------------------------------------------------------------+              

   {
   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)triggerBarTime = Time[1];  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
      
      double ema21 = iMA(NULL,60,21,0,1,0,1); 
       
        
         if (Low[1]<ema21)
         {
         
           OrderEntry(0); // BUY 
            }     
      
      
                
     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)triggerBarTime = Time[1]; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   
    double ema21_2 = iMA(NULL,60,21,0,1,0,1); 
  
            if(High[1]>ema21_2)
            {
             
              OrderEntry(1); // SELL
             }  
   
       }

}


//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
//| Order Enter Function                                             |
//+------------------------------------------------------------------+

void OrderEntry (int direction)
{    
   double LotSize=0;
   double Equity=AccountEquity();
   double RiskedAmount=Equity*RiskPercent*0.01;
   int buyStopCandle = iLowest(NULL,0,1,CandlesBack,1); 
   int sellStopCandle = iHighest(NULL,0,2,CandlesBack,1);
   
   double buyPrice = iHighest(Symbol(), 0, MODE_HIGH, iBarShift(Symbol(), 0, triggerBarTime, true), 0);    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
   double sellPrice = iLowest(Symbol(), 0, MODE_LOW, iBarShift(Symbol(), 0, triggerBarTime1, true), 0);   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   
   double buy_stop_price = iMA(NULL,60,60,0,1,0,1)-PadAmount*pips; 
   double pips_to_bsl = Ask-buy_stop_price;         //double pips_to_bsl = Ask-buy_stop_price;   --- Old code
   double buy_takeprofit_price = Ask+pips_to_bsl*RewardRatio;   
   double sell_stop_price = iMA(NULL,60,60,0,1,0,1)+PadAmount*pips; //- Greyed out, this is an old code line.
   double pips_to_ssl = sell_stop_price-Bid;     ////double pips_to_ssl = sell_stop_price-Bid;   --- Old code
   double sell_takeprofit_price = Bid-pips_to_ssl*RewardRatio;   
   
  
   if (direction==0)//<< "0" pushes out a Buy direction
   {
      //double buyPrice = iHighest(Symbol(), 60, MODE_HIGH, iBarShift(Symbol(), 0, triggerBarTime, true), 0);//-PadAmount*pips; // new line
      int Buy_Price = buyPrice;
      
      double bsl=buy_stop_price;
      double btp=buy_takeprofit_price;
      //LotSize = (100/(0.00500/0.00010))/10
      LotSize = (RiskedAmount/(pips_to_bsl/pips))/10;
      if(OpenOrdersThisPair(Symbol())==0) //--- This means that it will not open a new order if there is one already open!If greyed out, it will take everything.
      int buyticket = OrderSend(Symbol(),Buy_Price,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green); //ask
      if(buyticket>0)OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,CLR_NONE);
   }
            
   if (direction==1) //<< "1" pushes out a Sell direction
   {
      
      //double sellPrice = iLowest(Symbol(), 60, MODE_LOW, iBarShift(Symbol(), 0, triggerBarTime1, true), 0);//+PadAmount*pips;   // new line.
      int Sell_Price = sellPrice;
      
      double ssl=sell_stop_price;
      double stp=sell_takeprofit_price; 
      //LotSize = (100/(0.00500/0.00010))/10;
      LotSize = (RiskedAmount/(pips_to_ssl/pips))/10;    
      if(OpenOrdersThisPair(Symbol())==0)  //--- This means that it will not open a new order if there is one already open! If greyed out, it will take everything.
      int sellticket = OrderSend(Symbol(),Sell_Price,LotSize,Bid,3,ssl,stp,NULL,MagicNumber,0,Red); //bid
      if(sellticket>0)OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,CLR_NONE);
   }
   
}

return(0);

 
DomGilberto:

Hey All!

 This is obviously not all the code, but this is the main part that I am pulling my hair out with. Please check the code with the "<<<<<<<<<<<<<". These are the points I am confused about.

Please read the documentation for the functions you are using . . .  iHighest() does not return a price . . . it returns an int   "Returns the shift of the maximum value over a specific number of periods depending on type."
 

Thanks RaptorUK - In the kindest way possible, that doesn't explain anything to me? In layman's terms?

Is this a forum where someone can actually help me other than repeating the definitions of functions (in which I have read 100's of times from the "help" section...)

 After-all, I don't feel what I am asking (In comparison to some of the threads on here!) is enough to warrant someone to charge for their time...?

 

Translating to layman's terms will help you... once, today, with this particular issue.. and take some amount of time for the explainer.

If you say which ones of the definition you don't understand, maybe next time you can solve your own problems, and after a while, you can be one of the ones solving other peoples problems. 

 

If you try and investigate the problem yourself, you will also learn a lot more. For example, try this:

Alert(iHighest(Symbol(), 0, MODE_HIGH, iBarShift(Symbol(), 0, triggerBarTime, true), 0));

and you will see what value is being returned by the function... (like Raptor says, it's not a price).. and you will probably more easily understand the definition of the function.

Things like this will help you more than hoping someone on the board has time to test out your code and do the same troubleshooting that you should be learning how to do for yourself.

 

Huh? I am so confused - Am I asking too much for some help?

 After-all, I have built my entire EA from the ground up (which I have taught myself...) Just wanting some guidance from someone who has more knowledge than me... :s

 

"Things like this will help you more than hoping someone on the board has time to test out your code and do the same troubleshooting that you should be learning how to do for yourself." 

 What the hell... I was not expecting that from anyone!?

Your post has shed more light on the situation - Appreciate it.

 

This forum is really funny... I was not planning on free-loading off of anyone! I simply wanted some clarification as to how I correctly identify the bar that crosses with the EMA's...

 Jeez.... 

 

when I read raptor's answer it is telling you the exact problem you have made.

prices are stored as double variables e.g. 1.12345

the iHighest functions returns an integer (int) e.g. 1  or 2 or -100 .... clearly not a price.

I don't know what you're trying to achieve but the iHighest function is probably not the one you want to be using.

You would have discovered this for yourself if in metaeditor, you click on iHighest in your code, then press F1... you will see that it returns an integer (which Raptor also told you)

 

I don't know the source of your confusing. If you don't know the difference between int and double variables you are missing some vital basic knowledge, meaning you are trying to run before you can walk, and you want someone else to carry you :/

 

EDIT: You weren't expecting that you have to do your own troubleshooting before asking someone else?!?

 

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.
}
 

I understand the difference between int and double...

after the If statements are true, I am wanting to find the bar that crossed at the point where the MA's are spaced apart. I thought if I used:

"triggerBarTime = Time[1];" - after the "If" statements then triggerBarTime could be used in conjunction with iBarShift to lock in the first bar of the range I am interested in?

 

Then, using the iHighest, would allow me to convert this info into an int and use it into my order function?  (Buy_Price)?

 

You may understand the difference between int and double but you don't understand the difference between a bar number (int) and a price (double).

iHighest would allow you to convert you datetime into a bar number (int.) But OrderSend only takes a price (a double) it makes no sense to open an order at 9

 

Ah! Gotch-ya!

 Thank you very much :) I really do appreciate it!

Thanks for being patient with me - I didn't mean to come across in a way to expect anyone to do anything for me; just giving me some clarification on the help (pressing F1 on everything isnt always that clear to me.) is all I wanted.