---
description: >-
  In the given example, we will consider the process of creating the "Moving
  averages crossover" trading strategy algorithm. At the intersection of the two
  averages from below, we enter a long position,
---

# An example of creating a trading algorithm in TSLab

In the previous article ([an-example-of-creating-a-script-in-tslab](an-example-of-creating-a-script-in-tslab "mention")) a script called **First** was created. We will use this script to create a new algorithm.

Open the **First** script by double-clicking the left mouse button or by clicking the **Edit** button in the panel with buttons. Go to the **Editor (Script)** tab.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FnCFe82191uVA9nadt22G%2FEditorBtn.png?alt=media\&token=20186267-7243-4de4-b138-d0c1c430deda)

## Selecting the SMA indicator

In the **Toolbar** window, set the mouse pointer in the search field. Enter the **SMA** abbreviation. After that, the **SMA** indicator will be displayed below the search bar.

Select the **SMA** indicator and while holding the left mouse button drag it to the **Editor**.

| **Block: SMA** | Simple (arithmetic) moving average. It is calculated by adding the incoming values, for example, instrument's closing prices total amount for a certain period divided by the length of the period. | ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FSgKBiUIWhn6lK2axeBga%2FBlockSMA.png?alt=media\&token=15da835b-ec30-4eb3-98b8-ad94d07f7685) |
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |

![Drag-and-drop system](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FLtj7VcxyVjywHLeLYifR%2FSMADrug.gif?alt=media\&token=010abd4c-b2c1-4dbf-aa40-cc8ba6fbd4d7)

## Connecting the Blocks <a href="#anexampleofdevelopingatradingalgorithmintslab-connectingtheblocks" id="anexampleofdevelopingatradingalgorithmintslab-connectingtheblocks"></a>

Connect the **SMA** block to the **Closing** blocks and to the **Main** chart. To connect the blocks you need:

Set the mouse pointer in the on the block's input/output icon.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FYAyUHyfDOmmNgsKGfHJL%2FBlockIO.png?alt=media\&token=eccf0ab7-5197-48ab-a42d-a85cc03f0060)

Hold the left mouse button.While holding the left mouse button, stretch the connecting line to another block with which you need to establish a connection.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FlSxrVGTEaqReJYYJuEUy%2FDragLine.gif?alt=media\&token=43febf44-c03f-413f-aad4-3ce358cb07f8)

For your convenience, you can change the color and thickness of the lines displayed on the chart. To do this, you need to click on the line connecting the **SMA** block to the **Main** chart block.

After selecting the connecting line, the **Properties** menu will be available to you. As an example, we will change the thickness and color of the line.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FgMvCyn31C81P92aUHYD5%2FLineColor.gif?alt=media\&token=424adce4-3346-4e93-a38c-cb5c0c006031)

## Displaying the SMA indicator on the chart <a href="#anexampleofdevelopingatradingalgorithmintslab-displayingthesmaindicatoronthechart" id="anexampleofdevelopingatradingalgorithmintslab-displayingthesmaindicatoronthechart"></a>

Save and run the script.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FOpi8xWqRNYtDbUAtELYT%2FSaveAndRun.png?alt=media\&token=9210c641-5a13-4c40-9c98-7c80ee00c9f8)

After the script is compiled, the results will be displayed on the chart.

According to the terms of the strategy, the SMA is taken from the bar's closing, however, the SMA can be taken from any point of the bar: Maximum, Minimum, Opening or from any other significant values in the algorithm.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2F5wCW9MgJbRqYKqYFMcHc%2FSMAGraph.png?alt=media\&token=9e585c58-f50c-4b59-8186-25ac505f4c78)

For your convenience, all tabs can be moved relative to each other. Left-click on a tab shortcut and then drag it down. In the center of the page will appear a choice of places where the tab can be attached. If you have several monitors, tabs can be moved to other monitors. Then place the **Editor** tab to the right of the **Graphics**.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FSjEYIF17qViacjVU3Nre%2FDragScreen.gif?alt=media\&token=c75357bd-790c-4c85-8a8e-944c188dd739)

## Block: Control Panel <a href="#anexampleofdevelopingatradingalgorithmintslab-block-controlpanel" id="anexampleofdevelopingatradingalgorithmintslab-block-controlpanel"></a>

To implement our strategy, we need to add another **SMA** indicator to the script. Connect the **SMA** block to the **Closing** block and to the **Main** chart. Since **SMA**s currently have the same period, they merge into a single line on the chart.

Assign the name **SMAFast** to one of the averages. Display the average values on the **Control pane** block and then configure it.

| **Block: Control Pane** | A panel on which you can add control elements that control the optimization parameters of blocks. | ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FjfwfuvCZpcAINR0jlMMB%2FBlockControlPane.png?alt=media\&token=88d8cb2c-1bd8-42bd-b760-0c32b3122ea3) |
| ----------------------- | ------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2F2oLMbQumTXawPyLNJeDG%2FContPane.gif?alt=media\&token=e3a043da-f921-4787-b738-5b34f0474a44)

## Configuring SMA Indicator Blocks <a href="#anexampleofdevelopingatradingalgorithmintslab-configuringsmaindicatorblocks" id="anexampleofdevelopingatradingalgorithmintslab-configuringsmaindicatorblocks"></a>

