OPEN-SOURCE SCRIPT
已更新 PJ : Custom Horizontal Grid

Here is a comprehensive description of the **"PJ : Custom Horizontal Grid "** Pine Script that we built. You can use this as documentation or as notes for future reference.
### **Overview**
This Pine Script is a custom chart overlay indicator designed to draw a highly customizable grid of horizontal lines. It calculates a central base price, applies an offset (if desired), and generates a series of equidistant lines above and below the center. It also features clean, "floating" text labels that display both the base price and the offset price without obstructing the chart lines.
---
### **Key Features**
* **Dynamic Grid Generation:** Automatically draws a central line and a user-defined number of grid lines above and below it based on a specific point step (distance).
* **Price Offsetting (ทดราคา):** Allows the user to input a positive or negative offset. The script will calculate the "Actual Price" (Base Price + Offset) and draw the line at the actual price while still referencing the original base price.
* **Floating Text Labels:** Displays the price directly on the chart using the format `BasePrice (ActualPrice)`. The text is anchored using `style_label_lower_left`, allowing it to float neatly above the line to prevent visual overlap.
* **Independent Aesthetics:** Users can style the lines (color, thickness, solid/dashed/dotted) entirely independently from the text labels (color, size) via grouped settings.
* **Auto-Cleanup (Memory Management):** Uses Pine Script arrays (`array.new_line()` and `array.new_label()`) to track drawn objects. On every real-time tick (`barstate.islast`), it deletes the old lines and labels before drawing new ones, preventing chart clutter and memory issues.
---
### **Input Parameters (Settings)**
The script organizes its settings into three distinct groups for user convenience:
#### **1. Base Settings (No Group)**
* **Base Start (ราคาเริ่มต้น):** The central anchor price for the grid (e.g., 5000.0).
* **Offset +/- (ราคาที่ทด):** A fractional value added to or subtracted from the base price to shift the actual drawing location (e.g., -5.0).
* **Step Distance (ระยะห่างแต่ละเส้น):** The price distance between each subsequent grid line (e.g., 10.0).
* **Number of Lines +/- (จำนวนเส้นด้านบนและล่าง):** The quantity of lines to draw in *each* direction. (e.g., `5` means 5 above, 5 below, plus 1 center line = 11 lines total).
#### **2. Line Settings (--- ตั้งค่าเส้น ---)**
* **Line Color:** Color picker for the horizontal grid lines.
* **Line Width:** Thickness of the lines (1 to 4).
* **Line Style:** Dropdown to select Solid, Dotted, or Dashed lines.
#### **3. Text Settings (--- ตั้งค่าตัวหนังสือ ---)**
* **Text Color:** Color picker for the floating price labels.
* **Text Size:** Dropdown to adjust the font size (Tiny, Small, Normal, Large).
---
### **How the Logic Works (Under the Hood)**
1. **Helper Functions:**
* `getLineStyle()` and `getSize()` convert the user's string inputs from the dropdown menus into built-in Pine Script variables (e.g., `line.style_dashed`, `size.normal`).
2. **`drawLineAndLabel(basePrice)` Function:**
* Calculates `actualPrice = basePrice + offsetPrice`.
* Uses `line.new()` to draw the line extending infinitely to the left and right (`extend=extend.both`).
* Formats the text string to 4 decimal places: `strBase (strOffset)`.
* Uses `label.new()` at `bar_index + 2` (slightly to the right of the current candle) to render the floating text.
* Pushes the generated line and label IDs into their respective arrays.
3. **Execution Loop (`if barstate.islast`):**
* This ensures the script only updates on the most recent candle.
* It loops through the arrays, deletes the previous tick's drawings, and clears the arrays.
* It calls `drawLineAndLabel()` for the starting price.
* It runs a `for` loop to calculate and draw the required `numLines` above (`+ (stepDist * i)`) and below (`- (stepDist * i)`).
### **Overview**
This Pine Script is a custom chart overlay indicator designed to draw a highly customizable grid of horizontal lines. It calculates a central base price, applies an offset (if desired), and generates a series of equidistant lines above and below the center. It also features clean, "floating" text labels that display both the base price and the offset price without obstructing the chart lines.
---
### **Key Features**
* **Dynamic Grid Generation:** Automatically draws a central line and a user-defined number of grid lines above and below it based on a specific point step (distance).
* **Price Offsetting (ทดราคา):** Allows the user to input a positive or negative offset. The script will calculate the "Actual Price" (Base Price + Offset) and draw the line at the actual price while still referencing the original base price.
* **Floating Text Labels:** Displays the price directly on the chart using the format `BasePrice (ActualPrice)`. The text is anchored using `style_label_lower_left`, allowing it to float neatly above the line to prevent visual overlap.
* **Independent Aesthetics:** Users can style the lines (color, thickness, solid/dashed/dotted) entirely independently from the text labels (color, size) via grouped settings.
* **Auto-Cleanup (Memory Management):** Uses Pine Script arrays (`array.new_line()` and `array.new_label()`) to track drawn objects. On every real-time tick (`barstate.islast`), it deletes the old lines and labels before drawing new ones, preventing chart clutter and memory issues.
---
### **Input Parameters (Settings)**
The script organizes its settings into three distinct groups for user convenience:
#### **1. Base Settings (No Group)**
* **Base Start (ราคาเริ่มต้น):** The central anchor price for the grid (e.g., 5000.0).
* **Offset +/- (ราคาที่ทด):** A fractional value added to or subtracted from the base price to shift the actual drawing location (e.g., -5.0).
* **Step Distance (ระยะห่างแต่ละเส้น):** The price distance between each subsequent grid line (e.g., 10.0).
* **Number of Lines +/- (จำนวนเส้นด้านบนและล่าง):** The quantity of lines to draw in *each* direction. (e.g., `5` means 5 above, 5 below, plus 1 center line = 11 lines total).
#### **2. Line Settings (--- ตั้งค่าเส้น ---)**
* **Line Color:** Color picker for the horizontal grid lines.
* **Line Width:** Thickness of the lines (1 to 4).
* **Line Style:** Dropdown to select Solid, Dotted, or Dashed lines.
#### **3. Text Settings (--- ตั้งค่าตัวหนังสือ ---)**
* **Text Color:** Color picker for the floating price labels.
* **Text Size:** Dropdown to adjust the font size (Tiny, Small, Normal, Large).
---
### **How the Logic Works (Under the Hood)**
1. **Helper Functions:**
* `getLineStyle()` and `getSize()` convert the user's string inputs from the dropdown menus into built-in Pine Script variables (e.g., `line.style_dashed`, `size.normal`).
2. **`drawLineAndLabel(basePrice)` Function:**
* Calculates `actualPrice = basePrice + offsetPrice`.
* Uses `line.new()` to draw the line extending infinitely to the left and right (`extend=extend.both`).
* Formats the text string to 4 decimal places: `strBase (strOffset)`.
* Uses `label.new()` at `bar_index + 2` (slightly to the right of the current candle) to render the floating text.
* Pushes the generated line and label IDs into their respective arrays.
3. **Execution Loop (`if barstate.islast`):**
* This ensures the script only updates on the most recent candle.
* It loops through the arrays, deletes the previous tick's drawings, and clears the arrays.
* It calls `drawLineAndLabel()` for the starting price.
* It runs a `for` loop to calculate and draw the required `numLines` above (`+ (stepDist * i)`) and below (`- (stepDist * i)`).
版本注释
PJ Grid — Change Description- Renamed indicator to "PJ Grid - OI/IV".
- Added Vol2Vol Expected Range layer (CME-style ±1σ / ±2σ / ±3σ)
with shaded gray bands and dashed sigma lines.
- Inputs: Implied Volatility (%) and Days to Expiry (DTE), entered
manually on the indicator settings panel.
- Settlement Price defaults to (today's daily open + offsetPrice),
computed via request.security on the "D" timeframe; user can
toggle off "Auto" and enter a manual value.
- Expected move = Settle × (IV/100) × √(DTE/365); bands plotted
as horizontal boxes extending in both directions.
- σ fill colors are dark/medium/light gray (CME look), with
per-band color and line-width inputs.
- Settlement line drawn solid red with a summary label
(Settle | IV | DTE | ±1σ).
- Added on-chart Info Table (Settle, IV, DTE, Offset, ±1σ) with
selectable position (6 corners) and text size; yellow values
on solid black for high contrast on any chart theme.
- Original horizontal grid (base ± step × N, with offset and
floating "Option → Spot" labels) preserved unchanged.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。