Trend indicator
Moving Average Calculator (SMA, EMA, WMA)
Compute the current exponential moving average from the latest price and the previous EMA, or a simple moving average from a running sum. The real value is understanding how each type of average treats your data differently.
Exponential moving average (one step)
Simple moving average
What moving averages are
A moving average smooths a price series by averaging recent values, recalculated on every bar so the window slides forward through time. It strips out short-term noise to make the underlying direction easier to see. The common variants differ only in how much weight they give to recent prices versus older ones.
The simple moving average (SMA) is the plain arithmetic mean of the last N prices, so every bar in the window counts equally: add them up and divide by the count. The weighted moving average (WMA) applies linearly increasing weights, so the newest bar carries the most weight and the oldest the least. The exponential moving average (EMA) uses a smoothing factor k = 2 / (length + 1) and blends each new price with the previous EMA, as EMA = price × k + previous EMA × (1 − k). Because it carries forward the prior value, the EMA reacts faster to new prices while never fully discarding older data, which is why it is calculated one step at a time.
Why moving averages are used
Moving averages are the foundation of countless indicators and are used to smooth noisy price action and make the prevailing direction easier to read. Shorter averages track price closely and turn quickly; longer averages lag more but filter out more noise. The EMA is common where responsiveness matters, the SMA where stability matters.
Analysts often study how price sits relative to an average, or how two averages of different lengths relate to each other. Which lengths and combinations are meaningful, and how to act on them, is entirely a matter of your own approach.
What are TradingView and Pine Script?
TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.
People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.
The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.
Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.
Turn this into Pine Script
Here you computed a single moving average value. On a chart these are drawn as continuous lines that update every bar. Pine Script exposes each as a one-liner, ta.sma, ta.ema, and ta.wma, but combining them into crossovers, colored fills, or entry conditions is where a real indicator takes shape.
//@version=6
indicator("Moving Averages", overlay=true)
length = input.int(20, "Length")
plot(ta.sma(close, length), "SMA", color=color.blue)
plot(ta.ema(close, length), "EMA", color=color.orange)
plot(ta.wma(close, length), "WMA", color=color.green)PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v5 or v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.
Related free calculators
From the blog
PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.