local n_LP = 24 function Init() indicator:name("TimeLines 1.3 "); indicator:description("The indicator draws up to " .. n_LP .. " vertical lines at the specified times"); indicator:requiredSource(core.Bar); indicator:type(core.Indicator); --indicator:setTag("group", "Meini"); for i=1, n_LP, 1 do indicator.parameters:addGroup("Line " .. i ); indicator.parameters:addInteger("day" .. i, "Day", "", 0); indicator.parameters:addIntegerAlternative("day" .. i, "Disabled", "", 0); indicator.parameters:addIntegerAlternative("day" .. i, "Any Day", "", -1); indicator.parameters:addIntegerAlternative("day" .. i, "Monday", "", 2); indicator.parameters:addIntegerAlternative("day" .. i, "Tuesday", "", 3); indicator.parameters:addIntegerAlternative("day" .. i, "Wednesday", "", 4); indicator.parameters:addIntegerAlternative("day" .. i, "Thursday", "", 5); indicator.parameters:addIntegerAlternative("day" .. i, "Friday", "", 6); indicator.parameters:addIntegerAlternative("day" .. i, "Sunday", "", 1); indicator.parameters:addInteger("hour" .. i, "Hour (EST)", "Hour at which to draw line", math.min(-1+i,23), 0, 23); indicator.parameters:addInteger("minute" .. i, "Minutes", "Minute at which to draw line", 0, 0, 59); indicator.parameters:addColor("clr" .. i, "Color", "", core.rgb(0, 127, 0)); indicator.parameters:addInteger("width" .. i, "Width", "", 1, 1, 5); indicator.parameters:addInteger("style" .. i, "Style", "", core.LINE_DOT); indicator.parameters:setFlag("style" .. i, core.FLAG_LEVEL_STYLE); end indicator.parameters:addGroup("Weekly Line"); indicator.parameters:addColor("clrW", "Color", "", core.rgb(0, 127, 0)); indicator.parameters:addInteger("widthW", "Width", "", 5, 1, 5); indicator.parameters:addInteger("styleW", "Style", "", core.LINE_DOT); indicator.parameters:setFlag("styleW", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("Weekly", "Weekly Line", "", true); indicator.parameters:addGroup("Monthly Line"); indicator.parameters:addColor("clrM", "Color", "", core.rgb(0, 127, 0)); indicator.parameters:addInteger("widthM", "Width", "", 5, 1, 5); indicator.parameters:addInteger("styleM", "Style", "", core.LINE_DOT); indicator.parameters:setFlag("styleM", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("Monthly", "Monthly Line", "", true); end local source; local dummy, host; local day = {} --1,day2,day3; local hour = {} --1, hour2, hour3, local clr = {} local width = {} local style = {} --local canwork, error; local minute = {} --1, minute2, minute3; local DSize, WSize, MSize; local Monthly,styleM; local Weekly,styleW; local widthM, widthM,clrM; local widthW, widthW,clrW; local Size,HSize; function Prepare(onlyname) Monthly = instance.parameters.Monthly Weekly = instance.parameters.Weekly source = instance.source; for i=1, n_LP, 1 do day[i] = instance.parameters["day" .. i]; clr[i] = instance.parameters.clr1; width[i] = instance.parameters["width" .. i]; style[i] = instance.parameters["style" .. i]; minute[i] = instance.parameters["minute" .. i]; hour[i] = instance.parameters["hour" .. i]; end styleW = instance.parameters.styleW; styleM = instance.parameters.styleM; widthW = instance.parameters.widthW; widthM = instance.parameters.widthM; clrW = instance.parameters.clrW; clrM = instance.parameters.clrM; host = core.host; local s, e = core.getcandle(source:barSize(), 0, host:execute("getTradingDayOffset"), 0); -- canwork = not(math.floor(e - s) >= 1); local s1, e1 s1, e1 = core.getcandle(source:barSize(), 0, 0, 0); Size = e1-s1; s1, e1 = core.getcandle("D1", 0, 0, 0); DSize = e1-s1; s1, e1 = core.getcandle("W1", 0, 0, 0); WSize = e1-s1; s1, e1 = core.getcandle("M1", 0, 0, 0); MSize = e1-s1; s1, e1 = core.getcandle("H1", 0, 0, 0); HSize = e1-s1; --if not canwork then -- errtext = "The indicator must be applied on intraday charts"; -- if onlyname then -- assert(false, errtext); -- else -- core.host:execute("setStatus", "error:" .. errtext); -- end --end local name; name = profile:id() .. " (" .. source:name() .. ") " for i=1, n_LP, 1 do name = name .. ", (" .. Decode(day[i]) .. "," .. hour[i] .. "," .. minute[i] .. ")" end instance:name(name); if onlyname then return ; end dummy = instance:addStream("D", core.Line, name .. ".D", "D", clr[1], 0); end function Decode(day) if day == 0 then return "No Line"; elseif day ==-1 then return "Any"; elseif day == 2 then return "Monday"; elseif day == 3 then return "Tuesday"; elseif day == 4 then return "Wednesday"; elseif day == 5 then return "Thursday"; elseif day == 6 then return "Friday"; else return "Not Supported"; end end local last_s = nil; local id = 0; function Update(period, mode) -- if not canwork then -- return ; -- end -- calculation restarted if period == 0 then host:execute("removeAll"); id = 0; end local s = source:serial(period); if last_s ~= nil and s == last_s then return ; end last_s = s; local date = source:date(period); local pdate = source:date(period-1); local t = core.dateToTable(date); local p = core.dateToTable(pdate); for i=1, n_LP, 1 do if t.min == minute[i] and t.hour == hour[i] and (t.wday == day[i] or day[i] == -1 ) and day[i] ~= 0 and Size <= HSize then id = id + 1; host:execute("drawLine", id, date, 0, date, 100000, clr[i], style[i], width[i], core.formatDate(host:execute("convertTime", core.TZ_EST, core.TZ_TS, date))); --if day[i] == -1 then -- host:execute("drawLine", n_LP +id, date +DSize, 0, date +DSize, 100000, clr[i], style[i], width[i], core.formatDate(host:execute("convertTime", core.TZ_EST, core.TZ_TS, date))); --else -- host:execute("drawLine", n_LP +id, date +DSize*7, 0, date +DSize*7, 100000, clr[i], style[i], width[i], core.formatDate(host:execute("convertTime", core.TZ_EST, core.TZ_TS, date +DSize*7))); --end end end if t.month ~= p.month and Monthly and Size < MSize then id = id + 1; host:execute("drawLine", id, date, 0, date, 100000, clrM, styleM, widthM ); end if t.wday ~= p.wday and Weekly and t.wday < p.wday and Size < WSize then id = id + 1; host:execute("drawLine", id, date, 0, date, 100000, clrW, styleW, widthW ); end end