# How to write a function to get the highest/lowest value of indicator values?

I am am looking for a function that will be able to return to me the highest/lowest value of indicator values,

iHighest returns shift of the highest bar
I want a function like that but doesnt use bars but indicator value;

``````//---
int Indicator_Lowest(int count=end,int Start=begin) //to return the shift of the lowest indicator price
{
int shift=EMPTY_VALUE,tempshift=EMPTY_VALUE;
double value=EMPTY_VALUE,tempvalue=EMPTY_VALUE;
for(int i=Start; i==Start+count;i++ )
{
tempvalue =indicator(i);

if(value<tempvalue ||tempvalue==EMPTY_VALUE)
{
value=tempvalue ;
shift=i;
}
//if(shift==Start+count)
//Start+=count-1;

}
return shift;
}
``````

i tried this not sure if it works

Pseudo-code

``````   // The engine
highest = -1;            // set to small value
lowest = 10000000;       // set to large value
for(int i=index; i< lookback+index; i++)
{
highest = MathMax(highest, indicatorValue(i));
lowest = MathMin(lowest, indicatorValue(i));
}``````

Or put the values in an array and use Array Functions

I hope to get the Shift of the bar at that current level,
like the iHighest or iLowest functions return the Shift I am looking for the IndicatoriHighest

I thought of using ArrayMaximum/Minimum then that returns the Arrays max or minvalues where as I need the Shift.
with that in mind

``````double myarray[];
//code funcions
//then now he function to retrieve the array

//inside the IndicatoriHighest function
myMax=ArrayMaximum(myarray);
for(int i=0;i<=ArraySize(myarray);i++)
{
if(myMax==myarray[i])
{
shift=i; break;
}
}
return shift;
//doesnt feel right to me
``````

will this work or the first one I posted wayabove

ArrayMaximum doesn’t return the max value in the array but instead the index to the max value location. You don’t need to do the “manual” search loop for the location.

The max value can also be obtained by myarray[ ArrayMaximum(myarray) ]

You. Just solved it. Thank u

ok so it has gone well I think …

