ProQuant just recently announced QuantScript, a programming language you can use in ProQuant to build custom trading bots for Crypto and CFDs on Trading 212. This language is pretty new but already it’s pretty interesting. It’s quite easy to use and you can learn to use it pretty fast. So I created a small tutorial to show you what is possible in QuantScript and how you can quickly start using it.
To create a new strategy in QuantScript you have to click the plus sign in the right top corner of the screen:
Then choose QuantScript studio in the popup and then select an instrument you want to apply you strategy to:
It’s quite easy to start developing strategies in QuantScript. The minimal strategy can consist of fo just 2 lines of code:
enter long when CrossesUpwards(close, SMA(close, 100)) exit long when CrossesDownwards(close, SMA(close, 100))
If you want to create a short strategy you just need to do that same but replace the long keyword with short:
enter short when CrossesDownwards(close, SMA(close, 100)) exit short when CrossesUpwards(close, SMA(close, 100))
As you can see it’s quite easy to execute orders in QuantScript. The syntax for it is: enter/exit long/short when condition. Where condition should be boolean true or false variable.
After you wrote your first strategy you can click “Calculate Backtest” in the right sidebar:
This will run your strategy and you can check it’s performance. As a result of the backtesting you will see an equity plot with some basic statistics:
More detailed data you can find in a “Backtesting” tab below:
Nice things is that in QuantScript you can adjust your strategy SL, PT, and some other parameters without changing anything in the code. You can check available options in the Parameters tab in the right sidebar:
Now let’s look at the essential features of QuantScript. Here is a list of main build-in variables you can use in your strategies.
open # open of the current bar high # high of the current bar low # low of the current bar close # close of the current bar volume # volume of the current bar median # (high + low) / 2 typical # (high + low + close) / 3 weighted # (high + low + 2 * close) / 4 open # open of the previous bar open # open 100 bars ago open # open 499 bars ago open # unknown 5 + unknown # unknown isknown 5 # true isknown unknown # false
As you can see there is a historical operator so you can travel to the past for all variables and get values from previous bars. There is a limit of 500 available bars so every query with > 500 bars will output you and “unknown” value. You can use “isknown” operator to check if a variable is unknown or not.
Time variables are implemented quote interestingly in QuantScript, here are some examples of use:
day # Day of the week day == Monday time # current time time >= 09:00 time <= 16:30 time >= 09:00 and time <= 16:30 day == Tuesday and time >= 09:00 and time <= 16:30
So basically for now there are only 2 date/time variables available.
- day – for the day of the weel. You can compare it with “Monday” for example to check if the day of the week is Monday for the current bar.
- time – storing current bars time. You can work it with time in the format you used to, for example, time >= 09:00 will work fine in QuantScript.
As in all other programming languages in QuantScript you can use custom variables, for loops and if/else operator. Here is an example:
# creating a variables ohlc = (open + high + low + close )/ 4 x = SMA(ohlc) # if/else operator price = open if day == Monday else close # For loop volume50 = 0 for i from 0 to 49 volume50 = volume50 + volume[i]
Also, you can create your own functions:
# User defined functions function volume_sum(length): volume_sum = 0 for i from 0 to length volume_sum = volume_sum + volume[i] volume_sum volume50 = volume_sum(50)
QuantScript has a very useful type of variable – “Series”. It allows you to create complicated calculations on historical bars much faster and more efficient than using loops.
# Green Bars Number series green_bars_num: once: 0 rest: green_bars_num + (1 if close >= open else 0) # Red Bars Number series red_bars_num: once: 0 rest: red_bars_num + (1 if close < open else 0) all_bars = green_bars_num + red_bars_num
In QuantScript you can also get data from multiple instruments and/or multiple timeframes inside one strategy. Here are a few examples of how you can do that:
# getting data from another timeframe 1h open_1h = open 1d close_1d = close # getting data for another stock Tesla open_tesla = open Apple open_apple = open # getting data for another stock for another timeframe Tesla:1d open_tesl_1d = open Gold gold_sma = SMA(close, 14)