# Examples of algorithms and indicators

## Introduction

DISCLAIMER

Data, information, and material (“content”) is provided for informational and educational purposes only. This material neither is, nor should be construed as an offer, solicitation, or recommendation to buy or sell any securities. Any investment decisions made by the user through the use of such content is solely based on the users independent analysis taking into consideration your financial circumstances, investment objectives, and risk tolerance. Neither www.tslab.pro nor any of its content providers shall be liable for any errors or for any actions taken in reliance thereon.

This article presents examples of the implementation of algorithms and indicators using the TSLab visual script editor.

In order to run the example, you need:

2. Launch TSLab program.

3. Select the menu item Lab - Scripts

4. In the Scripts window click the Load from file button.

5. Go to the directory with the downloaded script. Select the required script and click on the Open button.

6. In the list of available scripts, select the loaded script and double-click on it with the mouse.

## Simple Indicators Example

The examples of using the block programming editor to create indicators

### Exponential moving average (EMA)

Is executed using the Formula and Constant blocks.

If necessary, the user can use the Formula block instead of the Constant or another indicator for calculating the indicator's Period. This is an example of creating indicators with a managed period.

A * close + (1-A) * ema[i-1]

A = (2/(period+1))

### Momentum

Momentum is calculated as the difference between the bar's closing price and the price n periods ago.

Is executed using the Formula and Constant blocks. The momentum is numerically equal to the profit that could be obtained when investing in a unit of this instrument for the period under consideration.

close - close[i-period]

### Rate of Change (RoC) - Normalized

Is executed using the Formula and Constant blocks. Normalized ROC is numerically equal to the return on investment per unit of this tool for the period under consideration.

100*((close - close[i-period])/ close[i-period]

### Rate of Change (RoC)

The rate of change shows the percentage change in price from one period to another. It is calculated as a comparison of the current price with the price of the previous period, which is separated from the current one by n periods. ROC is numerically equal to the increase in the cost of the instrument over the period under consideration.

((close / close[i-period])*100)-100

### Commodity Channel Index (CCI)

It is calculated as the given ratio of the current deviation of a typical price from its simple moving average to the average absolute deviation of this value.

Is executed using formula blocks. The CCI example contains two more examples. SMA calculation and Typical Price calculation.

TypicalPrice = (Close+Low+High)/3

MAD = SumIn(Math.Abs(TypicalPrice - SMA))/N

### Converted Index of the product channel

Difference from the standard indicator: EMA is used instead of Typical Price, also MAD, built-in TSlab program indicator AMA, is used at calculation.

### Relative Strength Index

To calculate the relative strength, all candles of the selected time period, which showed a closing higher than the previous candle, U Calculation, are selected.

Close>Close[i-1] ? Close-Close[i-1] : 0

The average growth value is determined using the EMA exponential moving average.

The same operation is performed for candles that show a close below the previous one. Calculation D

The ratio of these two values gives the relative strength value (RS).

RSI 100-(100/(1+RS))

Made in the Formula blocks. The Associated parameter block is used.

### Modified Relative Strength Index

In the given indicator there is an example of choosing the type of moving average for calculating RSI, EMA, SMA or AMA. The selection is made using the Constant.

### The example of implementing Ichimoku indicators

The Ichimoku indicator is a technical indicator developed in the 1930s by Japanese analyst Goichi Hosoda, who was published under the pseudonym Sanjin Ichimoku, to predict the movement of Japan's Nikkei stock index. The Ichimoku indicator combines several approaches to market analysis and is designed to identify trends, support and resistance lines and generate buy / sell signals. Working with formula blocks.

## Simple Scripts Example

The examples of using the block programming editor to create simple trading systems described in Wikipedia

### Manual Trading with automatic stop-loss

The example of the application of logical constants.

You can enter positions using the buttons on the control panel. Exit positions using trailing and stop-loss blocks or using the Drop buttons.

The recommended conversion for the Recalculations by events script. Or the Transaction recalculate.

### An example of using the EMA indicator

The enter to the Long position at the bottom-up intersection of the short-period EMA (ema2) and the long-period EMA(ema1).

Stop loss is calculated using the Updating value Block using the formula

entry*(1+(Profit-stopValue)/100)

Where Profit - MFE% maximal deviation of the price from the entry price in the better direction.

stopValue - Constant

entry - Entry price

### The example of using the ADX indicator

Enter the Long position, if DI+ is greater than DI- and the ADX value on the last closed bar is greater than the ADX value on the previous bar. adx[i-1] < adx[i]

Close the position if DI+ is less than DI- and adx[i-1] > adx[i]

### Alligator strategy, using Smoothed Moving Average (SMMA)

• «Alligator's jaw». (Jaw) This is the moving average with the longest period;

• «Alligator's teeth». (Teeth) The second moving average with a period less than Jaw;

• «Alligator's lips» (Lips) — the fastest one with a period less than Teeth.

Entering the Long position when the Median Price (mp) is > lips & lips > teeth & teeth > jaw Entering the Short position, when mp < lips & lips < teeth & teeth < jaw

The Exit from the positions is performed by TrailStop.

### The example of using the "Maximum for a period" and "Minimum for a period" indicators.

The system is based on the breakdown of the maximum and minimum price levels over a period of time. When the current price breaks up the upper line of the channel Maximum For, enter the Long position. When the current price breaks down the lower border of the channel, enter the Short position. Exit from the positions is also performed via the Donchian channel, with other periods.

### The example of Hi_Lo with the use of the Compress unit.

This example solves the problem of choosing a timeframe (an interval of bars). In the source the interval is 1 minute. Any interval greater than or multiple of a minute can be used in compression.

### The example is based on the MACD indicator, using filtering of the position entry signals.

Enter the Long position if the Macd is greater than the filter Constant or less than the -Constant, when the signal line crosses the MACD indicator.

Exit the position at the reverse intersection.

### Using the CMO indicator

Chande Momentum Oscillator (CMO) — modification of the Momentum indicator. Enter the Long position when the CMO crosses zero from bottom to top. Enter the Short position when the CMO crosses zero from top to bottom. Trailing stop is applied to exit positions. Assumes the presence of two opposite positions.

### The example of building a "Premium" chart.

One instrument minus the second instrument. This approach is used for arbitrage strategies. In this case, spatial arbitration.

### An example of using the StochK indicator

In the TSLab program all the indicators are considered "As in the book", Stochastic is no exception. In other words, no price smoothing is applied before the indicator is calculated.

In the example the SMA smoothing of the Stoch indicator is used. Constants are used to enter a position. Entering the long position when the SMA crosses the lower constant from bottom to top. Closing a position when the SMA crosses the upper constant from top to bottom.