isamuch - 2MA Cross and Stop Loss
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SamSignal

//@version=4
study("isamuch - 2MA Cross and Stop Loss", overlay=true, max_bars_back=999, max_labels_count=500)

// ====================
// Global Variable
// ====================
src = input(close, title = "Data Source")

// ====================
// MA Cross
// ====================
type = input(title = "Moving Average Type", defval = "EMA", options = ["SMA", "EMA", "WMA"])
fastLength = input(12, title = "Fast Length")
slowLength = input(26, title = "Slow Length")

float fastMa = na
float slowMa = na

if(type == "SMA")
    fastMa := sma(src, fastLength)
    slowMa := sma(src, slowLength)
else if (type == "EMA")
    fastMa := ema(src, fastLength)
    slowMa := ema(src, slowLength)
else 
    fastMa := wma(src, fastLength)
    slowMa := wma(src, slowLength)

fastL = plot(fastMa, title="Fast Ma",  color=color.green, linewidth=1)
slowL = plot(slowMa, title="Slow Ma",  color=color.red, linewidth=1)

plot(crossunder(slowMa, fastMa) ? fastMa : na, style = plot.style_cross, color=color.blue, linewidth = 3)
plot(crossover(slowMa, fastMa) ? fastMa : na, style = plot.style_cross, color=color.red, linewidth = 3)

fillcolor = fastMa > slowMa ? color.green : color.red
fill(fastL,slowL,fillcolor)

// ====================
// Stop Loss
// ====================

previousBearBar = 1
for i=1 to 999
    if fastMa[i] > slowMa[i]
        previousBearBar := i
        break
lowestPrice = lowest(low, previousBearBar)

previousBullBar = 1
for i=1 to 999
    if fastMa[i] < slowMa[i]
        previousBullBar := i
        break
highestPrice = highest(high, previousBullBar)

// ====================
// Function
// ====================
getMsg(title, entryPrice, stopLoss, risk) =>
    msg = "Entry Price: " + tostring(entryPrice, "#,###.########") + "\nStop Loss: " + tostring(stopLoss, "#,###.#######") + "\n(" + tostring(risk, "#.##") + "%)"

getMsgExitPrice(exitPrice) =>
    msg = "Exit Price: " + tostring(exitPrice, "#,###.#####")

// ====================
// Position Type
// ====================
positionType = input(title="Position Type", type=input.string, defval="long", options=["long", "short", "long & short"])
    
// ====================
// Calculate
// ====================
if crossover(fastMa, slowMa)
    if positionType == "long" or positionType == "long & short"
        msg = getMsg("BUY", close, lowestPrice, (((lowestPrice * 100) / close ) - 100))
        buyLabel = label.new(bar_index, close, text=msg, color=color.navy, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar)
        alert(msg, alert.freq_once_per_bar_close)
    else
        msg = getMsgExitPrice(close)
        buyLabel = label.new(bar_index, close, text=msg, color=color.black, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar)
        alert(msg, alert.freq_once_per_bar_close)
        

if crossunder(fastMa, slowMa)
    if positionType == "short" or positionType == "long & short"
        msg = getMsg("SELL", close, highestPrice, (((close * 100) / highestPrice ) - 100))
        sellLabel = label.new(bar_index, close, text=msg, color=color.olive, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
        alert(msg, alert.freq_once_per_bar_close)
    else
        msg = getMsgExitPrice(close)
        sellLabel = label.new(bar_index, close, text=msg, color=color.black, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
        alert(msg, alert.freq_once_per_bar_close)
