PINE LIBRARY

permutation

█ OVERVIEW

This library provides functions for generating permutations of string or float arrays, using an iterative approach where pine has no recursion. It supports allowing/limiting duplicate elements and handles large result sets by segmenting them into manageable chunks within custom Data types. The most combinations will vary, but the highest is around 250,000 unique combinations. depending on input array values and output length. it will return nothing if the input count is too low.

█ CONCEPTS

This library addresses two key challenges in Pine Script:

 • Recursion Depth Limits: Pine has limitations on recursion depth. This library uses an iterative, stack-based algorithm to generate permutations, avoiding recursive function calls that could exceed these limits.
 • Array Size Limits: Pine arrays have size restrictions. This library manages large permutation sets by dividing them into smaller segments stored within a custom Data or DataFloat type, using maps for efficient access.

█ HOW TO USE

1 — Include the Library: Add this library to your script using:




2 — Call the generatePermutations Function:



 • set: The input array of strings or floats.
 • size: The desired length of each permutation.
 • maxDuplicates (optional): The maximum allowed repetitions of an element within a single permutation. Defaults to 1.

3 — Access the Results: The function returns a Data (for strings) or DataFloat (for floats) object. These objects contain:
 • data: An array indicating which segments are present (useful for iterating).
 • segments: A map where keys represent segment indices and values are the actual permutation data within that segment.

Example: Accessing Permutations




█ TYPES

 • PermutationState/PermutationStateFloat: Internal types used by the iterative algorithm to track the state of permutation generation.
 • Data/DataFloat: Custom types to store and manage the generated permutations in segments.


█ NOTES

* The library prioritizes handling potentially large permutation sets. 250,000 i about the highest achievable.
* The segmentation logic ensures that results are accessible even when the total number of permutations exceeds Pine's array size limits.

----


Library "permutation"
This library provides functions for generating permutations of user input arrays containing either strings or floats. It uses an iterative, stack-based approach to handle potentially large sets and avoid recursion limitation. The library supports limiting the number of duplicate elements allowed in each permutation. Results are stored in a custom Data or DataFloat type that uses maps to segment large permutation sets into manageable chunks, addressing Pine Script's array size limitations.

generatePermutations(set, size, maxDuplicates)
  > Generates permutations of a given size from a set of strings or floats.
  Parameters:
    set (array<string>): (array<string> or array<float>) The set of strings or floats to generate permutations from.
    size (int): (int) The size of the permutations to generate.
    maxDuplicates (int): (int) The maximum number of times an element can be repeated in a permutation.
  Returns: (Data or DataFloat) A Data object for strings or a DataFloat object for floats, containing the generated permutations.




stringPermutations = generatePermutations(array.from("a", "b", "c"), 2, 1)
floatPermutations = generatePermutations(array.from(1.0, 2.0, 3.0), 2, 1)



generatePermutations(set, size, maxDuplicates)
  Parameters:
    set (array<float>)
    size (int)
    maxDuplicates (int)

PermutationState
  PermutationState
  Fields:
    data (array<string>): (array<string>) The current permutation being built.
    index (series int): (int) The current index being considered in the set.
    depth (series int): (int) The current depth of the permutation (number of elements).
    counts (map<string, int>): (map<string, int>) Map to track the count of each element in the current permutation (for duplicates).

PermutationStateFloat
  PermutationStateFloat
  Fields:
    data (array<float>): (array<float>) The current permutation being built.
    index (series int): (int) The current index being considered in the set.
    depth (series int): (int) The current depth of the permutation (number of elements).
    counts (map<float, int>): (map<float, int>) Map to track the count of each element in the current permutation (for duplicates).

Data
  Data
  Fields:
    data (array<string>): (array<string>) Array to indicate which segments are present.
    segments (map<int, Data>): (map<int, Data>) Map to store permutation segments. Each segment contains a subset of the generated permutations.

DataFloat
  DataFloat
  Fields:
    data (array<float>): (array<float>) Array to indicate which segments are present.
    segments (map<int, DataFloat>): (map<int, DataFloat>) Map to store permutation segments. Each segment contains a subset of the generated permutations.

arraysdebuggingpermutation

Pine腳本庫

在真正的TradingView精神中,作者將這段Pine程式碼發佈為開源程式庫,以便我們社群的其他Pine程式設計師可以重複使用它。請向作者致敬!您可以私下使用這個函式庫,或在其他開源出版品中使用,但在出版物中再次使用這段程式碼將受到網站規則的約束。

免責聲明