-- Id: 14621 --+------------------------------------------------------------------+ --| EMA RSI Signal.lua | --| Copyright © 2015, Gehtsoft USA LLC | --| http://fxcodebase.com | --+------------------------------------------------------------------+ --| Developed by : Mario Jemic | --| mario.jemic@gmail.com | --+------------------------------------------------------------------+ --| Donate/Support | --| Paypal: http://goo.gl/cEP5h5 | --| BitCoin : 1MfUHS3h86MBTeonJzWdszdzF2iuKESCKU | --+------------------------------------------------------------------+ function Init() strategy:name("EMA RSI Signal"); strategy:setTag("NonOptimizableParameters", "Email,SendEmail,SoundFile,RecurrentSound,PlaySound,ShowAlert"); strategy:description("EMA RSI Signal"); strategy.parameters:addGroup("Parameters"); strategy.parameters:addInteger("EMA1", "Period", "", 5); strategy.parameters:addInteger("EMA2", "Period", "", 14); strategy.parameters:addInteger("EMA3", "Period", "", 26); strategy.parameters:addInteger("RSIN", "RSI Period", "", 7, 2, 1000); strategy.parameters:addString("Method", "Price Smoothing method", "", "EMA"); strategy.parameters:addStringAlternative("Method", "MVA", "", "MVA"); strategy.parameters:addStringAlternative("Method", "EMA", "", "EMA"); strategy.parameters:addStringAlternative("Method", "LWMA", "", "LWMA"); strategy.parameters:addString("Type", "Price type", "", "Bid"); strategy.parameters:addStringAlternative("Type", "Bid", "", "Bid"); strategy.parameters:addStringAlternative("Type", "Ask", "", "Ask"); strategy.parameters:addString("Period", "Timeframe", "", "m1"); strategy.parameters:addString("Period", "Timeframe", "", "m5"); strategy.parameters:addString("Period", "Timeframe", "", "m15"); strategy.parameters:addString("Period", "Timeframe", "", "m30"); strategy.parameters:addString("Period", "Timeframe", "", "H1"); strategy.parameters:addString("Period", "Timeframe", "", "H2"); strategy.parameters:addString("Period", "Timeframe", "", "H4"); strategy.parameters:addString("Period", "Timeframe", "", "D1"); strategy.parameters:setFlag("Period", core.FLAG_PERIODS); strategy.parameters:addGroup("Signals"); strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true); strategy.parameters:addBoolean("ShowAlert", "alertMessage", "", date(Now)); strategy.parameters:addBoolean("PlaySound", "Play Sound", "", true); strategy.parameters:addFile("SoundFile", "Sound File", "", "decide16"); end local ShowAlert; local SoundFile; local EMA1,EMA2,EMA3,RSI; local BUY, SELL; local BarSource = nil; -- the source stream local FastN, SlowN, RSIN; --? local FastFlag; local RSIFlag, RSIOver; local FLAG; local reset; local first; function Prepare() -- collect parameters FastN = instance.parameters.FastN; SlowN = instance.parameters.SlowN; RSIN = instance.parameters.RSIN; assert(FastN < SlowN,"Number of periods for Fast MA must be less than number of periods for Mid MA"); ShowAlert = instance.parameters.ShowAlert; if instance.parameters.PlaySound then SoundFile = instance.parameters.SoundFile; else SoundFile = nil; end assert(not(PlaySound) or (PlaySound and SoundFile ~= " "), "decide16"); SELL = "Short"; BUY = "Long"; ExtSetupSignal("EMA RSI Signal", ShowAlert); BarSource = ExtSubscribe(1, nil, instance.parameters.Period, instance.parameters.Type == "Bid", "bar"); assert(core.indicators:findIndicator(instance.parameters.Method) ~= nil, instance.parameters.Method .. " indicator must be installed"); FastMA = core.indicators:create(instance.parameters.Method, BarSource.close, FastN); SlowMA = core.indicators:create(instance.parameters.Method, BarSource.close, SlowN); RSI = core.indicators:create("RSI", BarSource.close, RSIN); first=math.max(FastMA.DATA:first(),SlowMA.DATA:first(),RSI.DATA:first())+1; local name = profile:id() .. "(" .. instance.bid:instrument() .. "(" .. instance.parameters.Period .. ")" .. "," .. FastN .. SlowN .. ")"; instance:name(name); end -- when tick source is updated function ExtUpdate(id, source, period) EMA1:update(core.UpdateLast); EMA2:update(core.UpdateLast); EMA3:update(core.UpdateLast); RSI:update(core.UpdateLast); if period < first then return; end if core.crossesOver(EMA1.DATA, EMA2.DATA, EMA3.DATA, period) then FastFlag= "Buy"; reset=1; end if core.crossesUnder(EMA1.DATA, EMA2.DATA, EMA3.DATA, period) then FastFlag= "Sell"; reset=1; end if core.crossesOver( RSI.DATA, 70, period) then RSIFlag= "Buy"; RSIOver=nil; reset=1; end if core.crossesUnder( RSI.DATA, 30, period) then RSIFlag= "Sell"; RSIOver=nil; reset=1; end if reset== 1 then FLAG=nil; reset=0; end if FastFlag == "Buy" and FLAG ~= "Buy" and RSIFlag=="Buy" and RSIOver~= "Buy" and FastMA.DATA[period] > FastMA.DATA[period-1] then FLAG = "Buy"; ExtSignal(BarSource.close, period, BUY, SoundFile); end if FastFlag == "Sell" and FLAG ~= "Sell" and RSIFlag=="Sell" and RSIOver~= "Sell" and FastMA.DATA[period] < FastMA.DATA[period-1] then FLAG = "Sell"; ExtSignal(BarSource.close, period, SELL, SoundFile); end end dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");