TradingView Pine Script Tutorial: Simple Moving Average (SMA)

ยท

Introduction to Pine Script Development

Welcome to this comprehensive guide on creating custom indicators using TradingView's Pine Script. In this tutorial, we'll focus on developing a Simple Moving Average (SMA) indicator, one of the most fundamental technical analysis tools used by traders worldwide.

Why Learn Pine Script?

  1. Customization: Create indicators tailored to your specific trading strategy
  2. Automation: Implement complex calculations without manual work
  3. Visualization: Plot technical indicators directly on price charts
  4. Strategy Development: Build foundations for more advanced trading systems

Getting Started with the Pine Editor

To begin creating your SMA indicator:

  1. Navigate to TradingView's chart interface
  2. Click on "Pine Editor" to access the scripting environment
  3. Select "New" to create a blank indicator script
  4. Choose "Blank Indicator" template (we'll build from scratch)

Script Setup Essentials

Every Pine Script requires certain foundational elements:

//@version=4
study(title="Simple Moving Average", shorttitle="SMA", overlay=true)

Key components:

Building the Simple Moving Average

Understanding SMA Calculation

The Simple Moving Average:

Creating Configurable Inputs

Make your indicator flexible with user-adjustable parameters:

// User-configurable period input
maPeriod = input(title="Moving Average Period", type=input.integer, defval=21)

Parameters:

Calculating the Moving Average

Pine Script provides built-in functions for common calculations:

// Calculate 21-period SMA using closing prices
maValue = sma(close, maPeriod)

Where:

Visualizing the SMA Indicator

Customizing the Plot

Make your indicator visually distinctive:

// Plot SMA with custom styling
plot(maValue, title="SMA", color=color.purple, linewidth=2)

Customization options:

Practical Application

The SMA serves multiple purposes:

Example observations:

Advanced Customization Options

Additional Input Parameters

Enhance your indicator's flexibility:

// Add source selection (close, open, high, low, etc.)
src = input(title="Source", type=input.source, defval=close)

// Add color customization
maColor = input(title="Line Color", type=input.color, defval=color.purple)

// Update calculation with selected source
maValue = sma(src, maPeriod)

Multiple Moving Averages

Plot several SMAs simultaneously:

// Calculate multiple periods
shortMA = sma(close, 10)
mediumMA = sma(close, 21)
longMA = sma(close, 200)

// Plot with distinct colors
plot(shortMA, "10 SMA", color.blue)
plot(mediumMA, "21 SMA", color.purple)
plot(longMA, "200 SMA", color.orange)

Publishing and Sharing Your Script

Finalizing Your Indicator

Before publishing:

  1. Test across different timeframes and instruments
  2. Verify calculation accuracy
  3. Ensure clean, commented code
  4. Add descriptive metadata
// Script metadata
author = "Your Name"
version = "1.0"
description = "Custom Simple Moving Average Indicator"

Sharing Options

  1. Save to TradingView: Private or public visibility
  2. Publish to Community Scripts: Share with all TradingView users
  3. Export as File: Save locally for backup

Frequently Asked Questions

What's the difference between SMA and EMA?

The Simple Moving Average (SMA) calculates a straightforward average, while the Exponential Moving Average (EMA) gives more weight to recent prices, making it more responsive to price changes.

How do I change the moving average period?

After adding the indicator to your chart:

  1. Click the settings (gear) icon next to the indicator name
  2. Locate "Moving Average Period" input field
  3. Enter your desired value (e.g., 50, 200)
  4. Click "OK" to apply changes

Can I use SMA for trading strategies?

Yes, SMAs commonly form the basis of:

๐Ÿ‘‰ Learn more about advanced trading strategies

Why does my SMA look different on various timeframes?

Moving averages are period-dependent:

How do I add alerts based on SMA crossovers?

Pine Script allows alert conditions:

// Alert when price crosses above SMA
alertcondition(crossover(close, maValue), "Price Crosses Above SMA")

๐Ÿ‘‰ Discover more about Pine Script alert conditions

Conclusion and Next Steps

This tutorial covered the fundamentals of creating a Simple Moving Average indicator in Pine Script. You've learned how to:

For further development:

  1. Experiment with different source prices (high, low, etc.)
  2. Combine with other indicators like RSI or MACD
  3. Develop crossover detection systems
  4. Convert your indicator into a full trading strategy

Remember that technical indicators work best when combined with other analysis methods and proper risk management techniques.

Additional Resources