Drawing Horizontal Lines at Fixed Intervals Using a WHILE Loop

28 Jan 2021
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to use a WHILE loop to draw horizontal lines on a chart at fixed intervals of 100 points. The example is particularly useful for visualizing significant levels or grid lines on financial charts.

IF NOT DID THEN 
    NV = 0 
    while NV <= 30000 DO 
        drawhline(NV) coloured("cyan") 
        NV = NV + 100 
    wend 
    ONCE DID = 1 
ENDIF 
RETURN

The code snippet provided performs the following operations:

  • Conditional Check: The IF NOT DID THEN condition checks if the variable DID is false (or not yet set). This ensures that the lines are only drawn once.
  • Initialization: The variable NV is initialized to 0. This variable will be used to set the y-value of the horizontal lines.
  • WHILE Loop: The loop continues as long as NV is less than or equal to 30000. Inside the loop, a horizontal line is drawn at the current value of NV using drawhline(NV) coloured("cyan"). The color cyan is specified for the line.
  • Increment: Inside the loop, NV is incremented by 100 after each iteration. This increment sets the next level at which a horizontal line will be drawn.
  • Loop Termination: Once NV exceeds 30000, the loop terminates.
  • Flag Setting: After exiting the loop, DID is set to 1 by ONCE DID = 1. This prevents the loop from executing again on subsequent calculations or redraws of the chart.
  • End of Conditional and Function: The ENDIF and RETURN statements mark the end of the conditional block and the function, respectively.

This example is a practical demonstration of controlling flow with loops and conditionals in ProBuilder, useful for tasks requiring repeated actions until a certain condition is met.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/probleme-niveau-100/#post-195634

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Nicolas Legend
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
135
strategy
171

Recent Snippets

A multi indicator Dashboard for ProRealTime
indicator
This snippet creates a compact on-chart dashboard that aggregates multiple indicator states into a single, readable [...]
How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
Logo Logo
Loading...