Does anyone know how I can plot each occurrence of a defined variable after a certain date? So, before a certain timeframe input ignore all occurrances. I was able to find this:How to plot only the last x periods But, this appears to only plot a single occurrance of the variable on the input date.
For example and simplicity how can I plot every doji after the user input date:
//@version=5
indicator('Plot all Doji after input date and ignore before input date', overlay=true)
monthsBack = input.int(3, minval=0)
doji = close == open
targetDate = time >= timestamp(year(timenow), month(timenow) - monthsBack, 1, 0, 0, 0)
beginMonth = not targetDate[1] and targetDate
var float valueToPlot = doji
if beginMonth
valueToPlot := high
valueToPlot
plot(valueToPlot)
bgcolor(beginMonth ? color.green : na, transp=90)
Update:
Wow, thank you shanem I was not expecting such a thorough answer. After reading your response it makes sense to me now.
I'm primarily a trader, not much of a programmer, so I'm excited to see my trading strategies starting to come together in a more automated way. This script is just part of my most profitable trading strategy and will help save me tons of time spent identifying levels. It will also help me move to a new symbol with levels ready to go much faster as I have been hand drawing my levels for the last two years which is time-consuming.
I should have said "test candle" in my original post rather than "doji." I just meant to use a placeholder for the candle type since writing the code for the actual condition I'm looking for is going to be somewhat complicated. But, I wanted to make sure I would be able to plot what I needed to see before embarking on that.
Screenshot
Here's a screenshot of how I would normally hand draw a price level. Now done with a script automatically!
Here's the script I have with the help if shanem, still a work in progress, but getting there:
//@version=5
// with help from shanem it works!
indicator("Identify [a specific kind of candle] in past N months", shorttitle = "Auto Price Levels", overlay = true)
// 1) Define inputs used in the script
monthsBack = input.int(8, minval=0, title="How many months back should we scan for [candle type]?")
// 2) Declare variables used in the script, and set defaults (once per chart)
var tn = timenow
var firstDate = timestamp(year(tn), month(tn) - monthsBack, 1, 0, 0, 0)
var testCandle = false
var okayToPlot = false
// 3) Calculations.
okayToPlot := time > firstDate
// A note about the candle type. For the purpose of this script I plugged in a quick candle description to limit the number of
// candles that would appear on the chart, and to reduce the amount of code to deal with to focus on the 'plot' code. I first
// wanted to see if it was possible to plot what I needed to see before I set out to create code for the specifi candle I'm
// looking for. I will either fully describe the candle in the future script, or import from a Pine Library since it is somewhat complex.
testCandle := if close == open and barstate.isconfirmed and session.ismarket
close
// 4) Output / Plotting
// Plot a price line on the close of each occurance of the [candle type].
// Also, highlight the high and low of the [candle type] and extend right.
plot(okayToPlot and testCandle ? close : na, style = plot.style_circles, linewidth = 3, color = color.rgb(245, 66, 221))
if testCandle and okayToPlot
line.new(bar_index - 1, close, bar_index, close, color = color.rgb(255, 164, 23), extend = extend.right, width = 1)
box.new(bar_index - 1, high, bar_index, low, border_color = na, bgcolor = color.rgb(135, 135, 135, 80), extend = extend.right)
// To do next, working on now:
// 1. Define the candle and limit the timeframe (or resolution) the price level is calculated on.
// Perhaps 30 minute. But, allow the price levels to be seen on all other timeframes.
// Seen on 1m, 5m, 4hr, daily, etc., but derived from 30m (just example).
// 2. Alerts. Send JSON payload via webhook if current price in range. Found this and modified version works good:
// User "wlhm" - https://www.tradingview.com/script/FPq2xKyZ-DiscordWebhookFunction/