How-to: Creating trading robots on TradeScript vol. 2
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 operations manually or to control the actions of the robot.
However, even those merchants who work exclusively with their hands in the terminal, sometimes they want to automate some processes and program trading strategies. One of the ways of such automation is to write trading robots in the scripting language TradeScript, which is built into the terminal for trading on the SmatX exchange (the process of creating it is described in a separate topic ).
What is a trading robot
A trading robot or a mechanical trading system is usually called a program that “knows how” to analyze market conditions and, in accordance with a certain trading strategy, performs operations of buying or selling 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 TradeScript vector language created by Americans from Modulus Financial Engineering . This technology has been licensed by us (OEM) to create the SmartX terminal.
Scripts on TradeScript
The TradeScript language “engine” works on the terminal side — it is connected as an extension plug-in (such add - ons extend the program’s functionality well).
The syntax of the language is quite simple, but nonetheless, it allows you to describe trading strategies of almost any complexity. TradeScript has built-in functions (primitives) that facilitate writing scripts. An example of such a primitive, the TREND function:
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 "average" share price for the last 30 periods on TradeScript looks like this:
SET MedianAverage = SimpleMovingAverage((CLOSE + OPEN) / 2, 30)
In addition to the ability to write scripts, their performance can be tested on real and historical data using the back testing function.
For easier learning of 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 complex cases.
Parabolic SAR / MA System
This trading system is a variant of the standard moving average intersection system.
Usually a parabolic system is used only to receive signals to exit a position (sale of shares or “buyback” 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 the
closing price from the bottom up.
After opening a position, Parabolic SAR can be used in the usual context. Profit should 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 match the conditions of either the current trading session or the previous trading day.
Outside Day System
An Outside Day occurs when the maximum of the current bar is higher than the maximum of the previous bar, and the minimum of the current bar is lower than the minimum of the previous bar. The closing price should 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.
External days that occur after a long uptrend, as shown in the figure, indicate the indecision of the market, and can be a signal of a reversal or temporary correction of the trend.
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 sub-
handles, then they can be attributed to 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
The system of "bullish" and "bearish" absorption (Japanese Candlestick Engulfing Line System) An
effective short-term trading strategy is often to use technical analysis figures, such as "bullish" or "bearish absorption" - in case of a sharp increase in trading volume, they signal a trend reversal. The absorption pattern consists of a short candle, followed by a candle with a longer body, which “absorbs” the previous short candle.
Accordingly, a “bullish” absorption indicates a potential reversal of a downtrend, and a “bearish” one indicates a possible reversal of a bullish (that is, an upward) trend. The more noticeable the trend, the more predictive power these patterns have.
The code for a similar TradeScript system 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 site has a library that includes 22 small trading robots, based on which you can create a more complex mechanical trading system. After downloading the files presented in the library to the terminal, users will see the code of the trading system, in addition, the necessary data on the instrument will be entered in the necessary settings menu.
TradeScript code of the presented strategies 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 select the TradeScript Manager in the "Extensions" menu and click "Download".
A full description of the TradeScript language and a description of the construction of rather complex trading robots on it can be found in a special guide . You can test 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, one should take into account the size of the commission for transactions from the exchange and the broker in the strategy - usually the exchange takes about 0.25 rubles for each scalper transaction (which opens and closes within one trading day), and the broker commissions approximately correspond to exchange ones (for ITinvest, on some tariffs they are equal to exchange ones).
That's all for today. We will be happy to answer questions in the comments. Thanks for attention!
Related posts and links: