Implementing Conditional Background Colors Based on Trading Hours

01 Apr 2019
0 comment
0 attachment

This code snippet demonstrates how to change the background color of a chart based on specific trading hours using the ProBuilder language. The example sets a different background color during a defined time range to visually indicate trading hours on a price chart.

DEFPARAM CalculateOnLastBars = 500
r = 255
g = 255
b = 255
IF opentime >= 153000 AND opentime <= 221500 THEN
    g = 0
    b = 0
ENDIF
BACKGROUNDCOLOR(r,g,b,16)
RETURN

Explanation of the Code:

  • DEFPARAM CalculateOnLastBars = 500: This line sets the indicator to calculate based on the last 500 bars only, which optimizes performance by limiting the amount of data processed.
  • Initial Color Setting: The variables r, g, and b are set to 255, initializing the background color to white (RGB value of 255, 255, 255).
  • Conditional Color Change: The IF statement checks if the current bar's opening time is between 15:30:00 (153000) and 22:15:00 (221500). If true, it changes the green (g) and blue (b) components of the RGB color to 0, turning the background color to red during these hours.
  • Applying the Background Color: The BACKGROUNDCOLOR function is used to apply the RGB color defined by the variables r, g, and b, with an opacity level of 16 (on a scale from 0 to 100).
  • RETURN: Ends the execution of the code and outputs the result, which in this case, is the application of the background color.

This snippet is useful for traders who want to visually distinguish specific trading hours directly on their charts, enhancing readability and decision-making based on time-specific strategies.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/differenti-time-zone-su-diversi-simboli-dello-stesso-mercato/#post-85506

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.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
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...