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

Asked by Radhesh Joshi · SOLVED
1 reply 460 views Jump to solution Reply
Radhesh Joshi Original Post
Strategy Builder

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?

Posted Aug 8
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    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.

    Verified Solution
      Markdown supported · ```json for code blocks · paste or drop screenshots

      Be specific and kind. Never share API keys, passwords or personal account details.

      Welcome to the community

      Log in or create your account in under a minute.

      1. 1Mobile
      2. 2E-mail
      3. 3Password
      +91

      Have a password?

      By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.