TUF_LOGICTUF_LOGIC: Three-Value Logic for Pine Script v6
The TUF_LOGIC library implements a robust three-valued logic system (trilean logic) for Pine Script v6, providing a formal framework for reasoning about uncertain or incomplete information in financial markets. By extending beyond binary True/False states to include an explicit "Uncertain" state, this library enables more nuanced algorithmic decision-making, particularly valuable in environments characterized by imperfect information.
Core Architecture
TUF_LOGIC offers two complementary interfaces for working with trilean values:
Enum-Based API (Recommended): Leverages Pine Script v6's enum capabilities with Trilean.True , Trilean.Uncertain , and Trilean.False for improved type safety and performance.
Integer-Based API (Legacy Support): Maintains compatibility with existing code using integer values 1 (True), 0 (Uncertain), and -1 (False).
Fundamental Operations
The library provides type conversion methods for seamless interaction between integer representation and enum types ( to_trilean() , to_int() ), along with validation functions to maintain trilean invariants.
Logical Operators
TUF_LOGIC extends traditional boolean operators to the trilean domain with NOT , AND , OR , XOR , and EQUALITY functions that properly handle the Uncertain state according to the principles of three-valued logic.
The library implements three different implication operators providing flexibility for different logical requirements: IMP_K (Kleene's approach), IMP_L (Łukasiewicz's approach), and IMP_RM3 (Relevant implication under RM3 logic).
Inspired by Tarski-Łukasiewicz's modal logic formulations, TUF_LOGIC includes modal operators: MA (Modal Assertion) evaluates whether a state is possibly true; LA (Logical Assertion) determines if a state is necessarily true; and IA (Indeterminacy Assertion) identifies explicitly uncertain states.
The UNANIMOUS operator evaluates trilean values for complete agreement, returning the consensus value if one exists or Uncertain otherwise. This function is available for both pairs of values and arrays of trilean values.
Practical Applications
TUF_LOGIC excels in financial market scenarios where decision-making must account for uncertainty. It enables technical indicator consensus by combining signals with different confidence levels, supports multi-timeframe analysis by reconciling potentially contradictory signals, enhances risk management by explicitly modeling uncertainty, and handles partial information systems where some data sources may be unreliable.
By providing a mathematically sound framework for reasoning about uncertainty, TUF_LOGIC elevates trading system design beyond simplistic binary logic, allowing for more sophisticated decision-making that better reflects real-world market complexity.
Library "TUF_LOGIC"
Three-Value Logic (TUF: True, Uncertain, False) implementation for Pine Script.
This library provides a comprehensive set of logical operations supporting trilean logic systems,
including Kleene, Łukasiewicz, and RM3 implications. Compatible with Pine v6 enums.
method validate(self)
Ensures a valid trilean integer value by clamping to the appropriate range .
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer value to validate.
Returns: An integer value guaranteed to be within the valid trilean range.
method to_trilean(self)
Converts an integer value to a Trilean enum value.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer to convert (typically -1, 0, or 1).
Returns: A Trilean enum value: True (1), Uncertain (0), or False (-1).
method to_int(self)
Converts a Trilean enum value to its corresponding integer representation.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The Trilean enum value to convert.
Returns: Integer value: 1 (True), 0 (Uncertain), or -1 (False).
method NOT(self)
Negates a trilean integer value (NOT operation).
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer value to negate.
Returns: Negated integer value: 1 -> -1, 0 -> 0, -1 -> 1.
method NOT(self)
Negates a Trilean enum value (NOT operation).
Namespace types: series Trilean
Parameters:
self (series Trilean) : The Trilean enum value to negate.
Returns: Negated Trilean: True -> False, Uncertain -> Uncertain, False -> True.
method AND(self, comparator)
Logical AND operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The first integer value.
comparator (int) : The second integer value to compare with.
Returns: Integer result of the AND operation (minimum value).
method AND(self, comparator)
Logical AND operation for Trilean enum values following three-valued logic.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The first Trilean enum value.
comparator (series Trilean) : The second Trilean enum value to compare with.
Returns: Trilean result of the AND operation.
method OR(self, comparator)
Logical OR operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The first integer value.
comparator (int) : The second integer value to compare with.
Returns: Integer result of the OR operation (maximum value).
method OR(self, comparator)
Logical OR operation for Trilean enum values following three-valued logic.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The first Trilean enum value.
comparator (series Trilean) : The second Trilean enum value to compare with.
Returns: Trilean result of the OR operation.
method EQUALITY(self, comparator)
Logical EQUALITY operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The first integer value.
comparator (int) : The second integer value to compare with.
Returns: Integer representation (1/-1) indicating if values are equal.
method EQUALITY(self, comparator)
Logical EQUALITY operation for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The first Trilean enum value.
comparator (series Trilean) : The second Trilean enum value to compare with.
Returns: Trilean.True if both values are equal, Trilean.False otherwise.
method XOR(self, comparator)
Logical XOR (Exclusive OR) operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The first integer value.
comparator (int) : The second integer value to compare with.
Returns: Integer result of the XOR operation.
method XOR(self, comparator)
Logical XOR (Exclusive OR) operation for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The first Trilean enum value.
comparator (series Trilean) : The second Trilean enum value to compare with.
Returns: Trilean result of the XOR operation.
method IMP_K(self, comparator)
Material implication using Kleene's logic for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The antecedent integer value.
comparator (int) : The consequent integer value.
Returns: Integer result of Kleene's implication operation.
method IMP_K(self, comparator)
Material implication using Kleene's logic for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The antecedent Trilean enum value.
comparator (series Trilean) : The consequent Trilean enum value.
Returns: Trilean result of Kleene's implication operation.
method IMP_L(self, comparator)
Logical implication using Łukasiewicz's logic for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The antecedent integer value.
comparator (int) : The consequent integer value.
Returns: Integer result of Łukasiewicz's implication operation.
method IMP_L(self, comparator)
Logical implication using Łukasiewicz's logic for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The antecedent Trilean enum value.
comparator (series Trilean) : The consequent Trilean enum value.
Returns: Trilean result of Łukasiewicz's implication operation.
method IMP_RM3(self, comparator)
Logical implication using RM3 logic for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The antecedent integer value.
comparator (int) : The consequent integer value.
Returns: Integer result of the RM3 implication operation.
method IMP_RM3(self, comparator)
Logical implication using RM3 logic for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The antecedent Trilean enum value.
comparator (series Trilean) : The consequent Trilean enum value.
Returns: Trilean result of the RM3 implication operation.
method MA(self)
Modal Assertion (MA) operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer value to evaluate.
Returns: 1 if the value is 1 or 0, -1 if the value is -1.
method MA(self)
Modal Assertion (MA) operation for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The Trilean enum value to evaluate.
Returns: Trilean.True if value is True or Uncertain, Trilean.False if value is False.
method LA(self)
Logical Assertion (LA) operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer value to evaluate.
Returns: 1 if the value is 1, -1 otherwise.
method LA(self)
Logical Assertion (LA) operation for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The Trilean enum value to evaluate.
Returns: Trilean.True if value is True, Trilean.False otherwise.
method IA(self)
Indeterminacy Assertion (IA) operation for trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The integer value to evaluate.
Returns: 1 if the value is 0, -1 otherwise.
method IA(self)
Indeterminacy Assertion (IA) operation for Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The Trilean enum value to evaluate.
Returns: Trilean.True if value is Uncertain, Trilean.False otherwise.
method UNANIMOUS(self, comparator)
Evaluates the unanimity between two trilean integer values.
Namespace types: series int, simple int, input int, const int
Parameters:
self (int) : The first integer value.
comparator (int) : The second integer value.
Returns: Integer value of self if both values are equal, 0 (Uncertain) otherwise.
method UNANIMOUS(self, comparator)
Evaluates the unanimity between two Trilean enum values.
Namespace types: series Trilean
Parameters:
self (series Trilean) : The first Trilean enum value.
comparator (series Trilean) : The second Trilean enum value.
Returns: Value of self if both values are equal, Trilean.Uncertain otherwise.
method UNANIMOUS(self)
Evaluates the unanimity among an array of trilean integer values.
Namespace types: array
Parameters:
self (array) : The array of integer values.
Returns: First value if all values are identical, 0 (Uncertain) otherwise.
method UNANIMOUS(self)
Evaluates the unanimity among an array of Trilean enum values.
Namespace types: array
Parameters:
self (array) : The array of Trilean enum values.
Returns: First value if all values are identical, Trilean.Uncertain otherwise.