Dynamic Background Color Based on Bollinger Bands in ProBuilder

10 Nov 2020
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to dynamically change the background color of a chart based on the position of the closing price relative to the Bollinger Bands. Bollinger Bands are a popular technical analysis tool used to measure market volatility and identify overbought or oversold conditions.


ONCE Red = 0
ONCE Green = 0
ONCE Blue = 0
ONCE Fade = 0
ONCE BBVal = 20 //20 BB periods
ONCE BBdev = 2.0 //2.0 BB Deviation
BBavg = Average[BBval,0](close) //BB mean (middle line) 0=sma
BollUP = BBavg + ((std[BBval](close)) * BBdev)//Bollinger Upper Band
BollDN = BBavg - ((std[BBval](close)) * BBdev)//Bollinger Lower Band
IF close >= BollUP THEN
    Red = 0
    Green = 255
    Blue = 0
    Fade = 32
ELSIF close <= BollDN THEN
    Red = 255
    Green = 0
    Blue = 0
    Fade = 32
ENDIF
BackgroundColor(Red,Green,Blue,Fade)
RETURN

This code snippet is structured to adjust the chart's background color based on the closing price's relation to the Bollinger Bands:

  • Initialization: The variables for color components (Red, Green, Blue, Fade) and Bollinger Bands parameters (BBVal, BBdev) are set using the ONCE keyword, ensuring they are initialized only once.
  • Calculating Bollinger Bands: The middle line (BBavg) of the Bollinger Bands is calculated as a simple moving average (SMA) of the close prices over a specified period (BBVal). The upper (BollUP) and lower (BollDN) bands are then derived by adding and subtracting the product of the standard deviation of close prices and the deviation multiplier (BBdev) from the middle line.
  • Conditional Coloring: The background color is set to green (RGB: 0, 255, 0) with a fade effect if the closing price is above the upper Bollinger Band, indicating a potential overbought condition. Conversely, it is set to red (RGB: 255, 0, 0) with a fade effect if the closing price is below the lower band, suggesting a potential oversold condition.
  • Applying Background Color: The BackgroundColor function is used to apply the calculated color values to the chart's background.

This example is useful for visualizing the relationship between price movements and volatility, aiding in the identification of potential market turning points directly on the chart.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/backgroundcolor-bollinger/#post-84220

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...