PineCoders

Adapting a built-in [PineCoders]

7932 views
480
█ 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
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
naba_naif PineCoders
@PineCoders, its clear dear just im asking its ready to use or just a example for the coders .
Antworten
LucF naba_naif
@naba_naif, Ah. Sry. Sure you can use it! Base functionality is the same as the built-in, with a few improvements.
+1 Antworten
@LucF, Hi Luc, LonesometheBlue has a great tick volume indicator but the information resets. would it be possible for tradingview to save tick volume data and implement it in the volume indicator so we can see the upticks vs the downticks of historical candles? thank you. oops i just realized you answered my question in one of your indicators. sorry!
Antworten
LucF Deadca7
@Deadca7, ) Perhaps in the future TV devises a way for elapsed rt bar data to be saved on charts, who knows? Could be useful, as scripts like Lonesome's which use realtime calcs become more common.
Antworten
@LucF, Thank you! I just discovered your indicators. they're awesome! game changers for sure. for your Tape indicator would it be possible to add a cumulative volume section? that way we can leave the indicator running and come back to the page and see the total buys vs sells.
Antworten
Super
Antworten