5

What I want to get is the length of the series displayed on screen. Is this possible or must one specify length?

Phil Cogan
  • 51
  • 1
  • 4
  • 1
    `bar_index` returns the current bar index. That would tell you how many bars the chart has. Is that what you want? – vitruvius Jan 13 '20 at 19:40
  • What I want to do is find the number of bars displayed. barindex returns the total bars since the beginning of the series. I want a look back period of what is displayed on the screen. – Phil Cogan Jan 13 '20 at 21:24
  • That's not possible. What are you planning to do with that information? – vitruvius Jan 14 '20 at 09:57
  • 2
    I was try to find a pattern in the visible bars. I give up. Pinescript sucks. I have done similar (not on tradingview) with python, no problem. I am a premium tradingview subscriber. – Phil Cogan Feb 11 '20 at 16:08

5 Answers5

1

if you want to count the number of bars from a specific time period, then here's the pine script for this:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PHStudios

//@version=4
study("Candle Counter", shorttitle="CCount", overlay=true)

timeYear = input(2021, title="Year", minval=1991, maxval=2100, type=input.integer)
timeMonth = input(6, title="Month", minval=1, maxval=12, type=input.integer)
timeDay = input(21, title="Day", minval=1, maxval=31, type=input.integer)
timeHours = input(9, title="Hours", minval=0, maxval=23, type=input.integer)
timeMinutes = input(15, title="Minutes", minval=0, maxval=59, type=input.integer)
timeSeconds = input(0, title="Seconds", minval=0, maxval=59, type=input.integer)

// Initilization of variables only once
var greenCandleCount = 0 
var redCandleCount = 0
var totalCandleCount = 0

// Reset Candle Count to 0 from a particular time interval
if(year == timeYear and month == timeMonth and dayofmonth == timeDay and hour == timeHours and minute == timeMinutes and second == timeSeconds)
    greenCandleCount := 0
    redCandleCount := 0
    totalCandleCount := 0

// Counting Total Number of Green, Red, and Sum of Both Candles
if(close > open)
    greenCandleCount += 1
    totalCandleCount += 1

if(open > close)
    redCandleCount += 1
    totalCandleCount += 1

plotchar(greenCandleCount, title="Green Bars", color=color.green, char='')
plotchar(redCandleCount, title="Red Bars", color=color.red, char='')
plotchar(totalCandleCount, title="Total Bars", color=color.black, char='')

Just select the exact date and time that you want to count the bars from and get the output plotted on the chart.

PH Studios
  • 11
  • 1
1

I have tested a simpler way to do it in version 5:

//Calculate the visible bars
tf = timeframe.in_seconds(timeframe.period) // Convert timeframe to int (number of seconds)
delta = chart.right_visible_bar_time  -  chart.left_visible_bar_time // Time diffemrence between rightmost and leftmost visible bars
barDuration = time - time[1] // Time difference between consecutive bars
numBars = math.ceil(delta / barDuration) // Number of visible bars
  • legit! thank you... were you using 'tf' for some added functionality? – Ashton Engberg May 24 '23 at 08:36
  • 1
    No problem. I was trying to feed my indicator with the number of visible bars in order to find some of its max/min values within this time range. 'tf' is required to do the computation. – Cristian Dinca Jun 08 '23 at 10:13
0

To know the number of the visibles bar on my tradingview screen, I made this little script.
It use chart.left_visible_bar_time and chart.right_visible_bar_time to get the range of the time displayed on my screen.
Then I use timeframe.period to get the resolution chosen for my screen.

After some calculation, because the string obtained by timeframe.period is a bit strange (You can get 1D, 2M ou 72...), I display the number of visible bars at the middle top of my screen.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mentalRock19315

//@version=5
indicator("Number of visible bars",overlay = true)
start_time = chart.left_visible_bar_time // get unix time in milli seconds
end_time = chart.right_visible_bar_time
resolution = timeframe.period
var affichage = table.new(position.top_center, 1,1,bgcolor=color.white)

if barstate.islastconfirmedhistory
    second_divider = 0
    // Convert the screen timeframe in seconds
    if str.contains(resolution, "S")
        if str.length(resolution) == 1
            second_divider := 1
        else
            second_divider := int(str.tonumber(str.substring(resolution, 0,str.length(resolution)-1)))
    else if str.contains(resolution, "D")
        if str.length(resolution) == 1
            second_divider := 60*60*24
        else
            second_divider := int(str.tonumber(str.substring(resolution, 0,str.length(resolution)-1)))*60*60*24
    else if str.contains(resolution, "W")
        if str.length(resolution) == 1
            second_divider := 60*60*24*7
        else
            second_divider := int(str.tonumber(str.substring(resolution, 0,str.length(resolution)-1)))*60*60*24*7
    else if str.contains(resolution, "M")
        if str.length(resolution) == 1
            second_divider := 60*60*24*365/12
        else
            second_divider := int(str.tonumber(str.substring(resolution, 0,str.length(resolution)-1)))*60*60*24*365/12
    else
        if str.length(resolution) == 1
            second_divider := 60
        else
            second_divider := int(str.tonumber(str.substring(resolution, 0,str.length(resolution)-1)))*60

    if second_divider > 0
        Visibles_bars = (end_time-start_time)/1000/second_divider + 1
        table.cell(affichage, column=0, row=0, text="Visible bars = " + str.tostring(Visibles_bars,"#"))

I made this open library on Pinescript you can use to get the number of visible bars, just import it in your code :
import mentalRock19315/NumberOfVisibleBars/3

and then you can get the number of visibles bars in the variable of your choice :

a = NumberOfVisibleBars.NumberOfVisibleBars()

I hope it helps.

G.Lebret
  • 2,826
  • 2
  • 16
  • 27
0

I came up with a simpler solution that works correctly even with charts having discontinuous sessions.

Set the maximum no. of candles you would like to process.

Run this loop to find the number of visible bars:

    MAX_BAR_COUNT = 150
    numBars = 0 // Number of visible bars
    for int i = 0 to 1000
        if time[i] < chart.left_visible_bar_time
            break
        numBars += 1
Abhishek Kumar
  • 197
  • 2
  • 15
0
//usable positive number
numBars := ta.valuewhen(time == chart.left_visible_bar_time, math.abs(bar_index - last_bar_index),0) 
Rich
  • 6,470
  • 15
  • 32
  • 53
SGJOE
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 14 '23 at 21:14
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 23 '23 at 00:45