Let's configure the **SMA** and **SMAFast** blocks. To do this, double-click on the **Control Panel** block.

For **SMA** and **SMAFast** blocks, it is necessary to enable the **Recalculate option** (activates the script recalculation function with each change of parameters).

For **SMA** and **SMAFast** blocks, you have to set the type of control. Select the option **Numeric up/down**.

As an example, in the **SMA** indicator, set the values:

* **Current value** = 200&#x20;
* **Step** = 1

Set the **SMAFast** indicator values:

* **Current value** = 50
* **Step** = 1

Save the settings by clicking the **OK** button. Save and run the script.

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2Fxc0ycl43Pz2BU1QodEL2%2FSMA2.gif?alt=media\&token=861e81f1-a685-42b0-8088-33c3c1517755)

## Blocks: Opening/Closing market position. Blocks: Intersection from Above/Below. <a href="#anexampleofdevelopingatradingalgorithmintslab-blocks-opening-closingmarketposition.blocks-intersecti" id="anexampleofdevelopingatradingalgorithmintslab-blocks-opening-closingmarketposition.blocks-intersecti"></a>

Let's add blocks to the script editor:

| **Block: Cross over**                 | <p>The function determines the moments when the second input (signal) crosses the reference line (first input) from top to bottom.<br>The top link = what we cross. The second link of the block = using what we cross.</p>                                                                                                                                                                                                                                   |  ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FVigwM4MDi1AQbGpssFW4%2FCrossOver.png?alt=media\&token=574b6ab7-fc37-4dd7-8f47-e351ca48063a) |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| **Block: Cross under**                | <p>The function determines the moments when the second input (signal) crosses the reference line (first input) from the bottom up.<br>The top link = what we cross. The second link of the block = using what we cross.</p>                                                                                                                                                                                                                                   | ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FBifGtHoGAhRnkRUoBXit%2FCrossUnder.png?alt=media\&token=0ed165df-e94e-43ce-a0cc-07fc10146810) |
| **Block: Position opening By market** | Opening a position at the first prices offered on the market. In calculations on the history, the opening price of the next bar is used. The first link from the Market Position Opening block is always connected to the Source, as there can be many sources in the Editor. If you move the mouse cursor over the links in the Block, each input is signed, so, in the editor, you can always determine which type of data the Block needs for calculation. |  ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FzWalxvfZj5XHzftQudrE%2FOpByMark.png?alt=media\&token=8b00e933-1b79-4bbf-990e-8bc7b5970230)  |
| **Block: Position close By market**   | Closing a position at the first prices offered on the market. In calculations on the history, the opening price of the bar following the signal is used.                                                                                                                                                                                                                                                                                                      | ![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FaEJItKOZYneuEtc6Xqyy%2FClosByMark.png?alt=media\&token=8beb3611-688d-4a13-8810-7adf1ecee504) |

Connect the blocks:

* the outputs of the **SMA** and **SMAFast** blocks with the inputs of the **Cross under** and **Cross over** blocks
* the input of the **Position opening By market** block with the output of the **Source1** block
* the input of the **Position opening By market** block with the output of the **Cross under** block
* the output of the **Market Position Opening** block with the input of the **Position close By market** block
* the input of the **Position close By market** block with the output of the **Cross over** block

{% embed url="https://www.tslab.pro/files/video/an-example-of-creating-a-trading-algorithm-in-tslab/Video.mp4" %}

You can configure the useful information displaying on the chart. To do this, click on the **Menu** button located in the upper left corner. As an example, turn off the name of the Instrument (menu item: ticker).

![](https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2FHNG5WEOZOG1mXBvknNlS%2FHideTiker.png?alt=media\&token=45539ab9-ea7f-4f84-8f0d-e97d58d3e246)

## Script's Optimization <a href="#anexampleofdevelopingatradingalgorithmintslab-scriptsoptimization" id="anexampleofdevelopingatradingalgorithmintslab-scriptsoptimization"></a>

Let's optimize the created script. Since the volume is constant in the data formed by a random variable, the **Volume** block can be deleted so that it does not interfere with the chart.

Then you need to open the **Optimization** tab. Set the Maximum and the Minimum period for the **SMA** and **SMAFast** blocks. Choose a step for changing the period values equal to **10**. The goal of optimization is to select the best indicators, best indicators' configurations and the best algorithm on a loaded data sample.

In the lower left corner of the **Optimization** window, set the maximum number of passes for calculations. To launch, click on the **Start** button.

After the optimization process is completed, you can see the results on the **Optimization Results** tab.

Sort the results by the Net Profit/Loss column. The row with the highest value in this column can bring the greatest profit. In order to write to the script the values obtained during optimization, double-click on the top line. The optimization process is complete.

{% embed url="https://www.tslab.pro/files/video/an-example-of-creating-a-trading-algorithm-in-tslab/optim.mp4" %}

The described example can be downloaded in the ready-made form and loaded into the program:

{% file src="https://45805883-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MaCiHiFV0s-5gqtvIid-1703796690%2Fuploads%2F5OyiGaRfBOk4gUgyozOw%2FFirst.tscript?alt=media&token=ef799f55-db1b-4708-bdda-ff796101ad11" %}

Save the file to your computer. Open the program. Select **Lab - Scripts - Download from file.** Then specify the location of the downloaded file.
