This script is designed to visually highlight certain price levels during a specified trading session on an intraday chart (10:45AM). It draws horizontal lines at the high and low prices of the session. The problem is that when I change the timeframe, the lines changes its location. I want lines to be drawn on the high and low on 10:45 AM candle, and not change when I change the timeframe.
below the code plot diffrent lines value. I just want to plot same value whenever I change the timeframe. I tried to use request.security function but not working.
//@version=6
indicator('Lines', overlay = true, max_lines_count = 500)
// Define the trading session
tradingSession = input.session('1045-1525', title='Trading Session')
// Function to determine if the current bar is the first bar of the session
isSessionFirstBar() =>
t = time('D', tradingSession)
na(t[1]) and not na(t) or t[1] < t
// Check if today is the current day
isToday = (year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time))
// Condition to check if we're in an intraday timeframe and it's the first bar of the session
intraCondition = timeframe.isintraday and isSessionFirstBar()
// Draw lines if the condition is met
if intraCondition
// Draw lines at high and low for 6 hours into the session
line.new(time, high, time + int(6 * 60 * 60 * 1000), high, xloc.bar_time, width = 2, color=color.green)
line.new(time, low, time + int(6 * 60 * 60 * 1000), low, xloc.bar_time, width = 2, color=color.red)
This script is designed to visually highlight certain price levels during a specified trading session on an intraday chart (10:45AM). It draws horizontal lines at the high and low prices of the session. The problem is that when I change the timeframe, the lines changes its location. I want lines to be drawn on the high and low on 10:45 AM candle, and not change when I change the timeframe.
below the code plot diffrent lines value. I just want to plot same value whenever I change the timeframe. I tried to use request.security function but not working.
//@version=6
indicator('Lines', overlay = true, max_lines_count = 500)
// Define the trading session
tradingSession = input.session('1045-1525', title='Trading Session')
// Function to determine if the current bar is the first bar of the session
isSessionFirstBar() =>
t = time('D', tradingSession)
na(t[1]) and not na(t) or t[1] < t
// Check if today is the current day
isToday = (year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time))
// Condition to check if we're in an intraday timeframe and it's the first bar of the session
intraCondition = timeframe.isintraday and isSessionFirstBar()
// Draw lines if the condition is met
if intraCondition
// Draw lines at high and low for 6 hours into the session
line.new(time, high, time + int(6 * 60 * 60 * 1000), high, xloc.bar_time, width = 2, color=color.green)
line.new(time, low, time + int(6 * 60 * 60 * 1000), low, xloc.bar_time, width = 2, color=color.red)
Share
Improve this question
edited Feb 14 at 6:00
DarkBee
15.6k8 gold badges72 silver badges116 bronze badges
asked Feb 14 at 5:49
Colin KalColin Kal
113 bronze badges
1 Answer
Reset to default 0So, this is actually a complicated task. Your script will be executed on each bar. For a 5-min timeframe, getting the 5min range at 10:45 is not a problem because there is a bar at 10:45.
However, when you switch to 10-min timeframe, it is not as easy. Because you have a bar at 10:40 and then 10:50. So, 10:45 is somehow intrabar price action.
To get that, you should use request.security_lower_tf()
and try to find your range from the array it returns.
Here is a simplified code to get you started, however, there are some corner cases you should be checking. Below code is designed to get the 5 min range at 10:45 from higher timeframes.
//@version=6
indicator('Lines', overlay = true, max_lines_count = 500)
start_time_unix = timestamp(year, month, dayofmonth, 10, 45, 0)
was_sess_start_on_mid_prev_bar = (time[1] < start_time_unix) and (time > start_time_unix)
[prev_high, prev_low, prev_time] = request.security_lower_tf(syminfo.tickerid, "5", [high[1], low[1], time[1]])
len_time = array.size(prev_time)
sess_high = 0.0
sess_low = 0.0
if ((len_time > 0) and was_sess_start_on_mid_prev_bar)
for i = 0 to len_time - 1
t = array.get(prev_time, i)
if (t == start_time_unix)
sess_high := array.get(prev_high, i)
sess_low := array.get(prev_low, i)
break
if (timeframe.isintraday and was_sess_start_on_mid_prev_bar)
// Draw lines at high and low for 6 hours into the session
line.new(time[1], sess_high, time + int(6 * 60 * 60 * 1000), sess_high, xloc.bar_time, width = 2, color=color.green)
line.new(time[1], sess_low, time + int(6 * 60 * 60 * 1000), sess_low, xloc.bar_time, width = 2, color=color.red)