简体   繁体   English

需要一个循环来查找某个值在列表的顶部 3rd、中间 3rd 和底部 3rd 中的位置

[英]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.我正在使用 Ninjatrader 脚本来查找某个价格所在的酒吧。 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.所以在柱内有 x 个从低到高的价格,我使用 for 循环来查找这些值。 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.所以说我在列表中有 16 个值。 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.我需要确定这些值的前 3 位、中位 3 位和后 1/3 位,然后一旦确定将某个“价格”与它所在的三分之一进行比较。 the count will vary and wont be cleanly divisible by 3.计数会有所不同,并且不会被 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.从这里我需要弄清楚如何将列表分成三部分,然后检查 ABC_Price 是否在列表的顶部、中间或底部三分之一,列表从高到低排序。

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.要了解哪些价格限制划定了清单的三分之一,您可以计算价格清单的 1/3 和 2/3 百分位数。

We can calculate the percentiles like this with the percentile given in the range 0 to 1:我们可以使用 0 到 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.示例: Percentile(prices, 1.0 / 3.0)得出的价格位于按升序排序的价目表的 1/3 处。

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.这些值是分数,因为列表的 1/3 和 2/3 位于实际列表位置之间。 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通过这些计算出lowerPriceLimitupperPriceLimit ,您可以确定价格位于哪个 3rd

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.我使用了一个 for 循环并将它们添加到一个列表中。

  2. Sort the list.对列表进行排序。 MyList.Sort(); MyList.Sort();

  3. Determine the count of the list, myCount =divide by 3. used (decimal) Math.Round确定列表的计数,myCount = 除以 3。使用(十进制)Math.Round

  4. Divide the List into 3 sections: used 3 for loops that cycled through the List based on myCount将 List 分为 3 个部分:使用 3 个 for 循环,根据 myCount 在 List 中循环

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

5.Create a bool for each section: 5.为每个部分创建一个布尔值:

if (Bottom_List.Contains(myPrice)) BotPrice = true; if (Bottom_List.Contains(myPrice)) BotPrice = true; else BotPrice= false;否则机器人价格=假;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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