OPEN-SOURCE SCRIPT

EMA 9 & EMA 21

Indicator Declaration:

pinescript
Copy code
//version=5
indicator("EMA 9 & EMA 21", overlay=true)
Specifies the script uses Pine Script version 5.
The overlay=true ensures the EMAs are plotted directly on the price chart, rather than in a separate pane.
Input Parameters:

pinescript
Copy code
ema9Length = input.int(9, title="EMA 9 Length", minval=1)
ema21Length = input.int(21, title="EMA 21 Length", minval=1)
input.int() allows the user to customize the EMA lengths.
The minval=1 ensures the length is always a positive integer.
Default lengths are set to 9 and 21 for the short- and medium-term EMAs, respectively.
EMA Calculations:

pinescript
Copy code
ema9 = ta.ema(close, ema9Length)
ema21 = ta.ema(close, ema21Length)
ta.ema() computes the exponential moving average based on the close price of candles.
ema9 and ema21 are dynamically calculated for every candle on the chart.
Plot EMAs:

pinescript
Copy code
plot(ema9, color=color.blue, title="EMA 9", linewidth=2)
plot(ema21, color=color.red, title="EMA 21", linewidth=2)
plot() draws the EMAs on the chart:
ema9 is plotted in blue to represent the short-term EMA.
ema21 is plotted in red to represent the medium-term EMA.
The linewidth=2 makes the lines slightly thicker for better visibility.
educationalExponential Moving Average (EMA)multitimeframe

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer