# EMA 2

#### Summary

This script segment introduces a second EMA with a longer period, aiming to provide a broader perspective on the market trend. By comparing the directions of both EMAs, traders and analysts can gain insights into short-term versus long-term trends. The color coding based on the trend direction of EMA 2 offers a quick visual assessment of the market's momentum, potentially signaling areas of strength or weakness.

#### Source Data and Length

```javascript
src02 = close
len02 = input.int(21, minval=1, title='EMA 2')
```

* `src02` is set to the closing price (`close`), serving as the source data for the second EMA calculation.
* `len02` is an input variable that allows the user to specify the length of the EMA 2 calculation period, with a minimum value of 1 and a default value of 21. This is intended to provide a longer-term perspective compared to the first EMA.

#### Calculating the Second EMA

```javascript
ema02 = ta.ema(src02, len02)
```

* Calculates the EMA of the closing prices over the specified period (`len02`). This EMA acts as a smoothed version of the price data, offering a longer-term view compared to the first EMA.

#### Determining Trend Direction for EMA 2

```javascript
falling_2 = ta.falling(ema02, 2)
direction2 = ta.rising(ema02, 2)? +1 : falling_2? -1 : 0
```

* `falling_2` checks if the EMA 2 has been falling over the last 2 periods.
* `direction2` determines the overall trend direction for EMA 2:
  * If the EMA 2 is rising over the last 2 periods, `direction2` is set to +1.
  * If the EMA 2 has been falling over the last 2 periods, `direction2` is set to -1.
  * Otherwise, `direction2` remains neutral (0).

#### Setting Plot Color Based on Trend Direction for EMA 2

```javascript
plot_color2 = direction2 > 0? color.lime : direction2 < 0? color.red : na
```

* Determines the color for plotting the EMA 2 based on the `direction2`:
  * If `direction2` is positive (+1), indicating an uptrend, the plot color is set to lime.
  * If `direction2` is negative (-1), indicating a downtrend, the plot color is set to red.
  * If `direction2` is neutral (0), the plot color is set to `na` (not a number), meaning no color is assigned.

#### Plotting the Second EMA

```javascript
plot(ema02, title='EMA Signal 2', style=plot.style_line, linewidth=1, color=plot_color2)
```

* Plots the EMA 2 on the chart with a line style, a line width of 1, and the previously determined `plot_color2`. This EMA is labeled as "EMA Signal 2" to distinguish it from the first EMA.

Citations:
