i want to build pine script v6 indicator that will look at candles from any tf of my…
- User
i want to build pine script v6 indicator that will look at candles from any tf of my choose(settings) and find candles where by a candles purged iether high/low of its previous candle and the closed within that candle it should highlight those candles with a rectangle and i can still the highlighted Higher tf on the lower tf
Assistant
Creating a Pine Script v6 indicator that highlights candles where the current candle's high or low has purged the previous candle's high or low and closed within that range can be a bit complex, but it's definitely doable. Below is a step-by-step guide and the corresponding Pine Script code to achieve this.
Step-by-Step Guide
- Define the Higher Timeframe (HTF): Allow the user to select the higher timeframe.
- Fetch Data from the HTF: Use the
securityfunction to fetch the HTF data. - Identify the Purged Candles: Check if the current HTF candle's high or low has purged the previous HTF candle's high or low and if it closed within that range.
- Highlight the Candles: Use the
rectanglefunction to highlight the identified candles on the lower timeframe.
Pine Script Code
//@version=6 indicator("Purged Candles Highlighter", overlay=true) // User input for the higher timeframe htf = input.timeframe("D", title="Higher Timeframe") // Fetch the higher timeframe data htf_high = request.security(syminfo.tickerid, htf, high) htf_low = request.security(syminfo.tickerid, htf, low) htf_close = request.security(syminfo.tickerid, htf, close) // Variables to store the previous HTF high and low var float prev_htf_high = na var float prev_htf_low = na // Update the previous HTF high and low if (not na(prev_htf_high)) prev_htf_high := htf_high[1] if (not na(prev_htf_low)) prev_htf_low := htf_low[1] // Check if the current HTF candle has purged the previous HTF candle's high or low purged_high = htf_high < prev_htf_high and htf_close < prev_htf_high purged_low = htf_low > prev_htf_low and htf_close > prev_htf_low // Highlight the purged candles on the lower timeframe if (purged_high or purged_low) rectangle.new(bar_index[1], prev_htf_low, bar_index, prev_htf_high, color=color.red, border_color=color.red, style=rectangle.style_price, width=2, editable=true)Explanation
- User Input: The
input.timeframefunction allows the user to select the higher timeframe. - Fetching HTF Data: The
request.securityfunction fetches the high, low, and close prices from the selected higher timeframe. - Previous HTF Values: The
varkeyword is used to store the previous HTF high and low values. - Purged Conditions: The script checks if the current HTF candle's high or low has purged the previous HTF candle's high or low and if it closed within that range.
- Highlighting: The
rectangle.newfunction is used to draw a rectangle on the lower timeframe to highlight the purged candles.
Notes
- The
rectangle.newfunction is used to draw rectangles on the chart. Thebar_indexis used to position the rectangles correctly. - The
editable=trueparameter allows the user to edit the rectangles on the chart. - The
style=rectangle.style_priceparameter ensures that the rectangles are drawn at the price levels.
You can further customize the script to fit your specific needs, such as changing the colors, styles, or adding additional conditions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.