StableF-Main
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © E-Nifty

//@version=5
indicator("StableF-Main", overlay=true, timeframe="", timeframe_gaps=true)

atrPeriod = input(23, "ATR Length")
factor = input.float(2.7, "Factor", step = 0.01)

lensig = input.int(7, title="ADX Smoothing", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")

use_tp      = input(true, "Take Profit?")
tp_trigger  = input.float(0.8, "Take profit %", minval=0, step=0.5) * 0.01
ttp_trigger = input.float(0.2, "Trailing Take Profit %", minval=0, step=0.5) * 0.01

[supertrend, direction] = ta.supertrend(factor, atrPeriod)


upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_circles)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_circles)

plotshape(ta.crossover(close,supertrend) ,text="BUY",style=shape.labelup,color=color.green,textcolor=color.black,location=location.belowbar)
plotshape(ta.crossunder(close, supertrend),text="SELL",style=shape.labeldown,color=color.red,textcolor=color.black)


up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

plotshape(ta.crossover(plus,minus)?plus:na,color=color.green,location=location.belowbar,style=shape.triangleup,size=size.auto)
plotshape(ta.crossunder(plus,minus)?minus:na,color=color.red,location=location.abovebar,style=shape.triangledown,size=size.auto)

buy=ta.crossover(close,supertrend)
sell=ta.crossunder(close,supertrend)


since_buy       = ta.barssince(buy)
since_sell      = ta.barssince(sell)
buy_trend       = since_sell > since_buy
sell_trend      = since_sell < since_buy
change_trend    = (buy_trend and sell_trend[1]) or (sell_trend and buy_trend[1])


entry_price = ta.valuewhen(buy or sell, close, 0)


var tp_price_trigger    = -1.0
var is_tp_trigger_hit   = false
var tp_trail            = 0.0
tp_close_long       = false
tp_close_short      = false

if use_tp

    if change_trend

        tp_price_trigger    := entry_price * (1.0 + (buy_trend ? 1.0 : sell_trend ? -1.0 : na) * tp_trigger)
        is_tp_trigger_hit   := false


    if buy_trend
 
        is_tp_trigger_hit   := (high >= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


        tp_trail            := math.max(high, change_trend ? tp_price_trigger : tp_trail[1])
        tp_close_long       := (is_tp_trigger_hit and (high <= tp_trail * (1.0 - ttp_trigger))) or (not change_trend and tp_close_long[1])
    else if sell_trend

        is_tp_trigger_hit   := (low <= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


        tp_trail            := math.min(low, change_trend ? tp_price_trigger : tp_trail[1])
        tp_close_short      := (is_tp_trigger_hit and (low >= tp_trail * (1.0 + ttp_trigger))) or (not change_trend and tp_close_short[1])


plot(use_tp and tp_price_trigger != -1.0 ? tp_price_trigger : na,title='Take Profit Price Trigger', color=color.blue,style=plot.style_cross, linewidth=2)

