Home » Ticker Functions » Understanding timeframe.isdwm Function in Pine Script

Understanding timeframe.isdwm Function in Pine Script

Photo of author
Published on

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

  1. 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.
  2. 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.
  3. Price Definition: price = close – Here, we’re assigning the closing price of the bars to the variable price.
  4. Timeframe Check: isDWMTimeframe = timeframe.isdwm() – This line checks if the current chart’s timeframe is daily, weekly, or monthly, and stores the result in isDWMTimeframe.
  5. SMA Calculation: smaOnDWM = isDWMTimeframe ? ta.sma(price, length) : na – This ternary operation calculates the SMA if isDWMTimeframe is true; otherwise, it assigns na (not available).
  6. 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.

Leave a Comment