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.
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®
import kaigouthro/permutation/1 as permute
2 — Call the generatePermutations Function:
Pine Script®
stringPermutations = permute.generatePermutations(array.from("a", "b", "c"), 2, 1)
floatPermutations = permute.generatePermutations(array.from(1.0, 2.0, 3.0), 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
Pine Script®
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...
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.
파인 라이브러리
진정한 트레이딩뷰 정신에 따라 작성자는 이 파인 코드를 오픈 소스 라이브러리로 공개하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 건배! 이 라이브러리는 개인적으로 또는 다른 오픈 소스 출판물에서 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰의 적용을 받습니다.
면책사항
이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.
파인 라이브러리
진정한 트레이딩뷰 정신에 따라 작성자는 이 파인 코드를 오픈 소스 라이브러리로 공개하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 건배! 이 라이브러리는 개인적으로 또는 다른 오픈 소스 출판물에서 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰의 적용을 받습니다.
면책사항
이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.