### Fractal-based Support/Resistance Lines

Posted:

**Wed Feb 17, 2010 5:30 pm**The simple indicator which starts a new support/resistance line every time when up or down fractal appears.

Download:

FBSR with Fibonacci levels

- Code: Select all
`-- Indicator profile initialization routine`

-- Defines indicator profile properties and indicator parameters

function Init()

indicator:name("Fractal Based Support/Resistance lines");

indicator:description("");

indicator:requiredSource(core.Bar);

indicator:type(core.Indicator);

indicator.parameters:addColor("R_color", "Color of R", "Color of R", core.rgb(255, 192, 0));

indicator.parameters:addColor("S_color", "Color of S", "Color of S", core.rgb(0, 192, 255));

end

-- Indicator instance initialization routine

-- Processes indicator parameters and creates output streams

-- Parameters block

local first;

local source = nil;

-- Streams block

local R = nil;

local S = nil;

-- Routine

function Prepare()

source = instance.source;

first = source:first() + 4;

local name = profile:id() .. "(" .. source:name() .. ")";

instance:name(name);

R = instance:addStream("R", core.Dot, name .. ".R", "R", instance.parameters.R_color, first);

S = instance:addStream("S", core.Dot, name .. ".S", "S", instance.parameters.S_color, first);

end

-- Indicator calculation routine

function Update(period)

if period >= first and source:hasData(period) then

-- defect fractals on two periods ago

local up, down, curr;

up = false;

down = false;

curr = source.high[period - 2];

if (curr >= source.high[period - 4] and curr >= source.high[period - 3] and

curr >= source.high[period - 1] and curr >= source.high[period]) then

up = true;

end

if up then

R[period - 2] = curr;

R[period - 1] = curr;

R[period] = curr;

else

if R:hasData(period - 1) then

R[period] = R[period - 1];

end

end

curr = source.low[period - 2];

if (curr <= source.low[period - 4] and curr <= source.low[period - 3] and

curr <= source.low[period - 1] and curr <= source.low[period]) then

down = true;

end

if down then

S[period - 2] = curr;

S[period - 1] = curr;

S[period] = curr;

else

if S:hasData(period - 1) then

S[period] = S[period - 1];

end

end

end

end

Download:

FBSR with Fibonacci levels