Блог им. autotrade |Индикатор оценки инструмента для включения его в портфель

В отличии от оценки по теории Марковица, здесь учитывается просадка и рост
Значение инструмента объективно определяет вес инструмента в портфеле
Из картинки ниже видно, что Сбер уделывает на всем промежутке и ГМК и Лукойл и Русал
Индикатор оценки инструмента для включения его в портфель


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

function Init()

  plus = 0
  minus = 0
  
  y1 = nil
  y2 = nil
  x1 = 1
  x2 = 1
       
  return 1
  
end

function OnCalculate(index)

  de = Settings.Procent

  vl = C(index)
  if index == 1 then 
	y1 = vl
    y2 = vl
  else   
	  if C(index) > y1*(1+de/100) and y1 < y2 then 
        minus = minus + (x1-x2)*(y2-y1)	  
	    x2 = x1
	    y2 = y1	
	    x1 = index 
	    y1 = C(index)			
      else 
	    if C(index) > y1 and y1 >= y2 
		then 
	      x1 = index 
	      y1 = C(index)	  			  
	    end 		
	  end 	

	  	  		
	  if C(index) < y1*(1-de/100) and y1 > y2 then 
        plus = plus + (x1-x2)*(y1-y2)	  
	    x2 = x1
	    y2 = y1
	    x1 = index 
	    y1 = C(index)				
      else 
	    if C(index) < y1 and y1 <= y2 
		then 
	      x1 = index 
	      y1 = C(index)	  			  
	    end 		
	  end 	
	  	  		
	end 	

  
  vl=0
  if minus~=0 then
    vl=plus/minus
  end 
  
  return vl
 
  
end

Блог им. autotrade |индикатор EMA

индикатор EMA


--[[
EMA template
--]]
Settings=              
        {                          
            Name = "EMA",   -- indicator name
            ln=20,               -- period		
            line=                                     
                {                               
                    {  
                        Name = "EMA",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    }					
                }
       }


function Init()
  ema = {}

  return 1
end


function OnCalculate(index)
  
  ln = Settings.ln 
  if index-1 < 1 then 
    ema[index] = C(index)
  else 
    ema[index] = (ema[index-1]*(ln-1) + C(index))/ln
  end 
 
  return ema[index]

end

ОФФТОП |доработанный ндикатор A_zig_channel_nested


доработанный ндикатор A_zig_channel_nested


--[[
индикатор: 


--]]
Settings=              
        {                          
            Name = "A_zig_channel_nested",   -- название индикатора
          --  leng=50,               -- длина 	
			proc0=10,
			proc=2,
			delt=40,
			minh = 0.5,
            line=                                     
                {                               
  					{  
                        Name = "cur1",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "cur2",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "cur3",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "cur4",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "cur5",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "cur6",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(255,0,0)
                    },
					{  
                        Name = "cur7",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,255)
                    },
					{  
                        Name = "cur8",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(255,0,0)
                    },
					{  
                        Name = "cur9",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,255)
                    },
					{  
                        Name = "cur10",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(255,0,0)
                    },
					{  
                        Name = "cur11",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0, 255)
                    },
					{  
                        Name = "cur12",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(255,0, 0)
                    },
					{  
                        Name = "cur13",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,255)
                    },
					{  
                        Name = "cur14",
                        Type =TYPE_LINE,
                        Width = 2,
                        Color = RGB(0,0,0)
                    }
                }
       }


function Init()


  v1=nil
  v2=nil
  v3=nil
  v4=nil
  v5=nil
  v6=nil
  v7=nil
  v8=nil
  v9=nil
  v10=nil
  v11=nil
  v12=nil
  v13=nil  
  v14=nil  
  

  y_1 = nil
  y_2 = nil
  x_1 = 1
  x_2 = 1
  x_3 = 1
  
  sz = 1
  
  minh = 0

  
  return 14
end



function recalcline(num1, num2, vFrom, vTo, index, delt)
--[[
 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 --]]


  
	  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, vTo do
        y = a*i + b		
		if C(i) < y and i <= vTo then
            if y-C(i) > vminus then
              vminus = y-C(i) 			
            end				
        end		  
		if C(i) > y and i <= vTo then  
            if C(i)-y > vplus then
              vplus = C(i)-y 			
            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 	--]]	

      --  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 	--]]	

       -- end 		
	   end
	   --end
	   
		  if vTo+delt > index then
		    delt = 0
		  end 
	      
	   
	   if minh*C(vFrom)/100 < vplus + vminus then 	   
       for i = vFrom, vTo+delt do
	    if i <= index then 
          y = a*i + b	
          m = 0
          if C(vTo) >	C(vFrom) and y+vplus < C(vTo)+2*(C(vTo)-C(vFrom)) and y+vplus > C(vFrom)-2*(C(vTo)-C(vFrom)) then
            m = 1
		  end 	
          if C(vTo) <	C(vFrom) and y+vplus < C(vFrom)+2*(C(vFrom)-C(vTo)) and y+vplus > C(vTo)-2*(C(vFrom)-C(vTo)) then
            m = 1
		  end 
		  if i <= vTo then
			m = 1
		  end 			  
          if m == 1 then		  
	        SetValue(i, num1, y+vplus)
			Setval(i, num1, y+vplus)
		  end 
          m = 0
          if C(vTo) >	C(vFrom) and y-vminus < C(vTo)+2*(C(vTo)-C(vFrom)) and y-vminus > C(vFrom)-2*(C(vTo)-C(vFrom)) then
            m = 1
		  end 	
          if C(vTo) <	C(vFrom) and y-vminus < C(vFrom)+2*(C(vFrom)-C(vTo)) and y-vminus > C(vTo)-2*(C(vFrom)-C(vTo)) then
            m = 1
		  end 
		  if i <= vTo then
			m = 1
		  end 		  
          if m == 1 then		  
	        SetValue(i, num2, y-vminus)		  
			Setval(i, num2, y-vminus)
		  end 	
		end 
	   end 	
	  
      end
	  end 

 
 return 0
