Pine Script is TradingView's proprietary scripting language designed specifically for creating custom technical indicators and trading strategies. It has the following key characteristics:
Syntax Features:
- Syntax similar to Python and JavaScript, easy to learn
- Type-safe with automatic type inference
- Built-in extensive financial functions and mathematical operations
- Supports arrays and variable management
- Event-driven model triggered by price changes
Core Concepts:
- Version Selection: Pine Script v4 and v5 are the main versions, with v5 offering more features
- Input Parameters: Use
input()function to create configurable parameters - Variable Declaration: Use
var,varip,simplekeywords to declare variables - Function Definition: Supports custom functions that can return multiple values
- Strategy Logic: Use
strategy()function to define trading strategies
Common Functions:
ta.sma(),ta.ema(): Moving averagesta.rsi(): Relative Strength Indexta.macd(): MACD indicatorstrategy.entry(): Open positionsstrategy.close(): Close positionsplot(): Plot data on charts
Backtesting Features:
strategy()function automatically enables backtesting- Can set initial capital, commissions, slippage, and other parameters
- Provides detailed backtesting reports and performance metrics
- Supports multi-timeframe backtesting
Best Practices:
- Use
request.security()to access data from other timeframes - Use
varandvaripappropriately to optimize performance - Avoid complex calculations in loops
- Use
barstate.isrealtimeto distinguish between real-time and historical data - Add proper error handling and validation logic