
In a blog on Habré, we write a lot about exchange technologies and trading robots. In today's material, the issue of creating such systems will be discussed in more detail - using the example of the
TradeScript scripting language embedded in the
SmartX trading terminal as an
example .
What tradeScript
TradeSript is a vector programming language developed by the American company Modulus Financial Engineering specifically for creating trading robots. This tool is included in the package of technologies that have been licensed (OEM) by our company for creating the SmartX trading terminal (we talked about its creation
here ).
')
Among the advantages of TradeScript is the simplicity of the syntax, which, nevertheless, makes it possible to describe trading strategies of varying complexity. The language is well suited for use by beginners - for this it will be enough to learn a
simple guide , and experienced developers will deal with it in half an hour.
The language engine runs on the terminal side and connects as a plug-in extension to SmartX. In addition to the possibility of actually writing scripts, there is also a backtesting module for testing the created strategy on historical data.

Due to its integration directly into the terminal, to create robots you do not need to use connectors to send orders to it, which is necessary in the case of many other development environments. This allows us to achieve significantly higher reliability and speed.
We start writing robots: primitives and vectors
One of the important concepts in the language TradeScript - primitives, that is, built-in functions that are needed to facilitate the process of creating scripts. An example of a primitive is a TREND function:
TREND(CLOSE, 30) = UP
It will return True if there is an uptrend - it is calculated over the last 30 days at the closing prices of trading sessions.
TradeScript is a vector language. Each operation here is applied immediately to the whole set of values (vector or field). This allows you to think and operate with categories of data aggregates, without the need to use cycles or individual scalar operations.
For example, to calculate a simple moving average of the “median” stock price for
the last 30 periods with the help of a common programming language like BASIC,
but write something like:
For each symbol For bar = 30 to max Average = 0 For n = bar - 30 to bar median = (CLOSE + OPEN) / 2 Average = Average + median Next MedianAverages(bar) = Average / 30 Next bar 10 Next symbol
To describe an extremely simple action, you need to “spend” 9-10 lines of code. With the help of a vector language, the same useful action can be put on one line:
SET MedianAverage = SimpleMovingAverage((CLOSE + OPEN) / 2, 30)
Thus, in TradeScript you can describe all the same strategies as in procedural languages like C ++, VB or Java.
Examples of real strategies
To determine the time for opening or closing positions, many traders use technical analysis. This is the method by which merchants look for different patterns on financial instrument charts. For this, the so-called technical indicators are used.
Many of the existing indicators (moving averages, oscillators, indices, range and linear regression functions, etc.) are embedded in TradeSript as primitives, so that they can be used when programming robots. Such a program may include one or more indicators. Let's look at code examples of trade robot trading robots using technical analysis indicators.
Crossing Moving Average
One of the most popular indicators is
moving averages . When used as signals, they use facts of intersection of each other by lines.

Below is an example of a system code based on the moving average crossing (Moving Average Crossover). Such a robot will buy if the short moving average crosses the long moving average bottom-up, and to sell if the intersection goes in the opposite direction.
Buy Signals
Parabolic SAR / MA System
There are also more complex variations of systems based on moving averages - for example, this is the Parabolic SAR indicator. Typically, parabolic systems are used to receive exit signals, but you can also use them to make decisions about entering them (buy or sell in a short position).

In our example, the robot makes such decisions based on the intersection of moving averages and the intersection of the Parabolic SAR indicator from bottom to top of the closing price.
Buy Signals
Price gap
There are frequent situations on the stock exchange in which at the beginning of a new trading day the stock price immediately turns out to be higher than the previous day's high. This can happen if there are any positive news, important reports that affect the business, etc.

When traders see such a positive gap, many of them start giving orders to buy - as a result, the stock may be revalued. After such a sudden take-off, there is a possibility of a price rollback to previous values within a couple of hours after the start of trading.
In the example below, we take into account the fact that the moment of reversal usually occurs during the first hour of trading. That is, if the gap is not “closed” during the first hour, then it can be assumed that the purchase is more likely to continue. The script returns stocks that had a gap of at least 2% and closed close to the maximum. Here you can describe the behavior strategy for the next trading day. For example, if the position of the stock remains strong after the first hour of trading, you can buy it. Stop-loss (order for the sale of profit) is better to set at the minimum mark for the day - the conservative option involves making a profit of half a gap, which in our example will be 1%.
Buy Signals
"Bullish" and "bearish" absorption (Japanese Candlestick Engulfing Line System)
Another interesting element of technical analysis is the bullish or bearish figure. They may indicate a reversal of a market trend. The essence of the absorption figure is that a short Japanese candle appears, followed by a candle with a longer body that “absorbs” the previous short candle. Bullish absorption indicates a reversal of the downward trend - the price may rise, and the bearish, on the contrary, its possible decline.

This is how a script using this principle can be:
Signals
Conclusion
A
library containing 22 simple TradeScript trading robots has been published on our old website - based on them, you can create more complex trading strategies. In conclusion - a few more useful links on the topic of creating trading robots.