end 

function channel(curto, curfrom, index, num1, num2, num3, num4, num5, num6)

	  a1 = 0
	  a2 = 0
	  a3 = 0
	  a4 = 0
	  n = 0
	  a = 0
	  b = 0  
     for i = curfrom, curto 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 = curfrom, curto  do
        y = a*i + b		
		if y>C(i) and y-C(i) > vminus then
	        vminus = y - C(i)
        end		  
		if y<C(i) and C(i)-y > vplus then
	        vplus = C(i) - y
		end   
       end 
	   
       --[[
	    if vminus_n ~= 0 then
	     vminus = 2*vminus/vminus_n          
	    end 
	    if vplus_n ~= 0 then
	     vplus = 2*vplus/vplus_n          
	    end 
		--]]
		

  x1 = curfrom
  x2 = curfrom
  x3 = curfrom
  y1 = C(x1) 
  y2 = C(x2) 
  y3 = C(x3)   
  
  --de = 0.5*(vplus + vminus)/C(index-leng) --/(C(index-leng) - (a*(index-leng) + b - vminus))
  de = proc*C(curfrom)
  
          delt = 10
		  
		  if curto+delt > index then
		    delt = 0
		  end 
	   
	   if minh*C(curfrom)/100 < vplus + vminus then 
	      for i = 1, curfrom do
	        SetValue(i, num1, nil)
			SetValue(i, num2, nil)
			SetValue(i, num3, nil)
			SetValue(i, num4, nil)
			SetValue(i, num5, nil)
			SetValue(i, num6, nil)
          end 		   
		  for i = curfrom, curto+delt do
		    y = a*i + b	
			
            m = 0
            if C(curto) >	C(curfrom) and y+vplus < C(curto)+2*(C(curto)-C(curfrom)) and y+vplus > C(curfrom)-2*(C(curto)-C(curfrom)) then
              m = 1
		    end 	
            if C(curto) <	C(curfrom) and y+vplus < C(curfrom)+2*(C(curfrom)-C(curto)) and y+vplus > C(curto)-2*(C(curfrom)-C(curto)) then
              m = 1
		    end 
			if i <= curto then
			  m = 1
			end 
            if m == 1 then				
		      SetValue(i, num5, y + vplus)
			  Setval(i, num5, y+ vplus)	
			end 
			--SetValue(i, 2, y)
            m = 0
            if C(curto) >	C(curfrom) and y- vminus < C(curto)+2*(C(curto)-C(curfrom)) and y- vminus > C(curfrom)-2*(C(curto)-C(curfrom)) then
              m = 1
		    end 	
            if C(curto) <	C(curfrom) and y- vminus < C(curfrom)+2*(C(curfrom)-C(curto)) and y- vminus > C(curto)-2*(C(curfrom)-C(curto)) then
              m = 1
		    end 
			if i <= curto then
			  m = 1
			end 			
            if m == 1 then		
			  SetValue(i, num6, y - vminus)				  
              Setval(i, num6, y-vminus)			  
		    end
			
    	   --vl = C(i) -- (y - vminus)
		   vl = C(i) -- (y - vminus)
    	   if i == curfrom then 
	 	     y1 = vl
     	     y2 = vl
			 y3 = vl
    	   else   
	  	    if (vl > y1+de and y1 < y2) or (x1==curfrom and vl > y1) then 
	   	     x3 = x2
	   	     y3 = y2
	   	     x2 = x1
	   	     y2 = y1	
	   	     x1 = i
	   	     y1 = vl
	  	    end 	
	 	    if vl > y1 and vl > y2 then 
	 	     x1 = i
	 	     y1 = vl	  			  
	 	    end 
	  	  		
	 	    if (vl < y1-de and y1 > y2) or (x1==curfrom and vl < y1) then 
	   	     x3 = x2
	   	     y3 = y2			
	  	     x2 = x1
	  	     y2 = y1
	   	     x1 = i
	   	     y1 = vl	  	
	 	    end 	
	 	    if vl < y1 and vl < y2 then 
	  	     x1 = i
	  	     y1 = vl	  			  
	 	    end 	  	  		
		   end 		  
		 
		  end 
		  
		  --[[
          k = (C(x1)- C(x2))/(x1- x2)  
          for i = x2, x1  do
            curv = i*k + C(x1) - x1*k  		          
	        SetValue(i, 4, curv)
          end   		  

          k = (C(x2)- C(x3))/(x2- x3)  
          for i = x3, x2  do
            curv = i*k + C(x2) - x2*k  		          
	        SetValue(i, 5, curv)
          end  		
          --]]
		  
		  recalcline(num1, num2, x2, x1, index, 20)
		  
		  recalcline(num3, num4, x3, x2, index, 20)
	   
     end 
	 end 

