PINE LIBRARY

[ALGOA+] Markov Chains Library by @metacamaleo

מעודכן
Library "MarkovChains"

[ALGOA+] Markov Chains library by @metacamaleo. Created in 09/08/2024.

This library provides tools to calculate and visualize Markov Chain-based transition matrices and probabilities. This library supports two primary algorithms: a rolling window Markov Chain and a conditional Markov Chain (which operates based on specified conditions). The key concepts used include Markov Chain states, transition matrices, and future state probabilities based on past market conditions or indicators.

Key functions:
- `mc_rw()`: Builds a transition matrix using a rolling window Markov Chain, calculating probabilities based on a fixed length of historical data.
- `mc_cond()`: Builds a conditional Markov Chain transition matrix, calculating probabilities based on the current market condition or indicator state.
Basically, you will just need to use the above functions on your script to default outputs and displays.

Exported UDTs include:
- s_map: An UDT variable used to store a map with dummy states, i.e., if possible states are bullish, bearish, and neutral, and current is bullish, it will be stored
in a map with following keys and values: "bullish", 1; "bearish", 0; and "neutral", 0. You will only use it to customize your own script, otherwise, it´s only for internal use.
- mc_states: This UDT variable stores user inputs, calculations and MC outputs. As the above, you don´t need to use it, but you may get features to customize your own script.

For example, you may use mc.tm to get the transition matrix, or the prob map to customize the display. As you see, functions are all based on mc_states UDT. The s_map UDT is used within mc_states´s s array.

Optional exported functions include:
- `mc_table()`: Displays the transition matrix in a table format on the chart for easy visualization of the probabilities.
- `display_list()`: Displays a map (or array) of string and float/int values in a table format, used for showing transition counts or probabilities.
- `mc_prob()`: Calculates and displays probabilities for a given number of future bars based on the current state in the Markov Chain.
- `mc_all_states_prob()`: Calculates probabilities for all states for future bars, considering all possible transitions.

The above functions may be used to customize your outputs. Use the returned variable mc_states from mc_rw() and mc_cond() to display each of its matrix, maps or arrays using mc_table() (for matrices) and display_list() (for maps and arrays) if you desire to debug or track the calculation process.

See the examples in the end of this script.

Have good trading days!
Best regards,
metacamaleo

-----------------------------
KEY FUNCTIONS

mc_rw(state, length, states, pred_length, show_table, show_prob, table_position, prob_position, font_size)
  Builds the transition matrix for a rolling window Markov Chain.
  Parameters:
    state (string): The current state of the market or system.
    length (int): The rolling window size.
    states (array<string>): Array of strings representing the possible states in the Markov Chain.
    pred_length (int): The number of bars to predict into the future.
    show_table (bool): Boolean to show or hide the transition matrix table.
    show_prob (bool): Boolean to show or hide the probability table.
    table_position (string): Position of the transition matrix table on the chart.
    prob_position (string): Position of the probability list on the chart.
    font_size (string): Size of the table font.
  Returns: The transition matrix and probabilities for future states.

mc_cond(state, condition, states, pred_length, show_table, show_prob, table_position, prob_position, font_size)
  Builds the transition matrix for conditional Markov Chains.
  Parameters:
    state (string): The current state of the market or system.
    condition (string): A string representing the condition.
    states (array<string>): Array of strings representing the possible states in the Markov Chain.
    pred_length (int): The number of bars to predict into the future.
    show_table (bool): Boolean to show or hide the transition matrix table.
    show_prob (bool): Boolean to show or hide the probability table.
    table_position (string): Position of the transition matrix table on the chart.
    prob_position (string): Position of the probability list on the chart.
    font_size (string): Size of the table font.
  Returns: The transition matrix and probabilities for future states based on the HMM.
הערות שחרור
v2
Just a change in the output of mc_rw() and mc_cond() to return the mc_states variable with all the results.
arraysmarkovchainprobabiltystatisticstechindicator

ספריית Pine

ברוח TradingView אמיתית, המחבר פרסם קוד Pine זה כספריית קוד פתוח כדי שמתכנתי Pine אחרים מהקהילה שלנו יוכלו לעשות בו שימוש חוזר. כל הכבוד למחבר! אתה יכול להשתמש בספרייה זו באופן פרטי או בפרסומים אחרים בקוד פתוח, אך שימוש חוזר בקוד זה בפרסום כפוף לכללי הבית.


גם על:

כתב ויתור