COS
COS in ProBuilder returns the cosine of an angle given in radians, a value between -1 and 1. Syntax, the radian requirement, gotchas and worked examples.
Syntax
COS(a)Parameters
| Name | Type | Description |
|---|---|---|
a | number | The angle in radians. Degrees must be converted to radians before the call. |
Formula
COS(a) = cosine of angle a, with a in radians, result in [-1, 1]COS(0) returns 1, COS(PI/2) returns 0, and COS(PI) returns -1. The function is periodic with period 2*PI.
How it works
COS treats its argument as an angle measured in radians, not degrees. This is the single most common trap: feeding it a degree value returns a meaningless number. Convert with a * PI / 180 first. The result is bounded to [-1, 1], which makes cosine useful for building smooth cyclic series and oscillators.
Because the output never leaves [-1, 1], it is a safe input for ACOS, the inverse function, without clamping.
Examples
Example 1, Cosine of an angle in degrees (Indicator)
// Convert 60 degrees to radians, then take the cosine
angleInDegrees = 60
angleInRadians = angleInDegrees * 3.14159265 / 180
cosineValue = COS(angleInRadians)
RETURN cosineValue AS "COS(60 deg)"The conversion is essential. COS(60) would treat 60 as radians and return the wrong value; the code above returns 0.5, the cosine of 60 degrees.
Example 2, Cyclic wave over the bar index (ProScreener)
// A smooth cosine wave cycling every 20 bars
phase = BarIndex * 2 * 3.14159265 / 20
wave = COS(phase)
SCREENER[wave > 0.9](wave AS "Cosine wave")Scaling the bar index into radians produces a repeating cosine that peaks once per cycle.
Example 3, Cosine-weighted entry gate (ProOrder)
// Weight a signal by a slow cosine cycle
phase = BarIndex * 2 * 3.14159265 / 50
weight = COS(phase)
IF NOT LongOnMarket AND close CROSSES OVER Average[20](close) AND weight > 0 THEN
BUY 1 CONTRACT AT MARKET
ENDIFThe cosine term only allows entries during the positive half of its cycle, biasing trades toward one phase of the wave.
Common errors and gotchas
- Angle must be in radians.
COSexpects radians. To use degrees, multiply byPI / 180first, otherwise the result is meaningless. - Output is bounded to [-1, 1]. Cosine never returns a value outside this range, so a reading beyond it signals a coding mistake upstream.
- Periodic, so many inputs give the same output.
COS(a)andCOS(a + 2*PI)are equal. The function alone cannot tell you which cycle an angle came from.
