Home » Pinescript Syntax » Understanding Real-Time Calculations in Pine Script

Understanding Real-Time Calculations in Pine Script

Photo of author
Published on

This article dives into the intricacies of real-time bar calculations in Pine Script, highlighting the significant differences, and challenges, and providing a practical example to solidify the concept.

Understanding Real-Time Bars in Pine Script

Historical vs. Real-Time Execution

Scripts in Pine Script execute once per bar on historical data. However, in a real-time setting—specifically, on the rightmost bar of the chart, also known as the real-time bar—the script executes multiple times, responding to each update as price action unfolds. This difference is pivotal for scripts intended to guide trading decisions in real time.

Strategies and calc_on_every_tick

By default, strategies in Pine Script are executed at the close of the real-time bar. However, setting the calc_on_every_tick parameter to true within the strategy’s declaration changes this behavior, causing the strategy to execute with each tick, akin to how indicators operate. This setting is essential for strategies that need to react instantly to price movements.

Impact of Real-Time Updates on Script Variables

During the real-time bar, built-in variables such as high, low, and close are subject to change with each tick. Unlike historical bars, where these variables remain constant, their fluctuation in real time affects the script’s calculations, leading to different outcomes with each execution. This dynamic calculation ensures the script remains aligned with the latest market conditions.

Real-Time Bar Execution Process

Initial Execution and Rollback

When a script enters the real-time bar, it performs its first set of calculations using the current values of built-in variables. Before the next update, the script undergoes a “rollback,” resetting user-defined variables to their last committed state. This reset ensures that calculations for each update start from a consistent state, providing a clean slate for accurate real-time analysis.

Example: Tracking Price Crosses

Consider a script designed to track crosses above a specific price threshold, plotting a marker whenever such a cross occurs. In real time, the script’s calculations are continuously updated. If a price cross is detected, a marker is plotted. However, if subsequent price action negates the cross, the script recalculates, and the marker may disappear, reflecting the most current analysis.

Final Execution and Commit

As the real-time bar closes, the script executes one final time, with variables undergoing one last rollback before being committed to their final values for the bar. This ensures that the script’s outputs reflect the concluded state of the real-time bar, ready for the next bar’s analysis.

Practical Example: Implementing Real-Time Calculation

Let’s apply these concepts through a practical example in Pine Script. Our goal is to create a simple script that highlights when the closing price crosses above a dynamic threshold—here, a simple moving average (SMA) of the last 20 bars.

//@version=5
indicator("Real-Time Cross Example", overlay=true)
smaPeriod = input(20, title="SMA Period")
priceThreshold = ta.sma(close, smaPeriod)

// Detecting the cross
priceCross = ta.crossover(close, priceThreshold)

// Plotting the marker on a real-time cross
if (priceCross)
    label.new(bar_index, close, "↑", color=color.red)

Example

Walkthrough:

  • Indicator Declaration: We start by declaring an indicator that will overlay on the main chart.
  • Input and SMA Calculation: The user can specify the period for the SMA, which we then apply to the closing prices.
  • Cross Detection: Utilizing the ta.crossover function, we check if the closing price crosses above the SMA.
  • Marker Plotting: Upon detecting a cross, a marker is plotted at the current bar and price.

Key Features and Takeaways

  • Real-Time vs. Historical Execution: Pine Script distinguishes between real-time and historical execution, with real-time bars allowing for continuous updates.
  • Rollback Mechanism: Ensures scripts start from a consistent state with each update in the real-time bar, crucial for accurate real-time analysis.
  • calc_on_every_tick: This strategy setting is vital for scripts that require execution on every price update in real-time.
  • Dynamic Analysis: Scripts can adapt to real-time market conditions, offering up-to-date insights with each tick.

Leave a Comment