example.mdc•3.95 kB
---
description:
globs: **/*.pine, **/*.pinescript
alwaysApply: false
---
# Pine Script Code Organization Example
Below is an example of a properly structured Pine Script file that follows our guidelines:
```pine
//@version=6
// =================== METADATA =================== //
indicator("My Indicator", overlay=true, max_bars_back=500)
// © Author Name
// =================== INPUT GROUPS =================== //
var string G_GENERAL = "General Settings"
var string G_DISPLAY = "Display Settings"
var string G_FILTER = "Filter Settings"
// =================== INPUT PARAMETERS =================== //
// General settings
timeframe = input.string("D", "Timeframe", options=["M", "W", "D", "240", "60", "30", "15", "5", "1"], group=G_GENERAL)
lookback = input.int(14, "Lookback Period", minval=1, maxval=200, group=G_GENERAL)
// Display settings
show_signals = input.bool(true, "Show Signals", group=G_DISPLAY)
signal_color = input.color(color.blue, "Signal Color", group=G_DISPLAY)
// Filter settings
use_filter = input.bool(false, "Enable Filter", group=G_FILTER)
filter_threshold = input.float(2.0, "Filter Threshold", minval=0.1, step=0.1, group=G_FILTER)
// =================== VARIABLE DECLARATIONS =================== //
var float[] values = array.new_float()
var label[] signal_labels = array.new_label()
var int last_signal = 0
// =================== FUNCTION DEFINITIONS =================== //
// Helper function to calculate average
f_calculate_average(float[] arr) =>
int size = array.size(arr)
float sum = 0.0
for i = 0 to size - 1
sum += array.get(arr, i)
size > 0 ? sum / size : 0.0
// Function to check if we should generate a signal
f_check_signal(float currentValue, float averageValue, float threshold) =>
bool signal = math.abs(currentValue - averageValue) > threshold
signal ? (currentValue > averageValue ? 1 : -1) : 0
// Function to manage array size
f_manage_array(float[] arr, int maxSize) =>
while array.size(arr) > maxSize
array.shift(arr)
// =================== MAIN CALCULATIONS =================== //
// Add current value to array
array.push(values, close)
// Manage array size
f_manage_array(values, lookback)
// Calculate averages
float avg = f_calculate_average(values)
// Check for signals
int signal = f_check_signal(close, avg, filter_threshold)
if signal != 0 and (last_signal != signal or bar_index - last_signal > lookback)
last_signal := signal
// =================== VISUALIZATION =================== //
// Draw signals on chart
if show_signals and signal != 0
label_text = signal > 0 ? "Buy" : "Sell"
label_style = signal > 0 ? label.style_label_up : label.style_label_down
label_color = signal > 0 ? color.green : color.red
newLabel = label.new(
x=bar_index,
y=close,
text=label_text,
color=label_color,
style=label_style,
textcolor=color.white
)
array.push(signal_labels, newLabel)
// Plot average line
plot(avg, "Average", color=signal_color, linewidth=2)
// =================== ALERTS =================== //
alertcondition(signal > 0, "Buy Signal", "Price crossed above average by {{filter_threshold}} points")
alertcondition(signal < 0, "Sell Signal", "Price crossed below average by {{filter_threshold}} points")
```
## Implementation Notes
1. **Grouping Related Code**: Notice how all function definitions are together, all inputs are together, etc.
2. **Hierarchical Organization**: Functions that are used by other functions are defined first.
3. **Clear Section Headers**: Each major section is clearly marked with a comment header.
4. **Proper Indentation**: The code uses consistent indentation within blocks.
5. **Function Placement**: New functions should be added to the function definitions section, not at the end of the file.
By following this structure, your Pine Script code will be more readable, maintainable, and easier to debug.