end 

function Setval(i, num, v)
 
 if sz == i then 
  if num == 1 then v1 = v end 
  if num == 2 then v2 = v end 
  if num == 3 then v3 = v end 
  if num == 4 then v4 = v end   
  if num == 5 then v5 = v end 
  if num == 6 then v6 = v end 
  if num == 7 then v7 = v end 
  if num == 8 then v8 = v end 
  if num == 9 then v9 = v end 
  if num == 10 then v10 = v end 
  if num == 11 then v11 = v end 
  if num == 12 then v12 = v end 
  if num == 13 then v13 = v end 
  if num == 14 then v14 = v end   
 end
 
end 

function OnCalculate(index)
  
 -- leng = Settings.leng
  minh = Settings.minh
  delt = Settings.delt
  delt2 = delt
  proc0 = Settings.proc0
  proc0 = proc0/100
  proc = Settings.proc
  proc = proc/100
  sz = Size()
  
  
  vl = C(index)
  if index == 1 then 
	y_1 = vl
    y_2 = vl
  else   
	  if C(index) > y_1*(1+proc0) and y_1 < y_2 then 
	    x_3 = x_2
	    x_2 = x_1
	    y_2 = y_1	
	    x_1 = index 
	    y_1 = C(index)	        
	  end 	
	  if C(index) > y_1 and C(index) > y_2 then 
	    x_1 = index 
	    y_1 = C(index)	  			  
	  end 
	  	  		
	  if C(index) < y_1*(1-proc0) and y_1 > y_2 then 
	    x_3 = x_2
	    x_2 = x_1
	    y_2 = y_1
	    x_1 = index 
	    y_1 = C(index)	  		
	  end 	
	  if C(index) < y_1 and C(index) < y_2 then 
	    x_1 = index 
	    y_1 = C(index)	  			  
	  end 	  	  		
	end 	
  --[[
  if x_1 ~= index then 
    curfrom = x_1
	curto = index
  else --]]
    curfrom = x_2
	curto = x_1
  --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, 10, curv)
      end   	
	end 
  end --]]

  
  if index == sz then 
--[[
     curto = x_1
	 curfrom = x_2
  
     k = (C(curto)- C(curfrom))/(curto- curfrom)  
     for i = curfrom, curto  do
       curv = i*k + C(curto) - curto*k  		          
	   SetValue(i, 11, curv)
     end   --]]
 
     curto = index
	 curfrom = x_1
  --[[
     k = (C(curto)- C(curfrom))/(curto- curfrom)  
     for i = curfrom, curto  do
       curv = i*k + C(curto) - curto*k  		          
	   SetValue(i, 10, curv)
     end  --]]
  
  
     channel(curto, curfrom, index, 6, 7, 8, 9, 1, 2) 
	 
	 --[[ --]]
     curto = x_1
	 curfrom = x_2	 
	 
	 channel(curto, curfrom, index, 10, 11, 12, 13, 3, 4) 
	
  end   
 
    return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 , v12, v13, v14

end

Блог им. autotrade |Индикатор разницы объемов по положительным и отрицательным барам

Индикатор разницы объемов по положительным и отрицательным барам
--[[
MA volumn up down
--]]
Settings=              
        {                          
            Name = "MAV2_m",   -- indicator name
            per=10,               -- period			
            line=                                     
                {                               
                    {  
                        Name = "MA",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,0)
                    },
					{  
                        Name = "MA2",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    }	
                }
       }


function Init()   
  mav1 = {}
  mav2 = {}  
  return 2
end


function OnCalculate(index)
  
  per = Settings.per
  
  if index > 1 then 
    curv = C(index)
    prevv = C(index-1)	
  else 
    curv = C(index)
    prevv = C(index)	
  end   
 
  if curv - prevv > 0 then 
    if index > 1 then 
      mav1[index] = mav1[index-1] + V(index)
	  mav2[index] = mav2[index-1] 
    else 
      mav1[index] = V(index)  
	  mav2[index] = 0
    end    
  else 
    if index > 1 then 
	  mav1[index] = mav1[index-1]
      mav2[index] = mav2[index-1] + V(index)
    else 
	  mav1[index] = 0
      mav2[index] = V(index)  
    end 
  end 
  
  if index > per then
    vv1 = mav1[index] - mav1[index-per]
	vv2 = mav2[index] - mav2[index-per]
  else 
    vv1 = 0
	vv2 = 0
  end   

  return 0, vv1 - vv2

end

....все тэги
UPDONW
Новый дизайн