This Pine script shows how it can be quite simple to personalize a built-in indicator for your needs.
█ OUR SCRIPT
Our objective was to add the current values for volume and its moving average in prominent view, and use brighter colors than the built-in.
We started with the source code from the "Volume" built-in indicator. You can access the source code of many built-ins from the Pine Editor by clicking the "Open" button and choosing "New default built-in script..."
We changed the variable names so they conform to our Coding Conventions. Everybody is of course free to code their scripts the way they want; the conventions provide guidelines for those interested in Pine-specific recommendations. We use our conventions to make our code more readable, which helps readers of open-source publications. As Uncle Bob, a.k.a. Robert Cecil Martin, argues in his "Clean Code" book, code that is easier to read is also useful for its first user: you.
We assigned the colors we use to constants because they are used in multiple places in the script. If we decide to change them, we only need to change the constant definitions for the change to trickle down to the rest of the code.
We used the `inline` and `tooltip` parameters of input() to better organize our inputs and provide extra information under an "i" icon when needed.
We wanted to pack more information in the display of the moving average and volume than just the values, so we color-coded their background:
• When the MA is rising, the background of its table cell is in the bull color, otherwise it's in the bear color. The period used for the MA is also displayed in that cell's legend.
• When the current volume's value is higher/lower than its MA, the background of its cell is of bull/bear color.
We use a Pine table to display our values. We use extra cells to provide a configurable margin to the left, and a small space between the two values.
Because we only use constant colors in this script (i.e., values that are known at compile time), users can change the colors in the "Setting/Style" tab's color widgets. Users of the script can also use the tab to change other attributes of the plots.
Added `minval = 1` to MA length input (thx @midtownsk8rguy).
//@version=4 study("Adapting a built-in [PineCoders]", "Vol", format = format.volume) // ————— We assign our color values to constants so they can be changed here only once, when needed. var color C_BULL = #00FF00 var color C_BEAR = #FF0000 var color C_MA = color.new(color.gray, 40) // ————— Inputs. // We use `inline` to display the MA checkbox and its length on the same line. bool i_showMA = input(true, "MA, ", inline = "1") int i_maLength = input(20, "Length", minval = 1, inline = "1") // We use a `tooltip` to inform users of the allowed range of values, which we control with `minval` and `maxval`. float i_leftMargin = input(2.0, "Left margin for values", minval = 1, maxval = 100, step = 0.5, tooltip = "1-100") bool i_usePrevCl = input(false, "Color columns based on previous close") // ————— Calculations. // Color of the volume columns. color c_columns = i_usePrevCl ? close > close ? C_BEAR : C_BULL : open > close ? C_BEAR : C_BULL // MA of volume calculation. We use `nz()` so that value is zero when the symbol has no volume information. float ma = sma(nz(volume), i_maLength) // Detect when the MA is rising. Will be used to determine the color of the background of the MA value's table cell. bool maRises = rising(ma, 1) // Get daily volume and direction for color. [dailyVol, dailyDir] = security(syminfo.tickerid, "D", [volume, close > open ? 1 : -1]) // ————— Display values using a table. if barstate.islast // Create a 4-column, 1-row table. var table t = table.new(position.top_left, 6, 1) // Left margin. table.cell(t, 0, 0, " ", width = i_leftMargin) // MA cell's bg is bull color when MA is rising. color c_bgMa = maRises ? C_BULL : C_BEAR table.cell(t, 1, 0, "SMA(" + tostring(i_maLength) + tostring(ma, "): ###,####,###"), text_color = color.black, bgcolor = c_bgMa) // Small space between values (note that you can use a float width). table.cell(t, 2, 0, " ", width = 0.5) // Volume cell's bg is bull color when volume is above its MA. color c_bgVol = volume > ma ? C_BULL : C_BEAR table.cell(t, 3, 0, "Vol: " + tostring(nz(volume), "###,####,###"), text_color = color.black, bgcolor = c_bgVol) // Small space between values (note that you can use a float width). table.cell(t, 4, 0, " ", width = 0.5) color c_bgDailyVol = dailyDir > 0 ? C_BULL : C_BEAR table.cell(t, 5, 0, "Daily: " + tostring(nz(dailyVol), "###,####,###"), text_color = color.black, bgcolor = c_bgDailyVol) // ————— Plot columns and MA. plot(volume, "Volume", c_columns, 1, plot.style_columns) plot(i_showMA ? ma : na, "Volume MA", C_MA, 1, plot.style_area)