In this tutorial, we delve into the utilization of the `ta.wma()`

function within Pine Script, an essential tool for traders and analysts using the TradingView platform. The `ta.wma()`

function computes the weighted moving average (WMA) of a given data series over a specified number of periods, with an emphasis on recent data points by assigning them greater weights. This method contrasts with simple moving averages (SMA) by providing a more responsive measure to recent price changes.

## Introduction to ta.wma()

#### Syntax

ta.wma(source, length) → series float

### Arguments

`source`

(series int/float): The series of values to process. This can be any series of numbers, such as the closing price of a stock.`length`

(series int): The number of bars, or periods, over which to calculate the WMA.

### Example

//@version=5 indicator("Custom WMA Example") plot(ta.wma(close, 15), "Weighted Moving Average") // Alternative implementation using Pine Script, less efficient but educational customWma(series, periodLength) => normalizationFactor = 0.0 weightedSum = 0.0 for index = 0 to periodLength - 1 weight = (periodLength - index) * periodLength normalizationFactor := normalizationFactor + weight weightedSum := weightedSum + series[index] * weight weightedSum / normalizationFactor plot(customWma(close, 15), "Custom WMA")

### Deep Dive into the Example

**Indicator Declaration**:`//@version=5`

: Specifies the version of Pine Script used, which is version 5 in this case. This line is essential for compatibility and feature support.`indicator("Custom WMA Example")`

: Declares a new indicator with the name “Custom WMA Example”. This name appears on the chart where the indicator is applied.

**Built-in WMA Calculation and Plotting**:`plot(ta.wma(close, 15), "Weighted Moving Average")`

: This line does two things:- It calculates the Weighted Moving Average of the
`close`

price over the past 15 bars using Pine Script’s built-in`ta.wma()`

function. - It plots this calculated WMA on the chart with the label “Weighted Moving Average”.

- It calculates the Weighted Moving Average of the

**Custom WMA Function Definition**:`customWma(series, periodLength) =>`

: Defines a custom function named`customWma`

that takes two parameters:`series`

: A series of values (e.g., closing prices) to calculate the WMA on.`periodLength`

: The number of bars to look back for the calculation.

- Inside this function, two variables are initialized to zero:
`normalizationFactor`

and`weightedSum`

. These variables are used to calculate the normalization factor and the weighted sum of the series values, respectively.

**For Loop for WMA Calculation**:- The for loop
`for index = 0 to periodLength - 1`

iterates over each bar in the specified period length, calculating the weight for each bar and accumulating the weighted sum and normalization factor. `weight = (periodLength - index) * periodLength`

: Calculates the weight of each bar. The weight decreases in arithmetic progression as the`index`

increases.`normalizationFactor := normalizationFactor + weight`

: Accumulates the total weight to normalize the weighted sum.`weightedSum := weightedSum + series[index] * weight`

: Calculates the weighted sum of the series values.

- The for loop
**WMA Calculation and Return**:`weightedSum / normalizationFactor`

: After the loop, the function calculates the final WMA by dividing the weighted sum by the normalization factor. This result is the output of the`customWma`

function.

**Plotting the Custom WMA**:`plot(customWma(close, 15), "Custom WMA")`

: This line plots the WMA calculated by the`customWma`

function on the chart with the label “Custom WMA”. It uses the same`close`

price series and period length as the built-in function for comparison.

## Key Features and Takeaways

**Function Usability**:`ta.wma()`

provides a simple and efficient way to include weighted moving averages in your trading strategies or analytical models on TradingView.**Syntax and Application**: The function syntax is straightforward, requiring only the data series and the length of the period for calculation.**Manual Calculation Insight**: The manual implementation offers insight into the underlying calculation of WMA, which can be educational for users seeking to understand the mechanics of weighted averages in financial analysis.