Home » Technical Analysis Functions » Using ta.ema Function in Pine Script

Using ta.ema Function in Pine Script

Photo of author
Published on

Pine Script, offers the ta.ema function to effortlessly calculate the EMA. This article will delve deep into its functionality and explain how you can make the most out of this function in your scripts.

Understanding ta.ema

What is the Exponential Moving Average (EMA)?

Before diving into the specifics of the function, let’s first understand the EMA itself. Unlike a simple moving average where all prices are given equal importance, the EMA emphasizes the recent prices more. This results in the EMA being more sensitive to recent price changes and thus potentially offering quicker signals for traders.

Function Syntax

The ta.ema function in Pine Script is used as:

ta.ema(source, length) → series float


  • source (series int/float): The series of values you want the EMA to be calculated for, like closing prices, opening prices, etc.
  • length (simple int): Specifies the number of bars for calculation. This determines the ‘smoothness’ of the EMA.

Basic Example

plot(ta.ema(close, 15))

Here, the script calculates the EMA of the closing prices of the last 15 bars and plots it on the chart.

For those who’d like to manually create an EMA calculation without the built-in function, Pine Script offers flexibility. Here’s how you’d do it:

pine_ema(src, length) =>
    alpha = 2 / (length + 1)
    sum = 0.0
    sum := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])

Unique Use Case: EMA Crossover Strategy

Let’s design a basic EMA crossover strategy to provide a real-world scenario. This strategy will go long when a short-term EMA crosses above a long-term EMA, and vice versa.

strategy("EMA Crossover Strategy", shorttitle="EMA CS", overlay=true)

shortTermEma = ta.ema(close, 9)
longTermEma = ta.ema(close, 21)

plot(shortTermEma, color=color.red)
plot(longTermEma, color=color.blue)

longCondition = ta.crossover(shortTermEma, longTermEma)
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = ta.crossunder(shortTermEma, longTermEma)
if (shortCondition)
    strategy.entry("Short", strategy.short)


  1. We declare two EMAs: one short-term (9 periods) and one long-term (21 periods).
  2. These are plotted on the chart using red and blue colors, respectively.
  3. Using the ta.crossover and ta.crossunder functions, we determine when the short-term EMA crosses the long-term EMA.
  4. When the short-term EMA goes above the long-term EMA, it’s a potential buying opportunity. Conversely, when it goes below, it signals a potential selling point.

Key Takeaway:
The ta.ema function in Pine Script is a versatile tool for traders and scriptwriters, enabling effortless EMA calculations and implementations. By understanding its basics and combining it with other functions, traders can create a wide variety of strategies to enhance their technical analysis.


The Exponential Moving Average is an essential part of many trading strategies. Pine Script’s ta.ema function simplifies the process of integrating it into your custom scripts. Whether you’re looking to create a simple EMA overlay or an intricate crossover system, this function is sure to be a foundational tool in your Pine Script toolkit.

Leave a Comment