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

如何统计两个顶部索引间MACD直方图低于零轴的数量?求类似JS filter的函数

统计指定区间内MACD直方图低于零轴的数量

核心逻辑

不管用哪种编程语言,核心步骤都是:

  • 从完整的MACD直方图数组中,截取两个索引之间的子数组(注意包含首尾索引)
  • 过滤出子数组中小于0的元素
  • 统计过滤后数组的长度,就是你要的数量

代码示例

Python实现

# 示例MACD直方图数据
macd_hist = [0.5, -0.2, 0.1, -0.3, -0.1, 0.4, -0.5]
start_idx = 1  # 起始索引
end_idx = 5    # 结束索引

# 1. 截取区间子数组(切片左闭右开,所以end_idx+1)
sub_data = macd_hist[start_idx:end_idx+1]
# 2. 过滤小于0的元素(等价于JS的filter逻辑)
filtered_vals = [val for val in sub_data if val < 0]
# 3. 统计数量
count = len(filtered_vals)
print(count)  # 输出:3

JavaScript实现

const macdHistogramArray = [0.5, -0.2, 0.1, -0.3, -0.1, 0.4, -0.5];
const startIdx = 1;
const endIdx = 5;

// 1. 截取区间子数组
const subArray = macdHistogramArray.slice(startIdx, endIdx + 1);
// 2. 过滤小于0的元素(就是你要的filter写法)
const filtered = subArray.filter(val => val < 0);
// 3. 统计数量
const count = filtered.length;
console.log(count); // 输出:3

示意图(区间示例)

索引0123456
MACD直方图值0.5-0.20.1-0.3-0.10.4-0.5
统计区间(1-5)✅✅✅✅✅
低于零轴✅✅✅

从图中可见,区间1-5内共有3个低于零轴的MACD直方图值,和代码运行结果一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 13:30:12