autotrade
autotrade личный блог
24 апреля 2026, 15:12

Продолжение разработки индикатора

Результаты радуют
Получился простой индикатор
Предыдущий пост на эту тему: Идея построения индикатора для торговой системы. Продолжение 
График сделок и профита по Газпрому с Сберу преф.
Продолжение разработки индикатора
Продолжение разработки индикатора
Продолжение разработки индикатора

--[[
вопросы к автору: https://smart-lab.ru/profile/autotrade/
Zigzag + 2 MA
параметры: 
prc - процент зигзага 
--]]
Settings={
Name="MMZ_v4",
prc=4,
frm=200,
SLP=2.0,
sign=1,
prf=0,
tar=0.03,
    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)
                    },					
					{  
                        Name = "TRIANGLE_DOWN",
                        Type =TYPE_TRIANGLE_DOWN,
                        Width = 5,
                        Color = RGB(255, 0, 0)
                    },
					{  
                        Name = "TRIANGLE_UP",
                        Type =TYPE_TRIANGLE_UP,
                        Width = 5,
                        Color = RGB(0, 0, 255)
                    },
					{  
                        Name = "cur5",
                        Type =TYPE_POINT,
                        Width = 5,
                        Color = RGB(255, 0, 255)
                    },
					{  
                        Name = "cur6",
                        Type =TYPE_POINT,
                        Width = 5,
                        Color = RGB(0, 0, 255)
                    }
                }
}
 
function Init()
  
      
  return 6
  
end
 
function OnCalculate(index)
 
  de = Settings.prc
 
  vl = O(index)
  if index == 1 then 
    y1 = vl 
    y2 = vl
    y3 = vl
    y4 = vl    
    x1 = 1
    x2 = 1    
    x3 = 1
    x4 = 1    
    ch = {}
    ch[index] = 0
  else   
    ch[index] = ch[index-1] 
      if O(index) > y1*(1+de/100) and y1 < y2 then 
        x4 = x3
        y4 = y3          
        x3 = x2
        y3 = y2          
        x2 = x1
        y2 = y1    
        x1 = index 
        y1 = O(index)        
        ch[index] = ch[index-1] + 1
      else 
        if O(index) > y1 and y1 >= y2 
        then 
          x1 = index 
          y1 = O(index)            
		  if ch[index] % 10 == 0 then 
            ch[index] = ch[index-1] + 1
          end 		  
        end         
      end     
 
                    
      if O(index) < y1*(1-de/100) and y1 > y2 then 
        x4 = x3
        y4 = y3          
        x3 = x2
        y3 = y2          
        x2 = x1
        y2 = y1
        x1 = index 
        y1 = O(index)    
        ch[index] = ch[index-1] + 1        
      else 
        if O(index) < y1 and y1 <= y2 
        then 
          x1 = index 
          y1 = O(index)    
		  if ch[index] % 10 == 0 then 
            ch[index] = ch[index-1] + 1
          end 	
        end         
      end     
                    
    end     
  
