How Scripts Work: Difference between revisions
No edit summary |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 14: | Line 14: | ||
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. | 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. | ||
{| class="wikitable" | |||
!style="width:23%;"|Chart Resolution | |||
!Completed Bar | |||
|- | |||
|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. | |||
|- | |||
|Tick-based charts | |||
|The bar is closed once the defined number of ticks has been reached. | |||
|- | |||
|Range-based charts | |||
|The bar is closed once the tick with a price outside of the original bar's range has been received. | |||
|- | |||
|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. | |||
|- | |||
|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. | An incomplete bar is a summary of all ticks received since the previous bar was completed. | ||
== Maximum | == 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 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 47: | Line 61: | ||
[[Indicator Settings#Advanced properties|Update on Every Tick option]] can be turned off in the MultiCharts settings. | [[Indicator Settings#Advanced properties|Update on Every Tick option]] can be turned off in the MultiCharts settings. | ||
=== How MaxBarsBack | === How MaxBarsBack Is Set for Indicators === | ||
For indicators, the MaxBarsBack value can be detected automatically or set manually. | For indicators, the MaxBarsBack value can be detected automatically or set manually. | ||
Line 69: | Line 83: | ||
<div class="note">Only the last, incomplete, bar is re-evaluated on every new tick, and not the whole series.</div> | <div class="note">Only the last, incomplete, bar is re-evaluated on every new tick, and not the whole series.</div> | ||
=== How MaxBarsBack | === How MaxBarsBack Is Set for Signals === | ||
The MaxBarsBack value for the signals should be set manually. | The MaxBarsBack value for the signals should be set manually. | ||
Line 88: | Line 102: | ||
# Orders generated by <code>Next Bar [At] Market and Next Bar [At] Open</code> statements. | # Orders generated by <code>Next Bar [At] Market and Next Bar [At] Open</code> statements. | ||
# Orders generated by <code>Next Bar [At] Price Limit and Next Bar [At] Price Stop</code> statements. | # Orders generated by <code>Next Bar [At] Price Limit and Next Bar [At] Price Stop</code> statements. | ||
<br> | |||
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. | Higher value indicates greater priority. | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 137: | Line 152: | ||
Any orders left that could not be executed are discarded. | 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. | Multiple orders are generated, in the following order: Short Exit, Short Entry, Long Entry, Long Exit, and Long Entry. |
Latest revision as of 13:58, 26 July 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.
Chart Resolution | Completed Bar |
---|---|
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. |
Tick-based charts | The bar is closed once the defined number of ticks has been reached. |
Range-based charts | The bar is closed once the tick with a price outside of the original bar's range has been received. |
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. |
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.
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.
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:
- Orders generated by
This Bar [On] Close
statements. - Orders generated by
Next Bar [At] Market and Next Bar [At] Open
statements. - 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.
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 |
---|---|---|---|
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 | |
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 |
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 |
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 |
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.