Pine Script for Beginners: Plot a Close-Based Moving Average

Start with a small display tool rather than a trading strategy. This example plots a 20-close simple moving average without assuming a particular asset or ICT pattern.


English · 한국어

Start with a small display tool rather than a trading strategy. This example plots a 20-close simple moving average without assuming a particular asset or ICT pattern.

Turn a plain-language rule into a chart mark. An example rule: mark a completed candle if its close is above 100.
Concept illustration: Turn a plain-language rule into a chart mark. An example rule: mark a completed candle if its close is above 100.

Add the example in Pine Editor

Paste the code into TradingView’s Pine Editor, save it, and add it to a chart. The default length is 20 and can be changed in settings. The initial bars may not have enough observations to display an average.

//@version=6
indicator("Close average — learning example", overlay=true)
length = input.int(20, "Length", minval=1)
average = ta.sma(close, length)
plot(average, "SMA", color=color.blue)

indicator declares a display script. input.int exposes the length setting, ta.sma calculates the simple average, and plot draws it. This code does not place orders or calculate strategy performance.

When does it recalculate?

Historical calculations generally proceed bar by bar. A realtime indicator recalculates on price updates, so its current-bar average can change. Distinguish this from a confirmed closing value.

Strategies execute at bar close by default, with behavior affected by settings. Do not assume historical bars contain equivalent tick information.

Before adding more features

Display a condition and compare it manually with the chart before adding alerts or a strategy. Higher-timeframe requests, pivot confirmation, and backdated drawings require checks on when the information first became available.

A strategy also needs fees, slippage, fill assumptions, and evaluation periods. A correctly drawn line is not evidence of a profitable rule.

Sources and documentation

www.tradingview.com — execution-model

Continue with common foundations

TradingView guide · Python data workflow · Trading Journal · Position Size & Risk Calculator

Research methods and reporting standards

For education and research. Numerical examples are teaching assumptions unless explicitly identified otherwise. Historical results do not guarantee future performance.

ABOUT THIS ARTICLE

This article is for education and research, not advice tailored to your finances. Historical findings do not guarantee future returns.

Methodology · Disclosure · Corrections

TRADE EVIDENCE에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기