MathOperator Library
This is a powerful Pine Script library for TradingView that provides a comprehensive set of methods for handling mathematical and logical operations.
Core Functionality:
The "MathOperator" library allows you to easily perform a wide range of calculations and comparisons within your TradingView scripts. It is organized into several methods:
-
Arithmetic Operations:
-
add(a, b)
: Adds two numbers. -
subtract(a, b)
: Subtracts one number from another. -
multiply(a, b)
: Multiplies two numbers. -
divide(a, b)
: Divides one number by another. -
remainder(a, b)
: Calculates the remainder of a division.
-
-
Comparison Operations:
-
equal(a, b)
: Checks if two numbers are equal. -
not_equal(a, b)
: Checks if two numbers are not equal. -
over(a, b)
: Checks if the first number is greater than the second. -
under(a, b)
: Checks if the first number is less than the second. -
over_equal(a, b)
: Checks if the first number is greater than or equal to the second. -
under_equal(a, b)
: Checks if the first number is less than or equal to the second.
-
-
Logical Operations:
-
and_(a, b)
: Logical AND. -
or_(a, b)
: Logical OR. -
not_(a)
: Logical NOT. -
xor_(a, b)
: Logical XOR (exclusive OR). -
xnor_(a, b)
: Logical XNOR (exclusive NOR). -
nand_(a, b)
: Logical NAND. -
nor_(a, b)
: Logical NOR.
-
Advanced Features (v2):
The latest version introduces "proposition" methods, which allow you to use string inputs to define the operator, making your code more dynamic:
-
comparison_proposition(value_a, operator, value_b)
: Performs a comparison on two float values using a string to specify the operator (e.g., ">", "<=", "=="). -
logical_proposition(value_a, operator, value_b)
: Performs a logical operation on two boolean values using a string to specify the operator (e.g., "and", "or", "xor").
Benefits for Developers:
This open-source library is designed for Pine Script programmers who want to streamline their code. By using "MathOperator," you can:
- Simplify Complex Logic: Easily implement sophisticated mathematical and logical conditions in your indicators and strategies.
- Improve Code Readability: Offload common operations to this library to make your main script cleaner and easier to understand.
- Increase Flexibility: The dynamic proposition methods allow for more flexible and adaptable script logic.
This library is a valuable tool for any TradingView user looking to create more advanced and robust Pine scripts.
Thanks to RicardoSantos, the original author of script: tradingview.com/script/ZwFeHrgh-MathOperator.
Added new logical operators: NOT, XOR, XNOR, NAND, NOR.
MathOperator Library