TSLab
TSLab.proSupportTSLab LiveTSVerse
Eng
Eng
  • TSLab trading platform
  • For newbies
    • A beginner's guide
      • Purchasing a license key for a data provider
      • An example of connecting text quotes
      • Example of connecting a data provider to a broker / exchange
      • An example of creating a script in TSLab
      • An example of creating a trading algorithm in TSLab
  • Installing TSLab
    • TSLab Installation Guide
      • System requirements
      • TSLab Installation manual
    • Update TSLab
      • Preparing for update
      • Release build
      • Nightly build
    • Restarting the TSLab program
    • TSLab Changelog
      • TSLab 2.1 Changelog
      • TSLab 2.2 Changelog
        • 2.2.26.0 - 2024/01/31
        • 2.2.25.0 - 2024/01/17
        • 2.2.24.0 - 2024/12/04
        • 2.2.23.0 - 2024/09/19
        • 2.2.22.0 - 2024/08/30
        • 2.2.21.0 - 2024/05/17
        • 2.2.20.0 - 2024/05/15
        • 2.2.19.0 - 2024/02/21
        • 2.2.18.0 - 2024/02/09
        • 2.2.17.0 - 2023/11/16
        • 2.2.16.0 - 2023/10/20
        • 2.2.15.0 - 2023/10/13
        • 2.2.14.0 - 2023/08/11
        • 2.2.13.0 - 2023/05/12
        • 2.2.12.0 - 2023/02/28
        • 2.2.11.0 - 2022/12/15
        • 2.2.10.0 - 2022/10/27
        • 2.2.9.0 - 2022/09/16
        • 2.2.7.0 - 2022/06/23
        • 2.2.5.0 - 2022/04/29
        • 2.2.3.0 - 2022/04/01
        • 2.2.2.0 - 2021/12/29
  • Data providers
    • Cryptocurrency exchanges
      • Binance Cryptocurrency Exchange
        • Registration on Binance
          • Registration on Binance Exchange website
          • Configure Binance Wallet
          • Creating a Binance API Key for TSLab
          • Binance. Transferring USDT from wallet to wallet without commission
        • Registration on TSLab [Binance]
          • Account registration on the TSLab website [Binance]
          • Creating a free Binance Data Provider for TSLab
        • Setting up a Binance Data Provider in TSLab
        • TSLab Binance Edition
          • Starting the program
          • Configuring the Binance Data provider
      • Bitget Cryptocurrency Exchange
        • Registration on the Bitget official website
        • Creating a Free Bitget Data Provider
        • Configuring the Bitget Data Provider in TSLab
      • ByBit Cryptocurrency Exchange
        • Registration on the ByBit website
        • Creating a free ByBit data provider
        • Configuring the ByBit Data Provider in TSLab
      • Deribit Cryptocurrency Exchange
        • Deribit data provider settings
        • Deribit. Frequently asked questions and useful information
        • Deribit. Possible problems and solutions
        • Deribit Margin Requirements Calculation
        • An example of a ready-made script for Deribit
      • Kucoin Cryptocurrency Exchange
        • Registration on the KuCoin official website
        • Creating an API key on the KuCoin website
        • Issuing a free license for KuCoin Data Provider
        • Setting up the KuCoin data provider in TSLab
      • OKX Cryptocurrency Exchange
        • Setting up an OKX account
          • Registration on the OKX website
          • OKX API Key creation
          • Issuing a free license for OKX Data Provider
          • Setting up the OKX data provider in TSLab
          • OKX demo account
        • TSLab OKX Edition
          • First launch of TSLab OKEx Edition
          • Configuring the OKEx data provider
          • Trading in TSLab [OKEx]
          • OKEx Demo
    • Historical Data
      • YahooFinance
      • Offline data provider in CSV format
      • An example of connecting text quotes
      • Text files with historical data
    • Interactive Brokers
      • Getting Started with Interactive Brokers Data Provider
        • Installing and configuring the Trader Workstation (TWS) terminal
        • Installing and configuring the IB Gateway terminal
      • Features of working with Interactive Brokers data provider
      • Known issues for Interactive Brokers Data Provider
      • Loading instruments in a package
  • Working with the program
    • Main menu
      • File
        • Program Settings
      • View
        • Chart
          • Features of working with the Chart in TSLab
          • Chart Controls
        • Depth of Market
        • Quotes
        • All Trades
      • Data
        • Data Providers window
        • Add online data provider
        • Add offline data provider
      • Lab
        • Scripts window
          • Script Container
      • Trading
        • Accounts window
        • Positions window
        • My trades window
        • My orders window
          • Move orders and deals to an agent
        • Autotrading Control Center window
          • Agent window
          • Agent trading settings
            • Event recalculations
            • Slippage
          • "Forget errors" of the agent
        • Orders manager
          • Linking a manually completed order to an agent
        • Agent Control Center window
      • Tools
        • Data backup and recovery
        • Notifications manager
          • Notifications Manager Filters
          • Reference List of Service Message Numbers
          • Example of setting up notifications for Gmail
        • Export to Excel
    • Common interface
      • Status bar
      • Tab
      • Workspace
      • Spreadsheets
    • Visual editor
      • Toolbar
        • Compress and Decompress Blocks
      • List of visual blocks
        • Service Elements
        • Cycles
        • TSChannel
      • The syntax of the Formula, Boolean expression and String expression blocks.
    • Autotrading Control Center
      • Agent trade settings
        • Agent trade settings - Placing orders
        • Agent trading settings - Script execution
        • Agent trading settings - Events
    • TSLab API
      • API Introduction
        • Visual Studio Installation
        • First script (API)
        • First Indicator (API)
        • Script Debugging
        • Logging
      • Writing scripts on the API
        • Trading Instrument Data
        • Working with positions
        • List of deals
        • The order queue
        • Standard Indicators and Handlers
        • Script parameters
        • Caching
        • Local and global cache
        • Multiple Tools
      • Writing indicators on the API
        • Stream indicator
        • Bar indicator
        • Pre-processed indicator
        • Indicator with multiple calculations
      • Additional features
        • The ability to create your own optimizers *
        • C ++ / CLI script
        • API Control Panel
        • Result from script
        • * Оптимизация. Пул массивов.
      • Examples
        • Get script results
        • Get script parameters
        • Get script and agent settings
        • Get position balance (net worth)
        • An example of a strategy Breakout of the Donchian channel
        • Example of a Moving Average Crossover strategy
        • Indicator example
        • How to speed up the processing of a script on the API
        • Links to examples
      • Question - Answer
        • HandlerParameter Attribute
    • Optimization
      • Insufficient load on a multicore processor
  • Examples of scripts and indicators
    • Examples of algorithms and indicators
    • Examples of implementing strategies in TSLab
      • A trading system based on indicator CCI
      • A trading system based on Standard Deviation
      • A trading system based on Aroon
      • A trading system based on ADX and DI technical indicators
      • A trading system based on the RSI indicator
      • Example of a trading strategy without parameters
  • TSLab website
    • TSLab Support Service
      • Memory dump of TSLab application
      • TSLab program log files
