How Scripts Work

PowerLanguage studies can be used with the charts created by the MultiCharts trading platform and with the Portfolio Trader.

PowerLanguage studies operate on price data, organized as a series of data points, based on a defined interval and arranged in a chronological order. Each data point is a summary of a group of price points (ticks) that includes the price values of the first and of the last tick, as well as the range of price movement over the defined interval. Data points also include additional data, such as date and time of the last tick and trade volume.

The most popular format for visually presenting a data point is a bar. References to bars in this guide actually refer to data points. Any other visual formats for presenting data points, such as candlesticks, points, lines, etc., can equally well be substituted.

In this guide, references to charts are used as a visualization tool to aid in understanding how the PowerLanguage scripting works.

PowerLanguage studies are divided into two main types: Indicators and Signals.

An Indicator is a visual technical analysis tool, used to analyze market conditions and identify and forecast trends and market patterns. An indicator is a visualization of a mathematical formula, and consists of one or more Plots – lines, histograms, series of points or crosses, highs and lows, left and right ticks, or a combination of the above, displayed on a chart. Indicators are computed using only price, volume, and (occasionally) open interest data.

A Signal is a mechanical technical analysis tool, used to systematically specify market entry or exit points according to a set of trading rules implemented in the signal's algorithm. The trade points are indicated on a chart by ticks and arrows. Strategies can easily be constructed by combining a number of signals. Market entry or exit points, specified by the signals, can be used to send orders electronically directly to a broker, fully automating the trading process.

Due to the differences in their intended purposes, Indicators and Signals evaluate price data series somewhat differently, and will be discussed separately.


Indicators

The purpose of indicators is to plot visualizations of mathematical formulas on a chart. The plots are created based on one or more price data series.

When applied to a chart, an indicator script first evaluates all the completed bars one-by-one, starting with the very first (oldest) bar on the chart.

The entire script is executed once for each completed bar. On each bar, based on the results of the evaluation, an indicator script can generate output - graph, text, plot color change, audio alert, etc.

If indicator script contains references to previous bars' values, indicator can begin generating output starting with the first bar that follows the Maximum number of bars a study will reference.

Once all the completed bars on the chart have been evaluated, an indicator script will proceed to evaluate the last bar on the chart on tick-by-tick basis, without waiting for the bar to be completed. An incomplete bar is a summary of all ticks received since the previous bar was completed.

Each time a new tick is received, the entire script will be executed for that bar, until the bar is completed and the next bar is started. Indicator scripts treat incomplete bars the same way as the bars that are completed, and can take action each time an incomplete bar is evaluated.

Please note that only the last, incomplete, bar is re-evaluated on every new tick, and not the whole series.

The process of evaluation of a bar by an indicator is called a recalculation or an update.

Indicator update on every tick is enabled by default. However, in some instances it may be undesirable for indicators to be recalculated every time a new tick is received; updating a large number of complex indicators on every tick will require substantial system resources.

Update on Every Tick option can be turned off in the MultiCharts settings.


Completed Bars (Indicators)

A bar is considered completed when it is closed and no additional ticks can be added to it.

  • For time-based charts, the bar is closed once the first tick with a time stamp past the bar's interval is received, or if no additional ticks are received for a period of three seconds.
  • For tick-based charts, the bar is closed once the defined number of ticks has been reached.
  • For range-based charts, the bar is closed once the tick with a price outside of the original bar's range has been received.
  • For volume-based charts, the bar is closed once a tick, bringing the current bar's total to the defined number of contracts, has been received.
  • For change-based charts, the bar is closed once a tick with a price, bringing the current bar's total number of price changes to the defined number, has been received.


The Maximum number of bars a study will reference (Indicators)

A study's script can refer to the values of previous bars for use in evaluation of the current bar.

When a value of a bar N bars ago is referenced in a script, the study can only be visualized starting with the N + 1 bar.

The number of previous bars that must be available for a script in order to start performing calculations is called Maximum number of bars a study will reference, or MaxBarsBack.

The MaxBarsBack value can be selected to be detected automatically or set manually.

When detected automatically, MaxBarsBack will initially be set to the value of the largest data offset in the study; however, if a variable data offset is used in the script, the initial MaxBarsBack value may prove to be too small.

In such a case, the MaxBarsBack value will automatically be increased by 5 or by a factor of 1.618, whichever yields a higher value, and the study recalculated.

The process of automatic MaxBarsBack detection may cause some functions to be executed repeatedly for the first few bars of a chart when a study is first applied; this can be avoided by setting the MaxBarsBack value manually.

To select the smallest MaxBarsBack value sufficient for a particular study, set the value manually. The recommended initial MaxBarsBack value is 50.

Learn more about setting MaxBarsBack values for Indicators


Signals

Signals are the basic building blocks of strategies. Signals are substantially more complex than indicators and take in to account a far greater number of factors.

Signals compile strategy performance data and support backtesting and Automated Trade Execution.

When applied to a chart, a signal script first evaluates all the completed bars one-by-one, starting with the very first (oldest) bar on the chart. The entire script is executed once for each completed bar. On each bar, based on the results of the evaluation, a signal script may generate one or more trading orders. Orders are indicated by an arrow or a mark on the chart, by a visual or an audio alert, etc. If signal script contains references to previous bars' values, signal can begin generating orders staring with the first bar that follows the Maximum number of bars a study will reference.

