Settings={ Name="MNKKANAL_new", period=200, delta=0, line= { { Name = "cur1", Type =TYPE_LINE, Width = 1, Color = RGB(0,255, 0) }, { Name = "cur2", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) }, { Name = "cur3", Type =TYPE_LINE, Width = 2, Color = RGB(255,0, 0) } } } --[[ описание свойств: period - период, за каротрый делается расчет назначение: построение тенденции использовался: метод наименьших квадратов (аппроксимация линией) --]] function Init() a1_0 = {} a2_0 = {} a3_0 = {} a4_0 = {} a1_1 = {} a2_1 = {} a3_1 = {} a4_1 = {} return 3 end function OnCalculate(index) sz = Size() n = Settings.period d = Settings.delta if index == 1 then a1_0[index] = 0 a2_0[index] = 0 a3_0[index] = 0 a4_0[index] = 0 i = index a1_1[index] = i*C(i) a2_1[index] = i a3_1[index] = C(i) a4_1[index] = i*i else i = index a1_1[index] = a1_1[index-1]+i*C(i) a2_1[index] = a2_1[index-1]+i a3_1[index] = a3_1[index-1]+C(i) a4_1[index] = a4_1[index-1]+i*i end y = nil y1 = nil y2 = nil if index-n > 0 then i = index-n a1_0[index] = a1_0[index-1]+i*C(i) a2_0[index] = a2_0[index-1]+i a3_0[index] = a3_0[index-1]+C(i) a4_0[index] = a4_0[index-1]+i*i a1 = a1_1[index]-a1_0[index] a2 = a2_1[index]-a2_0[index] a3 = a3_1[index]-a3_0[index] a4 = a4_1[index]-a4_0[index] 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, index 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 j = index-d y = a*j + b y1 = y-2*c1 y2 = y+2*c2 end else a1_0[index] = 0 a2_0[index] = 0 a3_0[index] = 0 a4_0[index] = 0 end return y,y1,y2 end