How to specify session and identify lowest and highest and first and last of it in Pinescript Tradingview ?

First we define session 

 

				
					//@version=6
indicator(title = 'seesion', overlay = true, max_lines_count = 500)
session = input.session('0900-1100')
t = time(timeframe.period, session,timezone = "UTC")
bgcolor(not na(t) ? #4caf4f77 : na)
				
			

In the code above, we defined an input field to specify the session range by name. This allows users to customize the session time they want to analyze.

Next, we utilized the built-in time() function in Pine Script to check whether the current candle falls within the defined session range. Here’s how it works:

  • When the Candle is Within the Session Range: The time() function returns a valid timestamp, meaning the variable t will have a value.
  • When the Candle is Outside the Session Range: The time() function returns na (not available), indicating that the current candle is not part of the defined session.

This approach ensures we can dynamically identify and analyze specific trading sessions with precision, filtering out irrelevant data outside the chosen session.

				
					var line firstLine = na
var line lastLine = na

				
			

We declared two lines in the code, which will be used to identify and mark the first and last candles of the session for the user.

				
					
    if not na(t) and na(t[1])
        firstLine := line.new(bar_index, close, bar_index, close * 1.01, extend = extend.both, color = color.white)
				
			

so we said if t is not na at current candle but it was na at previous candle so it’s mean now is beginning of the session

				
					
    if not na(t[1]) and na(t)
        lastLine := line.new(bar_index[1], close[1], bar_index[1], close[1] * 1.01, extend = extend.both, color = #fcfcfc)
				
			

and if t is na at current candle but it was’nt na at previous candle so it’s mean one candle ago is last candle of the session.

so far we identify and marked first and last of session Now we go to mark the highest and lowest of session area

				
					var float highesthigh = na
var float lowestlow = na
var line highesthighLine = na
var line lowestlowLine = na
				
			

we declared 4 variable two float variable for declaring highest and lowest variable an two for drawing highest and lowest lines

				
					
    if not na(t) and na(t[1])
        highesthigh := high
        lowestlow := low
        highesthighLine := line.new(bar_index,highesthigh,bar_index+3,highesthigh,color = color.red,width = 2)
        lowestlowLine := line.new(bar_index,lowestlow,bar_index+3,lowestlow,color = color.yellow,width = 2)
				
			

now on beginning of every session we reassign highesthigh and lowestlow value and make 2 new lines but lines now is not correct we should manipulate  them

				
					
    if not na(t)
        if high > highesthigh 
            highesthigh := high
            line.set_y1(highesthighLine,highesthigh)
            line.set_xy2(highesthighLine,bar_index,highesthigh)
        if low < lowestlow
            lowestlow := low 
            line.set_y1(lowestlowLine,lowestlow)
            line.set_xy2(lowestlowLine,bar_index,lowestlow)
				
			

So when we are in the session area we check if current high is higher than highesthigh value so update highesthigh value with current high price and same logic for low

but still we have a small problem 

The lines are currently extending only to cover the highest high and lowest low, but we want the lines to extend further until the last candle in the session

				
					
if not na(t[1]) and na(t) 
    line.set_x2(highesthighLine,bar_index[1])
    line.set_x2(lowestlowLine,bar_index[1])
				
			

Our code is now complete!

Leave a Reply

Your email address will not be published. Required fields are marked *