# Writing Our Own Indicator

### Implementing SMA in QuantScript

Let's create our own QuantScript implementation of a **Simple Moving Average**.

*QuantScript includes a built-in implementation of SMA, and many other indicators. However, for the purposes of this guide, we are going to write one ourselves.*

A Simple Moving Average represents the average of some value, e.g.

Let's use everything we have learned so far. We begin by creating two variables -

2 period = 14

Next, we need to calculate the sum of the last 14

**for loop**:

2 period = 14

3

4 sum = 0

5

6 for distance from 0 to (period - 1)

7 sum = sum + applyTo[distance]

Two things need explaining here:

**What happens when the distance is 0?**

We're accessingapplyTo[0], which is equivalent toopen[0]. We are instructing the[]time-travel operator to go back0bars, so it stays in place - this is equivalent to justopen.**The distance ends at**period - 1.**Why?**

We don't want to addapplyTo[14]to the sum, because that would makesumthe sum of the last**15**open prices, instead of the last 14. Remember, we start from 0.

All that's left is to divide our sum by the period. Here's what our SMA looks like:

2 period = 14

3

4 sum = 0

5

6 for distance from 0 to (period - 1)

7 sum = sum + applyTo[distance]

8

9 sma = sum / period

Done! Now, let's write some entry/exit rules that use our SMA.

### Creating a strategy based on SMA

Let's enter a long position whenever our SMA has increased and exit that position whenever it has decreased, compared to the previous bar.

We can use the

2 period = 14

3

4 sum = 0

5

6 for distance from 0 to (period - 1)

7 sum = sum + applyTo[distance]

8

9 sma = sum / period

10

11 enter long when sma > sma[1]

12 exit long when sma < sma[1]

Here,

and

Let's make our strategy more robust by adding a second SMA. This time we'll calculate it over the

### Creating a strategy based on two SMAs

Creating a close-based 26-period SMA is easy - we just change the

However, we want to have the two SMAs simultaneously. To do that, we need to first compute our 14-period SMA, which we'll save in a variable called

2 applyTo = open

3 period = 14

4

5 sum = 0

6

7 for distance from 0 to (period - 1)

8 sum = sum + applyTo[distance]

9

10 sma14 = sum / period

11

12 # SMA-26 starts here

13 applyTo = close

14 period = 26

15

16 sum = 0 # We make sure to reset our sum

17

18 for distance from 0 to (period - 1)

19 sum = sum + applyTo[distance]

20

21 sma26 = sum / period

22

23 # Check that both SMAs have risen

24 enter long when sma14 > sma14[1] and sma26 > sma26[1]

25 # Check that both SMAs have fallen

26 exit long when sma14 < sma14[1] and sma26 < sma26[1]

This works, but once again we have a problem with code duplication.

Next, we'll look at how **functions** can help us solve this problem.