By default, once all the completed bars on a chart are evaluated, the execution of a signal script is paused until a new bar is completed (a bar is completed once an interval, defined for each bar, is over), and then the entire script is executed again for the new bar.

This signal script execution method can be modified by selecting intra-bar order generation. With intra-bar order generation enabled, the signal script will evaluate the last bar without waiting for the bar to be completed. Each time a new tick is received, the last bar will be re-evaluated, until the bar is completed and the next bar is started. Incomplete bar is a summary of all ticks received since the previous bar was completed. In intra-bar order generation mode, signal scripts treat incomplete bars the same way as the bars that are completed, and can take actions based on evaluation of incomplete bars.

Please note that only the last, incomplete bar is re-evaluated on every new tick, and not the whole series.


Completed Bars (Signals)

A bar is considered completed when it is closed and no additional ticks can be added to it.

  • For time-based charts, the bar is closed once the first tick with a time stamp past the bar's interval is received, or if no additional ticks are received for a period of 300 seconds.
  • For tick-based charts, the bar is closed once the defined number of ticks has been reached.
  • For range-based charts, the bar is closed once the tick with a price outside of the original bar's range has been received.
  • For volume-based charts, the bar is closed once a tick, bringing the current bar's total to the defined number of contracts, has been received.
  • For change-based charts, the bar is closed once a tick with a price, bringing the current bar's total number of price changes to the defined number, has been received.


The Maximum number of bars a study will reference (Signals)

A study's script can refer to the values of previous bars for use in evaluation of the current bar.

When a value of a bar N bars ago is referenced in a script, the study can only be visualized starting with the N + 1 bar.

The number of previous bars that must be available for a script in order to start performing calculations is called Maximum number of bars a study will reference, or MaxBarsBack. The MaxBarsBack value can be selected to be detected automatically or set manually.

When detected automatically, MaxBarsBack will initially be set to the value of the largest data offset in the study; however, if a variable data offset is used in the script, the initial MaxBarsBack value may prove to be too small.

In such a case, the MaxBarsBack value will automatically be increased by 5 or by a factor of 1.618, whichever yields a higher value, and the study recalculated.

The process of automatic MaxBarsBack detection may cause some functions to be executed repeatedly for the first few bars of a chart when a study is first applied; this can be avoided by setting the MaxBarsBack value manually.

To select the smallest MaxBarsBack value sufficient for a particular study, set the value manually. The recommended initial MaxBarsBack value is 50.

Learn more about setting MaxBarsBack values for Signals


Order Execution Priority

A signal can place multiple orders based on evaluation of a single bar. The orders may be generated by the same or different statement types.

The orders are executed in three groups, based on the type of statement that generated the order:

Order group execution sequence:

  1. Orders generated by This Bar [On] Close statements.
  2. Orders generated by Next Bar [At] Market and Next Bar [At] Open statements.
  3. Orders generated by Next Bar [At] Price Limit and Next Bar [At] Price Stop statements.

The orders generated within each order group are sorted by priority based on the current position:

Order Execution Priority Table
Higher value indicates greater priority
Position Flat Long Short
Long Entry 1 1 2
Short Entry 1 2 1
Long Exit - 1 -
Short Exit - - 1

Multiple same-direction orders are assigned priority relative to each other based on the order in which the order statements are listed in the signal script.

When multiple signals are combined to create a strategy, same-direction orders generated by different signals are assigned priority relative to each other based on the order that the signals appear under the Signals tab in the Format Objects window.

Once the orders are sorted, the order that has the highest priority is executed. It is important to note that an entry order in the direction, opposite to the current position, will not simply cover the position, but actually reverse it.

For example, a Long 1 order, executed for a Short 1 position, will result in a Long 1 and not in a Flat position.

Taking in to account the change in position, resulting from execution of the order, the remaining orders are sorted again, and the order that has the highest priority is executed.

This procedure is repeated until all the orders are executed, or until none of the orders left can be executed – for example, only Exit orders are left and the position is flat.

If the only Entry orders left are in the same direction as the current position, these orders will only be executed if Pyramiding is enabled.

Pyramiding settings allow the number of entries in the same direction to be limited to a set number.

Any orders left that could not be executed are discarded.


Order Execution Priority example:

Multiple orders are generated, in the following order: Short Exit, Short Entry, Long Entry, Long Exit, and Long Entry.

The current position is Short.

 1. The orders are sorted by priority (in descending order): Long entry, Long entry, Short exit, Short entry (Long exit could not be executed). SX SE LE LX LE
 2. Long entry is executed; the position is now Long. SX SE  V  LX LE
 3. The orders are sorted by priority (in descending order): Short entry, Long entry, Long exit (Short exit could not be executed).
 4. Short entry is executed; the position is now Short. SX  V   V  LX LE
 5. The orders are sorted by priority (in descending order): Long entry, Short exit (Long exit could not be executed).
 6. Long entry is executed; the position is now Long. SX  V   V  LX  V
 7. The orders are sorted by priority (in descending order): Long exit (Short exit could not be executed).
 8. Long exit is executed; the position is now Flat. SX  V   V   V   V
 9. The only order left, Short exit, can not be executed and is discarded. SX  V   V   V   V

The net result of executing the above orders, from a Short position, will be a Flat position.