-- More information about this indicator can be found at: -- http://fxcodebase.com/code/viewtopic.php?f=29&t=1864 -- Id: 1282 -- More information about this indicator can be found at: -- http://fxcodebase.com/ --+------------------------------------------------------------------+ --| Copyright © 2019, Gehtsoft USA LLC | --| http://fxcodebase.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 | --+------------------------------------------------------------------+ function Init() strategy:name("Stochastic Overbought/Oversold signal"); strategy:setTag("NonOptimizableParameters", "Email,SendEmail,SoundFile,RecurrentSound,PlaySound,ShowAlert"); strategy:description(""); strategy.parameters:addGroup("Parameters"); strategy.parameters:addString("Period", "Timeframe", "", "m5"); strategy.parameters:setFlag("Period", core.FLAG_PERIODS); strategy.parameters:addInteger("K", "K", "Parameter of stochastic", 5); strategy.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3); strategy.parameters:addInteger("D", "D", "Parameter of stochastic", 3); strategy.parameters:addInteger("DownLevel", "DownLevel", "", 20); strategy.parameters:addInteger("UpLevel", "UpLevel", "", 80); strategy.parameters:addString("TypeSignal", "Type of signal", "", "direct"); strategy.parameters:addStringAlternative("TypeSignal", "direct", "", "direct"); strategy.parameters:addStringAlternative("TypeSignal", "reverse", "", "reverse"); strategy.parameters:addGroup("Signals"); strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true); strategy.parameters:addBoolean("PlaySound", "Play Sound", "", false); strategy.parameters:addFile("SoundFile", "Sound File", "", ""); end local SoundFile; local gSourceBid = nil; local gSourceAsk = nil; local first; local BidFinished = false; local AskFinished = false; local LastBidCandle = nil; local Stochastic; local EMA8; function Prepare() ShowAlert = instance.parameters.ShowAlert; if instance.parameters.PlaySound then SoundFile = instance.parameters.SoundFile; else SoundFile = nil; end assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified"); assert(instance.parameters.Period ~= "t1", "Signal cannot be applied on ticks"); ExtSetupSignal("Altr Trend signal:", ShowAlert); gSourceBid = core.host:execute("getHistory", 1, instance.bid:instrument(), instance.parameters.Period, 0, 0, true); gSourceAsk = core.host:execute("getHistory", 2, instance.bid:instrument(), instance.parameters.Period, 0, 0, false); Stochastic = core.indicators:create("STOCHASTIC", gSourceBid, instance.parameters.K, instance.parameters.SD, instance.parameters.D); first = Stochastic.DATA:first(); local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period .. instance.parameters.K .. instance.parameters.SD .. instance.parameters.D .. instance.parameters.TypeSignal .. instance.parameters.DownLevel .. instance.parameters.UpLevel .. ")"; instance:name(name); end local LastDirection=nil; -- when tick source is updated function Update() if not(BidFinished) or not(AskFinished) then return ; end local period; -- update moving average Stochastic:update(core.UpdateLast); -- calculate enter logic if LastBidCandle == nil or LastBidCandle ~= gSourceBid:serial(gSourceBid:size() - 1) then LastBidCandle = gSourceBid:serial(gSourceBid:size() - 1); period = gSourceBid:size() - 1; if period > first then if Stochastic.K[period-1]>instance.parameters.UpLevel and Stochastic.K[period]instance.parameters.DownLevel and instance.parameters.TypeSignal=="direct" and LastDirection~=1 then ExtSignal(gSourceAsk, period, "Buy", SoundFile); LastDirection=1; end if Stochastic.K[period-1]instance.parameters.DownLevel and instance.parameters.TypeSignal=="reverse" and LastDirection~=-1 then ExtSignal(gSourceBid, period, "Sell", SoundFile); LastDirection=-1; end if Stochastic.K[period-1]>instance.parameters.UpLevel and Stochastic.K[period]