This code creates a pattern analysis table written in TradingView's Pine Script v5 language. The code analyzes specific candlestick patterns and displays the presence or absence of these patterns in a table. It also provides a brief description of each pattern.
Sections of the Code and Explanations: 1. Defining the Indicator pinescript Copy Code //version=5 indicator("Formation Analysis Table", overlay=true) version=5: Specifies that the code will work in Pine Script v5. indicator: Specifies that this code is an indicator. The overlay=true parameter allows the table to be displayed above the price chart. 2. User Settings pinescript Copy the code font_size = input.int(12, title="Font Size", minval=8, maxval=24) position_option = input.string("Top Right", title="Table Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"]) font_size: Allows the user to choose the font size. position_option: Provides the option to position the table in one of the four corners of the screen. The table position is determined as follows:
pinescript Copy the code var table_position = position.top_right if (position_option == "Top Right") table_position := position.top_right else if (position_option == "Top Left") table_position := position.top_left else if (position_option == "Bottom Right") table_position := position.bottom_right else if (position_option == "Bottom Left") table_position := position.bottom_left 3. Creating the Table pinescript Copy the code var table t1 = table.new(table_position, 3, 21, frame_color=color.white, border_width=1, border_color=color.white) table.new: Creates a new table. 3: Specifies the number of columns (Formation, Status, Description). 21: Specifies the number of rows (20 patterns + title).
frame_color and border_color: Color of the chart frame.
4. Pattern Detection Functions A function is defined for each pattern. For example:
Bullish Engulfing: pinescript Copy the code bullish_engulfing() => close[1] < open[1] and close > open and close > open[1] and open < close[1] Conditions for this pattern:
Previous candle must be a bearish candle (close[1] < open[1]). Current candle must be a bullish candle (close > open). Current candle must "engulf" the entire previous candle (close > open[1] and open < close[1]). Bearish Engulfing Candle: pinescript Copy the code bearish_engulfing() => close[1] > open[1] and close < open and close < open[1] and open > close[1] Similarly, the conditions for the bearish engulfing candle are defined.
Similar logical checks are made for other formations (Doji, Hammer, Morning Star, etc.).
5. Table Headers A header is added to the first row of the table:
pinescript Copy the code if (bar_index == 0) table.cell(t1, 0, 0, text="Formation", bgcolor=color.blue, text_color=color.white, text_size=size.normal) table.cell(t1, 1, 0, text="Status", bgcolor=color.blue, text_color=color.white, text_size=size.normal) table.cell(t1, 2, 0, text="Description", bgcolor=color.blue, text_color=color.white, text_size=size.normal) The "Formation", "Status", and "Description" headers are added to the table.
6. Formations, Statuses, and Descriptions Formations, statuses, and descriptions are stored in separate arrays. pinescript Copy the code formations = array.new_string(20) array.set(formations, 0, "Bullish Engulfing") Statuses are determined by whether each formation is detected: pinescript Copy the code statuses = array.new_string(20) array.set(statuses, 0, bullish_engulfing() ? "Detected" : "Not Detected") If the formation is detected, "Detected" is written; otherwise, "Not Detected".
The descriptions tell what each pattern means: pinescript Copy the code descriptions = array.new_string(20) array.set(descriptions, 0, "Potential bullish trend.") 7. Filling the Table Cells The table is filled for each pattern:
pinescript Copy the code for i = 0 to array.size(formations) - 1 table.cell(t1, 0, i + 1, text=array.get(formations, i), bgcolor=color.blue, text_color=color.white, text_size=text_size) table.cell(t1, 1, i + 1, text=array.get(statuses, i), bgcolor=color.gray, text_color=color.white, text_size=text_size) table.cell(t1, 2, i + 1, text=array.get(descriptions, i), bgcolor=color.white, text_color=color.black, text_size=text_size) The name, status and description of each formation are written in the table with the appropriate color. Output: When this code is run, a table is displayed above the price chart:
Formation: Which formations are being analyzed. Status: Which formations have been detected. Description: What the formation means. The table visualizes formation analysis in a user-friendly and dynamic way.