10 EMA Strategy 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © steph1980

//@version=5
indicator('10 EMA RLT', overlay=true)

// user input
maLength = input.int(title='MA Length', defval=10)
maType = input.string(title='MA type', defval='EMA', options=["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA", "VWAP"])

getMA(_maType, _maLength) =>
    switch _maType
        'EMA' => ta.ema(close, _maLength)
        'SMA' => ta.sma(close, _maLength)
        'HMA' => ta.hma(close, _maLength)
        'WMA' => ta.wma(close, _maLength)
        'VWMA' => ta.vwma(close, _maLength)
        'VWAP' => ta.vwap
        'DEMA' =>
            e1=ta.ema(close, _maLength)
            e2=ta.ema(e1, _maLength)
            2 * e1 - e2
        
ma = getMA(maType, maLength)

bullCandle = open < close
bearCandle = open > close

var int inTrade = 0

longCondition = bearCandle and close > ma and inTrade == 0
shortCondition = bullCandle and close < ma and inTrade == 0

if longCondition 
    inTrade := 1
    
if shortCondition
    inTrade := -1

if close < ma and inTrade == 1 or close > ma and inTrade == -1
    inTrade :=0
    

plot(ma)
plotshape(longCondition, style=shape.triangleup, location=location.belowbar, color=color.green, title='long')
plotshape(shortCondition, style=shape.triangledown, location=location.abovebar, color=color.red, title='short')

alertcondition(longCondition, title='10 EMA Long trade', message='10 EMA longtrade detected on {{ticker}}')
alertcondition(shortCondition, title='10 EMA short trade', message='10 EMA shorttrade detected on {{ticker}}')