# Chapter 7. Creating Strategies

At this very moment we presume that you are able to create a simple technical indicator as the most useful commands have been discussed in previous chapters. Let’s have a look at what this indicator could look like:

```input price = close;
input length = 20;
plot avg = Average(price, length);```

This script will plot a 20 period SMA of Close price with both length and price adjustable via the input parameters. You can also add a declaration stating that this study should be displayed on the lower subgraph, define several variables to be used in calculations, call some tricky mathematical functions, and specify conditions which will provide you with trading signals. However, the main part here is the plot whose values are going to be analyzed. In this chapter we are going to discuss strategies – a different type of indicators which have trading signals as the main target of analysis. These indicators are displayed on the “Strategies” tab of “Edit Studies and Strategies” window and this is where they should be added.

When you add a strategy to chart, Buy and Sell triggers appear in response to the specified conditions (and now you know a lot of ways to specify them; refer to chapter 5 and chapter 6 to refresh your knowledge). Strategies also provide you with ability to estimate the Profit/Loss value if you sent orders upon each Buy and Sell signal. This is what we call backtesting of a strategy: TOS Charts interface allows you to view the performance report upon clicking each signal on chart (the full procedure is described here).

As one can expect, strategies are similar to regular studies, but they just have something special to them. This something is AddOrder function which (if properly used) will turn any technical indicator into trading strategy. Now we are going to do it with the script above:

```input price = close;
input length = 20;
def avg = Average(price, length);

Now it is a strategy which will add a Buy signal every time Close price crosses above its 20 period SMA and a Sell signal when it crosses below. Aside from the AddOrder function which will be discussed a bit later, we could notice a couple other differences peculiar to strategies. First of all, as you can see, this strategy does not have any plots (as the most studies do). This is characteristic of strategies: they do not normally show any plots, however, it won’t do any harm if you add a plot or several to this script. Secondly, defining the trading condition is crucial: in our case, it is price crossing above or below its SMA. But the main difference remains the same: the AddOrder function. Let us puzzle out its syntax:

```AddOrder(OrderType.BUY_AUTO, price crosses above avg);

The second argument of the function was the condition upon which the order of specified side and position effect will be added. This order will be added to the next bar after condition is fulfilled.

When the strategy is applied to chart, each time the condition is fulfilled, an order is displayed. Orders are shown as up and down arrows above and below the price plot. These arrows are also accompanied by position effect, caption, and a tick marking the trading price. Appearance of these elements can be customized via the full syntax of the AddOrder function which is just a little bit more complicated than what you’ve seen before:

`AddOrder(type, condition, price, tradeSize, tickColor, arrowColor, name); `

Aside from previously described “type” and “condition”, arguments also include price, trade size, tick color, arrow color and name. Argument “price” defines price at which the order is added (by default, it is the Open of the following bar), “trade size” stands for the number of contracts traded; you can also specify colors for both tick and arrow. Colors need to be defined as Color constants, e.g., Color.RED, Color.GREEN, Color.ORANGE, etc. The full list of color constants can be found here; usage of these constants will be covered in the next chapter. The last argument is “name”; it defines the caption to be displayed (by default, it is the same as the name of the strategy itself).

Now we are ready to make the strategy we created before look outstanding:

```input price = close;
input length = 20;
def avg = Average(price, length);