Santi Santichai
Santi Santichai

Reputation: 3

I would like to plot line of high and low starting from the first day of the week and plot throughout the week

The problem is if the week start with others day than Monday, the plot line does not get high and low of that day.

For example if the week start with Tuesday (red circle) it only plot high and low of the first bar while if it is Monday (black circle) it will plot from Monday's high and low and extended throughout the week.

below is my pine script v.5, appreciate your help

//@version=5
indicator('First Bar of the Week Session High-Low', overlay=true)

var float sessionHigh = na
var float sessionLow = na

// Detect the first bar of the week
isFirstBarOfWeek = ta.change(weekofyear)


// Update session's high and low starting from the first bar of the week
if isFirstBarOfWeek
    sessionHigh := high
    sessionLow := low
    sessionLow

else if dayofweek == dayofweek.monday
    sessionHigh := math.max(high, sessionHigh)
    sessionLow := math.min(low, sessionLow)
    sessionLow

// Plotting the high and low of the session starting from the first bar of the week
plot(sessionHigh, title='Session High', color=color.new(color.blue, 0))
plot(sessionLow, title='Session Low', color=color.new(color.orange, 0))

the result

Upvotes: 0

Views: 158

Answers (1)

Arman Azadi
Arman Azadi

Reputation: 146

Your explanation was not completely clear to me but I tried to help you, I think your problem will be solved by using this code, if you have still an issue please feedback with more detail to get more support:

when weekday = Monday the highest and lowest value on that day is variable and it's clear in the chart, but on other days while Monday is the first day of the week, both lines are fixed, till the start day of the week = Tuesday, in this condition we have same trend like other start_week.

//@version=5
indicator('First Bar of the Week Session High-Low', overlay=true)

var float sessionHigh = na
var float sessionLow = na
var float week_start_day = 0

isFirstBarOfWeek = ta.change(weekofyear)

if ta.change(weekofyear) and dayofweek == dayofweek.monday
    week_start_day := 1
else if ta.change(weekofyear) and dayofweek == dayofweek.tuesday
    week_start_day := 3

if isFirstBarOfWeek
    sessionHigh := high
    sessionLow := low

if dayofweek == dayofweek.monday and week_start_day==1
    sessionHigh := math.max(high, sessionHigh)
    sessionLow := math.min(low, sessionLow)

else if dayofweek == dayofweek.tuesday and week_start_day==3
    sessionHigh := math.max(high, sessionHigh)
    sessionLow := math.min(low, sessionLow)
    
plot(sessionHigh, title='Session High', color=color.new(color.blue, 0))
plot(sessionLow, title='Session Low', color=color.new(color.orange, 0))

Upvotes: 1

Related Questions