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

在PineScript中如何按特定增量(如5)取整?

在PineScript中按特定增量取整的方法

当然有办法实现,核心思路是先把目标数值按增量缩放,用PineScript内置的floor/ceiling/round函数取整后,再缩放回原单位,完全可以满足你要的按5的倍数取整的需求。

1. 按增量向上取整(如414.2→415)

用ceiling函数配合缩放:先把数值除以增量,向上取整后再乘回增量。

// 示例:向上取整到5的倍数
value = 414.2
roundedValue = ceiling(value / 5) * 5 // 结果为415

2. 按增量向下取整(如112.1→110)

用floor函数同理,先缩放再取整后还原:

// 示例:向下取整到5的倍数
value = 112.1
roundedValue = floor(value / 5) * 5 // 结果为110

3. 按增量四舍五入(如138.5→140)

用round函数实现最接近增量倍数的取整:

// 示例:四舍五入到5的倍数
value = 138.5
roundedValue = round(value / 5) * 5 // 结果为140

封装成可复用函数

如果需要多次使用,可以把逻辑封装成自定义函数,方便调用任意增量的取整:

// 向上取整到指定增量
roundUp(val, increment) =>
    ceiling(val / increment) * increment

// 向下取整到指定增量
roundDown(val, increment) =>
    floor(val / increment) * increment

// 四舍五入到指定增量
roundNearest(val, increment) =>
    round(val / increment) * increment

// 调用示例
result1 = roundUp(414.2, 5)    // 415
result2 = roundNearest(138.5, 5) // 140
result3 = roundDown(112.1, 5)  // 110

这个方法适用于任何正增量值,不止是5,比如你要按10、0.1等增量取整,只要替换函数里的increment参数就行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:22:34