+1 888 340 6572

How Scripts Work: Difference between revisions

From MultiCharts
 
(2 intermediate revisions by the same user not shown)
Line 26: Line 26:


The script of a study can refer to the values of previous bars to evaluate the current bar.
The script of a study can refer to the values of previous bars to evaluate the current 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 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.
MaxBarsBack cannot be smaller than 1 and cannot be greater than the actual number of bars on the chart. It must also be equal to or larger than the number of bars the study requires to be calculated.
MaxBarsBack cannot be smaller than 1 and cannot be greater than the actual number of bars on the chart. It must also be equal to or larger than the number of bars the study requires to be calculated.
For example, if a value of a bar N bars ago is referenced in a script, MaxBarsBack should be larger than or equal to N. The study will only be visualized starting with the MaxBarsBack + 1 bar.
For example, if a value of a bar N bars ago is referenced in a script, MaxBarsBack should be larger than or equal to N. The study will only be visualized starting with the MaxBarsBack + 1 bar.
Line 151: Line 151:
!style="width:20%;"|Executed orders
!style="width:20%;"|Executed orders
|-
|-
|1. The orders are sorted by priority (in descending order): Long entry, Long entry, Short exit, Short entry .
|1. The orders are sorted by priority (in descending order): Long entry, Long entry, Short exit, Short entry .<br><br>Long exit cannot be executed.<br><br>Long entry is executed; the position is now Long.
 
Long exit cannot be executed.
 
Long entry is executed; the position is now Long.
|LE, LE, SX, SE
|LE, LE, SX, SE
|LX
|LX
|
|
|-
|-
|2. The orders are sorted by priority (in descending order): Short entry, Long entry, Long exit .
|2. The orders are sorted by priority (in descending order): Short entry, Long entry, Long exit.<br><br>Short exit cannot be executed.<br><br>Short entry is executed; the position is now Short.
 
Short exit cannot be executed.
 
Short entry is executed; the position is now Short.
|SE, LE, LX
|SE, LE, LX
|SE
|SE
|LE
|LE
|-
|-
|3. The orders are sorted by priority (in descending order): Long entry, Short exit.
|3. The orders are sorted by priority (in descending order): Long entry, Short exit.<br><br>Long exit could not be executed.<br><br>Long entry is executed; the position is now Long.
 
Long exit could not be executed.
 
Long entry is executed; the position is now Long.
|LE, SX
|LE, SX
|LX
|LX
|LE, SE
|LE, SE
|-
|-
|4. The orders are sorted by priority (in descending order): Long exit.
|4. The orders are sorted by priority (in descending order): Long exit.<br><br>Short exit could not be executed.<br><br>Long exit is executed; the position is now Flat.
 
Short exit could not be executed.
 
Long exit is executed; the position is now Flat.
|LX
|LX
|SX
|SX
Line 194: Line 178:


=== How Orders Are Sent Depending on the Last Bar Status ===
=== How Orders Are Sent Depending on the Last Bar Status ===
Three collections of orders are filled on each calculation: This Bar Close, Next Bar Market, Next Bar Price.<br>
There are three types of order generation statements:
If the last bar status of the base data series is closed (2 or -1) — This Bar Close orders are sent.<br>
*This Bar Close;
If the last bar status of the base data series is opened (0 or 1) — Next Bar Market, Next Bar Price orders are sent.<br>
*Next Bar Market;
After the batch of orders is sent, it is cleared and will be filled only at the next calculation.<br>
*Next Bar Price.
<div style="background-color: #E5F6FF;">Tip: It is possible to view bar statuses (bar status from the chart, not from the script is meant) with the help of an indicator that updates on every tick..</div>
They are sent depending on the status of the most recent tick in the current bar ([[BarStatus]]).
 
'''Examples'''<br>
 
''Situation 1''<br>
''2 and 2, where the number stands for the bar status from the script.''<br>
Only This Bar Close orders are sent if there are no new bars on the chart by this moment.<br>
If there is the new bar on the chart by this moment, Next Bar Market, Next Bar Price orders will be sent next.<br>
 
''Situation 2''</br>
''2 and 1''<br>
Only This Bar Close orders are sent if there are no new bars on the chart by this moment.<br>
If there is the new bar on the chart by this moment, Next Bar Market, Next Bar Price orders will be sent next.<br>
''1 and 2''<br>
Nothing is sent until the bar of the base data series is closed.<br>


''Situation 3''<br>
If the last bar status of the base data series is closed (BarStatus equals 2 or -1) — This Bar Close orders are sent.
''1 and 2''<br>
Nothing is sent.<br>
''2 and 1''<br>
Only This Bar Close orders are sent if there are no new bars on the chart by this moment.<br>
If there is the new bar on the chart by this moment, Next Bar Market, Next Bar Price orders will be sent next.<br>


