<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Форум QUIK [тема: Вызов функции ATR]</title>
		<link>http://forum.quik.ru</link>
		<description>Новое в теме Вызов функции ATR форума  на сайте Форум QUIK [forum.quik.ru]</description>
		<language>ru</language>
		<docs>http://backend.userland.com/rss2</docs>
		<pubDate>Sat, 02 May 2026 22:31:55 +0300</pubDate>
		<item>
			<title>Вызов функции ATR</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message29106/topic3387/">Вызов функции ATR</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			Сергей, спасибо за ответы.<br />Я нашёл ошибку.<br />Вот:<br />long_v&#91;i&#93;=func2(<B><span class="bx-font" style="color:#ff0000">candles</span></B>, {Period=252}, ds_v) &nbsp;<br />а надо<br />long_v&#91;i&#93;=func2(<B><span class="bx-font" style="color:#ff0000">i</span></B>, {Period=252}, ds_v) &nbsp;<br /><br />теперь всё считает. <br />
			<i>12.02.2018 12:43:23, Let_it_go.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message29106/topic3387/</link>
			<guid>http://forum.quik.ru/messages/forum10/message29106/topic3387/</guid>
			<pubDate>Mon, 12 Feb 2018 12:43:23 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Вызов функции ATR</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message29105/topic3387/">Вызов функции ATR</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<a class="blog-p-user-name" id="bp_utA144IZ" href="/user/3905/" bx-tooltip-user-id="3905">Let_it_go</a>, <br /><br />Попробуйте выделить две функции под разные параметры<br />
