math
属性
E
类型: const float
数学常数 e(欧拉数),约为 2.71828。自然对数的底数以及 n 接近无穷大时 (1 + 1/n)^n 的极限。
PHI
类型: const float
黄金比例φ(phi),约为1.61803。如果两个数量的比率等于它们的总和与较大数量的比率,则它们处于黄金比例。常见于自然模式和技术分析(斐波那契回调)。
PI
类型: const float
数学常数π(pi),约为3.14159。表示圆的周长与其直径的比率。
RECIPROCAL_PHI
类型: const float
黄金比例的倒数 (1/φ),约为 0.61803。等于 φ - 1。用于斐波那契分析,其中 61.8% 是关键回撤水平。
函数
abs
返回 n 的绝对值。对于正数,返回 n 不变。对于负数,返回 -n。对于零,返回零。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | int | 整数值。 |
返回: int
acos
acos(value: float): float返回 value 的反余弦(反余弦),以度为单位。给定一个余弦值,返回产生该值的角度。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | float | 余弦值(必须介于 -1 和 1 之间)。 |
返回: float — 范围 [0, 180] 内的角度(以度为单位)。
参见: math.cos
asin
asin(value: float): float返回 value 的反正弦(反正弦),以度为单位。给定一个正弦值,返回产生该值的角度。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | float | 正弦值(必须介于 -1 和 1 之间)。 |
返回: float — 范围 [-90, 90] 内的角度(以度为单位)。
参见: math.sin
atan
atan(value: float): float返回 value 的反正切(反正切),以度为单位。给定正切值,返回产生该正切值的角度。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | float | 正切值(任何实数)。 |
返回: float — (-90, 90) 范围内的角度(以度为单位)。
参见: math.tan, math.atan2
atan2
atan2(y: float, x: float): floatReturns the four-quadrant arctangent of y/x in degrees.
Unlike atan, this function takes two arguments and determines the correct quadrant using the signs of both. Useful for computing angles between vectors or points.
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
y | float | The Y component (numerator). | |
x | float | The X component (denominator). |
返回: float — An angle in degrees in the range (-180, 180].
参见: math.atan
avg
返回所有提供的参数的平均值(算术平均值)。接受可变数量的整数参数。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
numbers | int | 要平均的两个或多个整数值。 |
返回: float
cbrt
cbrt(n: float): float返回 n 的立方根。
与 pow(n, 1.0/3.0) 不同,能正确处理负数输入(例如 cbrt(-8.0) = -2.0),而后者对负数返回 na。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要求立方根的数值。 |
返回: float
ceil
ceil(n: float): int返回 n 的上限:大于或等于 n 的最小整数。例如,ceil(2.3) = 3,ceil(-2.3) = -2。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要向上舍入的值。 |
返回: int
参见: math.floor, math.trunc
clamp
clamp(value: float, min: float, max: float): float将 value 限制在闭区间 [min, max] 内。
若 value < min 返回 min,若 value > max 返回 max,否则返回 value。可替代繁琐的 math.max(min, math.min(max, x)) 写法。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value | float | 要限制的数值。 | |
min | float | 下界(含端点)。 | |
max | float | 上界(含端点)。 |
返回: float
cos
cos(angle: float): float返回 angle 的余弦。注意:Navi 使用度数,而不是弧度(与大多数编程语言不同)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
angle | float | 角度(以度为单位)。 |
返回: float — 介于 -1 和 1 之间的值。
参见: math.acos
exp
exp(n: float): float返回 e(欧拉数)的 n 次方。 log 的逆。对于指数增长/衰减计算很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 指数值。 |
返回: float
参见: math.log
floor
floor(n: float): int返回 n 的下限:小于或等于 n 的最大整数。例如,地板(2.7)= 2,地板(-2.3)= -3。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要向下舍入的值。 |
返回: int
参见: math.ceil, math.trunc
hypot
hypot(x: float, y: float): float返回直角三角形两直角边 x 和 y 所对应的斜边长。
等价于 sqrt(x*x + y*y),但数值上更稳定(对大数值不会溢出)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
x | float | 第一条直角边的长度。 | |
y | float | 第二条直角边的长度。 |
返回: float
log
log(n: float): float返回 n 的自然对数(以 e 为底)。 exp 的逆。对于非正值,返回 na。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 值(必须为正)。 |
返回: float
参见: math.exp, math.log10
log10
log10(n: float): float返回 n 的以 10 为底的(常用)对数。对于计算数量级很有用。对于非正值,返回 na。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 值(必须为正)。 |
返回: float
参见: math.log
max
返回所有提供的参数中的最大值。接受可变数量的整数参数。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
numbers | int | 要比较的两个或多个整数值。 |
返回: int
min
返回所有提供的参数中的最小值。接受可变数量的整数参数。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
numbers | int | 要比较的两个或多个整数值。 |
返回: int
pow
pow(base: float, exponent: float): float返回 base 的 exponent 次方。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
base | float | 基值。 | |
exponent | float | 将底座提升到的力量。 |
返回: float
random
random(
min: series float = 0,
max: series float = 1,
seed: series int = na
): series float生成 [min, max) 范围内的伪随机数。相同的种子产生相同的数字序列,这对于可重现的结果很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
min | series float | 0 | 范围的下限(含)。默认为 0。 |
max | series float | 1 | 范围的上限(不含)。默认为 1。 |
seed | series int | na | 可选种子以提高重现性。如果 na,则使用系统随机性。 |
返回: series float — 半开区间 [min, max) 中的浮点数。
round
round(n: float, precision: int = 0): int将 n 四舍五入到最接近的整数,或四舍五入到 precision 小数位。使用向上舍入:0.5 舍入到 1。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要舍入的值。 | |
precision | int | 0 | 小数位数(0 表示整数四舍五入)。 |
返回: int
round_to_mintick
round_to_mintick(n: float): float将 n 舍入到当前交易品种最接近的刻度值。结果始终是可用于订单的有效价格。使用 syminfo.mintick 作为舍入增量。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 价格值要四舍五入。 |
返回: float
sign
sign(n: float): float返回 n 的符号:1.0 表示正数,-1.0 表示负数,0.0 表示零。对于确定没有大小的方向很有用。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要检查的值。 |
返回: float — 1.0、-1.0 或 0.0。
参见: math.abs
sin
sin(angle: float): float返回 angle 的正弦值。注意:Navi 使用度数,而不是弧度(与大多数编程语言不同)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
angle | float | 角度(以度为单位)。 |
返回: float — 介于 -1 和 1 之间的值。
参见: math.asin
sqrt
sqrt(n: float): float返回 n 的平方根。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 该值(必须是非负数)。 |
返回: float — 平方根,或 na 表示负值。
sum
sum(source: series float, length: series int): series float计算 source 对最后一个 length bars 的滚动总和。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
source | series float | 系列总结。 | |
length | series int | 要包含的 bars 数量(必须 >= 1)。 |
返回: series float — 系列中最新 length 值的总数。
tan
tan(angle: float): float返回 angle 的正切值。注意:Navi 使用度数,而不是弧度。 ±90°、±270° 等处未定义(返回非常大的值)。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
angle | float | 角度(以度为单位)。 |
返回: float
参见: math.atan, math.atan2
to_degrees
to_degrees(rad: float): float将角度从弧度转换为度数。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
rad | float | 以弧度表示的角度。 |
返回: float
参见: math.to_radians
to_radians
to_radians(deg: float): float将角度从度数转换为弧度。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
deg | float | 角度(以度为单位)。 |
返回: float
参见: math.to_degrees
trunc
trunc(n: float): int将 n 向零截断(去除小数部分)。
与 floor 不同,向零截断是对称的:trunc(-1.9) = -1,而 floor(-1.9) = -2。
参数
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
n | float | 要截断的数值。 |
返回: int
参见: math.floor, math.ceil

