Daily ATR Levels Trading view script. Help in converting into prorealtime please
Forums › ProRealTime English forum › ProBuilder support › Daily ATR Levels Trading view script. Help in converting into prorealtime please
- This topic has 2 replies, 2 voices, and was last updated 1 year ago by yas.
-
-
05/24/2023 at 8:42 AM #215106
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sbtnc
// Created: 2020-09-06
// Last modified: 2023-02-22
// Version 5.0
// @version=5
indicator(“Daily ATR Levels”, “D ATR”, true)
//——————————————————————–
// Constants
//——————————————————————–
var DEFAULT_OFFSET_START = 1
var DEFAULT_LINE_LENGTH = 8
var DEFAULT_LINE_STYLE = line.style_dotted
var DEFAULT_LINE_WIDTH = 2
var DEFAULT_LABEL_COLOR = color(na)
var DEFAULT_LABEL_STYLE = label.style_label_left
var DEFAULT_NOTIF_POSITION = position.bottom_right
var DEFAULT_NOTIF_TEXTCOLOR = chart.fg_color
var DEFAULT_NOTIF_BGCOLOR = color.new(color.gray, 90)
//——————————————————————–
// Inputs
//——————————————————————–
// Groups
var g1 = “ATR”
var g2 = “Style”
var g3 = “Preferences”
// Tooltips
var t_value = “Calculate the levels using yesterday’s ATR or today’s ATR.”
var t_overflowColor = “Coloring after the price reached or exceeded the ATR boundary.”
var t_showStartOffset = “Draw levels on the right side of the price only.\n\n” +
“When disabled, draw levels from the start of the day.”
var t_showNext = “Plot the next day’s ATR levels when the market is closed.\n\n” +
“Use this option if you like to plan your trades ahead of the next day or prepare on the weekend for Monday.”
// Inputs
var i_period = input.int (20, “Length”, 1, group=g1)
var i_value = input.string (“Yesterday”, “Reference”, [“Today”, “Yesterday”], group=g1)
var i_color = input (color.gray, “Color”, group=g2)
var i_overflowColor = input (color.new(#50535e, 50), “Overflow Color”, t_overflowColor, group=g2)
var i_showStartOffset = input (true, “Show Start Offset”, t_showStartOffset, group=g3)
var i_showNextDay = input (false, “Show Next ATR Levels”, t_showNext, group=g3)
//——————————————————————–
// Functions
//——————————————————————–
// @function Return the number of decimals from a given number
// @return int
f_countDecimals(float _n) =>
_str = str.tostring(_n)
_count = 0
// Number is a decimal
if str.contains(_str, “.”)
_split = str.split(_str, “.”)
_count := str.length(array.get(_split, 1))
_count
// @function Return a number rounded to the precision of the symbol’s mintick (different than
math.round_to_mintick
)// @return float
f_roundToMinPrecision(float _n) =>
var minPrecision = f_countDecimals(syminfo.mintick)
math.round(_n, minPrecision)
// @function Check if the market is closed
// @return bool
f_isMarketClosed() =>
barstate.islast and barstate.islastconfirmedhistory
// @function Check if the indicator should display the next session ATR
// return bool
f_canShowNextATR() =>
i_showNextDay and f_isMarketClosed()
// @function Return the ATR value used as a reference
// @return float
f_getRefAtr(float _current, float _previous) =>
switch i_value
“Today” => _current
“Yesterday” => _previous
// @function Return the “true” high price (considering gaps volatility)
// @return float
f_getTopPrice(_prevClose, float _high) =>
math.max(_prevClose, _high)
// @function Return the “true” low price (considering gaps volatility)
// @return float
f_getBottomPrice(float _prevClose, float _low) =>
math.min(_prevClose, _low)
// @function Calculate the upside ATR level
// @return float
f_calculateAtrHigh(float _bottom, float _atr) =>
_bottom + _atr
// @function Calculate the downside ATR level
// @return float
f_calculateAtrLow(float _top, float _atr) =>
_top – _atr
// @function Draw a level’s line
// @return void
f_drawLine(int _x1, int _x2, float _y, color _c) =>
var _l = line.new(na, na, na, na, style=DEFAULT_LINE_STYLE, width=DEFAULT_LINE_WIDTH)
if barstate.islast
line.set_xy1(_l, _x1, _y)
line.set_xy2(_l, _x2, _y)
line.set_color(_l, _c)
// @function Draw a level’s label and tooltips
// @return void
f_drawLabel(int _x, float _y, string _t, color _textColor, string _tooltip) =>
var _l = label.new(na, na, style=DEFAULT_LABEL_STYLE, color=DEFAULT_LABEL_COLOR)
if barstate.islast
label.set_xy(_l, _x, _y)
label.set_text(_l, _t)
label.set_textcolor(_l, _textColor)
label.set_tooltip(_l, _tooltip)
// @function Draw the table showing the indicator notifications
// return int
f_drawNotificationTable(array<bool> _conditions, array<string> _texts, array<string> _tooltips) =>
// For the implementation purpose, throw an error when missing an element
_conditionsSize = array.size(_conditions)
_textsSize = array.size(_texts)
_tooltipsSize = array.size(_tooltips)
if _conditionsSize != _textsSize or _conditionsSize != _tooltipsSize
runtime.error(“Notification error: missing element in arrays.”)
// Create the notification table
var table _t = na
var _columns = _conditionsSize
var _rows = 1
if barstate.isfirst
_t := table.new(DEFAULT_NOTIF_POSITION, _columns, _rows)
if barstate.islast
// Clear previous notifications
table.clear(_t, 0, 0, _columns – 1, _rows – 1)
// Count the number of active notifications
_active = 0
for [index, _condition] in _conditions
if _condition
_text = array.get(_texts, index)
_tooltip = array.get(_tooltips, index)
table.cell(_t, _active, 0, _text, text_color=DEFAULT_NOTIF_TEXTCOLOR, bgcolor=DEFAULT_NOTIF_BGCOLOR, tooltip=_tooltip)
_active += 1
// @function Returns the indicator strings
// @return [string, string, string]
f_getStrings(float _upperLevel, float _lowerlevel, float _atr) =>
[
str.tostring(f_roundToMinPrecision(_upperLevel)),
str.tostring(f_roundToMinPrecision(_lowerlevel)),
str.tostring(f_roundToMinPrecision(_atr))
]
//——————————————————————–
// Variables declarations
//——————————————————————–
var float dayHigh = na
var float dayLow = na
var float prevDayClose = na
var int dayBarIndex = na
var float bottomPrice = na
var float topPrice = na
[dayAtr, prevDayAtr] = request.security(
syminfo.tickerid,
‘D’,
[ta.atr(i_period), ta.atr(i_period)[1]],
lookahead = barmerge.lookahead_on)
// For avoiding unexpected behaviors, don’t display the indicator when requesting daily data on higher time frames
canDisplay = timeframe.in_seconds() <= timeframe.in_seconds(“D”)
atr = f_getRefAtr(dayAtr, prevDayAtr)
//——————————————————————–
// Logic
//——————————————————————–
// *
// * For the historical values
// *
// Track the day’s high and low on runtime instead of fetching them from a security call
// This aims to avoid the
future leak
from the “jump” when using the bar replay function// 1/ Init on the first bar of the chart
// 2/ Reset on new day change (condition should cover the extended sessions bars)
if timeframe.change(“D”) or barstate.isfirst
dayHigh := high
dayLow := low
prevDayClose := close[1]
dayBarIndex := bar_index
// Track the new higher high and lower low
dayHigh := math.max(high, dayHigh)
dayLow := math.min(low, dayLow)
// Get the top and bottom anchor prices for calculating the levels (gap minded)
bottomPrice := f_getBottomPrice (prevDayClose, dayLow)
topPrice := f_getTopPrice (prevDayClose, dayHigh)
// Compute the ATR levels prices
atrHigh = f_calculateAtrHigh(bottomPrice, atr)
atrLow = f_calculateAtrLow (topPrice, atr)
// Handle the condiditional coloring
c_atrHigh = dayHigh >= atrHigh ? i_overflowColor : i_color
c_atrLow = dayLow <= atrLow ? i_overflowColor : i_color
// *
// * For the levels
// *
// Normally showing the current/last ATR levels
upperLevel = atrHigh
lowerLevel = atrLow
c_upperLevel = c_atrHigh
c_lowerLevel = c_atrLow
// Except when showing the next ATR levels
// In this case, we override values
if f_canShowNextATR()
upperLevel := f_calculateAtrHigh(close, atr)
lowerLevel := f_calculateAtrLow (close, atr)
c_upperLevel := i_color
c_lowerLevel := i_color
//——————————————————————–
// Plotting & styling
//——————————————————————–
// Draw the upper and lower levels (lines and labels)
if canDisplay
// Get all the strings
[_atrHighStr, _atrLowStr, _atrStr] = f_getStrings(upperLevel, lowerLevel, atr)
// Composes labels and tooltips’ texts
upperLabelText = str.format(“{0} ({1})”, _atrHighStr, _atrStr)
lowerLabelText = _atrLowStr
upperLabelTooltip = str.format(“{0} (D ATR: {1})”, _atrHighStr, _atrStr)
lowerLabelTooltip = str.format(“{0} (D ATR: {1})”, _atrLowStr, _atrStr)
// Draw lines and labels
_offset = bar_index + DEFAULT_OFFSET_START
_start = i_showStartOffset ? _offset : dayBarIndex
_end = _offset + DEFAULT_LINE_LENGTH
f_drawLine (_start, _end, atrHigh, c_upperLevel)
f_drawLine (_start, _end, atrLow, c_lowerLevel)
f_drawLabel (_end, atrHigh, upperLabelText, c_upperLevel, upperLabelTooltip)
f_drawLabel (_end, atrLow, lowerLabelText, c_lowerLevel, lowerLabelTooltip)
// Plot the levels’ labels on the price scale
// (as well as providing generic alert conditions)
plot(canDisplay ? upperLevel : na, “Upper Level”, c_upperLevel, editable=false, show_last=1, display=display.price_scale)
plot(canDisplay ? lowerLevel : na, “Lower Level”, c_lowerLevel, editable=false, show_last=1, display=display.price_scale)
// Plot all historical values on the status line and data window
// (as well as providing generic alert conditions)
plot(canDisplay ? atrHigh : na, “ATR High”, c_atrHigh, editable=false, display=display.status_line + display.data_window)
plot(canDisplay ? atrLow : na, “ATR Low”, c_atrLow, editable=false, display=display.status_line + display.data_window)
plot(canDisplay ? atr : na, “ATR”, i_overflowColor, editable=false, display=display.status_line + display.data_window)
// Draw the notification table
notifications = array.from(
f_canShowNextATR() and canDisplay,
not canDisplay
)
notificationMessages = array.from(
“»”,
“▽”
)
notificationsTooltips = array.from(
“The D ATR is currently displaying how the levels would look at the next session’s open.”,
“The D ATR is currently not displayed. Please select a higher timeframe on the indicator’s settings.”
)
f_drawNotificationTable(notifications, notificationMessages, notificationsTooltips)
05/24/2023 at 12:15 PM #215120Hello, please, thanks.
By reading the author description, it plots the daily ATR period 20 from the previous day and anchor it on current day low and high:
12345678910111213defparam drawonlastbaronly=truetimeframe(daily,updateonclose)atr=AverageTrueRange[20](close)[1]timeframe(default)upperlvl = floor(dlow(0)+atr,decimals)lowerlvl = floor(dhigh(0)-atr,decimals)drawsegment(barindex,upperlvl,barindex+10,upperlvl) style(dottedline)drawsegment(barindex,lowerlvl,barindex+10,lowerlvl) style(dottedline)drawtext("#upperlvl#",barindex+20,upperlvl)drawtext("#lowerlvl#",barindex+20,lowerlvl)return05/24/2023 at 12:29 PM #215122 -
AuthorPosts
Find exclusive trading pro-tools on