- Code: Select all
`function Init()`

indicator:name("Regression color indicator");

indicator:description("Regression color indicator");

indicator:requiredSource(core.Tick);

indicator:type(core.Indicator);

indicator.parameters:addGroup("Calculation");

indicator.parameters:addInteger("N", "Number of periods", "Number of periods", 14, 2, 1000);

indicator.parameters:addGroup("Style");

indicator.parameters:addColor("clrLinRegUP", "Line color UP", "Line color UP", core.rgb(255, 0, 0));

indicator.parameters:addColor("clrLinRegDN", "Line color DN", "Line color DN", core.rgb(0, 255, 0));

indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 1, 1, 5);

indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID);

indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE);

end

local first;

local source = nil;

local n;

local Regression;

local BuffUP=nil;

local BuffDN=nil;

function Prepare()

source = instance.source;

n = instance.parameters.N;

Regression = core.indicators:create("REGRESSION", source, n);

first = Regression.DATA:first()+2;

local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.N .. ")";

instance:name(name);

BuffUP = instance:addStream("BuffUP", core.Line, name .. ".UP", "UP", instance.parameters.clrLinRegUP, first);

BuffDN = instance:addStream("BuffDN", core.Line, name .. ".DN", "DN", instance.parameters.clrLinRegDN, first);

BuffUP:setWidth(instance.parameters.widthLinReg);

BuffUP:setStyle(instance.parameters.styleLinReg);

BuffDN:setWidth(instance.parameters.widthLinReg);

BuffDN:setStyle(instance.parameters.styleLinReg);

end

function Update(period, mode)

Regression:update(mode);

if (period>first) then

if Regression.DATA[period]>Regression.DATA[period-1] then

BuffUP[period]=Regression.DATA[period];

if Regression.DATA[period-1]<Regression.DATA[period-2] then

BuffUP[period-1]=Regression.DATA[period-1];

end

else

BuffDN[period]=Regression.DATA[period];

if Regression.DATA[period-1]>Regression.DATA[period-2] then

BuffDN[period-1]=Regression.DATA[period-1];

end

end

end

end

Single Stream version.