Standardizing time period representation in pinescript

Post Reply
SamiFX
Expert Trader
Posts: 7
Joined: Thu Nov 30, 2023 9:44 am

Standardizing time period representation in pinescript

Post by SamiFX »

While I was making a multi-timeframe indicator and wanted to analyze 4 time frames I ran into an issue.

I wanted to plot the results of that multi-timeframe analysis in a table against each time frame but the problem was there is no built-in feature to represent the input timeframe as it is in Pinescript.

Instead, it shows the time in terms of minutes for timeframes below 1 day as you can see in the image below the 12h is represented by 720 which is kind of confusing.


EURUSD_2024-11-15_18-09-46_0c0d6.png

So I had to design a custom function to tackle this issue and here is what it looks like after correction.

EURUSD_2024-11-15_18-09-19_86695.png
Here is the full code of the indicator.

Code: Select all

//@version=5
indicator("Timeframe Continuity & Trending Candles", overlay = true)

// Inputs for Timeframe Continuity Settings
tf1 = input.timeframe(defval = "15", title  = "TF1", group = "Time Frame Continuity Settings", inline = "t1", confirm = true)
tf2 = input.timeframe(defval = "30", title = "TF2", group = "Time Frame Continuity Settings", tooltip = "Select the First & Second Timeframe For Checking Timeframe Continuity", inline = "t1", confirm = true)
tf3 = input.timeframe(defval = "60", title = "TF3", group = "Time Frame Continuity Settings", inline = "t2", confirm = true)
tf4 = input.timeframe(defval = "240", title = "TF4", group = "Time Frame Continuity Settings", tooltip = "Select the Third & Fourth Timeframe For Checking Timeframe Continuity", inline = "t2", confirm = true)

// Function to convert timeframe to a string with units
get_tf_string(tf) =>
    tf_num = str.tonumber(tf)
    if not na(tf_num)
        if tf_num >= 525600 and tf_num % 525600 == 0
            str.tostring(tf_num / 525600) + "Y"
        else if tf_num >= 43200 and tf_num % 43200 == 0
            str.tostring(tf_num / 43200) + "M"
        else if tf_num >= 10080 and tf_num % 10080 == 0
            str.tostring(tf_num / 10080) + "W"
        else if tf_num >= 1440 and tf_num % 1440 == 0
            str.tostring(tf_num / 1440) + "D"
        else if tf_num >= 60 and tf_num % 60 == 0
            str.tostring(tf_num / 60) + "h"
        else
            tf + "m"
    else
        // For non-numeric timeframes like 'D', 'W', 'M'
        if tf == "D"
            "1D"
        else if tf == "W"
            "1W"
        else if tf == "M"
            "1M"
        else if tf == "Q"
            "1Q"
        else if tf == "Y"
            "1Y"
        else
            tf

// Function to detect trending candles
TrendingCandleup() =>
    high >= high[1] and low >= low[1]

TrendingCandledn() =>
    high <= high[1] and low <= low[1]

// Function to check high timeframe patterns
check_htf(tf , cond) =>
    request.security(symbol = syminfo.tickerid, timeframe = tf, expression = cond)

// Check patterns on different timeframes
bull_patterns_tf1 = check_htf(tf1, TrendingCandleup())
bear_patterns_tf1 = check_htf(tf1, TrendingCandledn())

bull_patterns_tf2 = check_htf(tf2, TrendingCandleup())
bear_patterns_tf2 = check_htf(tf2, TrendingCandledn())

bull_patterns_tf3 = check_htf(tf3, TrendingCandleup())
bear_patterns_tf3 = check_htf(tf3, TrendingCandledn())

bull_patterns_tf4 = check_htf(tf4, TrendingCandleup())
bear_patterns_tf4 = check_htf(tf4, TrendingCandledn())

// Create the table for displaying results
var table_new = table.new(position = position.bottom_right, columns = 6, rows = 3, frame_color = color.gray, frame_width = 1, border_color = color.black, border_width = 1)

table.cell(table_new, 0, 0, "TF", bgcolor = color.gray, text_color = color.white)
table.cell(table_new, 0, 1, "Pattern", bgcolor = #adadae, text_color = color.black)

table.cell(table_new, 1, 0, get_tf_string(tf1), bgcolor = color.gray, text_color = color.white)
table.cell(table_new, 1, 1, bull_patterns_tf1 ? "▲" : bear_patterns_tf1 ? "▼" : "⎯", bgcolor = bull_patterns_tf1 ? color.green : bear_patterns_tf1 ? color.red : #adadae, text_color = color.black, tooltip = bull_patterns_tf1 ? "The arrow is in the upward direction and cell color is green So the market is moving with the bullish momentum" : bear_patterns_tf1 ? "The arrow is pointing downward, and the cell color is red, indicating that the market is moving with bearish momentum." : "The cell color is grey meaning there is no clear momentum at this timeframe")

table.cell(table_new, 2, 0, get_tf_string(tf2), bgcolor = color.gray, text_color = color.white)
table.cell(table_new, 2, 1, bull_patterns_tf2 ? "▲" : bear_patterns_tf2 ? "▼" : "⎯", bgcolor = bull_patterns_tf2 ? color.green : bear_patterns_tf2 ? color.red : #adadae, text_color = color.black, tooltip = bull_patterns_tf2 ? "The arrow is in the upward direction and cell color is green So the market is moving with the bullish momentum" : bear_patterns_tf2 ? "The arrow is pointing downward, and the cell color is red, indicating that the market is moving with bearish momentum." : "The cell color is grey meaning there is no clear momentum at this timeframe")

table.cell(table_new, 3, 0, get_tf_string(tf3), bgcolor = color.gray, text_color = color.white)
table.cell(table_new, 3, 1, bull_patterns_tf3 ? "▲" : bear_patterns_tf3 ? "▼" : "⎯", bgcolor = bull_patterns_tf3 ? color.green : bear_patterns_tf3 ? color.red : #adadae, text_color = color.black, tooltip = bull_patterns_tf3 ? "The arrow is in the upward direction and cell color is green So the market is moving with the bullish momentum" : bear_patterns_tf3 ? "The arrow is pointing downward, and the cell color is red, indicating that the market is moving with bearish momentum." : "The cell color is grey meaning there is no clear momentum at this timeframe")

table.cell(table_new, 4, 0, get_tf_string(tf4), bgcolor = color.gray, text_color = color.white)
table.cell(table_new, 4, 1, bull_patterns_tf4 ? "▲" : bear_patterns_tf4 ? "▼" : "⎯", bgcolor = bull_patterns_tf4 ? color.green : bear_patterns_tf4 ? color.red : #adadae, text_color = color.black, tooltip = bull_patterns_tf4 ? "The arrow is in the upward direction and cell color is green So the market is moving with the bullish momentum" : bear_patterns_tf4 ? "The arrow is pointing downward, and the cell color is red, indicating that the market is moving with bearish momentum." : "The cell color is grey meaning there is no clear momentum at this timeframe")
Post Reply