📜 ⬆️ ⬇️

How-to: Creating trading robots in TradeScript vol. 2

image

We have repeatedly talked about algorithmic trading on the exchange and the creation of trading robots. We also mentioned that most of the participants in the stock market still use the trading terminal - to perform manual operations or control the actions of the robot.

However, even those dealers who work exclusively with their hands in the terminal sometimes want to automate some processes and program trading strategies. One of the ways of such automation is to write trade robots in the TradeScript script language, which is built into the terminal for trading on the SmatX exchange (the process of its creation is described in a separate topic ).
')

What is a trading robot


A trading robot or mechanical trading system is usually referred to as a program that “knows” how to analyze market conditions and, in accordance with a certain trading strategy, makes purchases or sales of securities and derivatives ( futures or options ).

Often, programming languages ​​like C / C ++ (sometimes Java), Python, Matlab, and R. are used to write robots. Scripting languages ​​that allow you to describe fairly complex strategies include the vector language TradeScript, created by Americans from Modulus Financial Engineering . This technology was licensed by us (OEM) to create a SmartX terminal.

TradeScript scripts


The “engine” of the TradeScript language works on the terminal side - it is connected as a plug-in extension (such add - ons well expand the functionality of the program).

The syntax of the language is quite simple, but nevertheless allows you to describe trading strategies of almost any complexity. TradeScript has built-in functions (primitives) that make it easier to write scripts. An example of such a primitive function TREND:

TREND(CLOSE, 30) = UP
This function will return the value “True” if there is an uptrend - its presence is calculated for the last 30 days at the closing prices of trading sessions.

An example of a simple script for calculating the moving average "median" stock price over the past 30 periods in TradeScript looks like this:

SET MedianAverage = SimpleMovingAverage((CLOSE + OPEN) / 2, 30)
In addition to the possibility of writing scripts, their performance can be tested on real and historical data using the back-testing function.

image

For easier mastering of the TradeScript language, a free library of ready-made trading robots was created, which can be modified.

Examples of trading strategies


We published examples of trading systems on TradeScript are given in a separate material on Habré, today we will consider slightly more complicated cases.

Parabolic SAR / MA System

This trading system is a variant of the standard moving average crossing system.

image

Typically, a parabolic system is used only to receive exit signals (selling shares or “buying off” when opening a short position).

Specifically in this MTS, however, the intersection of two EMAs (exponential moving averages) is used to decide whether to buy (sell) whenever the Parabolic SAR indicator crosses below
up the closing price.

After opening a position, Parabolic SAR can be used in a normal context. Profit must be recorded when the closing price crosses the Parabolic SAR.

 Buy Signals # ,         #  PSAR     (CROSSOVER(CLOSE, PSAR(0.02, 0.2)) OR CROSSOVER(REF(CLOSE,1), PSAR(0.02, 0.2))) AND (CROSSOVER(EMA(CLOSE, 10), EMA(CLOSE, 20)) OR CROSSOVER(REF(EMA(CLOSE, 10),1), REF(EMA(CLOSE, 20),1))) Sell Signals # ,         #  PSAR     (CROSSOVER(PSAR(0.02, 0.2), CLOSE) OR CROSSOVER(PSAR(0.02, 0.2), REF(CLOSE,1))) AND (CROSSOVER(EMA(CLOSE, 20), EMA(CLOSE, 10)) OR CROSSOVER(REF(EMA(CLOSE, 20),1), REF(EMA(CLOSE, 10),1))) 

This example shows how to use Boolean logic to find stocks that meet the conditions of either the current trading session or the previous trading day.

Outside Day System

Outside Day occurs when the high of the current bar is higher than the high of the previous bar, and the low of the current bar is lower than the low of the previous bar. The closing price must be the opposite of the current trend (if the trend is up, then Close should be less than Open). External days occur quite often and can be used as part of a short-term trading strategy.

image

External days that occur after a long uptrend, as shown in the figure, indicate market indecision, and can be a signal of a reversal or temporary trend correction.

Depending on the direction of the market, external days can be either bullish or bearish. If signs of a reversal occur at resistance levels, they can be regarded as bearish. If they appear at levels under-
holders, they can be attributed to the bull.

 Buy Signals #  outside days LOW < REF(LOW, 1) AND HIGH > REF(HIGH, 1) AND HIGH > REF(HIGH, 1) AND CLOSE < OPEN AND # Outside days  ,  #     HIGH - LOW > (REF(HIGH, 1) - REF(LOW, 1)) * 1.5 AND #     TREND(CLOSE, 30) = UP Sell Signals #  outside days LOW < REF(LOW, 1) AND HIGH > REF(HIGH, 1) AND HIGH > REF(HIGH, 1) AND CLOSE < OPEN AND HIGH - LOW > (REF(HIGH, 1) - REF(LOW, 1)) * 1.5 AND #     TREND(CLOSE, 30) = DOWN <h5> 

The system of bullish and bearish absorption (Japanese Candlestick Engulfing Line System)

An effective short-term trading strategy is often the use of technical analysis figures, such as “bullish” or “bearish takeover” - in the event of a sharp increase in trading volume, they signal a trend reversal. The absorption figure consists of a short candle followed by a candle with a longer body that “absorbs” the previous short candle.

image

Accordingly, a bullish takeover indicates a potential reversal of a downtrend, and a bearish one indicates a possible reversal of a bullish (ie upward) trend. The more noticeable the trend, the stronger the predictive power of these patterns.

The code for such a system on TradeScript might look like this:

 Signals #   CANDLESTICKPATTERN() = BULLISH_ENGULFING_LINE AND TREND(CLOSE, 30) = DOWN AND VOLUME > REF(VOLUME, 1) #   CANDLESTICKPATTERN() = BEARISH_ENGULFING_LINE AND TREND(CLOSE, 30) = UP AND VOLUME > REF(VOLUME, 1) 

Our website contains a library of 22 small trading robots, on the basis of which you can create a more complex mechanical trading system. Having loaded the files presented in the library into the terminal, users will see the code of the trading system, in addition, the necessary data about the instrument will be entered in the necessary settings menu.

The code of the presented strategies on TradeScript is easy to modify and supplement right in the terminal.

Note: in order to download the selected script to SmartX, you need to download the library to your computer, install the terminal, and then in the "Extensions" menu, select TradeScript Manager and click "Download".

Conclusion


A full description of the TradeScript language and a description of the construction of quite complex trading robots on it can be found in a special guide . You can test the operation of the terminal and your own robots using a test circuit - a risk-free trading system with virtual money (you can open such an account here).

In addition, when writing robots, the size of the transaction commission on the part of the exchange and the broker should be taken into account in the strategy - usually the exchange takes about 0.25 rubles per scalper transaction (which opens and closes during one trading day) for each correspond to the exchange (in ITinvest on some tariffs, they are equal to the exchange).

That's all for today. We will be happy to answer questions in the comments. Thanks for attention!

Posts and related links:

Source: https://habr.com/ru/post/237689/


All Articles