Powered by GitBook
LogoLogo

Мы в соцсетях

  • Группа в Telegram
  • Новости TSLab
  • Vkontakte
  • YouTube канал TSLab Live

Наши веб-сайты

  • TSLab
  • Служба поддержки
On this page
  • Selecting the SMA indicator
  • Connecting the Blocks
  • Displaying the SMA indicator on the chart
  • Block: Control Panel
  • Configuring SMA Indicator Blocks
  • Blocks: Opening/Closing market position. Blocks: Intersection from Above/Below.
  • Script's Optimization

Was this helpful?

Export as PDF
  1. For newbies
  2. A beginner's guide

An example of creating a trading algorithm in TSLab

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,

Last updated 3 years ago

Was this helpful?

In the previous article (An example of creating a script in TSLab) 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.

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.

Connecting the Blocks

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.

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.

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.

Displaying the SMA indicator on the chart

Save and run the script.

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.

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.

Block: Control Panel

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 SMAs 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.

Configuring SMA Indicator Blocks

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

  • 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.

Blocks: Opening/Closing market position. Blocks: Intersection from Above/Below.

Let's add blocks to the script editor:

Block: Cross over

The function determines the moments when the second input (signal) crosses the reference line (first input) from top to bottom. The top link = what we cross. The second link of the block = using what we cross.

Block: Cross under

The function determines the moments when the second input (signal) crosses the reference line (first input) from the bottom up. The top link = what we cross. The second link of the block = using what we cross.

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.

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.

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

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).

Script's Optimization

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.

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

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

55KB
First.tscript
Drag-and-drop system