# Cycles

## Cycle

At the top of the Cycle block there is a service connector for communication with the blocks of opening positions or updated values (simple and updatable cycle value). Thus, the entry into the cycle is indicated (the beginning of the calculation of the cycle). If this connector is not connected to anything, then the first formula (or logical formula) for which the output from the Cycle block is submitted and in which there is an appeal to the Cycle block by its name is considered to be the beginning of the calculation of the cycle.

**Block Parameters**

**Maximum number**- specifies the maximum number of iterations.

**Block Inputs**

**Number (integer)**. Optional input. Any integer that can be used to control the number of iterations on each candle of the trading cycle (but not more than the specified maximum number). If nothing is submitted to the input, then the cycle always has a specified maximum number of iterations.

**Block outputs**

**Number**. The current iteration number is from 0 to N-1.

The end of the cycle is considered to be the blocks:

Add a position in the cycle

Cycle Result

Logical result of the cycle

Cycle Result when displaying the block on the chat

Cycle amount

Cycle And

Cycle Or

All the blocks that are connected between the beginning of the cycle and the end of the cycle participate in the calculation of the results of the cycle. All the blocks that are connected between the beginning of the cycle and the end of the cycle participate in the calculation of the results of the cycle and they can't be tied to another cycle. For convenience, when the "Show block number" setting is enabled in the script properties, all blocks belonging to the cycle have double numbering.

**Pay attention.** The binding to the cycle is interrupted with the "Result(s) of the cycle" blocks. The value of these blocks is then used beyond the cycle. The last received value from the associated block is copied to such a block. The physical meaning is the Result and the Results blocks include all the blocks that are tied to the cycle, in the cycle itself.

## Add position in cycle

A pass on the bar is performed for all iteration cycles. During the passage, the positions that need to be opened are calculated. As many positions is added, as many iterations returned the result true.

**Block parameters**

**Use a Virtual closure**. When the parameter is enabled, IsVirtualClosed in IPosition is checked. This parameter is necessary for cases when the position closing signal is missed, so that there are no new gaps. It says that the position signal was issued by the script, but the real application has not yet passed. For example, auto-closing is expected. If the option is enabled, the Cycle will move to the next free iteration, if it is disabled, there will be no new position openings.

**Block Inputs**

**Position**- mandatory entry. Input data type: Position.**Condition**- mandatory entry. Input data type: Logical value.**Price**- optional entry. Input data type: Number. If the Price is not specified, it takes the price from the main position.**Amount**- optional entry. Input data type: Number. If the quantity is not specified, it takes it from the main position.

**Block outputs**

No outputs

**Physical meaning**

When the block is running, in the program, two cycles are performed on each bar:

The first cycle checks which positions need to be closed and closes them.

In the second cycle new positions are opened, for which there are signals. (on the same bar)

If there is not enough space in the iterations of the cycle (all iterations are occupied by positions), that there is not enough space to open a position.

**How does adding a position work**

When the position closing code is executed, the "Add position in cycle" block may occur. If the condition that comes to the block - true, then the search is performed (by sorting through) for the free number N of the iteration that does not have any active positions on the current candle and the opening of the "Signal:N" position is performed. In this case, the “Condition” at the input to the block is a trigger for creating a position and filling in a new iteration.

## Cycle And

If the Boolean values take the value **true **on all iterations, then, at the output of the block, the value **true **is returned on the bar (on recalculation). If one of the Logical Values takes the value **false**, the value **false **is returned at the output of the block.

**Block Parameters**

Without parameters

**Block Inputs**

Logical value

**Block outputs**

Logical value

## Cycle Or

If in one of the iterations the logical value takes the value **true**, then the block output returns the value **true **on the bar (on recalculation), otherwise the value **false **is returned. At the input, the results of the logical values of the cycle work, for example, a logical formula.

**Block Parameters**

Without parameters

**Block Inputs**

Logical value

**Block outputs**

Logical value

## Cycle logic results

Can be displayed on the chart. The chart will display all the iterations of the cycle. The block results can be passed to another Cycle, with the same number of iterations supported. Nested cycles are not supported.

**Block Parameters**

Without parameters

**Block Inputs**

Logical value. Logical values of the results of the cycle.

**Block outputs**

Logical value. A list of logical values for each iteration.

**Pay Attention!**

While in a cycle, when working with Formula blocks, it must be taken into account that if you write **Value>CycleResult(fromValue1)**, the check will be performed only on the current result. Thus, the **Value1** and the **Value1 **will be compared.
While in a loop, if you write the logical results of the cycle in the Logical Formula block, the Logical Formula will return the value of the last iteration.

## Cycle logic result

Outputs a logical value from the loop to the bar by the iteration number.

**Block Parameters**

**Index**- a logical result based on the iteration number of the cycle**Use last index**- always use the last iteration number of the cycle

**Block Inputs**

Logical value - logical value

**Block outputs**

Logical value. A list of logical values for each iteration

## Cycle Max

Runs through all the iterations of the cycle and finds the maximum value.

**Block Parameters**

Without parameters

**Block Inputs**

Number

**Block outputs**

Number

## Cycle Min

Runs through all iterations of the cycle and finds the minimum value.

**Block Parameters**

Without parameters

**Block Inputs**

Number

**Block outputs**

Number

## Cycle updatable value

The usual updated value in the cycle moves from the old iteration to the value of the new iteration. The updated cycle value works with an array of iteration values. The output is a list of real values. For each iteration of the cycle, its own separate, independently updated value is maintained.

**Block Parameters**

**Initial**- The initial value of the block.**Execution order**- Determines the queue for calculating the Updated value in the algorithm, in the general order (by default), at the end of the algorithm (in the queue of this calculation branch) or after leaving the position.

**Block Inputs**

Number

Logical value

Logical value

**Block outputs**

Number

**Pay Attention**

A handmade indicator with the "Updated cycle value" block starts working if the Result block or Cycle Results block is at the end of the calculations. Other blocks are turned on immediately. You can see the inclusion in the cycle if you enable "Show the number of the cube" in the properties.

## Cycle Result

Returns the value by the iteration number, and outputs the value to the bar.

**Block Parameters**

**Index**- indicates from which iteration it is necessary to copy the real value to output it to the bar.**Use the latest index**

**Block Inputs**

**Number**- a mandatory input. A list of values of the results of the cycle. (For example, a formula, an updated value, any block that is in a cycle).**Number**- not a mandatory input, to which the iteration number is sent, which needs to be copied to the result. If nothing is submitted, the last received value is taken.

**Block outputs**

Number

## Cycle Results

Can be displayed on a chart. It can be passed to another cycle, with the same number of iterations supported. Nested cycles are not supported. A service connector for displaying the parameter to the control panel.

**Block Parameters**

Without parameters

**Block Inputs**

Number. Values of the results of the cycle.

**Block outputs**

An array of the value of each iteration on each bar.

Pay Attention

While in a cycle, when working with Formula blocks, it must be taken into account that if you write **Value**>**CycleResults** (fromValue1), the check will be performed only on the current result. Thus, the Value1 and the Value1 will be compared.

## Cycle Sum

The result is the sum of the values of all iterations of the cycle.

**Block Parameters**

Without parameters

**Block Inputs**

Number. Values of the results of the cycle. (For example, a formula, an updated value, any block that is in a loop)

**Block Outputs**

Number.

Last updated