--Id:13207 -- More information about this indicator can be found at: -- http://fxcodebase.com/code/viewtopic.php?f=28&t=61403 --+------------------------------------------------------------------+ --| 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 | --+------------------------------------------------------------------+ --[[Time Frame Averaging can have up to 5 Indicators drawn, each for a different time frame. Average value of all selected Indicators is also drawn.]] function Add(id, TF) indicator.parameters:addGroup(id.. ". Pair" ); indicator.parameters:addBoolean("On"..id , "Show This Time Frame", "", true); indicator.parameters:addString("TF" .. id, id.. ". Time Frame", "", TF); indicator.parameters:setFlag("TF" .. id, core.FLAG_PERIODS ); end function Init() indicator:name("Time Frame Averaging"); indicator:description(""); indicator:requiredSource(core.Bar); indicator:type(core.Oscillator); indicator.parameters:addGroup("Calculation" ); indicator.parameters:addInteger("Period","Period", "", 14); Add(1,"H6"); Add(2,"H8"); Add(3,"D1"); Add(4,"W1"); Add(5, "M1"); indicator.parameters:addGroup("Style"); indicator.parameters:addColor("LineColor", "Line Color", "", core.rgb(0, 255, 0)); indicator.parameters:addInteger("width", "Width", "", 1, 1, 5); indicator.parameters:addInteger("style", "Style", "", core.LINE_SOLID); indicator.parameters:setFlag("style", core.FLAG_LEVEL_STYLE); indicator.parameters:addGroup("Levels Style" ); indicator.parameters:addInteger("overbought", "Strong Level", "", 20, 0, 100); indicator.parameters:addInteger("oversold", "Weak Level", "", 15, 0, 100); indicator.parameters:addInteger("level_overboughtsold_width", "Width", "", 5, 1, 5); indicator.parameters:addInteger("level_overboughtsold_style", "Style", "", core.LINE_SOLID); indicator.parameters:setFlag("level_overboughtsold_style", core.FLAG_LEVEL_STYLE); indicator.parameters:addColor("level_overboughtsold_color", "Color", "", core.rgb(255, 0, 0)); indicator.parameters:addGroup("Components" ); indicator.parameters:addBoolean("Show", "Show Components", "" , true); indicator.parameters:addColor("Color1", "1. Component Color", "", core.rgb(128, 128, 128)); indicator.parameters:addColor("Color2", "2. Component Color", "", core.rgb(128, 128, 128)); indicator.parameters:addColor("Color3", "3. Component Color", "", core.rgb(128, 128, 128)); indicator.parameters:addColor("Color4", "4. Component Color", "", core.rgb(128, 128, 128)); indicator.parameters:addColor("Color5", "5. Component Color", "", core.rgb(128, 128, 128)); end -- list of streams local Show; local source; local day_offset, week_offset; local Source={}; local Period; local TF={}; local Indicator ={}; local Out; local In={}; local Number; local loading={}; local Color={}; function Prepare(nameOnly) local name = profile:id() .. "(" .. instance.source:name() .. ")"; instance:name(name); if (nameOnly) then return; end Show=instance.parameters.Show; source = instance.source; host = core.host; Period=instance.parameters.Period; day_offset = host:execute("getTradingDayOffset"); week_offset = host:execute("getTradingWeekOffset"); local i; Number=0; local s1, e1, s2, e2; s1, e1 = core.getcandle(source:barSize(), 0, 0, 0); for i = 1, 5, 1 do s2, e2 = core.getcandle(instance.parameters:getString("TF" .. i), 0, 0, 0); if instance.parameters:getBoolean("On" .. i) and (e1 - s1) <= (e2 - s2) then Number=Number+1; Color[Number]= instance.parameters:getColor("Color" .. i); TF[Number]= instance.parameters:getString("TF" .. i); end end Out = instance:addStream("CRS", core.Line, name , "CRS", instance.parameters.LineColor, source:first()); Out:setWidth(instance.parameters.width); Out:setStyle(instance.parameters.style); Out:addLevel(0); Out:addLevel(instance.parameters.oversold, instance.parameters.level_overboughtsold_style, instance.parameters.level_overboughtsold_width, instance.parameters.level_overboughtsold_color); Out:addLevel(instance.parameters.overbought, instance.parameters.level_overboughtsold_style, instance.parameters.level_overboughtsold_width, instance.parameters.level_overboughtsold_color); Out:setPrecision(math.max(2, instance.source:getPrecision())); local id=0; for i = 1, Number, 1 do id=id+1; if Show then In[i]= instance:addStream("CRS".. i, core.Line, name , TF[i], Color[i], source:first()); In[i]:setPrecision(math.max(2, instance.source:getPrecision())); else In[i]= instance:addInternalStream(0, 0); end Source[i] = core.host:execute("getSyncHistory",source:instrument(), TF[i], source:isBid(), Period, 200+id, 100+id); loading[i]= true; Indicator[i] = core.indicators:create("ADX", Source[i], Period); end end function Update(period, mode) if period < first then return; end local Flag = true; for j = 1, Number, 1 do if loading[j] then Flag=false; end end if not Flag then return; end local i, p; local Sum = 0; local Count=0; for i = 1, Number, 1 do Indicator[i]:update(mode); p= Initialization(period,i); if p~= false then if Indicator[i].DATA:hasData(p) and p < (source:size()-1) and p > (source:first()) then In[i][period] = Indicator[i].DATA[p]; Sum= Sum+ Indicator[i].DATA[p]; Count=Count+1; Out[period] = Sum /Count; end end end end function Initialization(period,id) local Candle; Candle = core.getcandle(source:barSize(), source:date(period), day_offset, week_offset); if loading[id] or Source[id]:size() == 0 then return false; end if period < source:first() then return false; end local P = core.findDate(Source[id], Candle, false); -- candle is not found if P < 0 then return false; else return P; end end -- the function is called when the async operation is finished function AsyncOperationFinished(cookie) local j; local Flag = true; local Count=0; local id=0; for j = 1, Number, 1 do id=id+1; if cookie == (100+id) then loading[j] = true; elseif cookie == (200+id) then loading[j] = false; end if loading[j] then Count=Count+1; Flag=false; end end if Flag then core.host:execute ("setStatus", " "); instance:updateFrom(0); else core.host:execute ("setStatus", " Loading ".. (Number-Count) .."/" .. Number); end return core.ASYNC_REDRAW ; end