peacefulLizard50262

TUF_LOGIC

The TUF_LOGIC library incorporates three-valued logic (also known as trilean logic) into Pine Script, enabling the representation of states beyond the binary True and False to include an 'Uncertain' state. This addition is particularly apt for financial market contexts where information may not always be black or white, accommodating scenarios of partial or ambiguous data.

Key Features:

Trilean Data Type: Defines a tri type, facilitating the representation of True (1), Uncertain (0), and False (-1) states, thus accommodating a more nuanced approach to logical evaluation.
Validation and Conversion: Includes methods like validate, to ensure trilean variables conform to expected states, and to_bool, for converting trilean to boolean values, enhancing interoperability with binary logic systems.
Core Logical Operations: Extends traditional logical operators (AND, OR, NOT, XOR, EQUALITY) to work within the trilean domain, enabling complex conditionals that reflect real-world uncertainties.
Specialized Logical Operations:
Implication Operators: Features IMP_K (Kleene's), IMP_L (Łukasiewicz's), and IMP_RM3, offering varied approaches to logical implication within the trilean framework.
Possibility, Necessity, and Contingency Operators: Implements MA ("it is possible that..."), LA ("it is necessary that..."), and IA ("it is unknown/contingent that..."), derived from Tarski-Łukasiewicz's modal logic attempts, enriching the library with modal logic capabilities.
Unanimity Functions: The UNANIMOUS operator assesses complete agreement among trilean values, useful for scenarios requiring consensus or uniformity across multiple indicators or conditions.
This library is developed to support scenarios in financial trading and analysis where decisions might hinge on more than binary outcomes. By incorporating modal logic aspects and providing a framework for handling uncertainty through the MA, LA, and IA operations, TUF_LOGIC bridges the gap between classical binary logic and the realities of uncertain information, making it a valuable tool for developing sophisticated trading strategies and analytical models.

Library "TUF_LOGIC"
3VL Implementation (TUF stands for True, Uncertain, False.)

method validate(self)
  Ensures a valid trilean variable. This works by clamping the variable to the range associated with the trilean type.
  Namespace types: tri
  Parameters:
    self (tri)
  Returns: Validated trilean object.

method to_bool(self)
  Converts a trilean object into a boolean object. True -> True, Uncertain -> na, False -> False.
  Namespace types: tri
  Parameters:
    self (tri)
  Returns: A boolean variable.

method NOT(self)
  Negates the trilean object. True -> False, Uncertain -> Uncertain, False -> True
  Namespace types: tri
  Parameters:
    self (tri)
  Returns: Negated trilean object.

method AND(self, comparator)
  Logical AND operation for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The first trilean object.
    comparator (tri): The second trilean object to compare with.
  Returns: `tri` Result of the AND operation as a trilean object.

method OR(self, comparator)
  Logical OR operation for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The first trilean object.
    comparator (tri): The second trilean object to compare with.
  Returns: `tri` Result of the OR operation as a trilean object.

method EQUALITY(self, comparator)
  Logical EQUALITY operation for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The first trilean object.
    comparator (tri): The second trilean object to compare with.
  Returns: `tri` Result of the EQUALITY operation as a trilean object, True if both are equal, False otherwise.

method XOR(self, comparator)
  Logical XOR (Exclusive OR) operation for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The first trilean object.
    comparator (tri): The second trilean object to compare with.
  Returns: `tri` Result of the XOR operation as a trilean object.

method IMP_K(self, comparator)
  Material implication using Kleene's logic for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The antecedent trilean object.
    comparator (tri): The consequent trilean object.
  Returns: `tri` Result of the implication operation as a trilean object.

method IMP_L(self, comparator)
  Logical implication using Łukasiewicz's logic for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The antecedent trilean object.
    comparator (tri): The consequent trilean object.
  Returns: `tri` Result of the implication operation as a trilean object.

method IMP_RM3(self, comparator)
  Logical implication using RM3 logic for trilean objects.
  Namespace types: tri
  Parameters:
    self (tri): The antecedent trilean object.
    comparator (tri): The consequent trilean object.
  Returns: `tri` Result of the RM3 implication as a trilean object.

method MA(self)
  Evaluates to True if the trilean object is either True or Uncertain, False otherwise.
  Namespace types: tri
  Parameters:
    self (tri): The trilean object to evaluate.
  Returns: `tri` Result of the operation as a trilean object.

method LA(self)
  Evaluates to True if the trilean object is True, False otherwise.
  Namespace types: tri
  Parameters:
    self (tri): The trilean object to evaluate.
  Returns: `tri` Result of the operation as a trilean object.

method IA(self)
  Evaluates to True if the trilean object is Uncertain, False otherwise.
  Namespace types: tri
  Parameters:
    self (tri): The trilean object to evaluate.
  Returns: `tri` Result of the operation as a trilean object.

UNANIMOUS(self, comparator)
  Evaluates the unanimity between two trilean values.
  Parameters:
    self (tri): The first trilean value.
    comparator (tri): The second trilean value.
  Returns: `tri` Returns True if both values are True, False if both are False, and Uncertain otherwise.

method UNANIMOUS(self)
  Evaluates the unanimity among an array of trilean values.
  Namespace types: array<tri>
  Parameters:
    self (array<tri>): The array of trilean values.
  Returns: `tri` Returns True if all values are True, False if all are False, and Uncertain otherwise.

tri
  Three Value Logic (T.U.F.), or trilean. Can be True (1), Uncertain (0), or False (-1).
  Fields:
    v (series int): Value of the trilean variable. Can be True (1), Uncertain (0), or False (-1).
Notas de Lançamento:
v2
UNANIMOUS Should have been a method.
Notas de Lançamento:
Added an example of its usage and I also added a .safe() method to convert undefined tri objects into default valued tri objects. This is useful when you use a request security.

v3

Added:
method safe(self)
  Ensures that the type is never na/undefined. Useful if you are using a higher timeframe..
  Namespace types: tri
  Parameters:
    self (tri)
  Returns: Converts na trilean objects to default valued objects.

Biblioteca do Pine

No verdadeiro espírito TradingView, o autor publicou este código de Pine como uma biblioteca de código aberto para que outros programadores de Pine de nossa comunidade possam reutilizá-lo. Um brinde ao autor! Você pode usar esta biblioteca em particular ou em outras publicações de código aberto, mas a reutilização deste código em uma publicação é regida pelas Regras da Casa.

Aviso legal

As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.

Gostaria de usar essa biblioteca?

Copie a seguinte linha e cole-a em seu script.