TradingView
PineCoders
1 de Jun de 2021 20:01

Adapting a built-in [PineCoders] 

Bitcoin / U.S. dollarBitstamp

Descrição

█ OVERVIEW

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.



Look first. Then leap.

Notas de Lançamento

v2
Added `minval = 1` to MA length input (thx @midtownsk8rguy).

Notas de Lançamento

v3

Updated Code
The code has been updated to use Pine Script™ v5. It follows the most recent recommendations from the Pine Script™ User Manual's Style Guide.
Comentários
Deadca7
Great to use with the tape indicator. A small quality of life suggestion would be to allow an option to see the total volume traded for the day so we wont have to go to the daily timeframe or open watchlist and enable volume to see it. Or maybe someone needs to tell me to stop being so lazy :3 Great work as always!
PineCoders
@Deadca7, This includes the current daily volume:
//@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[1] > 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)
hksoros
Hi, i dont really understand this line of code
color c_columns = i_usePrevCl ? close[1] > close ? C_BEAR : C_BULL : open > close ? C_BEAR : C_BULL
how should i read this? thank you
PineCoders
@hksoros, Hi! You can read it like this:
when i_usePrevCl is enabled: when previous close > close -> color c_columns is C_BEAR, otherwise color c_columns is C_BULL when i_usePrevCl is NOT enabled: when open > close -> color c_columns is C_BEAR, otherwise color c_columns is C_BULL

Hope this helps!
sambash
Thanks for the indicator. How can I add volume "percentage" from the selected Moving Average?
PatrickJaneee
teşekkürler
MT2200
Great work, just to ask,can a display allow for indicators like MA with continues changing inputs . Thanks
PineCoders
@Emma1, Hi, The values displayed in the table will reflect changes in calculations, but they always display the values from the chart's last bar. If you want values from any bar, as your cursor moves on them, you can turn on the display of bar values by right-clicking on the indicator's name and selecting "Show indicator values".
gs1901
Hi Team

I need a help from you

I am building a breadth indicator, for which i required to go through all the stocks in the indices

Would it be possible for you to create a code which can get the details for all the stocks in the indices, based on which indices is selected

Thanks in advance

I am learning a lot of coding bcoz of you ....so thanks for teaching me
Mais