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);
AddOrder(OrderType.BUY_AUTO, price crosses above avg);
AddOrder(OrderType.SELL_AUTO, price crosses below avg);

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);
AddOrder(OrderType.SELL_AUTO, price crosses below avg);

We called this function twice: first for the Buy signal and second for the Sell. In order to specify which side of trading is considered, AddOrder function requires an OrderType constant as the first argument. BUY_AUTO is a constant which AddOrder function uses to add a buying order for entering a new long position or closing a short one. Vice versa, SELL_AUTO is used to add a selling order for entering a new short position or closing a long position. As you can see, both BUY_AUTO and SELL_AUTO constants open new positions and close previous ones. If you prefer a constant which only opens or closes a position, consider using some of the other four: BUY_TO_CLOSE, BUY_TO_OPEN, SELL_TO_CLOSE, and SELL_TO_OPEN. While names of the constants speak for themselves, feel free to read more about them in our reference.

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);
AddOrder(OrderType.BUY_AUTO, price crosses above avg, open[-1], 100, Color.YELLOW, Color.YELLOW, “Buy”);
AddOrder(OrderType.SELL_AUTO, price crosses below avg, open[-1], 100, Color.RED, Color.RED, “Sell”);

Now this strategy opens the long position or closes the short one at the Open price of the next bar upon respective crossovers of Close price above and below its 20 period SMA. The trade size will be equal to 100, Buy signals will be colored yellow, Sell signals will be colored red, and each signal will display the trade side. After you added a strategy to chart, you can view the performance report by right-clicking any of the signals and choosing “Show report” from the menu. More information on the report can be found here.

Before we pass to the next chapter which will explain how to make your plots even more beautiful, here is an important notice about the strategies: all the signals you get are hypothetical, i.e., you cannot send real orders using strategies.