Let's write our first scripted strategy using QuantScript.
Before we take our first steps in learning the language, we need to open the QuantScript Editor.
Keep in mind, the Editor does not work on mobile, only desktops. Open the web app from a desktop, click on the Plus icon in the top-right corner of the screen, select QuantScript Editor and pick an instrument from the list.
While we can convert any of our existing strategies to QuantScript, for now we are going to start from scratch.
QuantScript was designed to replace a classic strategy's Entry/Exit rules. When we open the editor, we see a panel on the right-hand side of the screen - this is where our strategy's parameters are located. They are exactly the same as those of a classic strategy - the only difference is the Entry/Exit rules are replaced with QuantScript code.
Usually, the first thing one writes when learning a new language is the infamous Hello, World! program. It simply prints the message Hello, World! before exiting.
However... QuantScript cannot exactly print anything, so we will have to adjust and create our own version of Hello, World!
The essence of a Hello, World! program is that the program "comes to life", by "speaking" for the first time.
QuantScript's first words won't be in English. It talks by trading, so let's make it trade all the time.
QuantScript's Hello, World! is a strategy which opens a long position on every bar and subsequently closes it on the next.
This is what it looks like:
We can copy and paste this code in the editor, click on Calculate backtest and watch the balance line plunge deep into the red.
Let's try to understand what happens in the code above.
Line 1 is a comment. Comments start with a hash sign- everything that follows the is completely ignored by QuantScript. This is useful for leaving notes in your code in order to make it more readable, especially if you're planning to shara it with other ProQuant users.
Lines 2 and 3 are what's called a Signal Condition Statement. Every QuantScript strategy ends with one or more of these. They consist of two parts - the signal type and the condition.
There are four possible signal types -, , and .
Following the signal type is thekeyword.
Finally, we have the condition itself. Whenever the condition turns out to be, the strategy performs the action described by the signal type. If the condition is , the strategy will not perform that action.
In the case of our first strategy, both conditions are alwaysso the strategy will open and close long positions every chance it gets.
Ultimately, the goal of a QuantScript strategy is to have meaningful conditions that produce meaningful signals.
Before we get to the "meaningful signals" part, we have to explain exactly when and how many times your QuantScript code is executed when you backtest or run your strategy.