How to Get Closest Value to a Number in Array

 

I have following code to get a list of values (double's type) in array.


extern double Input_Double = 30.0;
extern double Max_Tolerable = 2.0;
extern int Input_Multiplier = 10;

double iArray, iWaktu, iIndex;

double ArrayAspect[][2];



int start()

{

        int counted_bars=IndicatorCounted();


        //---- last counted bar will be recounted

        int limit;


        if(counted_bars>0) counted_bars--;

        limit=Bars-counted_bars;


        int shift = limit - FutureBars;

        while ( shift > 0 )

        {
                double value = somefunction(Time[shift]);


                ArrayResize(MyArray, BarsLimit);



                MyArray[shift][0] = shift;

                MyArray[shift][1] = value;

                MyArray[shift][2] = Time[shift];



                iIndex = MyArray[shift][0];

                iArray = MyArray[shift][1];

                iWaktu = MyArray[shift][2];


                if (iArray>=(Input_Double-Max_Tolerable) && iArray<=(Input_Double+Max_Tolerable))

                {
                        datetime thisBar = Time[(int)iIndex];

                        DrawVerticalLines("Line", thisBar);

                }

        }

        return(0);

}



For this moment, I use following code to get the closest value but off course it will get some values instead of one that closest to my desired:

if (iArray>=(Input_Double-Max_Tolerable) && iArray<=(Input_Double+Max_Tolerable))


So how to get closest value to a number (in this case is Input_Double) in array?


- Another question is: how to multiply (Input_Multiplier) this number in the loop?


Thanks in advanced

 
Panjianom Adi Pratomo:

I have following code to get a list of values (double's type) in array.



For this moment, I use following code to get the closest value but off course it will get some values instead of one that closest to my desired:


So how to get closest value to a number (in this case is Input_Double) in array?


- Another question is: how to multiply (Input_Multiplier) this number in the loop?


Thanks in advanced

        while ( shift > 0 )

are you sure with this loop? is that intentional? this will keep trying if shift is at least 1

            double sh = 50.00;
double upper = 20.00;
double lower = 10.00;
double val = 0.00;
double temp[];
int counter = 0;
while (sh < 70.00)
{
    sh +=1.00;
    val += 1.00;
    if (val>lower&&val<upper)
    {
        ArrayResize(temp,counter+1);
        temp[counter] = val;
        counter += 1;
    }
   
}
Print("lowest value is ",DoubleToString(temp[ArrayMinimum(temp)],2));
Print("all valid value is : ");
for (int i=0;i<ArraySize(temp);i++)
{
    Print(DoubleToString(temp[i],2));
}
 
Sardion Maranatha:

are you sure with this loop? is that intentional? this will keep trying if shift is at least 1

Forget to add some code at the end:

        shift = shift - 1;

        }

        return(0);
}

Btw thank you for your answer, but I can not implement your code to my code.

I am try with this following code but still got few objects instead of 1 object that closest to my desired:

while ( shift > 0 )
{

	double value = somefunction(Time[shift]);

	ArrayResize(MyArray, BarsLimit);
	MyArray[shift][0] = shift;
	MyArray[shift][1] = value;
	MyArray[shift][2] = Time[shift];


	iIndex = MyArray[shift][0];
	iArray = MyArray[shift][1];
	iWaktu = MyArray[shift][2];

	double step_number = MathRound(value/Input_Double) * Input_Double;
	double distance = MathAbs(iArray - step_number);

	double tempdistance[];
	int counter = 0;

	ArrayResize(tempdistance, shift);
	tempdistance[shift] = iArray;

	if(distance < Max_Tolerable)
	{
	        ArraySort(tempdistance, WHOLE_ARRAY, 0, MODE_ASCEND);
	        int getClose = ArrayBsearch(tempdistance, step_number, WHOLE_ARRAY, 0, MODE_ASCEND);
	        datetime getTime = Time[getClose];
	        DrawVerticalLines("Line", getTime);
	}

	shift = shift - 1;
}
 
  1. 	MyArray[shift][0] = shift;
    	MyArray[shift][1] = value;
    	MyArray[shift][2] = Time[shift];

    Don't do this. You are storing an int or uint, a double, and a datetime. And it works only because all three can be stored as a double.

    Code it as a proper struct and be self-documenting.

    struct Aname{ int mShift; double mValue; datetime mWhen; 
       void store(int s, double v){ mShift=s; mValue=v; mWhen=Time[s]; }
    }
    Aname MyArray[];
    ⋮
            MyArray[shift].mShift = shift;
            MyArray[shift].mValue = value;
            MyArray[shift].mWhen  = Time[shift];
    // or
            MyArray[shift].store(shift, value);
           
  2. Storing an as-series index (shift) is not a good idea; when a new bar starts, all your shifts will be wrong. Either get the shift using iBarShift(when) or convert to and from a non-series index

    #define  SERIES(I)   (Bars - 1 - I) // As-series to non-series or back.

 
William Roeder:
  1. Don't do this. You are storing an int or uint, a double, and a datetime. And it works only because all three can be stored as a double.

    Code it as a proper struct and be self-documenting.

  2. Storing an as-series index (shift) is not a good idea; when a new bar starts, all your shifts will be wrong. Either get the shift using iBarShift(when) or convert to and from a non-series index

Thank you for your info William Roeder.
Now the thread move to <deleted>

 
Panjianom Adi Pratomo:

Now the thread move to <deleted>

I have deleted your new topic.

It is related to this topic so continue here.

 
Keith Watford:

I have deleted your new topic.

It is related to this topic so continue here.

Owh okay, I will continue the thread here.

Reason: