4

I'd like to plot an indicator only for the last x periods. How do I do that?

If I could do time operations (substract x * period from plotStartDate), maybe I could use this code:

period = timeframe.ismonthly or timeframe.isweekly ? "12M" : "M"
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? mydata : na, color=mydata != mydata[1]:na, style=plot.style_line, linewidth=2)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
cody
  • 6,389
  • 15
  • 52
  • 77

2 Answers2

9

Version 1

Not sure this is what you're looking for. It uses plot()'s show_last= parameter to restrict the number of last bars plotted after your isPlotDate constraint has been satisfied:

//@version=4
study("", "", true)
xPeriods = input(10)
plotStartDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), 00, 00)
isPlotDate = time >= plotStartDate
plot(isPlotDate ? close : na, show_last = xPeriods)

Version 2

//@version=4
study("Plot starting n months back", "", true)
monthsBack      = input(3, minval = 0)
monthsExtra     = monthsBack % 12
monthsExcedent  = month(timenow) - monthsExtra
yearsBack       = floor(monthsBack / 12) + (monthsExcedent <= 0 ? 1 : 0)
targetMonth     = monthsExcedent <= 0 ? 12 + monthsExcedent : monthsExcedent
targetYearMonth = year == year(timenow) - yearsBack and month == targetMonth
beginMonth      = not targetYearMonth[1] and targetYearMonth

var float valueToPlot = na
if beginMonth
    valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)

enter image description here

Version 3

Simpler:

//@version=4
study("Plot starting n months back", "", true)
monthsBack = input(3, minval = 0)

targetDate = time >= timestamp(year(timenow), month(timenow) - monthsBack, 1, 0, 0, 0)
beginMonth = not targetDate[1] and targetDate

var float valueToPlot = na
if beginMonth
    valueToPlot := high
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Ahh, alright! That plots only the last X bars now (not the last X periods). So I only have to multiply that X number with the amount of bars in the period. Thank you!! :-) – cody Feb 24 '20 at 12:07
  • oh, thats not really what I wanted, as not every month has the same amount of bars. Do you also know how to use that with the exact period begin? – cody Feb 26 '20 at 01:41
  • What's your purpose exactly? Is your objective to only plot on the chart bars corresponding to the last dilation of the higher TF? Not sure how your concept of only plotting from a given date fits with the other constraint of only plotting last *n* bars. – PineCoders-LucF Feb 26 '20 at 03:03
  • Yes, you're right, it's something different. I'd like to plot a line for the last x months on a daily chart. and it should start on the firts bar of the month, that was x months ago. Do you know how to implement that? – cody Mar 04 '20 at 19:54
  • 1
    Added a v2 to answer. It starts plotting a user-specified n months back. – PineCoders-LucF Mar 04 '20 at 21:23
  • Yes, it does! I'm also looking for something to plot intraday sessions (= new indicator values every day) ...but I think that's a whole new topic... – cody Mar 05 '20 at 00:19
  • Ask away in a new question. We'll help if we can. – PineCoders-LucF Mar 05 '20 at 00:25
  • 2
    Turns out `timestamp()` is capable of the required gymnastics to subtract any number of months from a date. Version 3 takes advantage of this and is eerily similar to your first attempt. You were close ) [Thx to Pine team for the tip.] – PineCoders-LucF Mar 05 '20 at 05:33
  • Ahh, so simple!! You're a genius :-) Thanks a lot!! – cody Mar 05 '20 at 11:11
  • Credit goes to Pine tech lead—not me. [This FAQ entry](https://www.pinecoders.com/faq_and_code/#how-can-i-plot-a-value-starting-n-monthsyears-back) is an extension of the concept. – PineCoders-LucF Mar 05 '20 at 15:21
3

Version 4

At v4 you can set the variable show_last in the plot() function.

In "PineScript language reference manual" says:

show_last (input integer) If set, defines the number of bars (from the last bar back to the past) to plot on chart.

https://www.tradingview.com/pine-script-reference/#fun_plot

carloswm85
  • 1,396
  • 13
  • 23