PINE LIBRARY
تم تحديثه lib_statemachine_modified

Library "lib_statemachine_modified"
Modified to fix bugs and create getState and priorState methods.
method step(this, before, after, condition)
Namespace types: StateMachine
Parameters:
this (StateMachine)
before (int): (int): Current state before transition
after (int): (int): State to transition to
condition (bool): (bool): Condition to trigger the transition
Returns: (bool): True if the state changed, else False
method step(this, after, condition)
Namespace types: StateMachine
Parameters:
this (StateMachine)
after (int): (int): State to transition to
condition (bool): (bool): Condition to trigger the transition
Returns: (bool): True if the state changed, else False
method currentState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
method previousState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
method changed(this, within_bars)
Namespace types: StateMachine
Parameters:
this (StateMachine)
within_bars (int): (int): Number of bars to look back for a state change
Returns: (bool): True if a state change occurred within the timeframe, else False
method reset(this, condition, min_occurrences)
Namespace types: StateMachine
Parameters:
this (StateMachine)
condition (bool): (bool): Condition to trigger the reset
min_occurrences (int): (int): Minimum number of times the condition must be true to reset
Returns: (bool): True if the state was reset, else False
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_state (series int)
last_change_bar (series int)
Modified to fix bugs and create getState and priorState methods.
method step(this, before, after, condition)
Namespace types: StateMachine
Parameters:
this (StateMachine)
before (int): (int): Current state before transition
after (int): (int): State to transition to
condition (bool): (bool): Condition to trigger the transition
Returns: (bool): True if the state changed, else False
method step(this, after, condition)
Namespace types: StateMachine
Parameters:
this (StateMachine)
after (int): (int): State to transition to
condition (bool): (bool): Condition to trigger the transition
Returns: (bool): True if the state changed, else False
method currentState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
method previousState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
method changed(this, within_bars)
Namespace types: StateMachine
Parameters:
this (StateMachine)
within_bars (int): (int): Number of bars to look back for a state change
Returns: (bool): True if a state change occurred within the timeframe, else False
method reset(this, condition, min_occurrences)
Namespace types: StateMachine
Parameters:
this (StateMachine)
condition (bool): (bool): Condition to trigger the reset
min_occurrences (int): (int): Minimum number of times the condition must be true to reset
Returns: (bool): True if the state was reset, else False
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_state (series int)
last_change_bar (series int)
ملاحظات الأخبار
v2Added:
method didWeJustChangeThisBar(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
method didWeChangeFromTo(this, fromState, toState, within_bars)
Namespace types: StateMachine
Parameters:
this (StateMachine)
fromState (int)
toState (int)
within_bars (int)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_state (series int)
last_change_bar (series int)
last_distinct_prior_state (series int)
ملاحظات الأخبار
v3Added:
method getLatestState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_state (series int)
last_change_bar (series int)
last_distinct_prior_state (series int)
stateHistory (array<float>)
ملاحظات الأخبار
v4Added:
method changedPriorToCurrentBar(this, within_bars)
Namespace types: StateMachine
Parameters:
this (StateMachine)
within_bars (int): (int): Number of bars to look back for a state change
Returns: (bool): True if a state change occurred within the timeframe, else False
Removed:
method changed(this, within_bars)
ملاحظات الأخبار
v5Added:
method getLatestIndexOfState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
StateRecord
Fields:
state (series int)
bar_index (series int)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_state (series int)
last_change_bar (series int)
last_distinct_prior_state (series int)
stateHistory (array<StateRecord>)
ملاحظات الأخبار
Library: lib_statemachine_modified (v6)Types
StateRecord
Fields:
state (int): The state value recorded at a given moment.
bar_index (int): The bar index when that state was recorded.
StateMachine
Fields:
state (int): The current state of the machine (default 0).
neutral (int): The designated neutral (default) state.
enabled (bool): Flag to enable/disable state transitions.
reset_counter (int): Counter tracking how many times the reset condition has been met.
prior_state (int): The state immediately preceding the current state.
last_change_bar (int): The bar index at which the last state change occurred (or na if none).
last_distinct_prior_state (int): The last distinct state before the current transition.
stateHistory (StateRecord[]): An array storing a history of state transitions.
Methods
step(this, before, after, condition)
Namespace: StateMachine
Parameters:
this (StateMachine): The state machine instance.
before (int): The expected current state before the transition.
after (int): The target state to transition to.
condition (bool): Condition that triggers the transition.
Returns: (bool) — true if the state transition occurs; otherwise, false.
Notes:
If after differs from the current state, a new StateRecord is appended to stateHistory.
Updates prior_state, last_change_bar, and resets the reset_counter.
Overloaded step(this, after, condition)
Uses the current state as the expected before value.
Parameters:
this (StateMachine)
after (int): The target state.
condition (bool): Trigger condition.
Returns: (bool) — true if the state changed; otherwise, false.
currentState(this)
Parameters: this (StateMachine)
Returns: (int) — The current state.
previousState(this)
Parameters: this (StateMachine)
Returns: (int) — The state immediately prior to the current state change.
changedPriorToCurrentBar(this, within_bars)
Parameters:
this (StateMachine)
within_bars (int): Number of bars to look back for a state change (excluding the current bar).
Returns: (bool) — true if a state change occurred within the specified timeframe; otherwise, false.
didWeJustChangeThisBar(this)
Parameters: this (StateMachine)
Returns: (bool) — true if the state changed exactly on the current bar; otherwise, false.
didWeChangeFromTo(this, fromState, toState, within_bars)
Parameters:
this (StateMachine)
fromState (int): The state expected before the transition.
toState (int): The state expected after the transition.
within_bars (int): The timeframe (in bars) within which the transition should have occurred.
Returns: (bool) — true if the specific transition occurred recently; otherwise, false.
reset(this, condition, min_occurrences)
Parameters:
this (StateMachine)
condition (bool): The condition that may trigger a reset.
min_occurrences (int): The number of times the condition must be met before the reset occurs.
Returns: (bool) — true if the state machine was reset to the neutral state; otherwise, false.
getLatestState(this)
Parameters: this (StateMachine)
Returns: (int) — The latest recorded state from stateHistory (or na if none exists).
getLatestIndexOfState(this)
Parameters: this (StateMachine)
Returns: (int) — The bar index of the latest state record (or na if stateHistory is empty).
barsSinceState(this, searchState)
Parameters:
this (StateMachine)
searchState (int): The state to search for in the history.
Returns: (int) — The number of bars since the specified state was last active; returns na if that state has never been active.
Notes:
Iterates through stateHistory (accessed in reverse order) to find the most recent occurrence of searchState and computes the difference between the current bar index and the recorded bar index.
ملاحظات الأخبار
v7Updated:
method barsSinceState(this, searchState, isLast)
Namespace types: StateMachine
Parameters:
this (StateMachine)
searchState (int): (int): The state to search for.
isLast (bool): (bool): (Optional, default = true)
If true, returns the number of bars elapsed since the most recent record where the state is active
(i.e. where record.state == searchState);
if false, returns the number of bars elapsed since the record where the state was first triggered
(i.e. where record.before == searchState).
Returns: (int): Number of bars since the state was active, or na if never active.
StateRecord
Fields:
before (series int)
state (series int)
barIndex (series int)
ملاحظات الأخبار
v8Added:
method previousBarState(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
ملاحظات الأخبار
v9Added:
method print_transitions(this, n)
Namespace types: StateMachine
Parameters:
this (StateMachine)
n (int): (int): The number of most recent transitions to include in the log.
Returns: (string): A log of the last N state transitions.
method wasConditionMetOnBar(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_bar_state (series int)
last_change_bar (series int)
last_condition_bar (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
ملاحظات الأخبار
v10Fix issue with print_transitions
ملاحظات الأخبار
v11ملاحظات الأخبار
v12Added:
method setStateNames(this, mapping)
Namespace types: StateMachine
Parameters:
this (StateMachine)
mapping (array<string>)
method getStateName(this, s)
Namespace types: StateMachine
Parameters:
this (StateMachine)
s (int)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_bar_state (series int)
last_change_bar (series int)
last_condition_bar (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
ملاحظات الأخبار
v13Added:
method print_transitions_array(this, n)
Namespace types: StateMachine
Parameters:
this (StateMachine)
n (int): (int): The number of most recent transitions to include in the array (default is 2).
Returns: (int[]): An array of state integers from the last n transitions.
ملاحظات الأخبار
v14Added:
method statesArrayToText(this, states)
Namespace types: StateMachine
Parameters:
this (StateMachine)
states (array<int>): (int[]): An array of state integers.
Returns: (string): A string with each state name on a separate line.
method print_transitions_names(this, n)
Namespace types: StateMachine
Parameters:
this (StateMachine)
n (int): (int): The number of most recent transitions to include (default is 2).
Returns: (string): A formatted string where each state's name is on a new line.
ملاحظات الأخبار
v15- fix bug method statesArrayToText(StateMachine this, int[] states) check for empty array
ملاحظات الأخبار
v16Added:
method getOverlappingTransitions(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
StateOverlap
Fields:
state1 (series int)
state2 (series int)
barIndex (series int)
Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
prior_bar_state (series int)
last_change_bar (series int)
last_condition_bar (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
ملاحظات الأخبار
v17Added:
method printOverlappingTransitions(this)
Namespace types: StateMachine
Parameters:
this (StateMachine)
ملاحظات الأخبار
v18Added:
method showStateHistory(this, s)
Namespace types: StateMachine
Parameters:
this (StateMachine)
s (int): (int): The state to search for in the state history.
Returns: (string): A summary message indicating how many labels were created.
ملاحظات الأخبار
v19fix bugs
ملاحظات الأخبار
v20 Fixing the bug in previousState
ملاحظات الأخبار
v21ملاحظات الأخبار
v22Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
last_change_bar (series int)
last_condition_bar (series int)
prev_state (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
ملاحظات الأخبار
v23Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
last_change_bar (series int)
last_condition_bar (series int)
prior_bar_state (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
ملاحظات الأخبار
v24ملاحظات الأخبار
v25 update wasConditionMetOnBarملاحظات الأخبار
v26Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
last_processed_bar (series int)
last_change_bar (series int)
last_condition_bar (series int)
prior_bar_state (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
ملاحظات الأخبار
v27Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
last_processed_bar (series int)
last_change_bar (series int)
last_condition_bar (series int)
prior_bar_state (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
just_changed_flag (series bool)
ملاحظات الأخبار
v28Updated:
StateMachine
Fields:
state (series int)
neutral (series int)
enabled (series bool)
reset_counter (series int)
last_processed_bar (series int)
last_change_bar (series int)
last_condition_bar (series int)
prior_bar_state (series int)
previous_distinct_state (series int)
transitions_this_bar (series int)
stateHistory (array<StateRecord>)
stateNames (array<string>)
overlapList (array<StateOverlap>)
just_changed_flag (series bool)
lastTrueConditionState (series int)
ملاحظات الأخبار
v29Added:
method visualizeStateTransition(this, labelPrefix)
Namespace types: StateMachine
Parameters:
this (StateMachine)
labelPrefix (string)
method debugStateTransition(this, expectedFromState, expectedToState)
Namespace types: StateMachine
Parameters:
this (StateMachine)
expectedFromState (int)
expectedToState (int)
Updated:
method didWeChangeFromTo(this, fromState, toState, within_bars)
Namespace types: StateMachine
Parameters:
this (StateMachine)
fromState (int): (int): The state expected before the transition
toState (int): (int): The state expected after the transition
within_bars (int): (int): Number of bars within which the transition must have occurred
Returns: (bool): True if the specific transition occurred recently, else False
Enhanced version of didWeChangeFromTo with more robust checking
ملاحظات الأخبار
v30 fix bugمكتبة باين
كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشورات تخضع لقواعد الموقع.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
مكتبة باين
كمثال للقيم التي تتبناها TradingView، نشر المؤلف شيفرة باين كمكتبة مفتوحة المصدر بحيث يمكن لمبرمجي باين الآخرين من مجتمعنا استخدامه بحرية. تحياتنا للمؤلف! يمكنك استخدام هذه المكتبة بشكل خاص أو في منشورات أخرى مفتوحة المصدر، ولكن إعادة استخدام هذا الرمز في المنشورات تخضع لقواعد الموقع.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.