ZwiTrader
ZwiTrader

Reputation: 345

How to plot only within a range defined by x number of bars?

I understand that I can contingently plot on the chart using range of date and time values.

However, I would like to be able to plot using a specific number of candlesticks.

For example, I would like to be able to say something like

// Within the most recent 10 bars
// If close[0] > close[1]
// plotshape()

I have tried implementing numerous variations using barstate.isrealtime or barstate.islast but I keep running into limitations.

One major problem is that, although bar_index[#] works by indexing backwards from the most recent bar, the value of bar_index[10] is not 10 but some number in the thousands (depending on the timeframe of the chart — for me its Daily = 2,616 candles, 1hr = 6,217 candles, 15m = 5,222, etc.). In other words, it counts the number of bars from the oldest bar available.

Since referencing of the bars (starting from most recent) and the index values (starting from the oldest) are conflicting--due to counting from opposite ends--I am not sure how to specify: plotshape() for the most recent 10 bars.

I am also running into trouble due to the fact that bar_index[0] occurs every single iteration of the chart's timeframe--so I am getting caught in recursive calculations when trying to do bar_index[0]-bar_index[10].

It seems that what I need is something like bar_index.islast[10]

The reason that I would like to call a plotshape() based on the number of specified candles versus since x date/time (or within date range (x,y)), is because I want my indicator to function properly regardless of which timeframe my chart is displaying:

If I am showing a monthly chart, I want to plot across the last 10 monthly bars; If I am showing a daily chart, I want to plot across the last 10 daily bars; etc.

If I am forced to use a date range, then this functionality breaks down, since I will be shown increasingly more bars for smaller timeframes.

Caveat** I am able to kinda make this work by specifying the number of bars from the oldest candlestick by stating something like:

bar_index > 2600 ? color=color.black : na

However, given the fact that every single time frame displays a different number of bars, this is not a workable solution for me.

Thanks for any advice.

UPDATE I have been hunting around and and found that the functionality I desire is already built into the show_last argument of the various plot()functoins.

Ill leave my question posted, in case it helps someone else.

Upvotes: 0

Views: 1992

Answers (1)

ZwiTrader
ZwiTrader

Reputation: 345

UPDATE I have been hunting around and and found that the functionality I desire is already built into the show_last = int argument of the various plot() functions.

I'll leave my question posted, in case it helps someone else.

Upvotes: 1

Related Questions