-- initializes the indicator function Init() indicator:name("Gator"); indicator:description("") indicator:requiredSource(core.Tick); indicator:type(core.Oscillator); indicator.parameters:addInteger("JawN", "Number of periods for smoothing the alligator jaw", "", 13, 2, 1000); indicator.parameters:addInteger("JawS", "Number of periods for shifting the alligator jaw", "", 8, 1, 100); indicator.parameters:addInteger("TeethN", "Number of periods for smoothing the alligator teeth", "", 8, 2, 1000); indicator.parameters:addInteger("TeethS", "Number of periods for shifting the alligator teeth", "", 5, 1, 100); indicator.parameters:addInteger("LipsN", "Number of periods for smoothing the alligator lips", "", 5, 2, 1000); indicator.parameters:addInteger("LipsS", "Number of periods for shifting the alligator lips", "", 3, 1, 100); indicator.parameters:addString("MTH", "Alligator Smoothing method", "", "MVA"); indicator.parameters:addStringAlternative("MTH", "MVA", "", "MVA"); indicator.parameters:addStringAlternative("MTH", "EMA", "", "EMA"); indicator.parameters:addStringAlternative("MTH", "LWMA", "", "LWMA"); indicator.parameters:addColor("CL_color", "Color for covering line", "Color for covering line", core.rgb(255, 255, 255)); indicator.parameters:addColor("GO_color", "Color for higher bars", "Color for higher bars", core.rgb(0, 255, 0)); indicator.parameters:addColor("RO_color", "Color for lower bars", "Color for lower bars", core.rgb(255, 0, 0)); end -- lines parameters local JawN, JawS; local TeethN, TeethS; local LipsN, LipsC; -- indicator source local source; -- alligator lines local Jaw, Teeth, Lips; -- alligator lines sources local JawSrc, TeethSrc, LipsSrc; -- gator bars local Up, Down, UpRed, UpGreen, DownRed, DownGreen; -- bar's parameters local UpExtent, DownExtent; local UpFirst, DownFirst; -- process parameters and prepare for calculations function Prepare() JawN = instance.parameters.JawN; JawS = instance.parameters.JawS; TeethN = instance.parameters.TeethN; TeethS = instance.parameters.TeethS; LipsN = instance.parameters.LipsN; LipsS = instance.parameters.LipsS; source = instance.source; JawSrc = core.indicators:create(instance.parameters.MTH, source, JawN, core.rgb(0, 0, 0)); TeethSrc = core.indicators:create(instance.parameters.MTH, source, TeethN, core.rgb(0, 0, 0)); LipsSrc = core.indicators:create(instance.parameters.MTH, source, LipsN, core.rgb(0, 0, 0)); local name = profile:id() .. "(" .. source:name() .. ", " .. JawN .. "(" .. JawS .. ")," .. TeethN .. "(" .. TeethS .. ")," .. LipsN .. "(" .. LipsS .. "))"; instance:name(name); Jaw = instance:addInternalStream(JawSrc.DATA:first() + JawS, JawS); Teeth = instance:addInternalStream(TeethSrc.DATA:first() + TeethS, TeethS); Lips = instance:addInternalStream(LipsSrc.DATA:first() + LipsS, LipsS); UpExtent = math.min(JawS, TeethS); UpFirst = math.max(Jaw:first(), Teeth:first()); Up = instance:addStream("UP", core.Line, name .. ".UP", "UP", instance.parameters.CL_color, UpFirst, UpExtent); Up:addLevel(0); UpRed = instance:addStream("UPR", core.Bar, name .. ".UPR", "UPR", instance.parameters.RO_color, UpFirst, UpExtent); UpGreen = instance:addStream("UPG", core.Bar, name .. ".UPG", "UPG", instance.parameters.GO_color, UpFirst, UpExtent); DownExtent = math.min(TeethS, LipsS); DownFirst = math.max(Teeth:first(), Lips:first()); Down = instance:addStream("DOWN", core.Line, name .. ".DN", "DN", instance.parameters.CL_color, DownFirst, DownExtent); DownRed = instance:addStream("DNR", core.Bar, name .. ".DNR", "DNR", instance.parameters.RO_color, DownFirst, DownExtent); DownGreen = instance:addStream("DNG", core.Bar, name .. ".DNG", "DNG", instance.parameters.GO_color, DownFirst, DownExtent); end -- Indicator calculation routine function Update(period, mode) -- calculate alligator JawSrc:update(mode); TeethSrc:update(mode); LipsSrc:update(mode); if (period + JawS >= 0 and period >= JawSrc.DATA:first()) then Jaw[period + JawS] = JawSrc.DATA[period]; end if (period + TeethS >= 0 and period >= TeethSrc.DATA:first()) then Teeth[period + TeethS] = TeethSrc.DATA[period]; end if (period + LipsS >= 0 and period >= LipsSrc.DATA:first()) then Lips[period + LipsS] = LipsSrc.DATA[period]; end -- calculate gator if period >= UpFirst then Up[period + UpExtent] = math.abs(Jaw[period + UpExtent] - Teeth[period + UpExtent]); if period >= UpFirst + 1 then if Up[period + UpExtent] >= Up[period + UpExtent - 1] then UpGreen[period + UpExtent] = Up[period + UpExtent]; else UpRed[period + UpExtent] = Up[period + UpExtent]; end end end if period >= DownFirst then Down[period + DownExtent] = -math.abs(Teeth[period + DownExtent] - Lips[period + DownExtent]); if period >= DownFirst + 1 then if Down[period + DownExtent] >= Down[period + DownExtent - 1] then DownGreen[period + DownExtent] = Down[period + DownExtent]; else DownRed[period + DownExtent] = Down[period + DownExtent]; end end end end