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:
Pine Script®
2 — Call the generatePermutations Function:
Pine Script®
• 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
Pine Script®
█ 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.
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:
import kaigouthro/permutation/1 as permute
2 — Call the generatePermutations Function:
stringPermutations = permute.generatePermutations(array.from("a", "b", "c"), 2, 1)
floatPermutations = permute.generatePermutations(array.from(1.0, 2.0, 3.0), 2, 1)
• 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
for [segmentIndex,currentSegment] in stringPermutations.segments
for [index,segment] in currentSegment.segments
// Access individual permutations within the segment.
permutation = segmennt.data
for item in permutation
// Use the permutation elements...
█ 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.
Pineライブラリ
TradingViewの精神に則り、作者はPineコードをオープンソースライブラリとして公開し、コミュニティ内の他のPineプログラマーが再利用できるようにしました。作者に敬意を表します!このライブラリを個人的に、または他のオープンソースの投稿で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。
Pineライブラリ
TradingViewの精神に則り、作者はPineコードをオープンソースライブラリとして公開し、コミュニティ内の他のPineプログラマーが再利用できるようにしました。作者に敬意を表します!このライブラリを個人的に、または他のオープンソースの投稿で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。
免責事項
これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。