#### Recommended Best Practices

As soon as you start writing you own code, you will come across situations where there are two or more ways to solve a problem or calculate a certain result. In the following section, we want to give you a few recommendations as to what approach usually works better in our experience.

Avoid creating history for items where it is not necessary

The performance of a study depends on its memory consumption (among other things). The more values that have to be remembered, the slower the study will react when inserting it to a new chart or price page, changing the compression of a chart etc. As you have already learnt, you can access the history of every variable and even of already performed calculations. However, the TeleTrader Language does not automatically create a history for all items, only for those where the history is needed and used in the program. Thus, whenever you access the history of a variable, result, expression or calculation, you are actually creating this history. Creating a history for an item means adding a lot of data to the memory that has to be stored (temporarily). To optimize the performance of your studies, you should use this feature reluctantly. Often, it can be avoided to create additional history for an item, while still getting the same results.

Consider the following examples:

x = Sum(Source, period)[2]                                {AVOID}

This line will sum up the given values of a security Source for the period specified. It repeats this for every bar of the time series. Then, it assigns the value that was calculated two bars ago to the variable x. In order to do this, all results of the Sum function (its history) have to be stored in the memory. For charts with a lot of visible bars (more than 100.000 bars), this is a lot of data that has to be stored. Therefore, the performance of the study could suffer.

To avoid creating a history for Sum, you could get the same result by using the following line:

x = Sum(Source[2], period)                                   {USE}

As the security Source always has a history, this would not add any data to the memory, and so the performance of the study will be much better. You are using a simple Sum operation, only on displaced data (Source[2]) – instead of doing a Sum operation, storing all its data and then taking the results out of the history of the Sum results.

History is not only created when you explicitly address it – some ways of calling a function also create a history for it. The following line shows the Sum function that is used on an expression:

y = Sum(Source.Close Source.Low, period)                 {AVOID}

In order to be able to calculate the sum of the given array of values, for every bar in the time series the result of the expression Source.Close Source.Low has to be stored in the memory. This creates unnecessary history for this expression – you could get the same results by instead changing your program to include the following line:

y = Sum(Source.Close, period) Sum(Source.Low, period)      {USE}

Here, no additional history has to be created and stored, which will vastly improve the performance of your study.

The Sum function is performed twice now, but the overall number of operations has not increased much: Before, the expression Source.Close Source.Low had to be calculated period times, and the results had to be added period times to form the sum (2 x period operations). Now, the Close and Low values have to be summed up (2 x period operations), and one additional subtraction takes place. This means a huge benefit at the expense of just one additional operation.

Define a proper time region for price pages

If you want to use your custom studies in price pages, you should always define a value for the Time region for price pages setting. You can find this setting on the Parameters tab of the Study Editor (bottom right of the dialog). In this field, you can either enter a constant value (number of bars), or an expression that is derived from the study's parameters. For example, for the Moving Average Displaced, the appropriate expression is period+period_dis. You can also use some TeleTrader Language functions (such as min / max) in your expressions. If you don't define a value here, the default value of 100 bars will be used in price pages.

You should take some time to define a proper time region for your study, ideally one that depends on the study's parameters (if applicable). Keep in mind that if you add too little history, the study might not be calculated (leading to an empty value in the study column). On the other hand, if you add too much history per default, this can affect performance, since the history must be downloaded for every symbol in the price page.