Блог им. autotrade
--[[ вопросы к автору: https://smart-lab.ru/profile/autotrade/ индикатор отрисовки наклонных уровней как пользоваться: индикатор показывает степень волатильности, чем шире канал тем больше волатильность, чем уже тем меньше меньше волатильность при высокой волатильности торгуем в канале, при низкой волатильности торгуем пробитие канала Параметры: delta - чувствиельность индикатора на волатильность в % rep - количество повторений поиска конечного уровня shif - сдвиг влево конечного бара wt - признак наличия весового коэффициента =0 - без весового =1,2.. - свосовыми коф. чем ближе к концу тем больше show=1, --]] Settings= { Name = "AT-obl_can", -- название индикатора delta=2.0, -- параметр индикатора rep=10, shif=0, wt=1, --hl=1, show=1, line= { { Name = "ln1", Type =TYPE_LINE, Width = 1, Color = RGB(255, 0, 0) }, { Name = "ln2", Type =TYPE_LINE, Width = 1, Color = RGB(255, 0, 0) }, { Name = "ln3", Type =TYPE_LINE, Width = 1, Color = RGB(0, 0, 0) }, { Name = "ln4", Type =TYPE_LINE, Width = 1, Color = RGB(0, 0, 0) } } } function Init() vMin = 0 vMax = 0 vMinindex = 0 vMaxindex = 0 voldMinindex = 0 voldMaxindex = 0 oldind = 1 ret = {} inds = {} return 4 end function OnCalculate(index) rep = Settings.rep shif = Settings.shif wt = Settings.wt --hl = Settings.hl if index == 1 then inds = {} end sz = Size()-shif if Settings.show == 1 and inds[index] == nil then if index <= sz then if index <= 1 then vMin = O(index) vMax = O(index) vMinindex = index vMaxindex = index voldMinindex = index voldMaxindex = index v = O(index) else if voldMaxindex >= voldMinindex then if vMin~=nil and O(index)~=nil then if O(index) > (1 + Settings.delta/100)*vMin then vMin = O(index) vMax = O(index) vMaxindex = index voldMinindex = vMinindex vFrom = vMinindex else if vMin > O(index) then vMin = O(index) vMinindex = index vFrom = voldMaxindex else vFrom = vMinindex end end end else if voldMaxindex <= voldMinindex then if vMax~=nil and O(index)~=nil then if O(index) < (1 - Settings.delta/100)*vMax then vMax = O(index) vMin = O(index) vMinindex = index voldMaxindex = vMaxindex vFrom = vMaxindex else if vMax < O(index) then vMax = O(index) vMaxindex = index vFrom = voldMinindex else vFrom = vMaxindex end end end end end --if vFrom~=nil then --[[ for i = vFrom, index do k = (C(index)- O(vFrom))/(index- vFrom); v = i*k + O(index) - index*k SetValue(i, 1, v) end --]] --end if index == sz then for k = 1, 4 do for i = 1, sz do SetValue(i, k, nil) end vf = 1 vs = 0 if k == 1 or k == 2 then if vMinindex < vMaxindex then vf = vMinindex vs = vMaxindex up = 1 elseif vMinindex > vMaxindex then vs = vMinindex vf = vMaxindex up = 0 end elseif k == 3 or k == 4 then if voldMinindex < voldMaxindex then vf = voldMinindex vs = voldMaxindex up = 1 elseif voldMinindex > voldMaxindex then vs = voldMinindex vf = voldMaxindex up = 0 end end n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do if O(i) ~= nil then m = m +1 n = n + 1*(1+wt*m) xy = xy + i*O(i)*(1+wt*m) x = x + i*(1+wt*m) y = y + O(i)*(1+wt*m) xx = xx + i*i*(1+wt*m) end end if (n*xx - x*x) ~= 0 and n ~= 0 then a = (n*xy - x*y)/(n*xx - x*x) b = (y - a*x)/n --[[ del = 0 for i = vf, vs do v = a*i + b if up == 1 and v <= O(i) and (k==1 or k==3) then if O(i)-v > del then del = O(i)-v end end if up == 0 and v >= O(i) and ( k == 2 or k == 4) then if v-O(i) > del then del = v-O(i) end end end if up == 1 then del = -del end --]] --[[ vs2 = vs if k==1 then vs2 = sz end ret[3] = nil for i = vf, vs2 do if v >= O(vs) and v < 1.3*O(sz) or v <= O(vs) and v > 0.7*O(sz) then v = a*i + b + del -- SetValue(i, k+2, v) if k==1 then ret[k+2] = v end end end --]] for j = 1, rep do n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do if O(i) ~= nil then v = a*i + b clc = 0 if up == 1 and O(i) >= v and (k == 1 or k == 3) or up == 1 and O(i) <= v and (k == 2 or k==4) then -- clc = 1 end if up == 0 and O(i) >= v and (k == 1 or k == 3) or up == 0 and O(i) <= v and (k == 2 or k==4) then -- clc = 1 end if clc == 1 then m = m + 1 n = n + 1*(1+wt*m) --[[ if up == 1 and hl == 1 then xy = xy + i*H(i)*(1+wt*m) elseif up == 0 and hl == 1 then xy = xy + i*L(i)*(1+wt*m) else --]] xy = xy + i*O(i)*(1+wt*m) -- end x = x + i*(1+wt*m) --[[ if up == 1 and hl == 1 then y = y + H(i)*(1+wt*m) elseif up == 0 and hl == 1 then y = y + L(i)*(1+wt*m) else --]] y = y + O(i)*(1+wt*m) -- end xx = xx + i*i*(1+wt*m) end end end --[[--]] if (n*xx - x*x) ~= 0 and n ~= 0 and n >= 2 then a = (n*xy - x*y)/(n*xx - x*x) b = (y - a*x)/n --[[ --]] end end ret[k] = nil for i = vf, sz do v = a*i + b if ( v >= O(vs) and v < 1.3*O(sz) or v <= O(vs) and v > 0.7*O(sz) ) and (vs - vf)/2 > i - vs then SetValue(i, k, v) else v=nil end ret[k] = v end end end end end end end if inds[index] == nil then inds[index] = index end -- oldind = index return ret[1],ret[2],ret[3],ret[4] end