how to find minimum negative value from a array of negative values

 

Hi Friends

I have a array of negative or zero values.

I can find Minimum Lowest (-veLowest) by ArrayMinimum() method.

Is there any function which I can use to find Maximum Lowest (-ve Highest) ?

Currently I am using following code, but is there a better way to get this information.

//+---------------------------------------------------------------------------------------------------------------------------+
//| Get Lower Maximum(-veHighest) TSV Values
//+---------------------------------------------------------------------------------------------------------------------------+
  LowerMaxTSV[0].value = (double)LONG_MIN;                                     // LONG_MIN equals -9223372036854775808
  stop = posStopA+5;                                                           // Stop at 5 buffer bars before [posStopA]
  for(int n = posStart; n <= stop; n++) {                                      // Start Excluding TSV of pIndex bar
    if(TSVNegative[n] != 0.00) {
      if(TSVNegative[n] > LowerMaxTSV[0].value) {                              // Get Highest Lower(-veTSV) Value
        LowerMaxTSV[0].idx       = n;
        LowerMaxTSV[0].value = TSVNegative[n];
      }
    }
  }

stop = posStopB+5;                                                             // Stop at 5 buffer bars before [pIndex+301]
LowerMaxTSV[1].idx       = LowerMaxTSV[0].idx;                                                                                          
LowerMaxTSV[1].value = LowerMaxTSV[0].value;
for(int n = LowerMaxTSV[0].idx; n <= stop; n++) {                              // Start Excluding TSV of pIndex bar
  if(TSVNegative[n] != 0.00) {
    if(TSVNegative[n] > LowerMaxTSV[1].value) {                                // Get Highest Lower(-veTSV) Value
      LowerMaxTSV[1].idx       = n;
      LowerMaxTSV[1].value = TSVNegative[n];
    }
  }
}

Regards

 
Anil Varma:
        LowerMaxTSV[0].idx       = n;

I think you have it - loop through and find it.

Otherwise at the time of entering a value you could keep track of the highest/lowest but that's helpful with very large data sets, in this case it could be overkill

 
R4tna C #:

I think you have it - loop through and find it.

Otherwise at the time of entering a value you could keep track of the highest/lowest but that's helpful with very large data sets, in this case it could be overkill

@R4tna C thanks for your reply.

 

Hi Anil,

Please use the below ArraySorter code to get the least double value from a single dimensional array. I hope it helps.

#define  Descending     0
#define  Ascending      1

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//+------------------------------------------------------------------+ 
//----

   double arr[]= { 0,-10.6,-10.4,-12.5,-13.9,0,-13.3,-12.6,0,2.3,0.88,-5.6 };
   ArraySorter(arr, Ascending);
   ArrayPrint(arr);
   Print(arr[0]);

   //----
   return(INIT_SUCCEEDED);

}// End init()
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Array Sorter                                                      |
// - Single dimensional array sorter                                 |
//+------------------------------------------------------------------+
void ArraySorter( double &arrArray[], int AscDesc= Ascending )
{
   if (ArraySize(arrArray) == 1)    return;     // Only one item in array
   
   for(int i=0; i < ArraySize(arrArray); i++)   {
      for(int j=i+1; j < ArraySize(arrArray); j++)  {
         double store= arrArray[j];
         switch (AscDesc)  {
            case Ascending    :  if (arrArray[i] > arrArray[j])   {     arrArray[j]= arrArray[i];     arrArray[i]= store;     }
                                 break;
            case Descending   :  if (arrArray[i] < arrArray[j])   {     arrArray[j]= arrArray[i];     arrArray[i]= store;     }
                                 break;      
         }
      }
   }

//----
}
//+------------------------------------------------------------------+
 
Keni Chetankumar Gajanan - #:

Hi Anil,

Please use the below ArraySorter code to get the least double value from a single dimensional array. I hope it helps.

This works if the data had only -ve nos, but it contains 0's.

I did consider copying all -ve values to a second array and using the in-built ArraySort(), but it was not clear whether the sequence had to be maintained also.

Ultimately it has to be loop (which your function uses anyway) - that maintains sequences and handles non -ve values

 
Keni Chetankumar Gajanan - #:

Hi Anil,

Please use the below ArraySorter code to get the least double value from a single dimensional array. I hope it helps.

@Keni Chetankumar Gajanan - Thanks a lot friend.

However @R4tna C has perfectly captured my logic (yes I need the Index of such bar).

Seems what I am doing is a correct workable solution.

Thanks again for both you to share the thoughts.

Regards.

 
Anil Varma #:

@Keni Chetankumar Gajanan - Thanks a lot friend.

However @R4tna C has perfectly captured my logic (yes I need the Index of such bar).

Seems what I am doing is a correct workable solution.

Thanks again for both you to share the thoughts.

Regards.

You are welcome @Anil Varma 

Glad to know that you got the solution.

 
Anil Varma #:

However @R4tna C has perfectly captured my logic (yes I need the Index of such bar).

Glad it works for you 

Reason: