function Init() -- Supply & Demand v4 - Updated November 27, 2015 by Todd Desiato, tdesiato@mac.com. -- This indicator was created by Aprentice and posted to fxcodebase.com forum in 2012 as Advanced_Fractal_Support_&_Resistance. -- Ver. v4: Modified to use Clse price of bars, and limits the number of lines drawn on the screen to prevent crashes. Updated fractal routine and fixed line style and width parameters. indicator:name("Supply and Demand Levels For TS2"); indicator:description("Predicts a reversal in the current trend at supply and demand levels."); indicator:requiredSource(core.Bar); indicator:type(core.Indicator); indicator.parameters:addGroup("Calculation"); indicator.parameters:addInteger("Frame", "Fractal Strength", "Number of Candles to the left and right of a Fractal", 5, 1, 99); indicator.parameters:addInteger("Lenght", "Line Length", "", 100, 1, 1000); indicator.parameters:addGroup("Style"); indicator.parameters:addColor("R_color", "Up fractal color", "Up fractal color", core.rgb(0,128,255)); indicator.parameters:addColor("S_color", "Down fractal color", "Down fractal color", core.rgb(255,0,0)); indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 2, 1, 5); indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID); indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE); end local LAST; local source; local buff; local flag={}; local frame=0; local Rez=0; local Size; local id; local ID; local NEW={}; local OLD={}; local LEVEL={}; local Lenght; local FLAG={}; local FIRST={}; local COLOR={}; local COUNT={}; function Prepare() source = instance.source; Size = instance.parameters.Size; Lenght = instance.parameters.Lenght; frame=instance.parameters.Frame; styleLinReg=instance.parameters.styleLinReg; widthLinReg=instance.parameters.widthLinReg; first = source:first(); local name = profile:id() .. " ( " .. frame-1 .. " )"; instance:name(name); if (not (nameOnly)) then buff = instance:addInternalStream(first, 0); end ID =0; id=0; end function Update(period, mode) if period < first then ID =0; id=0; return; end buff[period]=0; local test=0; local i; local count=2*frame + 2; if (period > source:size()- Lenght) then local x = period - count; local curr = source.close[period - count/2]; for i= x, period, 1 do if curr >= source.close[i] and i ~=(period - count) then test=test+1; end end if test == period-x then buff[period - count/2] = source.close[period - count/2]; id=id+1; FIRST[id]= source:date(period - count/2); LEVEL[id]= source.close[period - count/2]; -- Changed high to close COLOR[id]= true; COUNT[id]= 2; FLAG[id]= true; end test=0; curr = source.close[period - count/2]; for i= x , period, 1 do if curr <= source.close[i] and i ~=(period - count) then test=test+1; end end if test == period-x then buff[period - count] = -source.close[period - count/2]; id=id+1; FIRST[id]= source:date(period - count/2); LEVEL[id]= source.close[period - count/2]; -- Changed low to close COLOR[id]= false; COUNT[id]= 2; FLAG[id]= true; end end if period > source:size()- Lenght then DRAW (period-1); end end function DRAW (period) local i; local j = period; for i=1, id, 1 do if FLAG[i] ~= "STOP" then if NEW[i] == nil then NEW[i] = FIRST[i]; end if j > core.findDate (source, FIRST[i], false) then if j == core.findDate (source, FIRST[i], false) +Lenght then FLAG[i] = "STOP"; end if FLAG[i] ~= "STOP" and COUNT[i] >= 0 then if core.crossesOver( source.close, LEVEL[i] , j) then OLD[i] = NEW[i]; NEW[i] = source:date(j); COLOR[i]= true; COUNT[i]= COUNT[i]-1; if COUNT[i] >= 0 then ID =ID+1; core.host:execute("drawLine", ID, OLD[i], LEVEL[i], NEW[i], LEVEL[i], instance.parameters.R_color, styleLinReg, widthLinReg); end FLAG[i]= false; elseif core.crossesUnder( source.close, LEVEL[i] , j) then OLD[i] = NEW[i] NEW[i] = source:date(j); COLOR[i]= false; COUNT[i]= COUNT[i]-1; if COUNT[i] >= 0 then ID =ID+1; core.host:execute("drawLine", ID, OLD[i], LEVEL[i], NEW[i], LEVEL[i], instance.parameters.S_color, styleLinReg, widthLinReg); end FLAG[i]= false; end if FLAG[i] and FLAG[i] ~= "STOP" and COUNT[i] >= 0 then if COLOR[i] then ID =ID+1; core.host:execute("drawLine", ID, FIRST[i], LEVEL[i], source:date( math.min(core.findDate (source, FIRST[i], false) +Lenght, period ) ), LEVEL[i], instance.parameters.R_color, styleLinReg, widthLinReg); else ID =ID+1; core.host:execute("drawLine", ID, FIRST[i], LEVEL[i], source:date( math.min(core.findDate (source, FIRST[i], false) +Lenght, period ) ), LEVEL[i], instance.parameters.S_color, styleLinReg, widthLinReg); end end end end end end end