This article will delve into the ta.cog()
function, exploring its syntax, arguments, and practical application with a focus on how it leverages statistical concepts and the Fibonacci golden ratio to calculate the center of gravity (COG) of price series.
Syntax
The ta.cog()
function is structured as follows:
ta.cog(source, length) → series float
Arguments
- source (series int/float): This is the series of values that the function processes. It can be any series of prices, such as open, high, low, or close prices.
- length (series int): This specifies the number of bars to include in the calculation, effectively determining the lookback period for the COG calculation.
Example
To illustrate the ta.cog()
function in action, let’s look at a simple example:
//@version=5 indicator("Custom COG Example", overlay=true) plot(ta.cog(close, 10))
And a custom implementation to achieve a similar result:
//@version=5 indicator("ta.cog", overlay=true) plot(ta.cog(close, 10)) // the same on pine pine_cog(source, length) => sum = math.sum(source, length) num = 0.0 for i = 0 to length - 1 price = source[i] num := num + price * (i + 1) -num / sum plot(pine_cog(close, 10))
Both scripts aim to plot the Center of Gravity (COG) based on the closing prices over the last 10 bars. The custom function, pine_customCOG
, mirrors the built-in ta.cog()
functionality, providing a hands-on example of how the COG is computed.
Understanding the Code
- Indicator Declaration (
indicator
)://@version=5
specifies that the script is written in version 5 of Pine Script, ensuring compatibility with TradingView’s scripting environment.indicator("ta.cog", overlay=true)
declares a new indicator named “ta.cog” and setsoverlay=true
to display the indicator directly on the price chart. This is crucial for indicators like COG, which are meant to be analyzed in the context of price action.
- Built-in COG Calculation and Plotting:
plot(ta.cog(close, 10))
calls the built-inta.cog()
function with two arguments:close
, which represents the closing prices of the bars, and10
, indicating the number of bars over which the COG is calculated. It then plots this value on the chart. This line succinctly demonstrates how to calculate and visualize the COG using Pine Script’s built-in functions.
- Custom COG Function Declaration (
pine_cog
):pine_cog(source, length) =>
begins the definition of a custom function namedpine_cog
, which mimics the behavior of the built-inta.cog()
function. This function takes two parameters:source
, a series of price values, andlength
, the number of bars to consider for the calculation.
- Summation of Source Values:
sum = math.sum(source, length)
calculates the sum of thesource
values over the specifiedlength
. This sum is used as the denominator in the COG calculation, representing the total weight of the prices.
- Loop for Weighted Sum Calculation:
- The loop
for i = 0 to length - 1
iterates through each bar in the specified period. For each iteration, it calculates a weighted value of the price at that bar (price = source[i]
) multiplied by its index (adjusted byi + 1
to start counting from 1 instead of 0), which is then accumulated in the variablenum
.
- The loop
- COG Calculation:
-num / sum
computes the final COG value by dividing the accumulated weighted sum (num
) by the total sum of the prices (sum
). The negative sign adjusts the direction of the COG value based on the calculation logic.
- Plotting the Custom COG:
plot(pine_cog(close, 10))
uses theplot()
function to visualize the COG calculated by the custompine_cog
function on the chart, similar to how the built-in COG was plotted. This line demonstrates how to apply and visualize custom analytical calculations in Pine Script.
Key Features and Takeaways
- Utility: The
ta.cog()
function provides a statistical measure of the balance point of prices over a specified period, offering insights into potential market turning points. - Syntax and Application: Its syntax requires a source series and a period length, making it versatile for various trading setups and strategies.
- Custom Implementation: Understanding the underlying calculation through a custom function like
pine_customCOG
enhances the ability to tweak or extend the COG calculation for specific analytical needs. - Visualization: Plotting the COG on price charts can aid in identifying trends and potential reversals, especially when combined with other indicators.