简体   繁体   中英

Need a loop to find where a certain value resides within the top 3rd, middle 3rd and bottom 3rd of a list

I am working in Ninjatrader script to find within a bar where a certain price resides. So far I have a value "price" that is one of the values in a list. So within the bar there are x number of prices from Low to High I use a for loop to find those values. I need the list to be sorted High to Low. The count will vary from bar to bar. So say I have 16 values in the list. I need to determine the top 3rd, middle 3rd and bottom third of those values then once determined to compare the certain "price" as to which third it resides. the count will vary and wont be cleanly divisible by 3.

for ( double myPrice = Low[0]; myPrice = High[0]; myPrice += Ticksize)
{
    myList.Add(myPrice);
}

From here I need to figure out how to divide list into thirds, then check if ABC_Price is in the top, middle or bottom third of the list, with list sorted High to Low.

To know which price limits are delimiting the thirds of your list, you can calculate the 1/3 and 2/3 percentiles of the price list.

We can calculate the percentiles like this with the percentile given in the range 0 to 1:

public static double Percentile(IList<double> sortedValues, double percentile)
{
    double realIndex = percentile * (sortedValues.Count - 1);
    int index = (int)realIndex;
    if (index < sortedValues.Count - 1) {
        double fraction = realIndex - index;
        return sortedValues[index] * (1 - fraction) + sortedValues[index + 1] * fraction;
    } else {
        return sortedValues[sortedValues.Count - 1];
    }
}

Example: Percentile(prices, 1.0 / 3.0) yields the price that lies at 1/3 of the price list sorted in ascending order.

This test

double[] prices = { 6.0, 10.0, 10.0, 10.0, 25.0, 30.0, 40.0, 45.0, 50.0, 55.0, 60.0, 100.0, 115.0, 250.0 };
double lowerPriceLimit = Percentile(prices, 1.0 / 3.0);
double upperPriceLimit = Percentile(prices, 2.0 / 3.0);
Console.WriteLine($"Price limits = {lowerPriceLimit:n3}, {upperPriceLimit:n3}");

Prints

Price limits = 26.667, 53.333

The values are fractions, because 1/3 and 2/3 of the list lie between real list positions. This means that the algorithm works well with any list size.

With these calculated lowerPriceLimit and upperPriceLimit you can determine in which 3rd a price resides with

if (price <= lowerPriceLimit) {
    // bottom third
} else if (price <= upperPriceLimit) {
    // middle third
} else {
    // top third
}

Whether you use <= or < in the comparisons depends on your decision in which category a price has to belong when it is equal to one of the limits.

Thanks for the suggestions unfortunately they were not what I was looking for. I had multiple issues to resolve:

  1. loop to find all prices within a bar. I used a for loop and added them to a List.

  2. Sort the list. MyList.Sort();

  3. Determine the count of the list, myCount =divide by 3. used (decimal) Math.Round

  4. Divide the List into 3 sections: used 3 for loops that cycled through the List based on myCount

    for ( double aDouble = myList[myCount - myCount]; aDouble =
    myList[TotalCount - (myCount*2))-2); aDouble += TickSize) { Bottom_List.Add(aDouble); }

5.Create a bool for each section:

if (Bottom_List.Contains(myPrice)) BotPrice = true; else BotPrice= false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM