-- More information about this indicator can be found at:
-- http://fxcodebase.com/code/viewtopic.php?f=17&t=65713

--+------------------------------------------------------------------+
--|                               Copyright © 2018, Gehtsoft USA LLC | 
--|                                            http://fxcodebase.com |
--+------------------------------------------------------------------+
--|                                      Developed by : Mario Jemic  |                    
--|                                          mario.jemic@gmail.com   |
--+------------------------------------------------------------------+
--|                                 Support our efforts by donating  | 
--|                                    Paypal: https://goo.gl/9Rj74e |
--|                    BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF  |  
--|                BitCoin Cash: 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg  | 
--|           Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D  |  
--|                   LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD  |  
--+------------------------------------------------------------------+

-- Indicator profile initialization routine

function Init()
    indicator:name("SimpleGuy Indicator");
    indicator:description("");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Oscillator);
	
	
 
	
	 indicator.parameters:addGroup("Non Lag MA Calculation");
    indicator.parameters:addInteger("Length", "Length", "Length", 9);
    indicator.parameters:addInteger("Filter", "Filter", "Filter", 0);
    indicator.parameters:addInteger("ColorBarBack", "ColorBarBack", "ColorBarBack", 2);
    indicator.parameters:addDouble("Deviation", "Deviation", "Deviation", 0);
	

 
	
	   indicator.parameters:addColor("color2", "2. Line Color", "", core.rgb(255, 0, 0));
	indicator.parameters:addInteger("style2", "Line Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("style2", core.FLAG_LEVEL_STYLE);	
	indicator.parameters:addInteger("width2", "Line Width", "", 3, 1, 5);
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block

local Length, Filter, ColorBarBack, Deviation,Period; 
local EMA, NLMA;
local first;
local source = nil;
local Line1,Line2;

-- Routine
 function Prepare(nameOnly)  


    
	 
	 Length= instance.parameters.Length;
	 Filter= instance.parameters.Filter;
	 ColorBarBack= instance.parameters.ColorBarBack;
	 Deviation= instance.parameters.Deviation;
 
    local name = profile:id() .. "(" ..  instance.source:name()    .. ", " ..  Length .. ", " ..  Filter .. ", " ..  ColorBarBack .. ", " ..  Deviation.. ")";
    instance:name(name); 


    if   (nameOnly) then
        return;
    end

	assert(core.indicators:findIndicator("SSNONLAGMA") ~= nil, "Please, download and install SSNONLAGMA.LUA indicator");
	
   
			
    source = instance.source;
	
	 
	  NLMA = core.indicators:create("SSNONLAGMA",source,Length, Filter, ColorBarBack, Deviation);
	 first = NLMA.DATA:first() ;
    
   
 
	 
    
	Line2 = instance:addStream("Line2" , core.Line, " 2. Line"," 2. Line",instance.parameters.color2, first);
	Line2:setWidth(instance.parameters.width2);
    Line2:setStyle(instance.parameters.style2)
	
	Line2:setPrecision (6);
end

-- Indicator calculation routine
function Update(period, mode)

  
 
   NLMA:update(mode);
   if period < first then
   return;
   end
 
    -- [Close] - [non lag moving average]
    Line2[period]= ( source[period]- NLMA.DATA[period] );
				  
end

