Description of the indicator.
The indicator «Bollinger Bands Color» is an enhancement exclusively created for the «Bollinger Bands» indicator of Thinkorswim®, based on its counterpart for TradingView®, whose code is based on the classic Bollinger Bands® indicator. Our sole objective is to add color to the space between the bands and the moving average line to improve its visual appearance and facilitate reading..
Of course, it can be customized so that the user can choose the colors, both of the bands and the median, as well as the space between these elements. Additionally, automatic alerts have been added, which the user can activate or deactivate in the chart study settings, with the aim of receiving notifications whenever the price touches the upper or lower bands.
Explanation of the code.
Block 5-10: We define and assign default inputs for the initial variables: price type, length of price displacement, moving average period, lower and upper standard deviation with respect to the moving average, and the choice of the type of moving average, respectively.
Block 12-16: We establish the mathematical structure of the Bollinger Bands. First, we define the function for the standard deviation of the prices over the previous 20 periods with the given displacement. Second, we plot the middle line of the bands based on the Simple Moving Average (SMA), which will serve as the basis for plotting the bands themselves. Third, we send the parameters to the standard deviation function and calculate the bands based on the result of the function and the middle line.
Block 18-27: We define the colors (in RGB format) and the style that the mean line and the two bands will have. We define the color that the space between the bands and the mean line will have. Finally, we pass the parameters to the pre-built function AddCloud so that it logically fills that space.
Block 29-30: We declare the alerts and define their conditionals so that the user can receive notifications whenever the conditions of the code are met, in this case, whenever the price crosses the upper or lower band.