Drawing Horizontal Lines at Rounded Prices

27 Nov 2021
0 comment
0 attachment

This ProBuilder script is designed to draw horizontal lines on a trading chart at prices that are rounded to the nearest cent from the last closed price. It also demonstrates the use of loops and conditional drawing in ProBuilder.

defparam drawonlastbaronly=true
start = round(close[1]*100)/100 //start price
step = 0.01 //price step
quantity = 2 //lines quantity
i = start
while i < start+(step*quantity) do
    drawhline(i) coloured(0,0,0,90)
    i=i+step
wend
while i > start-(step*quantity) do
    drawhline(i) coloured(0,0,0,90)
    i=i-step
wend
return start

Explanation of the Code:

  • defparam drawonlastbaronly=true: This line ensures that the drawing commands are executed only on the last bar of the chart, which helps in reducing clutter and improving performance.
  • start = round(close[1]*100)/100: This calculates the starting price for the horizontal lines. It rounds the previous bar’s closing price to the nearest cent.
  • step = 0.01: This sets the price increment for each line, which is one cent in this case.
  • quantity = 2: This defines the number of lines to be drawn above and below the starting price.
  • First while loop: This loop draws lines incrementally above the starting price. It continues until it has drawn the specified number of lines (defined by ‘quantity’).
  • Second while loop: Similar to the first, but this loop draws lines below the starting price.
  • drawhline(i) coloured(0,0,0,90): This function draws a horizontal line at price ‘i’. The ‘coloured’ function is used to set the color and transparency of the line (here, a semi-transparent black).
  • return start: This line outputs the starting price to the screen, which can be useful for debugging or information purposes.

This script is useful for traders who want to visualize key price levels that are neatly rounded, aiding in quick visual assessment of price proximity to these levels on historical data.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/dessiner-segments-sur-niveau-de-prix-rond/#post-103469

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.
Etops Junior
This author is like an anonymous function, present but not directly identifiable. More details on this code architect as soon as they exit 'incognito' mode.
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

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 [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...