How MultiCharts Executes Strategy#
PowerLanguage and Scripts#
A strategy is a set of instructions written in the programming language called PowerLanguage. You need to use the PowerLanguage Editor to write PowerLanguage instructions. The completed PowerLanguage instructions are called a script. A script must be compiled before it can be executed (press F3 in the PowerLanguage Editor to compile). The compilation confirms whether the syntax is correct and translates the script into a MultiCharts-executable format. After successful compilation, the script can be selected and executed in MultiCharts.
Script Types#
In the PowerLanguage Editor, there are three types of scripts: Function, Indicator and Signal. Open PowerLanguage Editor -> File -> Open a new file to select the type of script to be written.
| Script Type | Purpose | Executable Instructions |
|---|---|---|
| Function | Encapsulates reusable logic, see Function for details | - |
| Indicator | Draws lines on the chart (Ex: Plot), such as drawing a 5-period moving average | Drawing instructions |
| Signal | Executes trades (Ex: Buy, Sell, SellShort, BuyToCover) | Buy and sell instructions |
The main differences between indicator scripts and signal scripts:
- Indicator scripts: Can execute drawing instructions, but cannot execute buy and sell instructions. You can execute the indicator script only, observe the changes of lines on the chart, and then manually place an order to buy or sell.
- Signal scripts: Can execute buy and sell instructions, but cannot execute drawing instructions.
Script Execution Mechanism#
When trading manually, if you use a strategy of buying on the golden cross of 2 moving averages, you will keep staring at the chart that is constantly receiving market data, watching the bars form one by one, and then “constantly” checking whether the 2 moving averages have a golden cross to decide whether to buy. Maybe not consciously, but as market data continues to arrive, you are constantly repeating the logic of the strategy.
MultiCharts executes strategies with the same concept: as market data is continuously received, MultiCharts continuously re-executes the script. However, “continuously” does not mean that the next execution starts immediately after the previous one finishes. MultiCharts defines execution rules to determine when to re-execute the script.
The execution rules for indicator scripts and signal scripts are different. The respective rules are as follows.
Indicator Execution Rules#
- Historical Bars Phase (Bar by Bar): For past completed bars, the script is executed at the end of each bar sequentially.
- Real-time Phase (Tick by Tick): Once the script has been executed for all completed bars, it enters the real-time phase. Before the current bar is completed, the script is re-executed every time a tick is received.
Signal Execution Rules#
- Historical Bars Phase (Bar by Bar): For past completed bars, the script is executed at the end of each bar sequentially. (Same as indicators)
- Real-time Phase (Bar on Close): Once the script has been executed for all completed bars, it enters the real-time phase. Before the current bar is completed, the script will NOT be re-executed. It waits until the current bar is completed before re-executing.
IOG (Intra-bar Order Generation)#
Signal scripts can enable the IOG property to change the real-time phase behavior, making it the same as indicators: before the current bar is completed, the script is re-executed every time a tick is received.
Note: Even with IOG enabled, the historical bars phase remains Bar by Bar and will not become Tick by Tick.
Indicator vs Signal Execution Rules Comparison#
| Execution Phase | Indicator | Signal | Signal (IOG Enabled) |
|---|---|---|---|
| Historical Bars | Bar by Bar | Bar by Bar | Bar by Bar |
| Real-time (Current Bar Incomplete) | Tick by Tick | Not Executed | Tick by Tick |
| Real-time (Current Bar Completed) | Execute | Execute | Execute |
Completed Bars#
In the execution rules above, “completed bars” is a key concept. Taking time-based charts (Ex: minute charts) as an example, a bar is considered completed when either of the following conditions is met:
- The next tick received has a time stamp exceeding the time interval defined by the bar
- No additional ticks are received for a certain period of time
The timeout duration in condition 2 differs between indicators and signals:
| Script Type | Timeout Duration |
|---|---|
| Indicator | 3 seconds |
| Signal | 300 seconds |
Reference#
https://www.multicharts.com/trading-software/index.php/How_Scripts_Work