EMA Cross Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wirunekaewjai
 
//@version=5
strategy("EMA Cross Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, initial_capital=1000, commission_value=0.05)

fastLength = input.int(12, "EMA Fast")
slowLength = input.int(26, "EMA Slow")

priceGap = input.float(100, "Gap (%)", minval=0.01, maxval=100, tooltip="Gap Between Price and Slow EMA")

slValue = input.float(5, "SL (%)", minval=0)

tpMode = input.string('%','TP Mode', options=['%','Ratio'])
tpLongValue = input.float(200, "TP Long", minval=0)
tpShortValue = input.float(50, "TP Short", minval=0)

////
yearStart = input.int(1980, "Year Start", minval=1700, maxval=3000)
////

fast = ta.ema(close, fastLength)
slow = ta.ema(close, slowLength)

eCrossOver = ta.crossover(fast, slow)
eCrossUnder = ta.crossunder(fast, slow)

eHigher = fast > slow
eLower = fast < slow

enableLong = input.bool(true, "Long")
enableShort = input.bool(true, "Short")

currentYear = year(time)

pGap = priceGap / 100

avgPrice = strategy.position_avg_price

sl = slValue / 100
slLong = avgPrice * (1 - sl)
slShort = avgPrice * (1 + sl)

tpL = tpLongValue / 100
tpS = tpShortValue / 100

tpLong = tpMode == '%' ? avgPrice * (1 + tpL) : avgPrice * (1 + (sl * tpLongValue))
tpShort = tpMode == '%' ? avgPrice * (1 - tpS) : avgPrice * (1 - (sl * tpShortValue))

if currentYear >= yearStart
    if enableLong and eCrossOver
        gap = (close / slow) - 1

        if close > slow and gap < pGap
            strategy.entry("Long", strategy.long)

    if enableShort and eCrossUnder
        gap = 1 - (close / slow)

        if close < slow and gap < pGap
            strategy.entry("Short", strategy.short)

    if strategy.position_size > 0
        if slValue > 0 and tpLongValue > 0
            strategy.exit("Stop Long", "Long", stop=slLong, limit=tpLong, comment_profit="TP", comment_loss="SL")
        else if slValue > 0
            strategy.exit("SL Long", "Long", stop=slLong, comment_loss="SL")
        else if tpLongValue > 0
            strategy.exit("TP Long", "Long", limit=tpLong, comment_profit="TP")

        if eCrossUnder
            strategy.close("Long")

    if strategy.position_size < 0
        if slValue > 0 and tpShortValue > 0
            strategy.exit("Stop Short", "Short", stop=slShort, limit=tpShort, comment_profit="TP", comment_loss="SL")
        else if slValue > 0
            strategy.exit("SL Short", "Short", stop=slShort, comment_loss="SL")
        else if tpShortValue > 0
            strategy.exit("TP Short", "Short", limit=tpShort, comment_profit="TP")

        if eCrossOver
            strategy.close("Short")

/////////////////

plot(fast, color=color.white)
plot(slow, color=color.yellow)

plot(series=(strategy.position_size > 0 and slValue > 0) ? slLong : na, color=color.red, style=plot.style_linebr, title="SL Long")
plot(series=(strategy.position_size < 0 and slValue > 0) ? slShort : na, color=color.red, style=plot.style_linebr, title="SL Short")

plot(series=(strategy.position_size > 0 and tpLongValue > 0) ? tpLong : na, color=color.teal, style=plot.style_linebr, title="TP Long")
plot(series=(strategy.position_size < 0 and tpShortValue > 0) ? tpShort : na, color=color.teal, style=plot.style_linebr, title="TP Short")