How can I create Fibonacci levels from a specific candle in Pine Script?

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

I’m trying to create a Fibonacci indicator in Pine Script where I can select a specific candle by entering its hour and minute.

For example, if I enter 09:20 on a 5-minute chart, I want the script to identify that candle and use its High and Low to calculate the Fibonacci levels.

It’s really a help if someone tells me the right way to do this ?

Posted Aug 19
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    Yes sir, you can definitely do that.

    Instead of selecting a timeframe or a time range, we can allow the user to enter the exact candle time. The script can then identify that candle, take its High and Low, and calculate the Fibonacci levels from that range.

    For example, if the user enters 09:20:

    -Candle Time:
    09:20

    => The 09:20 candle’s High and Low will be used as the Fibonacci range.

    You can create it using:

    //@version=6
    indicator("Candle Fibonacci", overlay=true)
    
    candleHour = input.int(9, "Candle Hour")
    candleMinute = input.int(20, "Candle Minute")
    
    newDay = ta.change(time("D")) != 0
    
    var float fib0 = na
    var float fib236 = na
    var float fib382 = na
    var float fib500 = na
    var float fib618 = na
    var float fib786 = na
    var float fib100 = na
    
    if newDay
        fib0 := na
        fib236 := na
        fib382 := na
        fib500 := na
        fib618 := na
        fib786 := na
        fib100 := na
    
    isTargetCandle = hour == candleHour and minute == candleMinute
    
    if isTargetCandle
        rangeHigh = high
        rangeLow = low
        fibRange = rangeHigh - rangeLow
    
        fib0 := rangeLow
        fib236 := rangeLow + fibRange * 0.236
        fib382 := rangeLow + fibRange * 0.382
        fib500 := rangeLow + fibRange * 0.500
        fib618 := rangeLow + fibRange * 0.618
        fib786 := rangeLow + fibRange * 0.786
        fib100 := rangeHigh
    
    plot(fib0, "0%", color=color.gray)
    plot(fib236, "23.6%", color=color.yellow)
    plot(fib382, "38.2%", color=color.orange)
    plot(fib500, "50%", color=color.blue)
    plot(fib618, "61.8%", color=color.green)
    plot(fib786, "78.6%", color=color.red)
    plot(fib100, "100%", color=color.gray)

    -> The user only needs to enter the candle hour and minute.

    -> The script identifies that specific candle on the current chart timeframe.

    -> The candle’s High and Low become the Fibonacci range.

    -> The Fibonacci levels are then plotted for the rest of the day.

    => For example, on a 5-minute chart with the time set to 09:20, the 09:20 candle will be used to calculate the Fibonacci levels.

    So that’s how you can create Fibonacci levels from a specific candle in Pine Script.

    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.