NNFX ATR
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sueun123
// @version=4

// This is a product of combining the in-built ATR provided by TradingView and Dillon Grech's ATR (https://www.tradingview.com/script/bbxfw74y-Indicator-ATR-Profit-Loss-DG/)

study(title = "NNFX ATR", shorttitle = "ATR", overlay = true, format = format.price, precision = 5) //Don't add scale = scale.right as the indicator will move all over the place

atrLength = input(title = "X", defval = 14, minval = 1)
atrProfit = input(title = "TP", defval = 1, type = input.float)
atrLoss   = input(title = "SL", defval = 1.5, type = input.float)

smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
function(source, length) => 
	if smoothing == "RMA"
		rma(source, atrLength)
	else
		if smoothing == "SMA"
			sma(source, atrLength)
		else
			if smoothing == "EMA"
				ema(source, atrLength)
			else
				wma(source, atrLength)

formula(number, decimals) =>
    factor = pow(10, decimals)
    int(number * factor) / factor
    
atr = formula(function(tr(true), atrLength),5)

//Buy
bAtrBuy  = atrLength ? close + atr* atrProfit : close - atr* atrProfit
bAtrSell = atrLength ? close - atr* atrLoss   : close + atr* atrLoss

plot(bAtrBuy, title  = "Buy TP", color = color.blue, transp = 0, linewidth = 1, trackprice = true, editable=  true)
plot(bAtrSell, title = "Buy SL", color = color.red, transp = 25, linewidth = 1, trackprice = true, editable = true)

//Sell
sAtrBuy  = atrLength ? close - atr* atrProfit : close + atr* atrProfit
sAtrSell = atrLength ? close + atr* atrLoss   : close - atr* atrLoss

plot(sAtrBuy, title  = " Sell TP", color = color.blue, transp = 35, linewidth = 1, trackprice = true, editable = true)
plot(sAtrSell, title = "Sell SL", color = color.red, transp = 50, linewidth = 1, trackprice = true, editable = true)

//This one is way below price because I don't want there to be a 5th line inbetween the 2 TPs and SLs
plot(atr, title = " Main ATR", color = color.yellow, transp = 0, linewidth = 1, editable = true)

//---END---