# Math

***

## <mark style="color:green;">math.abs</mark>

Returns the absolute value of a number.

**Syntax:**

```javascript
math.abs(value)
```

**Examples:**

```javascript
// Basic absolute value
math.abs(-50)  // Returns 50

// Price difference absolute value
math.abs($BTC.close - $BTC.open)

// Indicator deviation
math.abs(ta.rsi($BTC, 14) - 50)

// Portfolio calculations
math.abs($BTC.close - ta.sma($BTC, 20))
```

**Returns:** Positive numerical value

***

## <mark style="color:green;">math.max</mark>

Returns the largest of the given numbers.

**Syntax:**

```javascript
math.max(value1, value2, ...)
```

**Examples:**

```javascript
// Find maximum of two prices
math.max($BTC.open, $BTC.close)

// Maximum of multiple indicators
math.max(ta.rsi($BTC, 14), ta.rsi($ETH, 14), ta.rsi($SOL, 14))

// Dynamic stop loss
math.max($BTC.low, ta.sma($BTC, 20) * 0.95)

// Risk management
math.max($BTC.close * 0.02, 100)  // Minimum position size
```

**Returns:** Largest numerical value from the arguments

***

## <mark style="color:green;">math.min</mark>

Returns the smallest of the given numbers.

**Syntax:**

```javascript
math.min(value1, value2, ...)
```

**Examples:**

```javascript
// Find minimum of two prices
math.min($BTC.open, $BTC.close)

// Minimum RSI across assets
math.min(ta.rsi($BTC, 14), ta.rsi($ETH, 14))

// Position sizing with cap
math.min($BTC.close * 0.1, 5000)  // Maximum position size

// Dynamic support level
math.min($BTC.low, ta.bb($BTC, 20, 2).lower)
```

**Returns:** Smallest numerical value from the arguments

***

## <mark style="color:green;">math.round</mark>

Rounds a number to the nearest integer, or to a specified number of decimal places.

**Syntax:**

```javascript
math.round(value)
math.round(value, precision)
```

**Examples:**

```javascript
// Round price to nearest dollar
math.round($BTC.close)

// Round to 2 decimal places
math.round($BTC.close, 2)  // e.g., 45123.456 -> 45123.46

// Round indicator values
math.round(ta.rsi($BTC, 14))

// Position sizing with precision
math.round($BTC.close * 0.01, 4)  // Round to 4 decimal places

// Volume normalization
math.round($BTC.volume / 1000000, 1)  // Round to 1 decimal place
```

**Returns:** Rounded value (integer if no precision, float if precision specified)

***

## <mark style="color:green;">math.floor</mark>

Rounds a number down to the nearest integer.

**Syntax:**

```javascript
math.floor(value)
```

**Examples:**

```javascript
// Floor price value
math.floor($BTC.close)

// Conservative position sizing
math.floor($BTC.close * 0.01)

// RSI level grouping
math.floor(ta.rsi($BTC, 14) / 10) * 10  // Round to nearest 10

// Time-based calculations
math.floor($BTC.volume / 1000) * 1000  // Floor to thousands
```

**Returns:** Rounded down integer value

***

## <mark style="color:green;">math.ceil</mark>

Rounds a number up to the nearest integer.

**Syntax:**

```javascript
math.ceil(value)
```

**Examples:**

```javascript
// Ceiling price value
math.ceil($BTC.close)

// Conservative stop loss
math.ceil($BTC.close * 1.05)

// Minimum lot sizing
math.ceil($BTC.close * 0.001)

// Volume ceiling
math.ceil($BTC.volume / 1000000)  // Round up to millions
```

**Returns:** Rounded up integer value

***

## <mark style="color:green;">math.sqrt</mark>

Returns the square root of a number.

**Syntax:**

```javascript
math.sqrt(value)
```

**Examples:**

```javascript
// Volatility calculations
math.sqrt(ta.atr($BTC, 14))

// Standard deviation approximation
math.sqrt(math.abs($BTC.close - ta.sma($BTC, 20)))

// Risk metrics
math.sqrt($BTC.volume / ta.sma($BTC.volume, 20))

// Price normalization
math.sqrt($BTC.close / 1000)
```

**Returns:** Square root of the input value

***

## <mark style="color:green;">math.pow</mark>

Returns the value of a number raised to a power.

**Syntax:**

```javascript
math.pow(base, exponent)
```

**Examples:**

```javascript
// Exponential calculations
math.pow($BTC.close / $BTC.close[1], 2)  // Squared returns

// Compound growth
math.pow(1.01, 30)  // 1% growth over 30 periods

// Volatility weighting
math.pow(ta.atr($BTC, 14) / $BTC.close, 2)

// Risk scaling
math.pow(ta.rsi($BTC, 14) / 50, 3)  // Cubic scaling
```

