autotrade
autotrade личный блог
01 января 2024, 01:17

Индикатор EMA со стоплоссом

Индикатор EMA со стоплоссом
Индикатор EMA со стоплоссом

--[[<br />Обсуждаем индикаторы: https://t.me/autotradering
ema template v1
--]]
Settings=              
        {                          
            Name = "EMA_SL_template_v1",   -- indicator name
            ln=100,               -- period	
			prf=0,		
            sign=0,
			sl = 2.0,
            line=                                     
                {                               
                    {  
                        Name = "ema_TEMPL",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    },
					{  
                        Name = "cur3",
                        Type =TYPE_TRIANGLE_DOWN,
                        Width = 5,
                        Color = RGB(255, 0, 0)
                    },
					{  
                        Name = "cur4", 
                        Type =TYPE_TRIANGLE_UP,
                        Width = 5,
                        Color = RGB(0, 0, 255)
                    },
					                    {  
                        Name = "ema_SL_TEMPL",
                        Type =TYPE_LINE,
                        Width = 1,
                        Color = RGB(0,0,255)
                    }
                }
       }


function Init()
  ema = {}  
  prof = {}
  sl = {}  
  pos = {}
  return 4
end


function OnCalculate(index)

  ln = Settings.ln 
  
  prof[index] = 0 

  if index == 1 then 
    sum = {}
    sum[index] = O(index)
    n=1
	pos[index] = 0	
  end 

  
  if index == 1 then 
    ema[index] = O(index)
  else 
    ema[index] = (ema[index-1]*(ln-1) + O(index))/ln
  end   
   
   
	       SetValue(i, 2, nil)	
	       SetValue(i, 3, nil)	
  --[[ --]] 
  if index == Size() then 
   if Settings.sign == 1 then   
    for i = 2, Size() do 
	  if sl[i] ~= nil and sl[i-1] ~= nil then 
	    if O(i) > sl[i] and O(i-1) <= sl[i-1] then
	      SetValue(i, 3, O(i))	
	    else	  
	     if O(i) < sl[i] and O(i-1) >= sl[i-1] then
	       SetValue(i, 2, O(i))		
	     else
	       SetValue(i, 2, nil)	
	       SetValue(i, 3, nil)	
	     end 	  
	    end 
	  end 
    end 
   end	
  end  

  --sl[index] = nil
  if ema[index-1] ~= nil and O(index-1) ~= nil and ema[index-2] ~= nil 
    and O(index-2) ~= nil then  
	
	if sl[index-1] ~= nil then 
	  tval = sl[index-1]
	else 
	  tval = ema[index-1]
	end 
	
	pos[index] = pos[index-1]
    
 	if O(index) > ema[index] and O(index-1) <= tval and ema[index-1] == tval and pos[index] ~= 1 then 
      sl[index] = O(index)*(1-Settings.sl/100)  
	  pos[index] = 1
    else
  --[[--]]  
      if O(index) < ema[index] and O(index-1) >= tval and ema[index-1] == tval and pos[index] ~= -1 then 
        sl[index] = O(index)*(1+Settings.sl/100)
        pos[index] = -1		
      else 	 
	    if sl[index-1] ~= nil then 
          if ema[index] > sl[index-1]*(1 + 2*Settings.sl/100) then 
            sl[index] = ema[index] 
          else 
            if ema[index] < sl[index-1]*(1 - 2*Settings.sl/100) then 
              sl[index] = ema[index]
			else 
			  if sl[index-1] == ema[index-1] then 
			    sl[index] = ema[index]
			  else 
                if O(index) < sl[index-1] and O(index-1) >= sl[index-1] then 
                  sl[index] = O(index)*(1+Settings.sl/100)  
				  pos[index] = -1
                else 
                  if O(index) > sl[index-1] and O(index-1) <= sl[index-1] then 
                    sl[index] = O(index)*(1-Settings.sl/100)
                    pos[index] = 1							
                  else 			  
                    sl[index] = sl[index-1]
				  end 
                end				
			  end 
            end 
          end 		  
	    end 
	  end 
	end 
	
  end 
  
  if Settings.prf==1 then  
  
    --prof[index] = prof[index-1]
    if prof[index-1] ~= nil and sl[index-1] ~= nil and O(index-1) ~= nil then 
      if O(index-1) >= sl[index-1] then 
        prof[index] = prof[index-1] +	O(index) - O(index-1)  
	  end 
      if O(index-1) < sl[index-1] then 
        prof[index] = prof[index-1] +	O(index-1) - O(index)  
	  end 	
    end  
    
	return prof[index], nil, nil, nil
  else   
    return ema[index], nil, nil, sl[index]
  end  
  
end

0 Комментариев

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

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