You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NinjaTrader脚本开发:判断目标价格在价格列表的三分位区间

NinjaTrader脚本:判断价格在K线内的三分位位置

方法一:基于价格区间计算(推荐,高效无冗余)

无需生成完整价格列表,直接通过K线高低价区间计算三分位分界点,性能更优,适合实时交易场景:

// 读取当前K线核心数据
double currentHigh = High[0];
double currentLow = Low[0];
double tickSize = TickSize;
double abcPrice = ABC_Price; // 你的目标价格变量

// 计算区间三分位阈值
double priceRange = currentHigh - currentLow;
double upperThirdThreshold = currentHigh - (priceRange / 3);
double lowerThirdThreshold = currentLow + (priceRange / 3);

// 判断价格所属分位
string pricePosition;
if (abcPrice >= upperThirdThreshold)
{
    pricePosition = "上三分位";
}
else if (abcPrice <= lowerThirdThreshold)
{
    pricePosition = "下三分位";
}
else
{
    pricePosition = "中三分位";
}

// 后续逻辑可基于pricePosition执行

方法二:基于已生成的价格列表(适配你的现有代码)

如果你坚持使用已生成的价格列表,可按以下逻辑判断:

// 假设你已生成包含Low[0]到High[0]及ABC_Price的价格列表
List<double> priceList = /* 你的价格列表生成逻辑 */;

// 将列表按从高到低排序
priceList.Sort((a, b) => b.CompareTo(a));

// 获取目标价格在排序后列表中的索引
int targetIndex = priceList.IndexOf(ABC_Price);
int listLength = priceList.Count;

// 计算三分位分界索引(整数除法自动处理无法被3整除的情况)
int upperCutoff = listLength / 3;
int lowerCutoff = (listLength * 2) / 3;

// 判断分位
string pricePosition;
if (targetIndex < upperCutoff)
{
    pricePosition = "上三分位";
}
else if (targetIndex >= lowerCutoff)
{
    pricePosition = "下三分位";
}
else
{
    pricePosition = "中三分位";
}

关键注意事项

  • 确保ABC_Price处于当前K线Low[0]到High[0]范围内,若超出可额外添加边界判断逻辑。
  • 浮点精度顾虑:因NinjaTrader价格对齐TickSize,直接比较即可;若需严格对齐Tick,可使用Math.Abs(abcPrice - threshold) < tickSize / 2判断是否接近分界点。
  • 优先选择方法一,避免生成冗余列表占用内存,提升脚本运行效率。

内容的提问来源于stack exchange,提问作者atljam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 17:24:48