Implementing a Custom Average True Range (ATR) Calculation Excluding Night Data

13 Feb 2020
0 comment
0 attachment

This code snippet demonstrates how to calculate a custom Average True Range (ATR) that excludes data from non-trading hours (night time) for a specific market. The ATR is a technical analysis indicator used to measure market volatility by decomposing the entire range of an asset price for that period.


p = 10 //10 periodi
IF time >= 090000 AND time <= 220000 THEN
    MyTR = max(Range,max(abs(high - close[1]),abs(low - close[1])))
    IF BarIndex < p THEN
        MyATR = MyTR
    ELSE
        MyATR = ((MyATR[1] * (p - 1)) + MyTR) / p
    ENDIF
ENDIF
RETURN MyATR

Explanation of the Code:

  • The variable p is set to 10, indicating that the ATR calculation is based on the last 10 periods.
  • The IF condition checks if the current time is between 09:00:00 and 22:00:00, which are considered as trading hours for this specific market.
  • MyTR (My True Range) is calculated only during these hours. It uses the maximum of:
    • The current bar's range (Range).
    • The absolute difference between the current high and the previous close (abs(high - close[1])).
    • The absolute difference between the current low and the previous close (abs(low - close[1])).
  • If the bar index is less than p, MyATR is set directly to MyTR. This is typically the case for the first 10 bars of the day.
  • For bars beyond the first 10, MyATR is calculated using an exponential moving average formula, where the previous ATR is adjusted to include the latest True Range.
  • The value of MyATR is returned, which represents the custom ATR for the specified trading hours.

This approach allows traders to focus on volatility during specific hours, potentially leading to more tailored trading strategies that align with market open times.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/atr-non-notturno/#post-98491

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...