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).
版本注释:
v2
UNANIMOUS Should have been a method.
版本注释:
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.

Pine脚本库

本着真正的TradingView精神,作者将此Pine代码以开源脚本库发布,以便我们社区的其他Pine程序员可以重用它。向作者致敬!您可以私下或在其他开源出版物中使用此库,但在出版物中重用此代码受网站规则约束。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想使用这个脚本库吗?

复制以下行并将其粘贴到您的脚本中。