''Situation 4''<br>
If the last bar status of the base data series is opened (Bar Status equals 0 or 1) — Next Bar Market, Next Bar Price orders are sent.
''1 and 2''<br>
Nothing is sent.<br>
''2 and -1''<br>
Only This Bar Close orders are sent if there are no new bars on the chart by this moment.<br>
If there is the new bar on the chart by this moment, Next Bar Market, Next Bar Price orders will be sent next.<br>


[[Category:PowerLanguage]]
[[Category:PowerLanguage]]

Latest revision as of 11:48, 26 June 2024

PowerLanguage studies can be used on charts, in Scanner and in 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. In this guide, bars should be understood as data points. Any other visual formats for presenting data points, such as candlesticks, points, lines, etc., can equally well be substituted.

In this guide, charts are referred to as a visualization tool to aid in understanding how the PowerLanguage scripting works. PowerLanguage studies are divided into two types: Indicators and Signals.

Indicators and Signals are intended for different purposes, therefore they evaluate price data series differently and will be discussed separately.

Completed and Incomplete Bars

The concept of completed and incomplete bars is important for understanding how studies evaluate data. 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 timestamp 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.


An incomplete bar is a summary of all ticks received since the previous bar was completed.

Maximum number of bars a study will reference

The script of a study can refer to the values of previous bars to evaluate the current 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. MaxBarsBack cannot be smaller than 1 and cannot be greater than the actual number of bars on the chart. It must also be equal to or larger than the number of bars the study requires to be calculated. For example, if a value of a bar N bars ago is referenced in a script, MaxBarsBack should be larger than or equal to N. The study will only be visualized starting with the MaxBarsBack + 1 bar.

Indicators

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. 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 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, alert, etc.

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.

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. The calculation of the indicator on incomplete bars occurs in the same way as on complete bars. The study can take actions each time an incomplete bar is evaluated.

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 can be undesirable since 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.

How MaxBarsBack is set for indicators

For indicators, the MaxBarsBack value can 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 will be 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. The recommended initial MaxBarsBack value in this case is 50. Then it can be decreased gradually to check if it remains sufficient for a particular study.

Learn more about how to detect MaxBarsBack automatically or set manually.

Signals

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 arrows. Strategies can be constructed by combining a number of signals. Market entry or exit points, specified by the signals, can be used to send orders to a broker automatically.

When applied to a chart, a signal script first evaluates all the completed bars one-by-one, starting with the very first 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. The signal can begin generating orders starting 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, and then the entire script is executed again for the new bar.

This signal script execution method can be modified by enabling 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. In intra-bar order generation mode, the calculation of the signal on incomplete bars occurs in the same way as on complete bars. The study can take actions each time an incomplete bar is evaluated.

Only the last, incomplete, bar is re-evaluated on every new tick, and not the whole series.

How MaxBarsBack is set for signals

The MaxBarsBack value for the signals should be set manually.

To select the smallest MaxBarsBack value sufficient for a particular strategy, set the value manually to the largest data offset across all signals applied to a given chart.

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

Order Flat Long Short
Long Entry 1 1 3
Short Entry 1 3 1
Long Exit - 2 -
Short Exit - - 2

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. The same is relevant for Long Entry and Short Entry orders when the current position is flat.

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. The same is relevant for Long Entry and Short Entry orders when the current position is flat.

Once the orders are sorted, the order that has the highest priority is executed.

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.

After the order is executed, the remaining orders are sorted again taking into account the change of the position. Then the order that received 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.

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

Stage Orders sorted by priority (in descending order) Orders that cannot be executed at the current stage Executed orders
1. The orders are sorted by priority (in descending order): Long entry, Long entry, Short exit, Short entry .

Long exit cannot be executed.

Long entry is executed; the position is now Long.
LE, LE, SX, SE LX
2. The orders are sorted by priority (in descending order): Short entry, Long entry, Long exit.

Short exit cannot be executed.

Short entry is executed; the position is now Short.
SE, LE, LX SE LE
3. The orders are sorted by priority (in descending order): Long entry, Short exit.

Long exit could not be executed.

Long entry is executed; the position is now Long.
LE, SX LX LE, SE
4. The orders are sorted by priority (in descending order): Long exit.

Short exit could not be executed.

Long exit is executed; the position is now Flat.
LX SX LE, SE, LE
5. The only order left, Short exit, can not be executed and is discarded. SX LE, SE, LE, LX

How Orders Are Sent Depending on the Last Bar Status

There are three types of order generation statements:

  • This Bar Close;
  • Next Bar Market;
  • Next Bar Price.

They are sent depending on the status of the most recent tick in the current bar (BarStatus).

If the last bar status of the base data series is closed (BarStatus equals 2 or -1) — This Bar Close orders are sent.

If the last bar status of the base data series is opened (Bar Status equals 0 or 1) — Next Bar Market, Next Bar Price orders are sent.