function Init() strategy:name("Trade profit/loss signal"); strategy:description("Singals when position P/L reaches a certain level."); strategy.parameters:addGroup("Parameters"); strategy.parameters:addDouble("ProfitLevel", "Profit level for signal in pips", "", 2); strategy.parameters:addString("TRD", "Trade", "", ""); strategy.parameters:setFlag("TRD", core.FLAG_TRADE); strategy.parameters:addGroup("Notification"); strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true); strategy.parameters:addBoolean("PlaySound", "Play Sound", "", true); strategy.parameters:addBoolean("RecurSound", "Recurrent Sound", "", true); strategy.parameters:addFile("SoundFile", "Sound File", "", ""); strategy.parameters:setFlag("SoundFile", core.FLAG_SOUND); strategy.parameters:addBoolean("SendEmail", "Send Email", "", true); strategy.parameters:addString("Email", "Email", "", ""); strategy.parameters:setFlag("Email", core.FLAG_EMAIL); end -- Parameters block local gSource = nil; -- the source stream local PlaySound; local RecurrentSound; local SoundFile; local Email; local SendEmail; local LastProfit; local Level; local TRD; local signaled = false; -- strategy instance initialization routine -- Processes strategy parameters and subscribe to price streams -- TODO: Calculate all constants, create instances all necessary indicators and load all required libraries function Prepare(nameOnly) TRD = instance.parameters.TRD; local name = profile:id() .. "(" .. instance.bid:instrument() .. ")"; instance:name(name); if nameOnly then return ; end AllowTicks = instance.parameters.AllowTicks; if (not(AllowTicks)) then assert(instance.parameters.TF ~= "t1", "The strategy cannot be applied on ticks."); end ShowAlert = instance.parameters.ShowAlert; if ShowAlert then PlaySound = instance.parameters.PlaySound; if PlaySound then RecurrentSound = instance.parameters.RecurSound; SoundFile = instance.parameters.SoundFile; else SoundFile = nil; end assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified"); SendEmail = instance.parameters.SendEmail; if SendEmail then Email = instance.parameters.Email; else Email = nil; end assert(not(SendEmail) or (SendEmail and Email ~= ""), "E-mail address must be specified"); end Level = instance.parameters.ProfitLevel; gSource = ExtSubscribe(1, nil, "t1", "Bid", "bar"); ExtSetupSignal(profile:id() .. ":", ShowAlert); ExtSetupSignalMail(name); LastProfit=nil; end function ExtUpdate(id, source, period) if not(signaled) then local trades = core.host:findTable("trades"); local enum = trades:enumerator(); while true do local row = enum:next(); if row == nil then break end if row.TradeID == TRD then if Level >= 0 then if row.PL >= Level and ShowAlert then ExtSignal(source, period, "Profit level crosses (" .. Level .. ")", SoundFile, Email, RecurrentSound); signaled = true; end end if Level < 0 then if row.PL < Level and ShowAlert then ExtSignal(source, period, "Profit level crosses (" .. Level .. ")", SoundFile, Email, RecurrentSound); signaled = true; end end end end end if signaled then core.host:execute("stop"); end end dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");