您好,我用tbl语言想了这个指标,总是通不过,可以帮忙修改下吗?的百分位值:把所有历史收盘价排个队,看最新价站在什么位置 收集数据:获取该合约自上市以来的所有日收盘价(假设共 N 天) 排序定位:将这 N 个收盘价从小到大排序 计算排名:找到最新价在这个有序序列中的位置(第 R 位) 百分位计算:百分位 = (R / N) × 100%。
// 历史百分位指标
// 功能:计算最新价在自上市以来所有收盘价中的百分位值
Params
Numeric Price; // 使用的价格序列
Bool ShowDetails(False); // 是否显示详细统计信息
Numeric HighColor(255); // 高位区域颜色:255=红色
Numeric LowColor(16711680); // 低位区域颜色:16711680=蓝色
Numeric NormalColor(65280); // 正常区域颜色:65280=绿色
Vars
Numeric i; // 循环计数器
Numeric totalBars; // 总K线数量
Numeric lowerCount; // 低于当前价的K线数量
Numeric equalCount; // 等于当前价的K线数量
Numeric percentile; // 百分位值(0-100)
Numeric rank; // 当前价格的排名
Numeric currentPrice; // 当前价格
Numeric totalCount; // 累计总数量
Begin
// 处理价格参数:如果用户未指定,使用收盘价
If Price == 0 Then
currentPrice = Close;
Else
currentPrice = Price;
// 初始化变量
totalBars = CurrentBar + 1; // 当前Bar索引+1 = 总K线数
lowerCount = 0;
equalCount = 0;
// 遍历所有历史K线
For i = 0 To CurrentBar
Begin
// 使用currentPrice而不是Price
If (Close[i] < currentPrice) Then
lowerCount = lowerCount + 1;
Else If (Close[i] == currentPrice) Then
equalCount = equalCount + 1;
End;
// 计算排名和百分位
// 排名 = 低于当前价的数量 + 等于当前价的数量×0.5
rank = lowerCount + equalCount * 0.5;
// 避免除零错误,当只有一根K线时设为50%
If totalBars > 1 Then
percentile = rank / totalBars * 100;
Else
percentile = 50;
// 保存计算结果供后续使用
SetGlobalVar(0, percentile);
totalCount = totalBars;
// 根据百分位值选择颜色
If percentile >= 80 Then
PlotNumeric("历史百分位", percentile, HighColor);
Else If percentile <= 20 Then
PlotNumeric("历史百分位", percentile, LowColor);
Else
PlotNumeric("历史百分位", percentile, NormalColor);
// 绘制参考线
PlotNumeric("80%线", 80, 12632256); // LightGray
PlotNumeric("50%线", 50, 8421504); // DarkGray
PlotNumeric("20%线", 20, 12632256); // LightGray
PlotNumeric("0%线", 0, 8421504); // DarkGray
PlotNumeric("100%线", 100, 8421504); // DarkGray
// 显示详细统计信息(可选)
If ShowDetails Then
Begin
Commentary("历史百分位指标详情");
Commentary("当前价格: " + Text(currentPrice));
Commentary("历史K线总数: " + Text(totalBars));
Commentary("排名R值: " + Text(rank));
Commentary("百分位值: " + Text(percentile, 2) + "%");
Commentary("低于当前价的数量: " + Text(lowerCount));
Commentary("等于当前价的数量: " + Text(equalCount));
Commentary(" ");
// 在图表上显示数值
If CurrentBar = BarCount - 1 Then // 只在最新K线显示
Begin
PlotString(Text(percentile, 1) + "%", High * 1.01, 0); // 黑色
If percentile >= 90 Then
PlotString("↖历史高位", High * 1.02, 255); // 红色
Else If percentile <= 10 Then
PlotString("↖历史低位", High * 1.02, 16711680); // 蓝色
End;
End;
// 返回百分位值
Return percentile;
End
这是AI写的, 你只能让ai改了,上面几乎没有对的东西,