Mathematical/probuilder · probacktest · proorder · proscreener

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

probuilder
COS(a)

Parameters

NameTypeDescription
anumberThe angle in radians. Degrees must be converted to radians before the call.

Formula

code
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)

probuilder
// 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)

probuilder
// 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)

probuilder
// 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
ENDIF

The 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. COS expects radians. To use degrees, multiply by PI / 180 first, 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) and COS(a + 2*PI) are equal. The function alone cannot tell you which cycle an angle came from.
  • SIN, sine of an angle in radians, the companion of cosine.
  • TAN, tangent of an angle in radians.
  • ACOS, arc cosine, the inverse of COS.
  • ASIN, arc sine.
  • ATAN, arc tangent.
  • ABS, absolute value, useful for symmetric cosine tests.
  • SQRT, square root, common in trigonometric identities.