``````//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
{
int limit = rates_total-prev_calculated;
int    i,pos;
//---
if(Bars<=mainperiod || mainperiod<2)
return(0);
//---
//ArraySetAsSeries(mainBuffer,true);
//ArraySetAsSeries(signalBuffer,true);
//--- preliminary calculations
pos=prev_calculated-1;
//---

//--- the mainline loop
for(i=0; i<limit ; i++)
mainBuffer[i]=iRSI(NULL,0,mainperiod, mainprice,i);
//--- the signalline loop of calculations
for(i=0; i<rates_total-1 ; i++)
signalBuffer[i]=iRSIOnArray(mainBuffer,Bars,signalperiod,i);
//---looking for first high/low points
for(i=limit;i==0;i--)
{
if(IsNewcandle())
{
// reset(); //reset all points
int controlmainhigh=0;
int controlmainlow=0;

do //loop to get high value,of the mainline loop it if high value found is the last index
{
controlmainhigh+=1;
int maintemphigh=ArrayMaximum(mainBuffer,InDepth+controlmainhigh,1);
if(maintemphigh>mainhighIndx)
mainhighIndx=maintemphigh;
}
while (mainhighIndx==InDepth+controlmainhigh);
//---
do //loop to get low value,of the mainline loop it if high value found is the last index
{
controlmainlow+=1;
int maintemplow=ArrayMinimum(mainBuffer,InDepth+controlmainhigh,1);
if(maintemplow<mainlowIndx)
mainlowIndx=maintemplow;
}
while (mainhighIndx==InDepth+controlmainhigh);
//---repeat for signal line
int controlsignalhigh=0;
int controlsignallow=0;
//for(int k=control; k=InDepth+control; k++)
//{}
do
{
controlsignalhigh+=1;
int signaltemphigh=ArrayMaximum(signalBuffer,InDepth+controlsignalhigh,1);
if(signaltemphigh>signalhighIndx)
signalhighIndx=signaltemphigh;
}
while (signalhighIndx==InDepth+controlsignalhigh);
//---
do
{
controlsignallow+=1;
int signaltemplow=ArrayMinimum(signalBuffer,InDepth+controlsignallow,1);
if(signaltemplow<signallowIndx)
signallowIndx=signaltemplow;
}
while (signalhighIndx==InDepth+controlsignallow);
//---

//---Checking to See which Index is bigger
if(mainhighIndx<mainlowIndx)
searchformainsecondlow=true;
if(mainhighIndx>mainlowIndx)
searchformainsecondhigh=true;
//---
//---Checking to See which Index is bigger
if(signalhighIndx<signallowIndx)
{searchforsignalsecondlow=true; Case1=true; Case2=false;}
if(signalhighIndx>signallowIndx)
{searchforsignalsecondhigh=true;  Case2=true; Case1=false;}
//--
}
if(mainlowIndx<0 || mainhighIndx<0 || signalhighIndx<0 || signallowIndx<0) return rates_total;

//--- Searching Second Points for main line
if(searchformainsecondlow )
{
int controlmainhigh=0;
int controlmainlow=0;
do
{
controlmainlow+=1;
if(Case1)
// if(mainhighIndx!=0)
mainsecondlowIndx=ArrayMinimum(mainBuffer,InDepth+controlmainlow,mainhighIndx);
if(Case2)
//  if(mainsecondhighIndex!=0)
mainsecondlowIndx=ArrayMinimum(mainBuffer,InDepth+controlmainlow,mainsecondhighIndex);
}
while(mainsecondlowIndx==InDepth+controlmainlow);
searchforsignalsecondhigh=true;
}
//---
if(searchformainsecondhigh)
{
int controlmainhigh=0;
int controlmainlow=0;

do
{
controlmainhigh+=1;
if(Case2)
//  if(mainlowIndx!=0)
mainsecondhighIndex=ArrayMaximum(mainBuffer,InDepth+controlmainhigh,mainlowIndx);
if(Case1)
//  if(mainsecondlowIndx!=0)
mainsecondhighIndex=ArrayMaximum(mainBuffer,InDepth+controlmainhigh,mainsecondlowIndx);
}
while (  mainsecondhighIndex==  InDepth+controlmainhigh);
searchformainsecondlow=true;
}
//---Done searching second points for main line

//--- Searching Second Points for signal line
if(searchforsignalsecondlow )
{
int controlsignalhigh=0;
int controlsignallow=0;
do
{
controlsignallow+=1;
if(Case1)
//   if(signalhighIndx!=0)
signalsecondlowIndx=ArrayMinimum(signalBuffer,InDepth+controlsignallow,signalhighIndx);
if(Case2)
//   if(signalsecondhighIndex!=0)
signalsecondlowIndx=ArrayMinimum(signalBuffer,InDepth+controlsignallow,signalsecondhighIndex);
}
while(signalsecondlowIndx==InDepth+controlsignallow);
searchforsignalsecondhigh=true;
}
//---
if(searchforsignalsecondhigh)
{
int controlsignalhigh=0;
int controlsignallow=0;

do
{
controlsignalhigh+=1;
if(Case2)
//   if(signallowIndx!=0)
signalsecondhighIndex=ArrayMaximum(signalBuffer,InDepth+controlsignalhigh,signallowIndx);
if(Case1)
//   if(signalsecondlowIndx!=0)
signalsecondhighIndex=ArrayMaximum(signalBuffer,InDepth+controlsignalhigh,signalsecondlowIndx);
}
while (  signalsecondhighIndex==  InDepth+controlsignalhigh);
searchforsignalsecondlow=true;
}
//---Done searching second points for signal line

//---Get points for mainline
//firsthightimemain=Time[mainhighIndx];
firstlowtimemain=Time[mainlowIndx];
mainRSI_low1=iRSI(Symbol(),Period(),mainperiod,mainprice,mainlowIndx);
secondlowtimemain=Time[mainsecondlowIndx];
mainRSI_low2=iRSI(Symbol(),Period(),mainperiod,mainprice,mainsecondlowIndx);
//---

//---Get points for signalline
//firsthightimesignal=Time[signalhighIndx];
firstlowtimesignal=Time[signallowIndx];
signalRSI_low1=iRSIOnArray(mainBuffer,Bars,signalperiod,signallowIndx);
secondlowtimesignal=Time[signalsecondlowIndx];
signalRSI_low2=iRSIOnArray(mainBuffer,Bars,signalperiod,signalsecondlowIndx);
//---

//---T1 as defined by Customer
if(signalRSI_low2<signalRSI_low1)
if(mainRSI_low2>mainRSI_low1)
create();
//---T2 as defined by Customer
if(signalRSI_low2>signalRSI_low1)
if(mainRSI_low2<mainRSI_low1)
create();

}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//---
bool IsNewcandle()
{
static int BarsOnChart = 1;

if (Bars == BarsOnChart)
return(false);
BarsOnChart = Bars;
return(true);
}
//---
void reset()
{
mainlowIndx=-1;//holds all low values of the main Buffer IndexMinimum(mainBuffer)
mainhighIndx=-1;//holds all High values of the main Buffer IndexMaximum(mainBuffer)
signallowIndx=-1;//holds the low values of signal buffer
signalhighIndx=-1;//holds high values of high buffer
Case1=false;
Case2=false;
}
//---
void create()
{

// if(drawmainRSI)
{
ObjectDelete("main_RSI__LL");
ObjectCreate("main_RSI__LL",OBJ_TREND,1,secondlowtimemain,mainRSI_low2,firstlowtimemain,mainRSI_low1);//secondLowTime,secondLowprice,firstLowTime,firstLowprice);
ObjectSet("main_RSI__LL",OBJPROP_COLOR,Red); //Goldenrod
ObjectSet("main_RSI__LL",OBJPROP_WIDTH, 3);
ObjectSet("main_RSI__LL",OBJPROP_RAY, false);}
//---
//  signal RSI_ High Trendline

//  if(drawSignalRSI)
{
ObjectDelete("signal_RSI__HL");
ObjectCreate("signal_RSI__HL",OBJ_TREND,1,secondlowtimesignal,signalRSI_low2,firstlowtimesignal,signalRSI_low1);//secondLowTime,secondLowprice,firstLowTime,firstLowprice);
ObjectSet("signal_RSI__HL",OBJPROP_COLOR,Orange); //Goldenrod
ObjectSet("signal_RSI__HL",OBJPROP_WIDTH, 3);
ObjectSet("signal_RSI__HL",OBJPROP_RAY, false);}

}

double line(int shift1,int shift2,double value1, double value2,int num)
{
double constant;
if(num==shift1||num==shift2)return(-1);
int xvalue=num;
double yintersect1,yintersect2;