--[[ индикатор отрисовки наклонных уровней Параметры: 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
--[[ AutoTrade target indicator shows target growth/fall varsion 3 line gr shows real growth line tgr_up shows target growth line tgr_dwn shows target fall line ln_up shows target growth history line ln_dwn shows target fall history --]] Settings= { Name = "AT-target_v3", -- indicator name per=20, -- period growth=1.0, -- growth by % fall=1.0, -- fall by % xshift=0, -- shifth by x axis showln=1, -- 1-show lines 0-do not show line= { { Name = "gr", Type =TYPE_LINE, Width = 1, Color = RGB(0,0,0) -- black }, { Name = "tgr_up", Type =TYPE_LINE, Width = 1, Color = RGB(0,0,255) -- blue }, { Name = "tgr_dwn", Type =TYPE_LINE, Width = 1, Color = RGB(255,0,0) -- red }, { Name = "ln_up", Type =TYPE_LINE, Width = 1, Color = RGB(0,0,255) -- blue }, { Name = "ln_dwn", Type =TYPE_LINE, Width = 1, Color = RGB(255,0,0) -- red } } } function Init() return 5 end function OnCalculate(index) sz = Size() per = Settings.per gr = Settings.growth fl = Settings.growth sh = Settings.xshift sl = Settings.showln if index == sz then for i = 1, sz do -- crearing SetValue(i, 1, nil) SetValue(i, 2, nil) SetValue(i, 3, nil) SetValue(i, 4, nil) SetValue(i, 5, nil) end for i = sz-per-sh, sz-sh do -- paint line v = (C(sz-sh) - C(sz-per-sh+1))*(i-(sz-per-sh+1))/per + C(sz-per-sh+1) v2 = (C(sz-per-sh+1)*(1+gr/100) - C(sz-per-sh+1))*(i-(sz-per-sh+1))/per + C(sz-per-sh+1) v3 = -(C(sz-per-sh+1)*(1+fl/100) - C(sz-per-sh+1))*(i-(sz-per-sh+1))/per + C(sz-per-sh+1) SetValue(i, 1, v) SetValue(i, 2, v2) SetValue(i, 3, v3) end if sl == 1 then for i = per, sz-sh do -- paint line v4 = (C(i-per+1)*(1+gr/100) - C(i-per+1)) + C(i-per+1) v5 = -(C(i-per+1)*(1+fl/100) - C(i-per+1)) + C(i-per+1) SetValue(i, 4, v4) SetValue(i, 5, v5) end end -- last value if sh == 0 then return v, v2, v3, v4, v5 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
Settings={ Name="Buy_osc_v01", period=50, delta=0, lim=100.0, line= { { Name = "cur1", Type =TYPE_LINE, Width = 1, Color = RGB(0, 0, 255) }, { Name = "cur2", Type =TYPE_LINE, Width = 1, Color = RGB(0, 0, 0) }, { Name = "cur3", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) }, { Name = "cur4", Type =TYPE_LINE, Width = 1, Color = RGB(0,0, 0) } } } --[[ описание свойств: period - период, за каротрый делается расчет delta - смещение назад назначение: горизонтальная ширина канала использовался: метод наименьших квадратов (аппроксимация линией) --]] function Init() return 2 end function OnCalculate(index) sz = Size() n = Settings.period d = Settings.delta lim = Settings.lim y = nil if index-n-d > 0 then a1 = 0 a2 = 0 a3 = 0 a4 = 0 for i=index-n+1-d, index-d do a1 = a1+i*C(i) a2 = a2+i a3 = a3+C(i) a4 = a4+i*i end if((n*a4 - a2*a2) ~= 0) then a = (n*a1 - a2*a3)/(n*a4 - a2*a2) b = (a3 - a*a2)/n c1 = 0 c2 = 0 n1 = 0 n2 = 0 for j=index-n+1-d, index-d do y = a*j + b if C(j) < y then c1 = c1 + (y -C(j)) n1 = n1 + 1 end if C(j) > y then c2 = c2 + (C(j) - y) n2 = n2 + 1 end end if n1 ~= 0 then c1 = c1/n1 end if n2 ~= 0 then c2 = c2/n2 end if a==0 then y = 0 else y = (c1+c2)/a if y > lim then y = lim else if y < -lim then y = -lim end end end end end return y,0 end