--[[ индикатор отрисовки наклонных уровней Параметры: 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 = 2, Color = RGB(255, 0, 0) }, { Name = "ln2", Type =TYPE_LINE, Width = 2, Color = RGB(255, 0, 0) } } } function Init() vMin = 0 vMax = 0 vMinindex = 0 vMaxindex = 0 voldMinindex = 0 voldMaxindex = 0 return 2 end function OnCalculate(index) rep = Settings.rep shif = Settings.shif wt = Settings.wt --hl = Settings.hl sz = Size()-shif if Settings.show == 1 then if index <= sz then if index <= 1 then vMin = C(index) vMax = C(index) vMinindex = index vMaxindex = index voldMinindex = index voldMaxindex = index v = C(index) else if voldMaxindex >= voldMinindex then if vMin~=nil and C(index)~=nil then if C(index) > (1 + Settings.delta/100)*vMin then vMin = C(index) vMax = C(index) vMaxindex = index voldMinindex = vMinindex vFrom = vMinindex else if vMin > C(index) then vMin = C(index) vMinindex = index vFrom = voldMaxindex else vFrom = vMinindex end end end else if voldMaxindex <= voldMinindex then if vMax~=nil and C(index)~=nil then if C(index) < (1 - Settings.delta/100)*vMax then vMax = C(index) vMin = C(index) vMinindex = index voldMaxindex = vMaxindex vFrom = vMaxindex else if vMax < C(index) then vMax = C(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)- C(vFrom))/(index- vFrom); v = i*k + C(index) - index*k SetValue(i, 1, v) end --]] --end if index == sz then for k = 1, 2 do vf = 1 vs = 0 if k == 1 then if vMinindex < vMaxindex then vf = vMinindex vs = vMaxindex up = 0 elseif vMinindex > vMaxindex then vs = vMinindex vf = vMaxindex up = 1 end elseif k == 2 then if voldMinindex < voldMaxindex then vf = voldMinindex vs = voldMaxindex up = 0 elseif voldMinindex > voldMaxindex then vs = voldMinindex vf = voldMaxindex up = 1 end end n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do if C(i) ~= nil then m = m +1 n = n + 1*(1+wt*m) xy = xy + i*C(i)*(1+wt*m) x = x + i*(1+wt*m) y = y + C(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 for j = 1, rep do n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do if C(i) ~= nil then v = a*i + b clc = 0 if up == 1 and C(i) >= v then clc = 1 end if up == 0 and C(i) <= v 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*C(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 + C(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 for i = vf, sz do v = a*i + b if up == 1 and v >= C(vs) or up == 0 and v <= C(vs) then SetValue(i, k, v) end end end end end end end end end
--[[ параметры: Procent - процент зигзага --]] Settings={ Name="ZIGZAGLEVELS", Procent=5.0, levels=6, delta=0.2, line= { { Name = "cur1", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur2", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur3", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur4", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur5", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur6", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) } } } function Init() y1 = nil y2 = nil x1 = 1 x2 = 1 levelsy={} levelsx={} cntlevels=0 return 6 end function OnCalculate(index) de = Settings.Procent levels = Settings.levels delta = Settings.delta sz = Size() vl = C(index) if index <= 1 then y1 = vl y2 = vl cntlevels=0 else if C(index) > y1*(1+de/100) and y1 < y2 then x2 = x1 y2 = y1 x1 = index y1 = C(index) cntlevels = cntlevels + 1 levelsx[cntlevels]=x2 levelsy[cntlevels]=y2 end if C(index) > y1 and C(index) > y2 then x1 = index y1 = C(index) end if C(index) < y1*(1-de/100) and y1 > y2 then x2 = x1 y2 = y1 x1 = index y1 = C(index) cntlevels = cntlevels + 1 levelsx[cntlevels]=x2 levelsy[cntlevels]=y2 end if C(index) < y1 and C(index) < y2 then x1 = index y1 = C(index) end end if x1 ~= index then curfrom = x1 curto = index else curfrom = x2 curto = x1 end if sz == index then cnt = levels for k = 1, cnt do for i = 1, index do SetValue(i, k, nil) end end -- cnt = 3 k = 0 for j = cntlevels, 1, -1 do d = 0 if levelsy[j] > C(index) then d = levelsy[j] - C(index) end if levelsy[j] < C(index) then d = C(index) - levelsy[j] end if d < delta*C(index) and d > 0 then k = k + 1 if k <= cnt then y = levelsy[j] for i = levelsx[j], index do SetValue(i, k, y) end end end end --[[ k = 0 for j = cntlevels, 1, -1 do d = 0 if levelsy[j] < C(index) then d = C(index) - levelsy[j] end if d < 0.2*C(index) and d > 0 then if k <= cnt then k = k + 1 y = levelsy[j] for i = levelsx[j], index do SetValue(i, k+3, y) end end end end --]] end end
Settings= { Name = "AT-obl_can", -- название индикатора delta=2.0, -- параметр индикатора rep=5, shif=0, wt=1, line= { { Name = "ln1", Type =TYPE_LINE, Width = 2, Color = RGB(255, 0, 0) }, { Name = "ln2", Type =TYPE_LINE, Width = 2, Color = RGB(255, 0, 0) } } } function Init() vMin = 0 vMax = 0 vMinindex = 0 vMaxindex = 0 voldMinindex = 0 voldMaxindex = 0 return 2 end function OnCalculate(index) rep = Settings.rep shif = Settings.shif wt = Settings.wt sz = Size()-shif if index <= sz then if index <= 1 then vMin = C(index) vMax = C(index) vMinindex = index vMaxindex = index voldMinindex = index voldMaxindex = index v = C(index) else if voldMaxindex >= voldMinindex then --if vMin~=nil then if C(index) > (1 + Settings.delta/100)*vMin then vMin = C(index) vMax = C(index) vMaxindex = index voldMinindex = vMinindex vFrom = vMinindex else if vMin > C(index) then vMin = C(index) vMinindex = index vFrom = voldMaxindex else vFrom = vMinindex end end --end else if voldMaxindex <= voldMinindex then --if vMax~=nil then if C(index) < (1 - Settings.delta/100)*vMax then vMax = C(index) vMin = C(index) vMinindex = index voldMaxindex = vMaxindex vFrom = vMaxindex else if vMax < C(index) then vMax = C(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)- C(vFrom))/(index- vFrom); v = i*k + C(index) - index*k SetValue(i, 1, v) end --]] --end if index == sz then for k = 1, 2 do vf = 1 vs = 0 if k == 1 then if vMinindex < vMaxindex then vf = vMinindex vs = vMaxindex up = 0 elseif vMinindex > vMaxindex then vs = vMinindex vf = vMaxindex up = 1 end elseif k == 2 then if voldMinindex < voldMaxindex then vf = voldMinindex vs = voldMaxindex up = 0 elseif voldMinindex > voldMaxindex then vs = voldMinindex vf = voldMaxindex up = 1 end end n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do m = m +1 n = n + 1*(1+wt*m) xy = xy + i*C(i)*(1+wt*m) x = x + i*(1+wt*m) y = y + C(i)*(1+wt*m) xx = xx + i*i*(1+wt*m) 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 for j = 1, rep do n = 0 xy = 0 x = 0 y = 0 xx = 0 m = 0 for i = vf, vs do v = a*i + b clc = 0 if up == 1 and C(i) > v then clc = 1 end if up == 0 and C(i) < v then clc = 1 end if clc == 1 then m = m + 1 n = n + 1*(1+wt*m) xy = xy + i*C(i)*(1+wt*m) x = x + i*(1+wt*m) y = y + C(i)*(1+wt*m) xx = xx + i*i*(1+wt*m) 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 for i = vf, sz do v = a*i + b if up == 1 and v >= C(vs) or up == 0 and v <= C(vs) then SetValue(i, k, v) end end end end end end end end
--[[ индикатор торговой системы QUIK: канал на последнем зигзаге индикатор строит канал по зигзагу, показывает слом тренда при выходе цены за границы канала параметры: Procent - величина зигзага в % k - коэффизиент ширины канала Ext - =0-канал стоится по средним =1-канал стоится по экстремуму Как им пользоваться: в папке QUIK\LuaIndicators создаем текстовый файл с содержимым кода индикатора добавляем индикатор на график задаем параметр чувствиельности инлдикатора — Процент индикатор показывает уровни, в которых находится тренд если тренд еще не сформировался, то границы канала могут расширяться по мере движения цены если в канале уже есть локальные вершины, то при следующем походе индикатора к границе канал возмен разворот тренда или коррекции в рамках данного канала --]] Settings= { Name = "Zigzag_channel_fast", -- название индикатора Procent=5.0, -- величина зигзага в % k=2, -- коэффизиент ширины канала Ext=1, -- =0-канал стоится по средним =1-канал стоится по экстремуму shif=0, line= { { Name = "zigzagline", Type =TYPE_LINE, Width = 1, Color = RGB(120,90, 140) }, { Name = "cur1", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) }, { Name = "cur2", Type =TYPE_LINE, Width = 2, Color = RGB(0,200, 200) }, { Name = "cur3", Type =TYPE_LINE, Width = 2, Color = RGB(200,200, 0) }, { Name = "cur4", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) }, { Name = "cur5", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur6", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur7", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur8", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur9", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur10", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur11", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) } } } function Init() y1 = nil y2 = nil y3 = nil x1 = 1 x2 = 1 x3 = 1 oldindex = 1 a_01 = 0 b_01 = 0 a_02 = 0 b_02 = 0 vminus_01 = 0 vplus_01 = 0 vminus_02 = 0 vplus_02 = 0 vFrom_01 = 1 vTo_01 = 1 vFrom_02 = 1 vTo_02 = 1 vminus = 0 vplus = 0 vminus_n = 0 vplus_n = 0 ret = 0 last_ind = 1 v1=nil v2=nil v3=nil v4=nil v5=nil v6=nil v7=nil v8=nil v9=nil v10=nil return 10 end function recalcline(num, index, rec) if index == Size()-sh and rec == 0 then for i = 1, index do if m == 1 then SetValue(i, 3, nil) SetValue(i, 4, nil) end if m == 2 then SetValue(i, 6, nil) SetValue(i, 7, nil) end end end if index == Size()-sh or rec == 1 then if num == 1 then vFrom = 0 --[[ --]] vTo = 0 if x3 < x2 then vFrom = x3 vTo = x2 else vFrom = x2 vTo = x3 end end if num == 2 then vFrom = 0 vTo = 0 if x1 < x2 then vFrom = x1 vTo = x2 else vFrom = x2 vTo = x1 end end if vFrom < 1 then vFrom = 1 end a1 = 0 a2 = 0 a3 = 0 a4 = 0 n = 0 a = 0 b = 0 for i = vFrom, vTo do n=n+1 a1 = a1+i*C(i) a2 = a2+i a3 = a3+C(i) a4 = a4+i*i end if((n*a4 - a2*a2) ~= 0) then -- if oldindex ~= Size() then a = (n*a1 - a2*a3)/(n*a4 - a2*a2) b = (a3 - a*a2)/n vminus = 0 vplus = 0 vminus_n = 0 vplus_n = 0 for i = vFrom, index do y = a*i + b if i>0 then if C(i) < y and i <= vTo then if ex == 0 then vminus = vminus + y - C(i) vminus_n = vminus_n + 1 else if i == vFrom then vminus = 0 end if y-C(i) > vminus then vminus = y-C(i) end end end end if i>0 then if C(i) > y and i <= vTo then if ex == 0 then vplus = vplus + C(i) - y vplus_n = vplus_n + 1 else if i == vFrom then vplus = 0 end if C(i)-y > vplus then vplus = C(i)-y end end end end m = 0 if C(vTo) > C(vFrom) and y < C(vTo)+0.1*(C(vTo)-C(vFrom)) and y > C(vFrom)-0.1*(C(vTo)-C(vFrom)) then m = 1 end if C(vTo) < C(vFrom) and y < C(vFrom)+0.1*(C(vFrom)-C(vTo)) and y > C(vTo)-0.1*(C(vFrom)-C(vTo)) then m = 1 end if m == 1 then --SetValue(i, 2, y) end end --end if ex == 0 then if vminus_n ~= 0 then vminus = kk*vminus/vminus_n end if vplus_n ~= 0 then vplus = kk*vplus/vplus_n end end end if num == 1 then a_01 = a b_01 = b vminus_01 = vminus vplus_01 = vplus vFrom_01 = vFrom vTo_01 = vTo end if num == 2 then a_02 = a b_02 = b vminus_02 = vminus vplus_02 = vplus vFrom_02 = vFrom vTo_02 = vTo end return 1 end return 0 end function levels(x, y, num, index) if y ~= nil and x ~= 1 then for i = 1, index do if i > x then SetValue(i, num, y) else SetValue(i, num, nil) end end end end function OnCalculate(index) de = Settings.Procent kk = Settings.k ex = Settings.Ext sh = Settings.shif v1=nil v2=nil v3=nil v4=nil v5=nil v6=nil v7=nil v8=nil v9=nil v10=nil if index > Size()-sh then --return nil, nil, nil, nil, nil, nil, nil return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 end vl = C(index) if vl~=nil then if index == 1 then y1 = vl y2 = vl else if C(index) > y1*(1+de/100) and y1 < y2 then x3 = x2 x2 = x1 y3 = y2 y2 = y1 x1 = index y1 = C(index) ret = recalcline(1, index, 0) ret = recalcline(2, index, 0) levels(x1, y1, 8, index) levels(x2, y2, 9, index) levels(x3, y3, 10, index) end if C(index) > y1 and C(index) > y2 then x1 = index y1 = C(index) end if C(index) < y1*(1-de/100) and y1 > y2 then x3 = x2 x2 = x1 y3 = y2 y2 = y1 x1 = index y1 = C(index) ret = recalcline(1, index, 0) ret = recalcline(2, index, 0) levels(x1, y1, 8, index) levels(x2, y2, 9, index) levels(x3, y3, 10, index) end if C(index) < y1 and C(index) < y2 then x1 = index y1 = C(index) end end end --[[ if x1 ~= index then curfrom = x1 curto = index else curfrom = x2 curto = x1 end if curto ~= curfrom and curfrom ~= nil and curto ~= nil then if C(curto) ~= nil and C(curfrom) ~= nil then k = (C(curto)- C(curfrom))/(curto- curfrom) for i = curfrom, index do curv = i*k + C(curto) - curto*k SetValue(i, 1, curv) end end end --]] if index == Size()-sh and ret == 0 then lpr = C(Size()-sh) ret = recalcline(1, index, 1) ret = recalcline(2, index, 1) if y1 ~= nil then if y1 > lpr*0.9 and y1 < lpr*1.1 then levels(x1, y1, 8, index) end end if y2 ~= nil then if y2 > lpr*0.9 and y2 < lpr*1.1 then levels(x2, y2, 9, index) end end if y3 ~= nil then if y3 > lpr*0.9 and y3 < lpr*1.1 then levels(x3, y3, 10, index) end end end if index == Size()-sh then --- and index ~= last_ind if vTo_01 - vFrom_01 > 500 then vFrom_01 = vTo_01-500 end for i = 1, index do SetValue(i, 3, nil) SetValue(i, 4, nil) SetValue(i, 6, nil) SetValue(i, 7, nil) -- SetValue(i, 8, nil) -- SetValue(i, 9, nil) -- SetValue(i, 10, nil) end for i = vFrom_01, index do y = a_01*i + b_01 + vplus_01 m = 0 if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then m = 1 end if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then m = 1 end if m == 1 then SetValue(i, 3, y) if i == index then v3 = y end end y = a_01*i + b_01 - vminus_01 m = 0 if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then m = 1 end if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then m = 1 end if m == 1 then SetValue(i, 4, y) if i == index then v4 = y end end end if vTo_02 - vFrom_02 > 500 then vFrom_02 = vTo_02-500 end for i = vFrom_02, index do y = a_02*i + b_02 + vplus_02 m = 0 if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then m = 1 end if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then m = 1 end if m == 1 then SetValue(i, 6, y) if i == index then v6 = y end end y = a_02*i + b_02 - vminus_02 m = 0 if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then m = 1 end if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then m = 1 end if m == 1 then SetValue(i, 7, y) if i == index then v7 = y end end end last_ind = index end return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 end
--[[ индикатор: канал на последнем зигзаге индикатор строит канал по зигзагу, показывает слом тренда при выходе цены за границы канала параметры: Procent - величина зигзага в % k - коэффизиент ширины канала Ext - =0-канал стоится по средним =1-канал стоится по экстремуму --]] Settings= { Name = "Zigzag_channel_fast", -- название индикатора Procent=5, -- величина зигзага в % k=2, -- коэффизиент ширины канала Ext=1, -- =0-канал стоится по средним =1-канал стоится по экстремуму shif=0, line= { { Name = "zigzagline", Type =TYPE_LINE, Width = 1, Color = RGB(120,90, 140) }, { Name = "cur1", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) }, { Name = "cur2", Type =TYPE_LINE, Width = 2, Color = RGB(0,200, 200) }, { Name = "cur3", Type =TYPE_LINE, Width = 2, Color = RGB(200,200, 0) }, { Name = "cur4", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) }, { Name = "cur5", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 255) }, { Name = "cur6", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur7", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur8", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur9", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur10", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur11", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) } } } function Init() y1 = nil y2 = nil y3 = nil x1 = 1 x2 = 1 x3 = 1 oldindex = 1 a_01 = 0 b_01 = 0 a_02 = 0 b_02 = 0 vminus_01 = 0 vplus_01 = 0 vminus_02 = 0 vplus_02 = 0 vFrom_01 = 1 vTo_01 = 1 vFrom_02 = 1 vTo_02 = 1 vminus = 0 vplus = 0 vminus_n = 0 vplus_n = 0 ret = 0 last_ind = 1 v1=nil v2=nil v3=nil v4=nil v5=nil v6=nil v7=nil v8=nil v9=nil v10=nil return 10 end function recalcline(num, index, rec) if index == Size()-sh and rec == 0 then for i = 1, index do if m == 1 then SetValue(i, 3, nil) SetValue(i, 4, nil) end if m == 2 then SetValue(i, 6, nil) SetValue(i, 7, nil) end end end if index == Size()-sh or rec == 1 then if num == 1 then vFrom = 0 --[[ --]] vTo = 0 if x3 < x2 then vFrom = x3 vTo = x2 else vFrom = x2 vTo = x3 end end if num == 2 then vFrom = 0 vTo = 0 if x1 < x2 then vFrom = x1 vTo = x2 else vFrom = x2 vTo = x1 end end if vFrom < 1 then vFrom = 1 end a1 = 0 a2 = 0 a3 = 0 a4 = 0 n = 0 a = 0 b = 0 for i = vFrom, vTo do n=n+1 a1 = a1+i*C(i) a2 = a2+i a3 = a3+C(i) a4 = a4+i*i end if((n*a4 - a2*a2) ~= 0) then -- if oldindex ~= Size() then a = (n*a1 - a2*a3)/(n*a4 - a2*a2) b = (a3 - a*a2)/n vminus = 0 vplus = 0 vminus_n = 0 vplus_n = 0 for i = vFrom, index do y = a*i + b if i>0 then if C(i) < y and i <= vTo then if ex == 0 then vminus = vminus + y - C(i) vminus_n = vminus_n + 1 else if i == vFrom then vminus = 0 end if y-C(i) > vminus then vminus = y-C(i) end end end end if i>0 then if C(i) > y and i <= vTo then if ex == 0 then vplus = vplus + C(i) - y vplus_n = vplus_n + 1 else if i == vFrom then vplus = 0 end if C(i)-y > vplus then vplus = C(i)-y end end end end m = 0 if C(vTo) > C(vFrom) and y < C(vTo)+0.1*(C(vTo)-C(vFrom)) and y > C(vFrom)-0.1*(C(vTo)-C(vFrom)) then m = 1 end if C(vTo) < C(vFrom) and y < C(vFrom)+0.1*(C(vFrom)-C(vTo)) and y > C(vTo)-0.1*(C(vFrom)-C(vTo)) then m = 1 end if m == 1 then --SetValue(i, 2, y) end end --end if ex == 0 then if vminus_n ~= 0 then vminus = kk*vminus/vminus_n end if vplus_n ~= 0 then vplus = kk*vplus/vplus_n end end end if num == 1 then a_01 = a b_01 = b vminus_01 = vminus vplus_01 = vplus vFrom_01 = vFrom vTo_01 = vTo end if num == 2 then a_02 = a b_02 = b vminus_02 = vminus vplus_02 = vplus vFrom_02 = vFrom vTo_02 = vTo end return 1 end return 0 end function levels(x, y, num, index) if y ~= nil and x ~= 1 then for i = 1, index do if i > x then SetValue(i, num, y) else SetValue(i, num, nil) end end end end function OnCalculate(index) de = Settings.Procent kk = Settings.k ex = Settings.Ext sh = Settings.shif v1=nil v2=nil v3=nil v4=nil v5=nil v6=nil v7=nil v8=nil v9=nil v10=nil if index > Size()-sh then --return nil, nil, nil, nil, nil, nil, nil return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 end vl = C(index) if vl~=nil then if index == 1 then y1 = vl y2 = vl else if C(index) > y1*(1+de/100) and y1 < y2 then x3 = x2 x2 = x1 y3 = y2 y2 = y1 x1 = index y1 = C(index) ret = recalcline(1, index, 0) ret = recalcline(2, index, 0) levels(x1, y1, 8, index) levels(x2, y2, 9, index) levels(x3, y3, 10, index) end if C(index) > y1 and C(index) > y2 then x1 = index y1 = C(index) end if C(index) < y1*(1-de/100) and y1 > y2 then x3 = x2 x2 = x1 y3 = y2 y2 = y1 x1 = index y1 = C(index) ret = recalcline(1, index, 0) ret = recalcline(2, index, 0) levels(x1, y1, 8, index) levels(x2, y2, 9, index) levels(x3, y3, 10, index) end if C(index) < y1 and C(index) < y2 then x1 = index y1 = C(index) end end end --[[ if x1 ~= index then curfrom = x1 curto = index else curfrom = x2 curto = x1 end if curto ~= curfrom and curfrom ~= nil and curto ~= nil then if C(curto) ~= nil and C(curfrom) ~= nil then k = (C(curto)- C(curfrom))/(curto- curfrom) for i = curfrom, index do curv = i*k + C(curto) - curto*k SetValue(i, 1, curv) end end end --]] if index == Size()-sh and ret == 0 then ret = recalcline(1, index, 1) ret = recalcline(2, index, 1) levels(x1, y1, 8, index) levels(x2, y2, 9, index) levels(x3, y3, 10, index) end if index == Size()-sh then --- and index ~= last_ind if vTo_01 - vFrom_01 > 500 then vFrom_01 = vTo_01-500 end for i = 1, index do SetValue(i, 3, nil) SetValue(i, 4, nil) SetValue(i, 6, nil) SetValue(i, 7, nil) end for i = vFrom_01, index do y = a_01*i + b_01 + vplus_01 m = 0 if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then m = 1 end if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then m = 1 end if m == 1 then SetValue(i, 3, y) if i == index then v3 = y end end y = a_01*i + b_01 - vminus_01 m = 0 if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then m = 1 end if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then m = 1 end if m == 1 then SetValue(i, 4, y) if i == index then v4 = y end end end if vTo_02 - vFrom_02 > 500 then vFrom_02 = vTo_02-500 end for i = vFrom_02, index do y = a_02*i + b_02 + vplus_02 m = 0 if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then m = 1 end if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then m = 1 end if m == 1 then SetValue(i, 6, y) if i == index then v6 = y end end y = a_02*i + b_02 - vminus_02 m = 0 if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then m = 1 end if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then m = 1 end if m == 1 then SetValue(i, 7, y) if i == index then v7 = y end end end last_ind = index end return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 end
--[[ параметры: Procent - процент зигзага --]] Settings={ Name="ZIG_MA_v2", Procent=2.0, ln=50, line= { { Name = "ZIG_MA", Type =TYPE_LINE, Width = 2, Color = RGB(0,0, 0) }, { Name = "ZIG_MA2", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 255) } } } function Init() ema = {} y1 = nil y2 = nil x1 = 1 x2 = 1 return 2 end function OnCalculate(index) de = Settings.Procent ln = Settings.ln if index <= 1 then ema[index] = C(index) else ema[index] = (ema[index-1]*(ln-1) + C(index))/ln end if ln==0 then pr = C(index) else pr = C(index)-ema[index] end vl = C(index) if index == 1 then y1 = vl y2 = vl else if pr > y1+C(index)*de/100 and y1 < y2 then x2 = x1 y2 = y1 x1 = index y1 = pr else if pr > y1 and y1 >= y2 then x1 = index y1 = pr end end if pr < y1-C(index)*de/100 and y1 > y2 then x2 = x1 y2 = y1 x1 = index y1 = pr else if pr < y1 and y1 <= y2 then x1 = index y1 = pr end end end if x1 ~= index then curfrom = x1 curto = index else curfrom = x2 curto = x1 end if curto ~= curfrom and curfrom ~= nil and curto ~= nil then if C(curto) ~= nil and C(curfrom) ~= nil then k = (C(curto)- C(curfrom))/(curto- curfrom) for i = curfrom, index do curv = i*k + C(curto) - curto*k SetValue(i, 1, curv) end end end return vl, ema[index] end