индикатор наклонных уровней


Индикатор наклонных уровней

сигналом к покупке является пересечение ценой кривой снизу вверх, а на продажу — наоборот.
вход по цене закрытия бара, пересекшего индикатор или цена открытия следующего бара.
в дальнейшем немного поменяю логику

Индикатор наклонных уровней

--[[
параметры: 
Procent - процент зигзага 
--]]
Settings={
Name="ZIGZAGPROF",
Procent=1,
    line=                                     
                {  
					{  
                        Name = "cur1",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0, 0)
                    },
                    {  
                        Name = "cur2",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0, 255)
                    }							
                }
}

function Init()
  
  y1 = nil
  y2 = nil
  x1 = 1
  x2 = 1
    
  return 1
  
end

function OnCalculate(index)

  de = Settings.Procent
  
  delt = 0.01

  vl = C(index)
  if index == 1 then 
	y1 = vl
    y2 = vl
  else   
	  if C(index) > y1*(1+de/100) and y1 < y2 then 
	    x2 = x1
	    y2 = y1	
	    x1 = index 
	    y1 = C(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 
	    x2 = x1
	    y2 = y1
	    x1 = index 
	    y1 = C(index)  		
	  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 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
--]]
    
    lev = nil
	if x1 ~= x2 then  
     
	
	k = (C(x1)- C(x2))/(x1- x2)    
	maxd = 0
    for i = x2, x1  do
      
	  lev = i*k + C(x1) - x1*k  		
	  
	  if  C(x2) > C(x1) and lev <= H(i) 
	  then 
	    if maxd < H(i) - lev  then 
          maxd = H(i) - lev 
		end
        --maxd = 0.5		
      end 
	  
      if  C(x2) < C(x1) and lev >= L(i) 
	  then 
	    if maxd > L(i) - lev  then 
          maxd = L(i) - lev
		end 
		--maxd = -0.5
      end 	  
	  
    end   	
      
    lev = nil 
    --[[if x1 < index 
	  and 
	  (
	  C(x2) > C(x1) and C(x1) < C(index) 
      or 
      C(x2) < C(x1) and C(x1) > C(index) 	  
	 )
	then --]]
      lev = 
	    index*k + C(x1) - x1*k + 
		maxd
	--end   
    
	  
	  
	--[[  
	
	map = 10
	lev = 0
	if index-map+1 > 0 then 
      for i = index-map+1, index  do
        lev = lev + C(i)
      end   		
	  lev = lev/map
	  ma = lev
	end
	
	map = 30
	lev2 = 0
	if index-map+1 > 0 then 
      for i = index-map+1, index  do
        lev2 = lev2 + C(i)
      end   		
	  lev2 = lev2/map
	  ma2 = lev2
	end	

	
	if 
	  C(x2) > C(x1) and C(x1) < C(index) and C(index) > lev and C(index) - C(x1) > C(index)*delt
	  or 
	  C(x2) > C(x1) and C(x1) < C(index) and C(index) > lev2 
	then 
	  lev = C(x1)--*(1-delt)
	  prev = lev        
	else  	
	  if 
	    C(x2) < C(x1) and C(x1) > C(index) and C(index) < lev and C(x1) - C(index) > C(index)*delt
	    or
	    C(x2) < C(x1) and C(x1) > C(index) and C(index) < lev2 
	  then 
	    lev = C(x1)--*(1+delt)
	    prev = lev
	  else
        lev = lev2  
	  end		
    end	

	if 
	  C(x1) > C(x2) and ( lev < C(index) or prev == C(x2) )
	then 
	  lev = C(x2)--*(1+delt)
	  prev = lev
	end
	
	if
	  C(x1) < C(x2) and ( lev > C(index) or prev == C(x2) )
	then 
	  lev = C(x2)--*(1-delt)
	  prev = lev
	end	
	
    if C(x1) < C(x2) and ( lev < C(index) or prev == C(x1) )
	then         	  
	  lev = C(x1)
	  prev = lev
    end
	  
    if C(x1) > C(x2) and ( lev > C(index) or prev == C(x1) ) 
	then         	  
	  lev = C(x1)
	  prev = lev       		
	end
	 --]] 

	
  end   
  
  return  lev
 
  
end



....все тэги
UPDONW