Files
Superalgos/docs/_includes/data_mine/procedure-loop.md
T

4.2 KiB

{% assign title = "Procedure Loop" %} {% assign definition = site.data.data_mine.procedure_loop %} {% assign preposition = "a" %} {% assign plural = "s" %}

{% if include.more == "yes" and include.heading == "more" %}

Click to learn more about {{ title | downcase }}{{plural}} {% endif %}

{% if include.heading != "" and include.heading != "more" %} {{include.heading}} {{title}} {% endif %}

{% if include.icon != "no" %}

{% if include.table == "yes" and include.icon != "no" %}

{% endif %}

{% if include.table == "yes" and include.icon != "no" %}

{% endif %}

{% endif %}

{% if include.definition == "bold" %} {{ definition }} {% else %} {% if include.definition != "no" %} {{ definition }} {% endif %} {% endif %}

{% if include.table == "yes" and include.icon != "no" %}

{% endif %}

{% if include.more == "yes" and include.content == "more" and include.heading != "more" %}

Click to learn more about {{ title | downcase }}{{plural}} {% endif %}

{% if include.content != "no" %}

This is an example of a procedure loop code snippet, in particular, the code that calculates the Popular SMAs product of the Simple Moving Average (SMA) indicator.

{% include important.html content="Calculation procedures may only access data up to 48 hours in the past counting from the datetime of the record being processed. For that reason, the calculation of a moving average requires temporarily storing data in an array." %}

/* Loop Code. */

let candle = record.current // Our main dependencies are candles 
variable.last200.push(candle.close) // Add the current close value to the last 200 array.

if (variable.last200.length > 200) {
    variable.last200.splice(0, 1) // Remove the first element of the array to keep it at a maximun of 200 elements.
}

variable.sma20 = calculateSMA(20)
variable.sma50 = calculateSMA(50)
variable.sma100 = calculateSMA(100)
variable.sma200 = calculateSMA(200)

function calculateSMA(periods) {  // Having a function saves us from duplicating code.
    /* We check we have enough values to make the calculation */
    if (variable.last200.length < periods) { return 0 } // If we dont, we define the value is zero.

    let sum = 0 // Initialize sum variable. 
    for (let i = variable.last200.length - periods; i < variable.last200.length; i++) { // Iterate through the last periods
        sum = sum + variable.last200[i]
    }
    let sma = sum / periods
    return sma
}

{% endif %}

{% if include.more == "yes" and include.content != "more" and include.heading != "more" %}

Click to learn more about {{ title | downcase }}{{plural}} {% endif %}

{% if include.adding != "" %}

{{include.adding}} Adding {{preposition}} {{title}} Node

To add a procedure loop, select Add Procedure loop on the corresponding procedure node menu. A procedure loop node is created along with the corresponding JavaScript node.

{% endif %}

{% if include.configuring != "" %}

{{include.configuring}} Configuring the {{title}}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

{% endif %}

{% if include.starting != "" %}

{{include.starting}} Starting {{preposition}} {{title}}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

{% endif %}

{% if include.more == "yes" %}

{% endif %}