Finding Highest High Of Bull Candles Only

 
//+------------------------------------------------------------------+
//|                              Highest High Bullish Candles.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
int candleStart=5;
int candleEnd=10;
ENUM_TIMEFRAMES timeframe = PERIOD_M15;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
double c_candle[];
ArrayResize(c_candle,candleEnd);

for(int i=candleStart;i<candleEnd;i++)
  {
   if( iOpen(Symbol(),timeframe,i) < iClose(Symbol(),timeframe,i) )
   c_candle[i]=iHigh(Symbol(),timeframe,i);
//Print("candle "+(i+1)+" :cC = "+c_candle[i]);
  }
int cC=ArrayMaximum(c_candle,0,WHOLE_ARRAY);
Print("candle "+(cC+1)+": is Highest Bull Candle = "+(string)c_candle[cC]);
   
  }
//+------------------------------------------------------------------+
I have tried using the above code to find the highest high of Bull candles in a range i set. Unfortunately the bull condition put in telling to only store the high vales of bull candles.  What is a better way to do it 
 
In the above code, the problem I have is that it is not saving the Bull candles only but it is including the bears. I then came up with the below. 
//+------------------------------------------------------------------+
 double bullhighest(ENUM_TIMEFRAMES timeframe, int startBar, int endBar)
 {//double bullhighest()
 double dex1=0,dex2=0,dex3=0,dex4=0,dex5=0,dex6=0,dex7=0,dex8=0,dex9=0,dex10=0,
 dex11=0,dex12=0,dex13=0,dex14=0,dex15=0;
   int bar1 = FindBull(timeframe,startBar,endBar);
   int bar2 = FindBull(timeframe,bar1+1,endBar);
   int bar3 = FindBull(timeframe,bar2+1,endBar);
   int bar4 = FindBull(timeframe,bar3+1,endBar);
   int bar5 = FindBull(timeframe,bar4+1,endBar);
   int bar6 = FindBull(timeframe,bar5+1,endBar);
   int bar7 = FindBull(timeframe,bar6+1,endBar);
   int bar8 = FindBull(timeframe,bar7+1,endBar);
   int bar9 = FindBull(timeframe,bar8+1,endBar);
   int bar10 = FindBull(timeframe,bar9+1,endBar);
   int bar11 = FindBull(timeframe,bar10+1,endBar);
   int bar12 = FindBull(timeframe,bar11+1,endBar);
   int bar13 = FindBull(timeframe,bar12+1,endBar);
   int bar14 = FindBull(timeframe,bar13+1,endBar);
   int bar15 = FindBull(timeframe,bar14+1,endBar);
  if(bullcount(timeframe,startBar,endBar) >= 1) dex1 = iHigh(Symbol(),timeframe,bar1) ;
  if(bullcount(timeframe,startBar,endBar) >= 2) dex2 = iHigh(Symbol(),timeframe,bar2) ;
  if(bullcount(timeframe,startBar,endBar) >= 3) dex3 = iHigh(Symbol(),timeframe,bar3) ;
  if(bullcount(timeframe,startBar,endBar) >= 4) dex4 = iHigh(Symbol(),timeframe,bar4) ;
  if(bullcount(timeframe,startBar,endBar) >= 5) dex5 = iHigh(Symbol(),timeframe,bar5) ;
  if(bullcount(timeframe,startBar,endBar) >= 6) dex6 = iHigh(Symbol(),timeframe,bar6) ;
  if(bullcount(timeframe,startBar,endBar) >= 7) dex7 = iHigh(Symbol(),timeframe,bar7) ;
  if(bullcount(timeframe,startBar,endBar) >= 8) dex8 = iHigh(Symbol(),timeframe,bar8) ;
  if(bullcount(timeframe,startBar,endBar) >= 9) dex9 = iHigh(Symbol(),timeframe,bar9) ;
  if(bullcount(timeframe,startBar,endBar) >= 10) dex10 = iHigh(Symbol(),timeframe,bar10) ;
  if(bullcount(timeframe,startBar,endBar) >= 11) dex11 = iHigh(Symbol(),timeframe,bar11) ;
  if(bullcount(timeframe,startBar,endBar) >= 12) dex12 = iHigh(Symbol(),timeframe,bar12) ;
  if(bullcount(timeframe,startBar,endBar) >= 13) dex13 = iHigh(Symbol(),timeframe,bar13) ;
  if(bullcount(timeframe,startBar,endBar) >= 14) dex14 = iHigh(Symbol(),timeframe,bar14) ;
  if(bullcount(timeframe,startBar,endBar) == 15) dex15 = iHigh(Symbol(),timeframe,bar15) ;
  double max= NormalizeDouble(MathMax(dex1,MathMax(dex2,MathMax(dex3,MathMax(dex4,MathMax(dex5,
  MathMax(dex6,MathMax(dex7,MathMax(dex8,MathMax(dex9,MathMax(dex10,MathMax(dex11,MathMax(dex12,
  MathMax(dex13,MathMax(dex14,dex15))))))))))))))    
  , _Digits) ;
return(max);
 }//double bullhighest()

 But this code is long way round of getting my result. I would need help generalizing it. Please note the problem I have.

