Trade Smarter with Automation

Math & Decimal Numbers

Alta5 provides a global utility function API, $$, to make it easier to work with decimal numbers and calculate statistics in code.

Examples

// Target price, rounded to 2 decimal places
var target = $$($stock.last + $stock.averageMove, 2);
// target = 23.38

// Round down to the nearest nickel
target = $$.floor(target, .05);
// target = 23.35

// Round up to nearest $1 increment
var strike = $$.ceil(target, 1);
// strike = 24

// Array of 3 strike prices in $.50 increments
var prices = $$.prices(strike, 3, .5);
// prices = [24, 24.5, 25];

// Calculate stats from arrays
var high = $$.max(prices);
// high = 25

$$ API

By Default, all numerical return values are rounded to 2 decimal places. Use the optional precision argument to adjust the number of decimal places.

$$(value, precision) Number

Rounds to specified decimal precision.

  • Number

    The number to round.

  • Number

    The number of decimal places to round to (defaults to 2).

// Price change, 2 decimal places
var change = $$($stock.last - $stock.close);

// Change %, 5 decimal places
var pct = $$(change / $stock.close, 5);

$$.abs(value, precision) Number

Rounds to specified decimal precision and returns the absolute value.

  • Number

    The number to round.

  • Number

    The number of decimal places to round to (defaults to 2).

$$.ceil(value, increment, max) Number

Rounds up to specified increment.

  • Number

    The number to round.

  • Number

    A decimal or integer increment (optional, defaults to 0.01).

  • Number

    The highest value to round to. If the result number is greater than max, max is returned.

// Round up to the nearest nickel
var price = $$.ceil($stock.last * .9, .05);

// Round to OTM call strike price, $1 increment
var strikePrice = $$.ceil($stock.last, 1);

$$.floor(value, increment, min) Number

Rounds down to specified increment.

  • Number

    The number to round.

  • Number

    A decimal or integer increment (optional, defaults to 0.01).

  • Number

    The lowest value to round to. If the result number is less than min, min is returned. (defaults to 0)

// Round down to the nearest nickel
var price = $$.floor($stock.last * .9, .05);

// Round to OTM put strike price, $1 increment
var strikePrice = $$.floor($stock.last, 1);

$$.max(array, property, precision) Number

Returns the highest value in array.

  • Number

    An array of numbers (or objects if property is provided).

  • Number

    If array contains objects, use the value of a specific property of those objects. (optional)

  • Number

    The number of decimal places to round to (optional, defaults to 2).

var values = [1.38, 1.23, 1.69];
var max = $$.max(values);
// max = 1.69
// Get the highest value of the property "high" in bars
var high = $$.max($chart.bars, 'high');

$$.mean(array, property, precision) Number

Returns the average of the values in array.

  • Number

    An array of numbers (or objects if property is provided).

  • Number

    If array contains objects, use the value of a specific property of those objects. (optional)

  • Number

    The number of decimal places to round to (optional, defaults to 2).

var values = [1.38, 1.23, 1.69];
var mean = $$.mean(values);
// mean = 1.43
// Get the average of the property "close" in bars
var avg = $$.mean($chart.bars, 'close');

$$.min(array, property, precision) Number

Returns the lowest value in array.

  • Number

    An array of numbers (or objects if property is provided).

  • Number

    If array contains objects, use the value of a specific property of those objects. (optional)

  • Number

    The number of decimal places to round to (optional, defaults to 2).

var values = [1.38, 1.23, 1.69];
var min = $$.min(values);
// min = 1.23
// Get the lowest value of the property "low" in bars
var low = $$.min($chart.bars, 'low');

$$.prices(start, count, increment) Array

Returns an array containing count prices starting with start and incrementing/decrementing by increment.

  • Number

    The price to start with (the first item in the returned array).

  • Number

    The number of prices in the returned array.

  • Number

    The price increment for the generated prices (e.g. 0.05 for nickels). Negative increments are supported.

// 4 prices starting at 23, $.50 increments.
var prices = $$.prices(23, .5, 4);
// prices = [23, 23.5, 24, 24.5]

$$.priceRange(start, end, increment) Array

Returns an array containing all prices between start and end at specified price increments.

  • Number

    The price to start with (the first item in the returned array).

  • Number

    The price to end with (the last item in the returned array).

  • Number

    The price increment for the generated prices (e.g. 0.05 for nickels)

// All prices between $.50 and $.75 in nickel increments
var prices = $$.priceRange(.50, .75, .05);
// prices = [.5, .55, .6, .65, .7, .75]

$$.range(min, max, value, precision) Number

Restricts value to specified range (min, max) and precision.

  • Number

    The minimum value allowed

  • Number

    The maximum value allowed

  • Number

    The number to round.

  • Number

    The number of decimal places to round to (defaults to 2).

var value = 2.23;

var price = $$.range(1, 2, value);
// price = 2

price = $$.range(2, 2.50, value);
// price = 2.23

price = $$.range(3, 5, value);
// price = 3

$$.sum(array, property, precision) Number

Returns the sum of the values in array.

  • Number

    An array of numbers (or objects if property is provided).

  • Number

    If array contains objects, use the value of a specific property of those objects. (optional)

  • Number

    The number of decimal places to round to (optional, defaults to 2).

var values = [1.38, 1.23, 1.69];
var sum = $$.sum(values);
// sum = 4.3
// total draw
var draw = $$.sum($a5.opps, 'draw');

$$.stddev(array, property, precision) Number

Returns the standard deviation of the values in array.

  • Number

    An array of numbers (or objects if property is provided).

  • Number

    If array contains objects, use the value of a specific property of those objects. (optional)

  • Number

    The number of decimal places to round to (optional, defaults to 2).

var values = [1.38, 1.23, 1.69];
var sd = $$.stddev(values);
// sd = .19
// std dev of bars in $chart
var sd = $$.stddev($chart.bars, 'close');