**Returns:** Base raised to the power of exponent

***

## <mark style="color:green;">math.sin</mark>

Returns the trigonometric sine of an angle (in radians).

**Syntax:**

```javascript
math.sin(angle)
```

**Examples:**

```javascript
// Sine wave calculations
math.sin(math.toradians(45))  // Sine of 45 degrees

// Cyclical analysis
math.sin($BTC.close / 10000)

// Wave pattern detection
math.sin(ta.rsi($BTC, 14) * math.toradians(3.6))  // Map RSI to sine wave

// Market cycle modeling
math.sin(2 * Math.PI * $BTC.close / ta.sma($BTC, 50))
```

**Returns:** Sine of the angle in radians

***

## <mark style="color:green;">math.cos</mark>

Returns the trigonometric cosine of an angle (in radians).

**Syntax:**

```javascript
math.cos(angle)
```

**Examples:**

```javascript
// Cosine wave calculations
math.cos(math.toradians(45))  // Cosine of 45 degrees

// Phase-shifted analysis
math.cos($BTC.close / 10000)

// Complementary wave patterns
math.cos(ta.rsi($BTC, 14) * math.toradians(3.6))

// Market momentum cycles
math.cos(2 * Math.PI * $BTC.close / ta.sma($BTC, 20))
```

**Returns:** Cosine of the angle in radians

***

## <mark style="color:green;">math.tan</mark>

Returns the trigonometric tangent of an angle (in radians).

**Syntax:**

```javascript
math.tan(angle)
```

**Examples:**

```javascript
// Tangent calculations
math.tan(math.toradians(45))  // Returns 1.0

// Slope calculations
math.tan(math.atan(($BTC.close - $BTC.close[10]) / 10))

// Trend angle analysis
math.tan(math.atan((ta.sma($BTC, 20) - ta.sma($BTC, 20)[5]) / 5))
```

**Returns:** Tangent of the angle in radians

***

## <mark style="color:green;">math.asin</mark>

Returns the arcsine (inverse sine) of a number, result in radians.

**Syntax:**

```javascript
math.asin(value)
```

**Examples:**

```javascript
// Inverse sine calculations (value must be between -1 and 1)
math.asin(0.5)  // Returns π/6 radians (30 degrees)

// Normalized RSI to angle
math.asin((ta.rsi($BTC, 14) - 50) / 50)  // Map RSI to -π/2 to π/2

// Price oscillation analysis
math.asin(($BTC.close - ta.sma($BTC, 20)) / ta.atr($BTC, 14))
```

**Returns:** Arcsine in radians (range: -π/2 to π/2)

***

## <mark style="color:green;">math.acos</mark>

Returns the arccosine (inverse cosine) of a number, result in radians.

**Syntax:**

```javascript
math.acos(value)
```

**Examples:**

```javascript
// Inverse cosine calculations (value must be between -1 and 1)
math.acos(0.5)  // Returns π/3 radians (60 degrees)

// Phase analysis
math.acos(($BTC.close - $BTC.low) / ($BTC.high - $BTC.low))

// Correlation angle
math.acos(ta.rsi($BTC, 14) / 100)  // Map RSI to angle
```

**Returns:** Arccosine in radians (range: 0 to π)

***

## <mark style="color:green;">math.atan</mark>

Returns the arctangent (inverse tangent) of a number, result in radians.

**Syntax:**

```javascript
math.atan(value)
```

**Examples:**

```javascript
// Inverse tangent calculations
math.atan(1)  // Returns π/4 radians (45 degrees)

// Trend angle calculation
math.atan(($BTC.close - $BTC.close[10]) / 10)

// Price slope analysis
math.atan((ta.ema($BTC, 20) - ta.ema($BTC, 20)[5]) / 5)

// Momentum angle
math.atan(ta.rsi($BTC, 14) - 50)
```

**Returns:** Arctangent in radians (range: -π/2 to π/2)

***

## <mark style="color:green;">math.log</mark>

Returns the natural logarithm (base e) of a number.

**Syntax:**

```javascript
math.log(value)
```

**Examples:**

```javascript
// Natural logarithm
math.log($BTC.close)  // Log of current price

// Price ratio analysis
math.log($BTC.close / $BTC.close[30])  // 30-period log return

// Volatility calculations
math.log(ta.atr($BTC, 14) / ta.atr($BTC, 50))

// Growth rate analysis
math.log($BTC.volume / ta.sma($BTC.volume, 20))
```

**Returns:** Natural logarithm of the input value

***

## <mark style="color:green;">math.log10</mark>

Returns the base-10 logarithm of a number.

**Syntax:**

```javascript
math.log10(value)
```

**Examples:**

```javascript
// Base-10 logarithm
math.log10($BTC.close)  // Log base 10 of price

// Order of magnitude analysis
math.log10($BTC.volume)

// Scale normalization
math.log10($BTC.close / 1000)

// Exponential scaling
math.log10(ta.rsi($BTC, 14))
```

**Returns:** Base-10 logarithm of the input value

***

## <mark style="color:green;">math.exp</mark>

Returns e raised to the power of the given number (e^x).

**Syntax:**

```javascript
math.exp(value)
```

**Examples:**

```javascript
// Exponential function
math.exp(1)  // Returns e (≈2.718)

// Exponential growth modeling
math.exp(ta.rsi($BTC, 14) / 100)

// Volatility expansion
math.exp(ta.atr($BTC, 14) / $BTC.close)

// Compound growth calculation
math.exp(math.log($BTC.close / $BTC.close[30]) * 12)  // Annualized
```

**Returns:** e raised to the power of the input value

***

## <mark style="color:green;">math.sign</mark>

Returns the sign of a number: 1 for positive, -1 for negative, 0 for zero.

**Syntax:**

```javascript
math.sign(value)
```

**Examples:**

```javascript
// Sign of price change
math.sign($BTC.close - $BTC.close[1])

// Trend direction
math.sign(ta.sma($BTC, 20) - ta.sma($BTC, 50))

// Momentum direction
math.sign(ta.rsi($BTC, 14) - 50)

// Volume flow direction
math.sign($BTC.volume - ta.sma($BTC.volume, 20))
```

**Returns:** -1, 0, or 1 depending on the sign of the input

***

## <mark style="color:green;">math.avg</mark>

Calculates the average of multiple values.

**Syntax:**

```javascript
math.avg(value1, value2, ...)
```

**Examples:**

```javascript
// Average of multiple prices
math.avg($BTC.open, $BTC.close, $BTC.high, $BTC.low)

// Average RSI across timeframes
math.avg(ta.rsi($BTC, 14), ta.rsi($BTC, 21), ta.rsi($BTC, 28))

// Portfolio average
math.avg($BTC.close, $ETH.close, $SOL.close)

// Multi-indicator average
math.avg(ta.sma($BTC, 20), ta.ema($BTC, 20), ta.wma($BTC, 20))
```

**Returns:** Average of all provided values

***

## <mark style="color:green;">math.sum</mark>

Calculates the sum of values, either from an array with optional lookback or multiple arguments.

**Syntax:**

```javascript
math.sum(array, length)
math.sum(value1, value2, ...)
```

**Examples:**

```javascript
// Sum of recent volumes (if you had volume array)
// math.sum(volumeArray, 10)  // Sum of last 10 values

// Sum of multiple values
math.sum($BTC.open, $BTC.close, $BTC.high, $BTC.low)

// Sum of indicators
math.sum(ta.rsi($BTC, 14), ta.rsi($ETH, 14), ta.rsi($SOL, 14))

// Portfolio value sum
math.sum($BTC.close * 0.5, $ETH.close * 0.3, $SOL.close * 0.2)
```

**Returns:** Sum of all values

***

## <mark style="color:green;">math.random</mark>

Returns a pseudo-random number between min and max.

**Syntax:**

```javascript
math.random(min, max, seed)
```

**Examples:**

```javascript
// Random number between 0 and 1
math.random()

// Random price adjustment
$BTC.close + math.random(-100, 100)

// Random position sizing
math.random(0.01, 0.05) * $BTC.close

// Random threshold for dynamic strategies
ta.rsi($BTC, 14) > math.random(30, 70)
```

**Returns:** Random number between min and max (exclusive)

***

## <mark style="color:green;">math.todegrees</mark>

Converts an angle from radians to degrees.

**Syntax:**

```javascript
math.todegrees(radians)
```

**Examples:**

```javascript
// Convert radians to degrees
math.todegrees(Math.PI)  // Returns 180

// Convert trend angle to degrees
math.todegrees(math.atan(($BTC.close - $BTC.close[10]) / 10))

// Phase angle in degrees
math.todegrees(math.asin((ta.rsi($BTC, 14) - 50) / 50))
```

**Returns:** Angle converted to degrees

***

## <mark style="color:green;">math.toradians</mark>

Converts an angle from degrees to radians.

**Syntax:**

```javascript
math.toradians(degrees)
```

**Examples:**

```javascript
// Convert degrees to radians
math.toradians(180)  // Returns π

// 45-degree angle in radians
math.toradians(45)

// Use with trigonometric functions
math.sin(math.toradians(30))  // Sine of 30 degrees
```

**Returns:** Angle converted to radians