--[[
  if x1 ~= index then 
    curfrom = x1
    curto = index
  else 
    curfrom = x2
    curto = x1
  end 
 
  if Settings.t==0 then
  if curto ~= curfrom and curfrom ~= nil and curto ~= nil then 
    if O(curto) ~= nil and O(curfrom) ~= nil then 
      k = (O(curto)- O(curfrom))/(curto- curfrom)  
      for i = curfrom, index  do
        curv = i*k + O(curto) - curto*k                    
        SetValue(i, 1, curv)
      end       
    end 
  end 
  end
 --]]
 
 
  if index == 1 then 
    sma = {}
    sma[index] = O(index)
    per1 = {}
    per2 = {}
    per1[index] = 8
    per2[index] = 2
    sm1 = {}
    sm1[index] = O(index)	
    sm2 = {}
    sm2[index] = O(index)		
  else 
    sma[index] = sma[index-1] + O(index)
    per1[index] = per1[index-1]
    per2[index] = per2[index-1]

    sm1[index] = sm1[index-1]
    sm2[index] = sm2[index-1]	
    
    if ch[index] ~= ch[index-1]  then          
         
     --per1_0 = Settings.frm    
    --[[     
     for i = Settings.frm, 5, -1  do
	
      j = math.ceil(i/4) 
      cros0 = 0
      for k = x2, x1 do                  
       if k-2 > Settings.frm then 
        sma1 = (sma[k] - sma[k-i])/i  
        sma2 = (sma[k] - sma[k-j])/j  
        sma1_0 = (sma[k-1] - sma[k-i-1])/i  
        sma2_0 = (sma[k-1] - sma[k-j-1])/j         
        if 
         (
           sma1 > sma2 and sma1_0 <= sma2_0 and y1 > y2 or 
           sma1 < sma2 and sma1_0 >= sma2_0 and y1 < y2
         ) --and cros0 = 0
        then
         cros0 = cros0 + 1   
         pr0 = O(k) 		 
        end      
       end 
      end  -- for k 
      if cros0 <= 1 then 
        per1_0 = i
        per2_0 = j	  
      end  	              
     end -- for i    
--]]
 
     for i = Settings.frm, 5, -1  do  
	 
      j = math.ceil(i/4) 
      cros = 0
	  updwn = 0 
      for k = x4, x1 do                  
       if k-2 > Settings.frm then         
        sma1 = (sma[k] - sma[k-i])/i  
        sma2 = (sma[k] - sma[k-j])/j  
        sma1_0 = (sma[k-1] - sma[k-i-1])/i  
        sma2_0 = (sma[k-1] - sma[k-j-1])/j         
        if 
         (
         sma1 > sma2 and sma1_0 <= sma2_0 --and y2 > y3 
		 or 
         sma1 < sma2 and sma1_0 >= sma2_0 --and y2 < y3
         ) --and cros = 0
        then
         cros = cros + 1
		 pr1 = O(k)
		 if sma1 > sma2 then 
		   updwn = 1
		   xx = k
		 end 
		 if sma1 < sma2 then 
		   updwn = -1
		   xx = k
		 end 		 
        end      
       end 
      end  -- for k   
	  
      if ( cros <= 3 ) and (y1 > y2 and updwn == 1 or y1 < y2 and updwn == -1 ) and xx > x2 
	    or
	  ( cros == 0 ) 
	  then 
        per1_1 = i
        per2_1 = j	  
		per1[index] = i
        per2[index] = j
      end  	 
     end -- for i         
     
    --[[ 
     for i = Settings.frm, 5, -1  do
      
	  j = math.ceil(i/4) 
      cros2 = 0
      for k = x4, x3 do                  
       if k-2 > Settings.frm then         
        sma1 = (sma[k] - sma[k-i])/i  
        sma2 = (sma[k] - sma[k-j])/j  
        sma1_0 = (sma[k-1] - sma[k-i-1])/i  
        sma2_0 = (sma[k-1] - sma[k-j-1])/j         
        if 
         (
          sma1 > sma2 and sma1_0 <= sma2_0 and y3 > y4 or 
          sma1 < sma2 and sma1_0 >= sma2_0 and y3 < y4
         ) --and cros2 = 0
        then
         cros2 = cros2 + 1
		 pr2 = O(k)
        end      
       end 
      end  -- for k 
      if cros2 <= 1 then 
        per1_1 = i
        per2_1 = j	  
      end  	 	  
 
     end -- for i              
  
      
      if cros0 == 1 and cros == 1 and 
	   ( y1 > y2 and pr0 < pr1 or y1 < y2 and pr0 > pr1 ) 
	   or
	   cros == 1 and cros2 == 1 and 
	   ( y2 > y3 and pr1 < pr2 or y2 < y3 and pr1 > pr2 ) 	   
	  then
	    per1[index] = per1_0
        per2[index] = per2_0
        if per1_0 < per1_1 then 
           per1[index] = per1_1
           per2[index] = per2_1
        else
          if per1_1 < per1_2 then 
            per1[index] = per1_2
            per2[index] = per2_2       
          end 
        end  
      end 
            

      if cros == 1   
	  then

           per1[index] = per1_1
           per2[index] = per2_1
 
      end 	--]]	  
     
    end  
    
    if index-2 > Settings.frm  then             
      sma1 = (sma[index] - sma[index-per1[index]])/per1[index]  
      sma2 = (sma[index] - sma[index-per2[index]])/per2[index]
	  sm1[index] = sma1
      sm2[index] = sma2
    end    
    
  end 
 
 
  SLP = Settings.SLP 	
  -- trades   
   if index == 1 then
    prof = {}
	pos = {}
	SLV = {}
	deal = {}
	dealx = {}
	prof[index] = 0  
	pos[index] = 0  
	SLV[index] = 0
	deal[index] = 0
	dealx[index] = 1

	
  else 	
	prof[index] = prof[index-1] 
	pos[index] = pos[index-1]
	SLV[index] = SLV[index-1]
	deal[index] = deal[index-1]
	dealx[index] = dealx[index-1]
	


    --if indx[index-1] ~= nil and indx[index] ~= nil and ma_[index] ~= nil and ma_[index-1] ~= nil then 
	  if 		
        pos[index] ~= 1 and 
	    ( 
           sm1[index] < sm2[index] and sm1[index-1] >= sm2[index-1]
		)--[[and 
		(
	    deal[index] > O(index) and deal[index]~= 0
		or
		deal[index] == 0
		) --]]
	  then -- long 

		  pos[index] = 1
		  deal[index] = O(index)
		  dealx[index] = index
		  SLV[index] = SLP*O(index)/100 
 
	  end
	  
	
      if 		
        pos[index] ~= -1 and 
		(
		  sm1[index] > sm2[index] and sm1[index-1] <= sm2[index-1]			
		) --[[and 
		(
	    deal[index] < O(index) and deal[index]~= 0
		or
		deal[index] == 0
		)--]]
	  then -- short 

		  pos[index] = -1
		  deal[index] = O(index)
		  dealx[index] = index
		  SLV[index] = SLP*O(index)/100 
 
	  end 	

	--end 
