# Trend Color EMA

#### Summary

This script segment calculates an EMA based on the closing prices and a user-specified period. It then evaluates the trend direction over the last 2 periods to determine whether the trend is upward, downward, or neutral. Finally, it plots the EMA on the chart with a color that changes according to the trend direction, providing visual feedback on the market's momentum. This can be particularly useful for traders and analysts looking to quickly assess the prevailing trend and potential reversals.

#### Source Data and Length

```javascript
src0 = close
len0 = input.int(13, minval=1, title='EMA 1')
```

* `src0` is set to the closing price (`close`), which serves as the source data for the EMA calculation.
* `len0` is an input variable allowing the user to specify the length of the EMA calculation period, with a minimum value of 1 and a default value of 13.

#### Calculating the EMA

```javascript
ema0 = ta.ema(src0, len0)
```

* Calculates the EMA of the closing prices over the specified period (`len0`). This EMA acts as a smoothed version of the price data, often used to identify trends.

#### Determining Trend Direction

```javascript
falling_1 = ta.falling(ema0, 2)
direction = ta.rising(ema0, 2)? +1 : falling_1? -1 : 0
```

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

#### Setting Plot Color Based on Trend Direction

```javascript
plot_color = direction > 0? color.lime : direction < 0? color.red : na
```

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

#### Plotting the EMA

```javascript
plot(ema0, title='EMA', style=plot.style_line, linewidth=1, color=plot_color)
```

* Plots the EMA on the chart with a line style, a line width of 1, and the previously determined `plot_color`.
