In this article, we’ll delve into the timeframe.isdwm
function in Pine Script Version 5, breaking down its use and application in creating more dynamic and responsive trading scripts.
Introduction to timeframe.isdwm
The timeframe.isdwm
function is a built-in boolean function in Pine Script that checks if the current chart timeframe is a daily, weekly, or monthly timeframe. It plays a crucial role in scripts where the behavior needs to be adjusted based on the chart’s timeframe.
Syntax of timeframe.isdwm
isDWM = timeframe.isdwm
In this code, isDWM
is a boolean variable that holds the result of timeframe.isdwm()
. It returns true
if the current timeframe is either daily (D), weekly (W), or monthly (M), and false
otherwise.
Example Use Case
Let’s create an example to illustrate the use of timeframe.isdwm
. Suppose we want to plot a simple moving average (SMA) but only on daily, weekly, or monthly timeframes.
Example
//@version=5 indicator("SMA on DWM Timeframes", overlay=true) length = input(14, title="SMA Length") price = close isDWMTimeframe = timeframe.isdwm() smaOnDWM = isDWMTimeframe ? ta.sma(price, length) : na plot(isDWMTimeframe ? smaOnDWM : na, title="SMA on DWM", color=color.blue)
Line-by-Line Explanation
- Declaration of Version and Indicator:
//@version=5 indicator("SMA on DWM Timeframes", overlay=true)
– This line sets the script version to 5 and declares a new indicator named “SMA on DWM Timeframes” that overlays on the price chart. - Input for SMA Length:
length = input(14, title="SMA Length")
– This line allows the user to input the length of the SMA, with a default value of 14. - Price Definition:
price = close
– Here, we’re assigning the closing price of the bars to the variableprice
. - Timeframe Check:
isDWMTimeframe = timeframe.isdwm()
– This line checks if the current chart’s timeframe is daily, weekly, or monthly, and stores the result inisDWMTimeframe
. - SMA Calculation:
smaOnDWM = isDWMTimeframe ? ta.sma(price, length) : na
– This ternary operation calculates the SMA ifisDWMTimeframe
is true; otherwise, it assignsna
(not available). - Plotting the SMA:
plot(isDWMTimeframe ? smaOnDWM : na, title="SMA on DWM", color=color.blue)
– This line plots the SMA on the chart but only for DWM timeframes.
Key Features and Takeaways
- Function Utility:
timeframe.isdwm
is extremely useful for scripts that need different behaviors on different timeframes, especially when focusing on higher timeframes like D, W, and M. - Syntax: The function returns a boolean value and doesn’t take any arguments, making it straightforward to use.
- Application: It’s widely used in strategies and indicators that are specifically designed for longer timeframes or need to filter out lower timeframe noise.