-- More information about this indicator can be found at: -- http://fxcodebase.com --+------------------------------------------------------------------+ --| 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 | --| Patreon : https://goo.gl/GdXWeN | --| BitCoin : 15VCJTLaz12Amr7adHSBtL9v8XomURo9RF | --| BitCoin Cash : 1BEtS465S3Su438Kc58h2sqvVvHK9Mijtg | --| Ethereum : 0x8C110cD61538fb6d7A2B47858F0c0AaBd663068D | --| LiteCoin : LLU8PSY2vsq7B9kRELLZQcKf5nJQrdeqwD | --+------------------------------------------------------------------+ local Modules = {}; function Init() indicator:name("Trend Confirmation") indicator:description("v2") indicator:requiredSource(core.Bar) indicator:type(core.Indicator) local Period = {5, 10, 20, 50, 100, 200} for i = 1, 6, 1 do Add(i .. "_1", Period[i]) Add(i .. "_2", Period[i] * 2) end indicator.parameters:addString("Y", " Y Placement", "", "Top") indicator.parameters:addStringAlternative("Y", "Top", "Top", "Top") indicator.parameters:addStringAlternative("Y", "Bottom", "Bottom", "Bottom") indicator.parameters:addString("X", " X Placement", "", "Left") indicator.parameters:addStringAlternative("X", "Right", "Right", "Right") indicator.parameters:addStringAlternative("X", "Left", "Left", "Left") indicator.parameters:addInteger("ShiftY", "Shift", "", 0) indicator.parameters:addGroup("Style") indicator.parameters:addColor("LabelColor", "Label Color", "", core.rgb(0, 0, 0)) indicator.parameters:addInteger("Size", "Font Size", "", 20) indicator.parameters:addGroup("Alert Parameters") indicator.parameters:addBoolean("On", "Use Alert", "", true) indicator.parameters:addBoolean("On5_1", "Use Alert for 5:1", "", true) indicator.parameters:addBoolean("On3_3", "Use Alert for 3:3", "", true) indicator.parameters:addString("Live", "Execution", "", "Live") indicator.parameters:addStringAlternative("Live", "End of Turn", "", "End of Turn") indicator.parameters:addStringAlternative("Live", "Live", "", "Live") signaler:Init(indicator.parameters); end function Add(id, Period) indicator.parameters:addGroup(id .. ". MA Calculation") indicator.parameters:addString("Price" .. id, "Price Source", "", "close") indicator.parameters:addStringAlternative("Price" .. id, "OPEN", "", "open") indicator.parameters:addStringAlternative("Price" .. id, "HIGH", "", "high") indicator.parameters:addStringAlternative("Price" .. id, "LOW", "", "low") indicator.parameters:addStringAlternative("Price" .. id, "CLOSE", "", "close") indicator.parameters:addStringAlternative("Price" .. id, "MEDIAN", "", "median") indicator.parameters:addStringAlternative("Price" .. id, "TYPICAL", "", "typical") indicator.parameters:addStringAlternative("Price" .. id, "WEIGHTED", "", "weighted") indicator.parameters:addInteger("Period" .. id, "Period", "", Period) indicator.parameters:addString("Method" .. id, "MA Method", "Method", "MVA") indicator.parameters:addStringAlternative("Method" .. id, "MVA", "MVA", "MVA") indicator.parameters:addStringAlternative("Method" .. id, "EMA", "EMA", "EMA") indicator.parameters:addStringAlternative("Method" .. id, "LWMA", "LWMA", "LWMA") indicator.parameters:addStringAlternative("Method" .. id, "TMA", "TMA", "TMA") indicator.parameters:addStringAlternative("Method" .. id, "SMMA", "SMMA", "SMMA") indicator.parameters:addStringAlternative("Method" .. id, "KAMA", "KAMA", "KAMA") indicator.parameters:addStringAlternative("Method" .. id, "VIDYA", "VIDYA", "VIDYA") indicator.parameters:addStringAlternative("Method" .. id, "WMA", "WMA", "WMA") end local LastSignal local OnlyOnceFlag = true local FIRST = true local Up, Down, On local On5_1; local On3_3; local first local source = nil local X, Y local font local LabelColor local Size local ShiftY local Signal = {0, 0, 0, 0, 0, 0} local S, B local MA = {} local MA_2 = {} -- Routine function Prepare(nameOnly) for _, module in pairs(Modules) do module:Prepare(nameOnly); end local name = profile:id() .. "(" .. instance.source:name() .. ")" instance:name(name) if (nameOnly) then return end LastSignal = nil Live = instance.parameters.Live Initialization() Signal = {0, 0, 0, 0, 0, 0} S = 0 B = 0 Y = instance.parameters.Y X = instance.parameters.X ShiftY = instance.parameters.ShiftY LabelColor = instance.parameters.LabelColor Size = instance.parameters.Size source = instance.source first = source:first() for i = 1, 6, 1 do local id = i .. "_1"; MA[i] = core.indicators:create( instance.parameters:getString("Method" .. id), source[instance.parameters:getString("Price" .. id)], instance.parameters:getInteger("Period" .. id) ) id = i .. "_2"; MA_2[i] = core.indicators:create( instance.parameters:getString("Method" .. id), source[instance.parameters:getString("Price" .. id)], instance.parameters:getInteger("Period" .. id) ) first = math.max(first, MA[i].DATA:first()) end core.host:execute("setTimer", 1, 1) instance:ownerDrawn(true) end function Initialization() On = instance.parameters.On On5_1 = instance.parameters.On5_1; On3_3 = instance.parameters.On3_3; end -- Indicator calculation routine -- TODO: Add your code for calculation output values function Update(period) for _, module in pairs(Modules) do if module.ExtUpdate ~= nil then module:ExtUpdate(1, source, period); end end end local init = false local SIGNAL_BUY = 1; local SIGNAL_SELL = -1; local SIGNAL_NEUTRAL = 0; function Draw(stage, context) if stage ~= 2 then return end if not init then context:createFont(1, "Arial", context:pointsToPixels(Size), context:pointsToPixels(Size), 0) init = true end local Text1 = "XXXXXXXX" width, height = context:measureText(1, Text1, 0) local X = {1, 2, 3, 1, 2, 3} local Y = {1, 1, 1, 2, 2, 2} for i = 1, 6, 1 do if Signal[i] == SIGNAL_BUY then LabelText = "Buy" elseif Signal[i] == SIGNAL_SELL then LabelText = "Sell" else LabelText = "Neutral" end context:drawText(1, LabelText, LabelColor, -1, iX(context, width, X[i], 1), iY(context, height, Y[i], 0), iX(context, width, X[i], 2), iY(context, height, Y[i], 1), 0) end local x = 1 local y = 3 context:drawText(1, "Buy :", LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) local x = 2 local y = 3 context:drawText(1, B, LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) local x = 1 local y = 4 context:drawText(1, "Sell :", LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) local x = 2 local y = 4 context:drawText(1, S, LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) local x = 1 local y = 5 context:drawText(1, "Summary :", LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) local x = 2 local y = 5 if B > S then LabelText = "Buy" elseif B < S then LabelText = "Sell" else LabelText = "Neutral" end context:drawText(1, LabelText, LabelColor, -1, iX(context, width, x, 1), iY(context, height, y, 0), iX(context, width, x, 2), iY(context, height, y, 1), 0) end local last_signal_date; -- the function is called when the async operation is finished function AsyncOperationFinished(cookie, success, message, message1, message2) for _, module in pairs(Modules) do if module.AsyncOperationFinished ~= nil then module:AsyncOperationFinished(cookie, success, message, message1, message2); end end if cookie == 1 then B = 0 S = 0 if Live ~= "Live" then Shift = 1 else Shift = 0 end for i = 1, 6, 1 do MA[i]:update(core.UpdateLast) MA_2[i]:update(core.UpdateLast); if MA[i].DATA:hasData(NOW - Shift) then if MA_2[i].DATA[NOW - Shift] > MA[i].DATA[NOW - Shift] then Signal[i] = SIGNAL_BUY; B = B + 1 elseif MA_2[i].DATA[NOW - Shift] < MA[i].DATA[NOW - Shift] then Signal[i] = SIGNAL_SELL; S = S + 1 else Signal[i] = SIGNAL_NEUTRAL; end end end if On then if LastSignal == nil then if B > S then LastSignal = 1 elseif B < S then LastSignal = -1 end else if B == 6 and LastSignal ~= 2 and last_signal_date ~= source:date(NOW) then LastSignal = 2 last_signal_date = source:date(NOW); signaler:Signal("Strong Up Trend", source, true); elseif S == 6 and LastSignal ~= -2 and last_signal_date ~= source:date(NOW) then LastSignal = -2 last_signal_date = source:date(NOW); signaler:Signal("Strong Down Trend", source, false); elseif B > S and LastSignal ~= 1 and B ~= 6 and last_signal_date ~= source:date(NOW) then LastSignal = 1 last_signal_date = source:date(NOW); signaler:Signal("Up Trend", source, true); elseif B < S and LastSignal ~= -1 and S ~= 6 and last_signal_date ~= source:date(NOW) then LastSignal = -1 last_signal_date = source:date(NOW); signaler:Signal("Down Trend", source, false); end end end if On5_1 then if B == 5 and S == 1 and LastSignal ~= 3 and last_signal_date ~= source:date(NOW) then LastSignal = 3 last_signal_date = source:date(NOW); signaler:Signal("B-S 5:1 ratio", source, true); elseif S == 5 and B == 1 and LastSignal ~= -3 and last_signal_date ~= source:date(NOW) then LastSignal = -3 last_signal_date = source:date(NOW); signaler:Signal("B-S 1:5 ratio", source, false); end end if On3_3 then if B == 3 and S == 3 and LastSignal ~= 4 and last_signal_date ~= source:date(NOW) then LastSignal = 4 last_signal_date = source:date(NOW); signaler:Signal("B-S 3:3 ratio", source, true); end end instance:updateFrom(0) return core.ASYNC_REDRAW end end function iX(context, width, Shift, x) if X == "Left" then return context:left() + Shift * width + width * (x - 1) else return context:right() - width * Shift - width * (1 - (x - 1)) end end function iY(context, height, Index, Line) if Y == "Top" then return context:top() + Index * height + ShiftY * height + Line * height else if Line == 1 then return context:bottom() - (Index + 1) * height - ShiftY * height + height else return context:bottom() - (Index + 1) * height - ShiftY * height end end end signaler = {}; signaler.Name = "Signaler"; signaler.Debug = false; signaler.Version = "1.5"; signaler._show_alert = nil; signaler._sound_file = nil; signaler._recurrent_sound = nil; signaler._email = nil; signaler._ids_start = nil; signaler._advanced_alert_timer = nil; signaler._tz = nil; signaler._alerts = {}; function signaler:trace(str) if not self.Debug then return; end core.host:trace(self.Name .. ": " .. str); end function signaler:OnNewModule(module) end function signaler:RegisterModule(modules) for _, module in pairs(modules) do self:OnNewModule(module); module:OnNewModule(self); end modules[#modules + 1] = self; self._ids_start = (#modules) * 100; end function signaler:ToJSON(item) local json = {}; function json:AddStr(name, value) local separator = ""; if self.str ~= nil then separator = ","; else self.str = ""; end self.str = self.str .. string.format("%s\"%s\":\"%s\"", separator, tostring(name), tostring(value)); end function json:AddNumber(name, value) local separator = ""; if self.str ~= nil then separator = ","; else self.str = ""; end self.str = self.str .. string.format("%s\"%s\":%f", separator, tostring(name), value or 0); end function json:AddBool(name, value) local separator = ""; if self.str ~= nil then separator = ","; else self.str = ""; end self.str = self.str .. string.format("%s\"%s\":%s", separator, tostring(name), value and "true" or "false"); end function json:ToString() return "{" .. (self.str or "") .. "}"; end local first = true; for idx,t in pairs(item) do local stype = type(t) if stype == "number" then json:AddNumber(idx, t); elseif stype == "string" then json:AddStr(idx, t); elseif stype == "boolean" then json:AddBool(idx, t); elseif stype == "function" or stype == "table" then --do nothing else core.host:trace(tostring(idx) .. " " .. tostring(stype)); end end return json:ToString(); end function signaler:ArrayToJSON(arr) local str = "["; for i, t in ipairs(self._alerts) do local json = self:ToJSON(t); if str == "[" then str = str .. json; else str = str .. "," .. json; end end return str .. "]"; end function signaler:AsyncOperationFinished(cookie, success, message, message1, message2) if cookie == self._advanced_alert_timer and #self._alerts > 0 and (self.last_req == nil or not self.last_req:loading()) then if self._advanced_alert_key == nil then return; end local data = self:ArrayToJSON(self._alerts); self._alerts = {}; self.last_req = http_lua.createRequest(); local query = string.format('{"Key":"%s","StrategyName":"%s","Platform":"FXTS2","Notifications":%s}', self._advanced_alert_key, string.gsub(self.StrategyName or "", '"', '\\"'), data); self.last_req:setRequestHeader("Content-Type", "application/json"); self.last_req:setRequestHeader("Content-Length", tostring(string.len(query))); self.last_req:start("http://profitrobots.com/api/v1/notification", "POST", query); end end function signaler:FormatEmail(source, period, message) --format email subject local subject = message .. "(" .. source:instrument() .. ")"; --format email text local delim = "\013\010"; local signalDescr = "Signal: " .. (self.StrategyName or ""); local symbolDescr = "Symbol: " .. source:instrument(); local messageDescr = "Message: " .. message; local ttime = core.dateToTable(core.host:execute("convertTime", core.TZ_EST, self._ToTime, source:date(period))); local dateDescr = string.format("Time: %02i/%02i %02i:%02i", ttime.month, ttime.day, ttime.hour, ttime.min); local priceDescr = "Price: " .. source[period]; local text = "You have received this message because the following signal alert was received:" .. delim .. signalDescr .. delim .. symbolDescr .. delim .. messageDescr .. delim .. dateDescr .. delim .. priceDescr; return subject, text; end function signaler:Signal(message, source) if source == nil then if instance.source ~= nil then source = instance.source; elseif instance.bid ~= nil then source = instance.bid; else local pane = core.host.Window.CurrentPane; source = pane.Data:getStream(0); end end if self._show_alert then terminal:alertMessage(source:instrument(), source[NOW], message, source:date(NOW)); end if self._sound_file ~= nil then terminal:alertSound(self._sound_file, self._recurrent_sound); end if self._email ~= nil then terminal:alertEmail(self._email, profile:id().. " : " .. message, self:FormatEmail(source, NOW, message)); end if self._advanced_alert_key ~= nil then self:AlertTelegram(message, source:instrument(), source:barSize()); end if self._signaler_debug_alert then core.host:trace(message); end if self._show_popup then local subject, text = self:FormatEmail(source, NOW, message); core.host:execute("prompt", self._ids_start + 2, subject, text); end if self._dde_alerts then dde_server:set(self.dde_topic, self.dde_alerts, message); end end function signaler:AlertTelegram(message, instrument, timeframe) if core.host.Trading:getTradingProperty("isSimulation") then return; end local alert = {}; alert.Text = message or ""; alert.Instrument = instrument or ""; alert.TimeFrame = timeframe or ""; self._alerts[#self._alerts + 1] = alert; end function signaler:Init(parameters) parameters:addInteger("signaler_ToTime", "Convert the date to", "", 6) parameters:addIntegerAlternative("signaler_ToTime", "EST", "", 1) parameters:addIntegerAlternative("signaler_ToTime", "UTC", "", 2) parameters:addIntegerAlternative("signaler_ToTime", "Local", "", 3) parameters:addIntegerAlternative("signaler_ToTime", "Server", "", 4) parameters:addIntegerAlternative("signaler_ToTime", "Financial", "", 5) parameters:addIntegerAlternative("signaler_ToTime", "Display", "", 6) parameters:addBoolean("signaler_show_alert", "Show Alert", "", true); parameters:addBoolean("signaler_play_sound", "Play Sound", "", false); parameters:addFile("signaler_sound_file", "Sound File", "", ""); parameters:setFlag("signaler_sound_file", core.FLAG_SOUND); parameters:addBoolean("signaler_recurrent_sound", "Recurrent Sound", "", true); parameters:addBoolean("signaler_send_email", "Send Email", "", false); parameters:addString("signaler_email", "Email", "", ""); parameters:setFlag("signaler_email", core.FLAG_EMAIL); if indicator ~= nil and strategy == nil then parameters:addBoolean("signaler_show_popup", "Show Popup", "", false); end parameters:addBoolean("signaler_debug_alert", "Print Into Log", "", false); parameters:addBoolean("use_advanced_alert", "Send Advanced Alert", "Telegram/Discord/other platform (like MT4)", false) parameters:addString("advanced_alert_key", "Advanced Alert Key", "You can get a key via @profit_robots_bot Telegram Bot. Visit ProfitRobots.com for discord/other platform keys", "") if DDEAlertsSupport then parameters:addBoolean("signaler_dde_export", "DDE Export", "You can export the alert into the Excel or any other application with DDE support (=Service Name|DDE Topic!Alerts)", false); parameters:addString("signaler_dde_service", "Service Name", "The service name must be unique amoung all running instances of the strategy", "TS2ALERTS"); parameters:addString("signaler_dde_topic", "DDE Topic", "", ""); end end function signaler:Prepare(name_only) self._ToTime = instance.parameters.signaler_ToTime if self._ToTime == 1 then self._ToTime = core.TZ_EST elseif self._ToTime == 2 then self._ToTime = core.TZ_UTC elseif self._ToTime == 3 then self._ToTime = core.TZ_LOCAL elseif self._ToTime == 4 then self._ToTime = core.TZ_SERVER elseif self._ToTime == 5 then self._ToTime = core.TZ_FINANCIAL elseif self._ToTime == 6 then self._ToTime = core.TZ_TS end self._dde_alerts = instance.parameters.signaler_dde_export; if self._dde_alerts then assert(instance.parameters.signaler_dde_topic ~= "", "You need to specify the DDE topic"); require("ddeserver_lua"); self.dde_server = ddeserver_lua.new(instance.parameters.signaler_dde_service); self.dde_topic = self.dde_server:addTopic(instance.parameters.signaler_dde_topic); self.dde_alerts = self.dde_server:addValue(self.dde_topic, "Alerts"); end if instance.parameters.signaler_play_sound then self._sound_file = instance.parameters.signaler_sound_file; assert(self._sound_file ~= "", "Sound file must be chosen"); end self._show_alert = instance.parameters.signaler_show_alert; self._recurrent_sound = instance.parameters.signaler_recurrent_sound; self._show_popup = instance.parameters.signaler_show_popup; self._signaler_debug_alert = instance.parameters.signaler_debug_alert; if instance.parameters.signaler_send_email then self._email = instance.parameters.signaler_email; assert(self._email ~= "", "E-mail address must be specified"); end --do what you usually do in prepare if name_only then return; end if instance.parameters.advanced_alert_key ~= "" and instance.parameters.use_advanced_alert then self._advanced_alert_key = instance.parameters.advanced_alert_key; require("http_lua"); self._advanced_alert_timer = self._ids_start + 1; core.host:execute("setTimer", self._advanced_alert_timer, 1); end end function signaler:ReleaseInstance() if self.dde_server ~= nil then self.dde_server:close(); end end signaler:RegisterModule(Modules);