--[[
	  if deal[index] ~= 0 and pos[index] == 1 and 
	    deal[index] - O(index) > SLV[index]--SLP*deal[index]/100
	  then -- cash
		pos[index] = 0
		deal[index] = 0
		dealx[index] = 1
	  else   
		if deal[index] ~= 0 and pos[index] == -1 and 
	      O(index) - deal[index] > SLV[index]--SLP*deal[index]/100
		then -- cash
		  pos[index] = 0    
		  deal[index] = 0
		  dealx[index] = 1
		end 
	  end 	
	--]]
  end 

--[[
	--]]

  if index == Size() then 
   if Settings.sign == 1 then   
    for i = 2, Size() do 
	  if pos[i] ~= nil and pos[i-1] ~= nil then 
	    if pos[i-1] ~= 1 and pos[i] == 1 then
	        SetValue(i, 4, O(i))		  
	    else	  
	     if pos[i-1] ~= -1 and pos[i] == -1 then
	        SetValue(i, 3, O(i))				  
	     else
	       SetValue(i, 3, nil)	
	       SetValue(i, 4, nil)	
	     end 
         if pos[i] ~= pos[i-1] and pos[i] == 0 then  
		   if pos[i-1] == 1 then 
				SetValue(i, 5, O(i))					  
           else 	
				SetValue(i, 6, O(i))				  
           end 			 
         end  
	    end 
	  end 
    end 
   end	
  end  
--[[--]]
	
  -- profit 
  if Settings.prf==1 then  
    if pos[index-1] ~= nil then 
      if pos[index-1] == 1 then  
          prof[index] = prof[index-1] +	(O(index) - O(index-1))	  
	  end 
      if pos[index-1] == -1 then         	  
          prof[index] = prof[index-1] +	(O(index-1) - O(index))
	  end 	
	  if pos[index-1] == 0 then 
	    prof[index] = prof[index-1]
	  end 
	  if pos[index] ~= pos[index-1] then    
        if pos[index] == 0 or pos[index-1] == 0 then    	  
		  prof[index] = prof[index] - Settings.tar*O(index)/100		
		else 
		  prof[index] = prof[index] - 2*Settings.tar*O(index)/100		
		end
	  end	
    end  
  end 	
 
 
  if Settings.prf==1 then  
    return prof[index] --per1[index], per2[index]
  else     
	return sm1[index], sm2[index] 
  end 
 
  
end<br /><br />
MAX: max.ru/join/W9aRBAY0-Q8g_Q-ECN0w6HBBtTh2QB_lBWUWHGHs_24
0 Комментариев

Активные форумы
Что сейчас обсуждают

Старый дизайн
Старый
дизайн