βœ–

Home Forums Trading view How can I detect the first candle of the day in Pine Script?

How can I detect the first candle of the day in Pine Script?

  • Author
    Posts
    • I’m creating a TradingView indicator and I have some logic that should execute only on the first candle of a new trading day.

      I don’t want to run that logic on every candle. I want to detect when a new day starts and then perform a specific action only on that first candle.

      What is the recommended way to detect the first candle of the day in Pine Script?

    • Yes sir, you can detect the first candle of a new day by comparing the current bar’s day with the previous bar’s day.

      A simple way is to use dayofmonth:

      //@version=6
      indicator("First Candle of Day", overlay=true)
      
      newDay = ta.change(dayofmonth) != 0
      
      plotshape(
           newDay,
           title="First Candle",
           style=shape.labelup,
           location=location.belowbar,
           text="Day Start"
      )

      Here, ta.change(dayofmonth) checks whether the day has changed compared with the previous candle.

      When the day changes:

      -> newDay becomes true

      -> The current candle is treated as the first candle of the new day

      -> Your required logic can be executed only when newDay is true

      For example:

      if newDay
          // Your logic here

      You can use this for tasks such as:

      -Resetting daily variables

      -Capturing the first candle’s high and low

      -Calculating daily opening levels

      -Starting a new daily trading cycle

      -Resetting strategy conditions

      So the main idea is to detect the change from the previous day’s value rather than checking every candle individually.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top