====code====
<pre>
dofile ("C:&#92;&#92;Program Files&#92;&#92;Lua&#92;&#92;5.1&#92;&#92;lua&#92;&#92;ATR.lua")--ATR
ds_v = CreateDataSource(instr&#91;k&#93;.v_class,instr&#91;k&#93;.v_sec,INTERVAL_D1)
ds_v:SetEmptyCallback()
sleep(1000)
func1 = ATR() 
func2 = ATR() 
candles=ds_v:Size() --до этого места доходит и выдаёт количество свечей
long_v={}
short_v={}
for i=1,candles do&nbsp;&nbsp; 
long_v&#91;i&#93;=func1(candles, {Period=252}, ds_v)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
short_v&#91;i&#93;=func2(candles, {Period=5}, ds_v)&nbsp;&nbsp; 
end
</pre>
============= <br />
			<i>12.02.2018 12:41:53, Sergey Gorokhov.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message29105/topic3387/</link>
			<guid>http://forum.quik.ru/messages/forum10/message29105/topic3387/</guid>
			<pubDate>Mon, 12 Feb 2018 12:41:53 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Вызов функции ATR</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message29104/topic3387/">Вызов функции ATR</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			Все инструменты ликвидные.<br />Например SBER (TQBR)<br />Нилом являются ВСЕ значения внутри цикла for i=1,candles do &nbsp;(я специально это проверил). То есть функция не выдаёт ни одного значения.<br />---Вот код. Может быть он какой-то устаревший по сравнению с версией терминала.<br />
====code====
<pre>Settings = {
Name = "*ATR (Average True Range)", 
Period = 14,
line = {{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name = "Horizontal line",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type = TYPE_LINE, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Color = RGB(140, 140, 140)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Width = 3,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name = "ATR_Up", 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type = TYPE_HISTOGRAM, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Color = RGB(0, 206, 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Width = 3,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name = "ATR_Down", 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type = TYPE_HISTOGRAM, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Color = RGB(221, 44, 44)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
Round = "off",
Multiply = 1,
Horizontal_line="0"
}

local prev=0
function Init()
&nbsp;&nbsp;&nbsp;func = ATR()
&nbsp;&nbsp;&nbsp;return #Settings.line
end

function OnCalculate(Index)
local Out = ConvertValue(Settings, func(Index, Settings))
local HL = tonumber(Settings.Horizontal_line)
&nbsp;&nbsp;&nbsp;if Index == 1 then prev=0 end
&nbsp;&nbsp;&nbsp;if Out then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if Out &#62; prev then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev = Out
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HL,Out,nil
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev = Out
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HL,nil,Out
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HL,nil,nil
&nbsp;&nbsp;&nbsp;end
end

function ATR() --Average True Range ("ATR")
&nbsp;&nbsp;&nbsp;local f_TR = TR()
&nbsp;&nbsp;&nbsp;local tmp = {pp=nil, p=nil}
&nbsp;&nbsp;&nbsp;local it = {p=0, l=0}
return function (I, Fsettings, ds)
local Fsettings=(Fsettings or {})
local P = (Fsettings.Period or 14)
if (P &#62; 0) then
&nbsp;&nbsp;&nbsp;if I == 1 then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = {pp=nil, p=nil}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = {p=0, l=0}
&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;local tr = f_TR(I,ds)
&nbsp;&nbsp;&nbsp;if CandleExist(I,ds) then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if I~=it.p then 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = {p=I, l=it.l+1} 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.pp = tmp.p
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if it.l &#60; P then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.p = (tmp.pp or 0) + tr
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif it.l == P then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.p = ((tmp.pp or 0) + tr) / P
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return tmp.p
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif it.l &#62; P then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp.p = ((tmp.pp or 0) * (P-1) + tr) / P
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return tmp.p
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;end
end
return nil
end
end

function TR() --True Range ("TR")
&nbsp;&nbsp;&nbsp;local it = {pp=0, p=0, l=0}
return function (I, ds)
&nbsp;&nbsp;&nbsp;if I == 1 then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = {pp=0, p=0, l=0}
&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;if CandleExist(I,ds) then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if I~=it.p then it={pp=it.p, p=I, l=it.l+1} end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if it.l == 1 then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return math.abs(GetValueEX(it.p,DIFFERENCE, ds))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return math.max(math.abs(GetValueEX(it.p,DIFFERENCE, ds)), 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;math.abs(GetValueEX(it.p,HIGH,ds) - GetValueEX(it.pp,CLOSE,ds)), 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;math.abs(GetValueEX(it.pp,CLOSE,ds)-GetValueEX(it.p,LOW,ds)))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;end
return nil
end
end


SMA,MMA,EMA,WMA,SMMA,VMA = "SMA","MMA","EMA","WMA","SMMA","VMA"
OPEN,HIGH,LOW,CLOSE,VOLUME,MEDIAN,TYPICAL,WEIGHTED,DIFFERENCE,ANY = "O","H","L","C","V","M","T","W","D","A"

function CandleExist(I,ds)
return (type(C)=="function" and C(I)~=nil) or
&nbsp;&nbsp;&nbsp;(type(ds)=="table" and (ds&#91;I&#93;~=nil or (type(ds.Size)=="function" and (I&#62;0) and (I&#60;=ds:Size()))))
end

function Squeeze(I,P)
&nbsp;&nbsp;&nbsp;return math.fmod(I-1,P+1)
end

function ConvertValue(T,...)
local function r(V, R) 
&nbsp;&nbsp;&nbsp;if R and string.upper(R)== "ON" then R=0 end
&nbsp;&nbsp;&nbsp;if V and tonumber(R) then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if V &#62;= 0 then return math.floor(V * 10^R + 0.5) / 10^R
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else return math.ceil(V * 10^R - 0.5) / 10^R end
&nbsp;&nbsp;&nbsp;else return V end
end
&nbsp;&nbsp;&nbsp;if arg.n &#62; 0 then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i = 1, arg.n do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg&#91;i&#93;=arg&#91;i&#93; and r(arg&#91;i&#93; * ((T and T.Multiply) or 1), (T and T.Round) or "off")
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return unpack(arg)
&nbsp;&nbsp;&nbsp;else return nil end
end

function GetValueEX(I,VT,ds) 
VT=(VT and string.upper(string.sub(VT,1,1))) or ANY
&nbsp;&nbsp;&nbsp;if VT == OPEN then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Open
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (O and O(I)) or (ds and ds:O(I))
&nbsp;&nbsp;&nbsp;elseif VT == HIGH then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--High
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (H and H(I)) or (ds and ds:H(I))
&nbsp;&nbsp;&nbsp;elseif VT == LOW then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Low
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (L and L(I)) or (ds and ds:L(I))
&nbsp;&nbsp;&nbsp;elseif VT == CLOSE then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Close
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (C and C(I)) or (ds and ds:C(I))
&nbsp;&nbsp;&nbsp;elseif VT == VOLUME then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Volume
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (V and V(I)) or (ds and ds:V(I)) 
&nbsp;&nbsp;&nbsp;elseif VT == MEDIAN then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Median
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ((GetValueEX(I,HIGH,ds) + GetValueEX(I,LOW,ds)) / 2)
&nbsp;&nbsp;&nbsp;elseif VT == TYPICAL then&nbsp;&nbsp;&nbsp;--Typical
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ((GetValueEX(I,MEDIAN,ds) * 2 + GetValueEX(I,CLOSE,ds))/3)
&nbsp;&nbsp;&nbsp;elseif VT == WEIGHTED then&nbsp;&nbsp;&nbsp;--Weighted
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ((GetValueEX(I,TYPICAL,ds) * 3 + GetValueEX(I,OPEN,ds))/4) 
&nbsp;&nbsp;&nbsp;elseif VT == DIFFERENCE then&nbsp;&nbsp;&nbsp;--Difference
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (GetValueEX(I,HIGH,ds) - GetValueEX(I,LOW,ds))
&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--Any
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (ds and ds&#91;I&#93;)
&nbsp;&nbsp;&nbsp;end
return nil
end</pre>
============= <br />
			<i>12.02.2018 12:30:11, Let_it_go.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message29104/topic3387/</link>
			<guid>http://forum.quik.ru/messages/forum10/message29104/topic3387/</guid>
			<pubDate>Mon, 12 Feb 2018 12:30:11 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Вызов функции ATR</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message29103/topic3387/">Вызов функции ATR</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			<a class="blog-p-user-name" id="bp_cUFL7xk7" href="/user/3905/" bx-tooltip-user-id="3905">Let_it_go</a>, <br /><br />Сообщите на каком инструменте смотрите график.<br /><br />К слову, ATR не вернет данных если номер свечки меньше чем заданный период (по умолчанию 14) <br />Т.е. если у Вас период 14, то первые 14 значений будут nil и это нормально. <br />
			<i>12.02.2018 12:25:38, Sergey Gorokhov.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message29103/topic3387/</link>
			<guid>http://forum.quik.ru/messages/forum10/message29103/topic3387/</guid>
			<pubDate>Mon, 12 Feb 2018 12:25:38 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
		<item>
			<title>Вызов функции ATR</title>
			<description><![CDATA[<b><a href="http://forum.quik.ru/messages/forum10/message29102/topic3387/">Вызов функции ATR</a></b> в форуме <a href="http://forum.quik.ru/forum10/">Программирование на языке Lua</a>. <br />
			Прошу помочь с вызовом функции ATR<br />						
====code====
<pre>dofile ("C:&#92;&#92;Program Files&#92;&#92;Lua&#92;&#92;5.1&#92;&#92;lua&#92;&#92;ATR.lua")--ATR
ds_v = CreateDataSource(instr&#91;k&#93;.v_class,instr&#91;k&#93;.v_sec,INTERVAL_D1)
ds_v:SetEmptyCallback()
sleep(1000)
func2 = ATR() 
candles=ds_v:Size() --до этого места доходит и выдаёт количество свечей
long_v={}
short_v={}
for i=1,candles do&nbsp;&nbsp;&nbsp;
long_v&#91;i&#93;=func2(candles, {Period=252}, ds_v)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
short_v&#91;i&#93;=func2(candles, {Period=5}, ds_v)&nbsp;&nbsp;&nbsp;
end</pre>
=============
<br />но итоговые значения <br />long_v&#91;i&#93;<br />short_v&#91;i&#93;<br /><br />равны <B>нил</B>.<br />Благодарю за помощь. <br />
			<i>12.02.2018 12:19:48, Let_it_go.</i>]]></description>
			<link>http://forum.quik.ru/messages/forum10/message29102/topic3387/</link>
			<guid>http://forum.quik.ru/messages/forum10/message29102/topic3387/</guid>
			<pubDate>Mon, 12 Feb 2018 12:19:48 +0300</pubDate>
			<category>Программирование на языке Lua</category>
		</item>
	</channel>
</rss>
