# Close Turns Up or Down MA

Hi Matthew, I am trying to find a script that I can use for a Show Plot to identify the first time the Bar closes above or below an MA. I tried using: CLOSE() TurnsDown MA(30) - for the first time the bar closes below MA 30 but it didn't provide the desired result. It showed plots where the close was still above MA 30. I am assuming the correct script for either a first time turn up or turn down could be used for other indicators eg BB. thanks I appreciate your help.

Hi Mathew,

For a Close crossing above a Moving Average use:

`CLOSE() CrossesAbove MA(BARS=30)`

This example uses a 30SMA, you can adjust it as needed.

For Close crossing below a 30 period Simple Moving Average use:

`CLOSE() CrossesBelow MA(BARS=30)`

The same type of setup can be used for many other functions. With a Bollinger Band, here is an example of the High crossing above the upper band:

`High() CrossesAbove BB().UpperLine`

For more information on scripting i’d recommend checking out the free courses here: https://learn.optuma.com/scripting-courses/

Hi Matthew,

Thank you. That was helpful. What if I didn't want the arrows to appear if the MA is turning down in an uptrend and turning up in a downtrend? Is there a way to exclude those arrows?

Hi Mathew,

How would you define an uptrend? If it’s when the 30 period MA is sloping up you could use MA(Bars=30) IsUp but you could only include where a short-term MA was also sloping up, eg MA(Bars=5) IsUp, therefore excluding a short-term downtrend.

So the complete formula would be:

```MA(Bars=30) IsUp and
MA(Bars=5) IsUp```

It would look like the green shaded areas on this chart (created using a Show View tool dragged over the price chart), so when the red line (MA5) is sloping down when the long term trend blue line is up it’s excluded. Is that what you had in mind?