PineCoders

Adapting a built-in [PineCoders]

9068 views
520
█ 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.

Versionshinweise: v2
Added `minval = 1` to MA length input (thx @midtownsk8rguy).
Open-source Skript

Ganz im Sinne von TradingView hat der Autor dieses Skripts es als Open-Source veröffentlicht, damit Trader es verstehen und überprüfen können. Ein Hoch auf den Autor! Sie können es kostenlos verwenden, aber die Wiederverwendung dieses Codes in einer Publikation unterliegt den Hausregeln. Sie können das Skript den Favoriten hinzufügen, um es auf dem Chart zu verwenden.

Möchten Sie dieses Skript auf einem Chart verwenden?
Tools and ideas for all Pine coders: http://www.pinecoders.com
Our Pine FAQ & Code: http://www.pinecoders.com/faq_and_code/
Pine news broadcasts: https://t.me/PineCodersSquawkBox or https://twitter.com/PineCoders

Kommentare

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!
+4 Antworten
PineCoders Deadca7
@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)
Antworten
teşekkürler
Antworten
Great work, just to ask,can a display allow for indicators like MA with continues changing inputs . Thanks
Antworten
@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".
Antworten
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
Antworten
@gs1901, Hi, We're happy our publications are helping you. There's is no automated way of extracting the list of symbols making up indices, so that would have to be done manually in your code and would be limited to 40 symbols, as that's the maximum quantity of `security()` calls one script can use. We have plans for a Pine screener that would allow us to run a script on a selection of symbols, but there's not ETA on that feature for now.
Antworten
how i can use it ?pls
Antworten
PineCoders naba_naif
@naba_naif, Look above the script's code, in the banner ▲. Above the big blue "Add to favorite indicators" button, you will see "Want to use this script on a chart?" with a question mark to the right of the text. Click on it to show instructions, and let us know if you have problems.
+3 Antworten