This article will delve into the ta.nvi
function in Pine Script, providing insights into its application, syntax, and practical implementation through a custom function example.
Introduction to ta.nvi
- Type: series float
- Purpose: The NVI is designed to reveal how securities move on days with lower volume, based on the premise that smart money’s actions are most evident during quieter trading periods.
Example Overview
The provided Pine Script code snippet illustrates two approaches to calculating and plotting the Negative Volume Index:
- Using the built-in
ta.nvi
function. - Implementing a custom function,
f_nvi
, to manually calculate the NVI.
//@version=5 indicator("Negative Volume Index") plot(ta.nvi, color=color.yellow)
//@version=5
: This line specifies the version of Pine Script being used, which is version 5 in this case.indicator("Negative Volume Index")
: This line sets the title of the indicator to “Negative Volume Index”. This title will be displayed on the chart.plot(ta.nvi, color=color.yellow)
: This line plots the Negative Volume Index on the chart. Theplot()
function is used to plot data on the chart. In this case, it plots the values ofta.nvi
, which presumably is a variable or a function representing the Negative Volume Index. Thecolor=color.yellow
argument specifies that the plot should be displayed in yellow color.
Custom nvi Function
f_nvi() => float customNvi = 1.0 float prevCustomNvi = (nz(customNvi[1], 0.0) == 0.0) ? 1.0: customNvi[1] if nz(close, 0.0) == 0.0 or nz(close[1], 0.0) == 0.0 customNvi := prevCustomNvi else customNvi := (volume < nz(volume[1], 0.0)) ? prevCustomNvi + ((close - close[1]) / close[1]) * prevCustomNvi : prevCustomNvi result = customNvi plot(f_nvi())
The custom function f_nvi
calculates the NVI based on the original formula, starting with an initial value and adjusting it only on days when the volume decreases compared to the previous session.
Walkthrough of the Custom nvi Function
- Initialization:
customNvi
starts at 1.0, establishing the base value for the NVI calculation. - Previous NVI Value:
prevCustomNvi
holds the previous day’s NVI value, defaulting to 1.0 if not previously set. - Conditional NVI Calculation: The NVI is adjusted only on days with lower volume than the previous day. The formula
((close - close[1]) / close[1]) * prevCustomNvi
calculates the percentage change in price and applies it to the previous NVI value. - Result Assignment: The final NVI value is stored in
result
, ready for plotting.
Key Features and Takeaways
- Function Usability: The
ta.nvi
function provides a straightforward method for incorporating the NVI into your trading strategy without manual calculations. - Syntax and Application: Custom implementations, like
f_nvi
, offer flexibility for adjustments and deeper analysis, demonstrating Pine Script’s versatility. - Practical Implementation: Both methods serve to highlight the importance of volume dynamics in market analysis, with the custom function offering insights into manual calculation and adaptation.
In summary, the Negative Volume Index is a valuable tool in the arsenal of technical analysts, and Pine Script provides both built-in and customizable options for integrating this indicator into trading strategies. Whether using ta.nvi
for convenience or crafting a custom function for nuanced analysis, Pine Script facilitates comprehensive market analysis through its versatile scripting capabilities.