Calculate Bar1 example answer is 5 .  Bar2 must do its calculation from answer gotten in Bar1. A for loop is not working because when the first count is done which Bar1 it will get answer is 5 but when the count goes to 2 and Bar2 needs to be calculated it will give the same answer as Bar1 because the answer from Bar1 was not saved. I have tried saving in array but there is compile error of array is out.  

 

Hi .Correct me if i understood wrong.

You want to get the highest price in a region of the chart that has the maximum # of bull candles , scanning right to left , until you hit a limit of bars searched total ?

So with a limit of 200 bars you would like to find the highest price in a region of 5 bars that has the most bullish candles and if there are regions with equal # of bull candles you take the highest price ?  

 
Bwalya Tambule:
I have tried using the above code to find the highest high of Bull candles in a range i set. Unfortunately the bull condition put in telling to only store the high vales of bull candles.  What is a better way to do it 

I am not entirely sure what you are trying to do.

I have no idea what "Unfortunately the bull condition put in telling to only store the high vales of bull candles." means.

However, I think that this code should give you a base.

//+------------------------------------------------------------------+
//|                                        Highest_Bull_In_Range.mq5 |
//|                                                    Keith Watford |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Keith Watford"
#property link      ""
#property version   "1.00"
int candleStart=5;
int candleEnd=10;
ENUM_TIMEFRAMES timeframe = PERIOD_M15;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   int bars_to_copy=(candleEnd-candleStart)+1;
   int bar_index=0;
   double highest_high=0;
   int copied=CopyRates(Symbol(),timeframe,candleStart,bars_to_copy,rates);
   if(copied>0)
      {
      for(int x=0; x<copied; x++)
         {
         if(rates[x].close>rates[x].open) //Is bullish
            {
            if(rates[x].high>highest_high)
               {
               bar_index=x+candleStart;
               highest_high=rates[x].high;
               }
            }
         }
      }
   else
      Print("Failed to get history data for the symbol ",Symbol());

   Print("Highest Bull Candle High is ",DoubleToString(highest_high,_Digits), " at bar index ",bar_index);


}
//+------------------------------------------------------------------+

Not tested, just quickly typed so there may be errors.

 
Bwalya Tambule What is a better way to do it 
Simplify your code
double HH = DBL_MIN;
for(int i=candleStart;i<candleEnd;i++)
   if( iOpen(_Symbol,timeframe,i) < iClose(_Symbol,timeframe,i) )
      HH=MathMax( HH, iHigh(_Symbol,timeframe,i) );
 

Bull Candles Please

Here is a visual explanation using the above image. The code wants to find the highest high of the bull candles (green candles) in the blue box. It will start from Bar 22 , Place marked start and end at Bar 33. So the candles in between Bar 22 and 33. Find each bull candle save the bar number and high price. Thereafter find the highest high of the bull candles. Hope this is clear

 
Bwalya Tambule #: Hope this is clear

You've had code that did that. And you have already been given at least three other variations. Why are you still posting?

 
Keith Watford #:

I am not entirely sure what you are trying to do.

I have no idea what "Unfortunately the bull condition put in telling to only store the high vales of bull candles." means.

However, I think that this code should give you a base.

Not tested, just quickly typed so there may be errors.

perfect

Reason: