OPEN-SOURCE SCRIPT
Aggiornato

langshen K-Line Counter

464
English Description:**

This indicator, named “langshen K-Line Counter,” is designed to count the number of K-line bars (candlesticks) during the current trading day. The key functionalities include:

- **Daily Reset**: At the start of a new trading day (detected by the change in daily time), the counter automatically resets to 1.
- **Incremental Counting**: For each new bar within the same trading day, the counter increments by 1, thus recording the sequence number of each K-line.
- **Chart Display**: The current count is displayed as a numerical label above the high of each K-line on the chart. Users can customize the text size and color of these numbers.

This feature enables traders to visually track the number of K-lines and their sequence within the day, which can assist in analyzing market trends and the pace of price movements.

中文介绍:**

此指标名为“langshen K线计数器”,主要用于统计当前交易日内的K线(蜡烛图)数量。指标的核心功能包括:

- **每日计数重置**:在交易所日切换时(即新的一天开始时),计数器自动重置为1。
- **逐根累加**:在同一交易日内,每根新出现的K线都会使计数器加1,从而记录当天的K线序号。
- **图表展示**:计数器的当前值会以数字标签的形式显示在每根K线的最高价上方,用户可自定义数字的显示大小和颜色。

这种功能可以帮助交易者直观地了解当天市场中的K线数量以及K线的序号变化,辅助分析市场的走势和节奏。


Note di rilascio
Below is an explanation of the modified Pine Script code, written in English and adhering to TradingView's guidelines for clarity, functionality, and compatibility. The modification ensures that only even-numbered K-line counts are displayed, while maintaining the original functionality of counting K-lines per trading day.
Indicator Description
The "langshen K-Line Counter" is a Pine Script indicator designed for TradingView that counts the number of K-lines (bars) within each trading day and displays the count above the bars. The modified version only displays the count for even-numbered bars (e.g., 2, 4, 6, etc.), as requested. The indicator resets the count at the start of each new trading day, based on the exchange's daily cutoff, and allows customization of text size, color, and visibility.
Modified Code
pinescript
//version=5
indicator("langshen K-Line Counter", overlay=true, max_labels_count=500)

// Determine if it's a new day (using the exchange's daily cutoff)
isNewDay = ta.change(time("D")) != 0

// Persistent variable: counts the K-lines within the current trading day.
// Resets to 1 on a new day; otherwise, increments.
var int barCount = 0
barCount := isNewDay ? 1 : barCount + 1

// User inputs: whether to display numbers, text size, and text color.
showNumbers = input.bool(true, "Display K-Line Number")
textSize = input.string("small", "Text Size", options=["tiny", "small", "normal", "large"])
textColor = input.color(color.white, "Text Color")

// Calculate the display position: a certain distance above the current bar's high.
yLocation = high + (high - low) * 0.1

// Use label.new to draw the number only for even barCount (with a transparent background).
if showNumbers and barCount % 2 == 0
label.new(bar_index, yLocation, str.tostring(barCount), color=color.new(color.black, 100), textcolor=textColor, style=label.style_label_down, size=textSize, yloc=yloc.price)
Explanation of Modifications
The original script displayed a label with the K-line count for every bar in the trading day. The modification introduces a condition to display labels only for even-numbered K-lines, enhancing visual clarity by reducing the number of labels shown. Below is a detailed breakdown of the changes:
Even-Number Condition:
Added and barCount % 2 == 0 to the if statement controlling the label.new function.
The modulo operator (%) checks if barCount is divisible by 2 with no remainder, identifying even numbers (e.g., 2, 4, 6, etc.).
This ensures that labels are only drawn for bars where the count is even, skipping odd-numbered bars (e.g., 1, 3, 5, etc.).
Preserved Functionality:
The core logic remains unchanged:
isNewDay uses ta.change(time("D")) to detect the start of a new trading day.
barCount resets to 1 at the beginning of each day and increments by 1 for each subsequent bar.
Labels are positioned slightly above the bar's high (yLocation = high + (high - low) * 0.1) for clear visibility.
User inputs (showNumbers, textSize, textColor) still allow customization of whether to display labels, their size, and color.
TradingView Compliance:
The script uses version=5, ensuring compatibility with the latest Pine Script features.
The max_labels_count=500 parameter in the indicator function prevents excessive label creation, adhering to TradingView's performance guidelines.
The color.new(color.black, 100) creates a transparent background for labels, ensuring only the text is visible, which aligns with best practices for clean chart visuals.
The size parameter uses the user-selected textSize (with valid options: "tiny", "small", "normal", "large"), ensuring proper rendering.
Features
Daily K-Line Counting: Resets the counter to 1 at the start of each trading day and increments per bar.
Even-Number Display: Only shows labels for even-numbered bars (e.g., 2, 4, 6), reducing chart clutter.
Customizable Display: Users can toggle label visibility, adjust text size, and choose text color via input settings.
Clean Visuals: Labels are placed above bars with a transparent background, ensuring readability without obstructing price data.
Usage
Add the indicator to your TradingView chart.
Adjust inputs in the settings panel:
Display K-Line Number: Enable/disable label display.
Text Size: Choose from "tiny", "small", "normal", or "large".
Text Color: Select a color for the labels.
The indicator will display the K-line count (e.g., 2, 4, 6, etc.) above even-numbered bars for each trading day.
Notes
The script is optimized for performance by limiting the maximum number of labels (max_labels_count=500).
If you need labels for different conditions (e.g., odd numbers or specific intervals), the if condition can be further modified.
Ensure the chart's timeframe aligns with your intended use case, as the daily reset depends on the exchange's daily cutoff.
This modification maintains the indicator's original purpose while fulfilling the request to display only even-numbered K-line counts, adhering to TradingView's standards for functionality and user experience.
Let me know if you need further adjustments or additional features!

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.