Algorithmic language QPILE for programming trading robots for QUIK.
Trading robots can be written in a specific language, similar to program code. QPILE is one of them, the article will consider this language, compare the QPILE and
LUA languages , and also provide examples of robots in this language.
- General information about QPILE
- Working with a table
- QPILE constructs
- Data types
- Debugging programs
- QPILE or LUA?
- How to create a trading robot on QPILE?
- Trading robots on QPILE – ready-made solutions
- Moving average robot
- N. Moroshkin position calculator
- Volume filter
- Options Greeks
- TRIX trading robot for QUIK
- M4 preprocessor
General information about QPILE
QPILE is an abbreviation that stands for QUIK Programmable Interface and Logic Environment.
This is a series of commands interpreted by the QUIK workstation. It is mainly used to calculate the price of portfolios of securities. The language is also used for:
- dynamic recalculation of the price of clients’ assets on the broker’s desktop and in their portfolios. In the latter case, their total price is also recalculated;
- finding missing indicators using their own algorithms and data for margin lending;
- developing the right trading strategy.
The language describes the structure of the table: the appointment of columns and rows, formulas. In the latter, mathematical operations, variables, data from other tables can be used for calculation. The program code loaded from the QUIK server or from the user’s disk is processed by the language interpreter, which calculates the values in the formulas. The tables of one program have one data source, but the calculations are not duplicated and do not affect the efficiency of the system. When working with QUIK tables, tables on QPILE have standard functions. QUIK in the workplace is equipped with a QPILE code debugger. https://articles.opexflow.com/software-trading/torgovyj-terminal-quik.htm The language allows you to describe new tables with a given structure, highlight cells with certain values, calculate fields based on mathematical and logical expressions. The user will receive notifications in the form of a sound signal or a text message. Tables on QPILE can be edited, printed, copied, exported via ODBC and DDE server. The initial data are from the QUIK tables:
- transactions, including for execution and impersonal;
- orders, including stop orders, for over-the-counter trades and orders – reports for trades in settlement deals;
- “client portfolio”, “buy/sell”
- data from the participant’s positions by money, trading accounts, instrument.
Tables based on QPILE cannot be copied to a text file via the context menu and exported to technical analysis systems, and charts cannot be developed based on them. QPILE-based tables cannot be filtered or sorted.
Working with a table
To load the program code, you need to select the services menu, then QPILE scripts. You can also use the combination Ctrl+F11. After you need to click “Add” and select the desired file. It has a .qpl extension and its name will appear in the Available Scripts list.
If there is an error in the program, the system will make a notification in the “Message Window”, which will contain the name of the defective file and the line number with the error. If no failures were found in the file, the following data will be indicated in the fields:
- table name;
- number of columns and clients;
- list of identifiers and clients;
- the list of parameters and the source code of the file.
The “Download locally” button will allow you to get the table from a file, and “Upload to server” will send the program to the server so that all users can access it. To create a table, click Create Table. In the “Available scripts” select the program you need to execute, set the number of lines through the “Client Filter”, define the required columns and their sequence.
Pausing and resuming the calculation, starting the calculation again and in debug mode, saving the description to a file, demonstrating the formula, and script parameters are available in the context menu.
- String – string.
- Double is a floating point number.
- Collection – a collection.
- Map – an associative array – a sequence of pairs that allows you to get information by key.
The arithmetic operations of addition, subtraction, multiplication, division are applied. The logical expressions “and”, “or”, equals, more, less, inequalities, conditional construction “if … then …” are also used.
Functions can be located anywhere in the program and have different names. In total, 18 groups of mathematical functions and functions are available to obtain the values of tables and parameters, associative arrays, a list of tables, and other tasks. First grade:
- Mathematical functions that allow you to return the value of the sine, cosine, tangent, cotangent of the argument, calculate the exponent of the argument, generate a random number, etc.
- General commands : NEW_GLOBAL to initialize a global variable and MESSAGE to open messages.
Functions to work with:
- Collections of objects (Collection) . They allow you to create a new collection, return the required number of elements, replace or insert the desired values.
- Associative Arrays (MAP) . Help to create and manage the array.
- Files – working with text files, maintaining a log-log of the program. The file name may contain the path to it.
- Strings .
- Graphs . Functions GET_CANDLE to access candle data and GET_CANDLE EX to return an associative array.
- Applications . Creating orders and sending them to the trading system.
- Tags . Their construction and installation on the chart. Adding, deleting one or all labels, getting and setting parameters for a specific label.
There are also functions for:
- For accessing rows of arbitrary QUIK tables and lists of available parameters . Access to workplace table data. These include GET_ITEM to return the MAP and GET_NUMBER_OF to return the number of entries.
- To work with a programmable table . These commands operate on the OWN table. It is readable by the standard functions from the previous paragraph and by these functions. This group includes commands for adding, modifying and deleting a row with an index, and completely clearing the OWN table.
To get values use:
- Tables of current trades . Getting exchange information parameters using GET_PARAM (_EX) commands.
- Quote windows . Obtaining the values of instrument quotes.
- Tables of positions by instruments and money . Obtaining data on a client, firm, instrument, depo account by code.
Service functions – returning the date of the current trading session, associative series, current date and time, determining the connection state, interrupting the calculation mode.
Step-by-step control over the operation of the program is carried out in the “Debug” window. It is opened from the context menu “Start calculation in debug mode”. It can also be opened with the breakpoint() command, and the program line will be highlighted in red. The window contains fields with program code and variable values. At the bottom of the window there are buttons “Next step”, “Continue execution”, “Stop calculation”. Pressing F5 will continue the execution of the program, the combination Shift + F5 will stop debugging, the F10 key will direct to the next line.
QPILE or LUA?
LUA is a new programming language for creating trading robots. Like QPILE, it is implemented in the QUIK terminal. https://articles.opexflow.com/programming/lua.htm A site dedicated to the development of trading robots for trading shows the advantages of the LUA language over QPILE. So, it can function as an uncompiled script and bytecode, equipped with developer tools and object-oriented programming mechanisms. Objects written in other programming languages can be connected to LUA programs. LUA provides 8 data types, including metatables. The LUA language is multi-threaded, fast, and transactions and terminal events are asynchronous. LUA is more common than QPILE, and many extensions have been written for it.
The QPILE language is now deprecated. Experts on the forums recommend using LUA. Although useful and effective programs are still being used.
However, the QPILE language is simple compared to LUA, so if you have no experience in programming, it is better to choose QPILE. In this language, you can write a simple robot if you do not need to perform complex calculations.
How to create a trading robot on QPILE?
To create a trading advisor, you will need the following programs:
- ITS Quik.
- Notepad++ code editor.
- Guide to QPILE.
- XML plugin for code detection in Notepad++.
Download and install these programs. Enable the language syntax by placing userDefineLang.xml in the path C:\Users\User\ AppData\Roaming\Notepad++\
First, let’s create the following trading strategy. The script should calculate Hi and Low for the last few candles every minute, check the current price of the instrument. A position is opened in the required direction if the price of the instrument reaches the maximum or minimum value for the period. When a signal for short is received, the long position is automatically closed. The robot uses the GAZP tool, the time frame is set to 1 min. The hi-low measurement depth is set to 5 candles. To enter Long, a rule is set and the intersection of the current price of the maximum value for the period. The robot will enter Short if the current price crosses the minimum value for these 5 candles. Write a block of code in Notepad++ with basic settings and server date and time. This code is shown in the picture below.
The first 4 lines of code form the program header. Between lines 6 and 44 is the body of the program, which contains the contents of the robot. Lines 7 – 12 contain user settings, which include the codes of the instrument, group and client, the account number on the MICEX. These values do not change during program execution. Lines 13 – 16 are the initial values of the variables. They will be reset to zero on each iteration pass. Lines 17 – 21 contain an indication to use the server date and time. This data is passed to the DATETIME function and converted to text format. Lines 22 – 29 send the received data to the table. The code must be saved and the qpl extension must be specified.
Open the program in QUIK by pressing Ctrl+F10 and selecting the generated qpl file. Use the Ctrl+F11 combination to open the “Work with Portfolios” window and set the portfolio calculation period to 5 minutes. Press Ctrl+F12 to set the columns to display in the table. The picture shows the result of the work of the robot.
Let’s add a few more functions and conditions to our program.
The “User settings” block is supplemented with the NUMBER and INTERVAL variables, which will be used in the function of obtaining the last N bars. The DATETIME function sets the server date and time, and the current date and time functions CURDATE and CURTIME are called from it. The current time is converted to a number on line 24. Line 26 sets the time for the algorithm to run from 10:00:01 to 18:40:00 UTC.
Most often, indicators, oscillators, statistical calculations are calculated by the last N candles of the chart. QPILE does not have a function for collecting the number of candles N periods ago as such, but there is a GET_CANDLE function that accesses candles on the chart by date and time in text format. Our code contains the function to get the last N bars OHLC(). It creates a collection with the parameters of the last candles nested in the COLLECTION list. The program checks the INTERVAL value, and if it does not fall within the range 0…60, a message is displayed that the value does not correspond to the allowed range. A MAP collection with candles is created in line 88. A FOR loop polling the chart to the required depth with the INTERVAL step from the new to the old candle. In line 90, after the cycle is declared, the robot goes back an interval. When you get a candle with GET_CANDLE, place it in the BARLIST collection using the INSERT_COLLECTION_TEAM function. As a result of the operation of the OHLC() command, BARLIST is replenished with the last candles from the instrument chart in the amount of NUMBER. The number of elements in the collection is requested by the GET_COLLECTION_COUNT() command and reported by the BARCOUNT variable. Server date, time, values of INSTRUMENT and BARCOUNT variables are reported by SET_VALUE() function in OUTPUT. This array is created by the CREATE_MAP() command. The data falls into the custom table, which can be seen in the photo below. the values of the INSTRUMENT and BARCOUNT variables are reported by the SET_VALUE() function in OUTPUT. This array is created by the CREATE_MAP() command. The data falls into the custom table, which can be seen in the photo below. the values of the INSTRUMENT and BARCOUNT variables are reported by the SET_VALUE() function in OUTPUT. This array is created by the CREATE_MAP() command. The data falls into the custom table, which can be seen in the photo below.
Trading robots on QPILE – ready-made solutions
Moving average robot
Demo robot not suitable for real trading.
The robot includes 5 parameters. DATE, TIME, PRICE identifier values are set to level 30. MOVING and TP identifiers are set to zero precision. Robot code on moving average written under QPILE:
N. Moroshkin position calculator
A program for calculating the levels of the maximum allowable drawdown and the target for long and short positions at current ask and bid prices. The levels are calculated for 2 values of the position entry volume. The robot finds the allowable position volume when assigning a stop order in one step from the opening price with the calculation of opening a position in the direction of the bar growth. The found levels are entered into the terminal window, which are subsequently reflected in the price chart. Transactions are fixed for a given instrument. If a position is opened, the robot starts calculating its parameters. Depending on the change in position, the assigned orders are adjusted.
A robot-portfolio for calculating the arithmetic mean of the volume for candles and comparing it with the average product by the X coefficient. Works correctly with plotted charts in a selected period of time.
TRIX trading robot for QUIK
The program is based on the TRIX Indicator. When the indicator closes above the zero line, the specified level, the robot takes the Long position. The position is closed by Take Profit, Stop Loss or trailing stop.
Program for working with QPILE and Lua. Includes archives with executable files, documentation and DLL files with regular expression parsing. To use the program, you need to unpack the executable files and place regexp2 in the C:\Windows path. Lessons on QPILE for QUIK: https://youtu.be/vMTXwDUujpI Installing a script on QPILE in the Quik terminal: https://youtu.be/0B7qL8Svh7I A section on github that describes the use of the QPILE algorithmic language built into the QUIK system workstation located at the link – https://euvgub.github.io/quik_user_manual/ch8.html. QPILE is an outdated language, but quite simple and accessible even to novice traders. Trading robots and programs that have proven themselves for a long time continue to work on it. However, for more complex tasks it is better to use LUA.