Star niks
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © naiknikunj1

//@version=5
indicator('My script', overlay=true)
win_vwap = ta.vwap
// show plots
is_show = input(defval=false, title='show plots')

plot(is_show ? ta.vwap : na, color=color.new(color.black, 0))
buy_vwap = close > ta.vwap
sell_vwap = close < ta.vwap

//CCI
length = input.int(38, minval=1)
src = input(hlc3, title="CCI Source")
ma = ta.sma(src, length)
cci = (src - ma) / (0.015 * ta.dev(src, length))
plot(is_show?cci:na, "CCI", color=#2962FF)


ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

smoothingLine = ma(cci, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, display=display.none)

buy_cci=cci>100
sell_cci=cci<-100


//TDFI

lookback = input(13, title='Lookback')
filterHigh = input(0.05, title='Filter High')
filterLow = input(-0.05, title='Filter Low')
price = input(close, 'Period')

mma = ta.ema(price * 1000, lookback)
smma = ta.ema(mma, lookback)

impetmma = mma - mma[1]
impetsmma = smma - smma[1]
divma = math.abs(mma - smma)
averimpet = (impetmma + impetsmma) / 2

number = averimpet
pow = 3
result = float(na)

for i = 1 to pow - 1 by 1
    if i == 1
        result := number
        result
    result *= number
    result

tdf = divma * result
ntdf = tdf / ta.highest(math.abs(tdf), lookback * 3)

c = ntdf > filterHigh ? color.green : ntdf < filterLow ? color.red : color.gray
plot(is_show?ntdf:na, linewidth=2, color=c)

buy_tdfi=ntdf>filterHigh
sell_tdfi=ntdf<filterLow 



//heikenshi 


EMAlength = input(225, 'EMA LENGTH?')

// Empty `table1` table ID.
var table table1 = na
// `table` type is unnecessary because `table.new()` returns "table" type.
var table2 = table.new(position.top_left, na, na)


Heisrc = ohlc4
haOpen = 0.0
haOpen := (Heisrc + nz(haOpen[1])) / 2
haC = (ohlc4 + nz(haOpen) + math.max(high, nz(haOpen)) + math.min(low, nz(haOpen))) / 4
EMA1 = ta.ema(haC, EMAlength)
EMA2 = ta.ema(EMA1, EMAlength)
EMA3 = ta.ema(EMA2, EMAlength)
TMA1 = 3 * EMA1 - 3 * EMA2 + EMA3
EMA4 = ta.ema(TMA1, EMAlength)
EMA5 = ta.ema(EMA4, EMAlength)
EMA6 = ta.ema(EMA5, EMAlength)
TMA2 = 3 * EMA4 - 3 * EMA5 + EMA6
IPEK = TMA1 - TMA2
YASIN = TMA1 + IPEK
EMA7 = ta.ema(hlc3, EMAlength)
EMA8 = ta.ema(EMA7, EMAlength)
EMA9 = ta.ema(EMA8, EMAlength)
TMA3 = 3 * EMA7 - 3 * EMA8 + EMA9
EMA10 = ta.ema(TMA3, EMAlength)
EMA11 = ta.ema(EMA10, EMAlength)
EMA12 = ta.ema(EMA11, EMAlength)
TMA4 = 3 * EMA10 - 3 * EMA11 + EMA12
IPEK1 = TMA3 - TMA4
YASIN1 = TMA3 + IPEK1

mavi = YASIN1
kirmizi = YASIN


longCond = mavi > kirmizi and mavi[1] <= kirmizi[1]
shortCond = mavi < kirmizi and mavi[1] >= kirmizi[1]

trendState = kirmizi < mavi ? true : kirmizi > mavi ? false : na
closePlot = plot(is_show?kirmizi:na, title='Close Line', color=color.new(#009900, 90), linewidth=10, style=plot.style_line)
openPlot = plot(is_show?mavi:na, title='Open Line', color=color.new(#CC0000, 90), linewidth=10, style=plot.style_line)
closePlotU = plot(trendState ? kirmizi : na, editable=false, transp=100)
openPlotU = plot(trendState ? mavi : na, editable=false, transp=100)
closePlotD = plot(trendState ? na : kirmizi, editable=false, transp=100)
openPlotD = plot(trendState ? na : mavi, editable=false, transp=100)

last_signal = 0
long_final = longCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == -1)
short_final = shortCond and (nz(last_signal[1]) == 0 or nz(last_signal[1]) == 1)


last_signal := long_final ? 1 : short_final ? -1 : last_signal[1]

plotshape(long_final, style=shape.labelup, location=location.belowbar, color=color.new(color.blue, 0), size=size.tiny, title='buy label', text='BUY', textcolor=color.new(color.white, 0))
plotshape(short_final, style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny, title='sell label', text='SELL', textcolor=color.new(color.white, 0))

variable = true
if (long_final==true)
    variable:=true
if (long_final==false and short_final==false and variable[1]==true)
    variable=true
if(short_final==true)
    variable:=false
if(long_final==false and short_final==false and variable[1]==false)
    variable:=false

variable2 = if variable==false
    true
else
    false


// Constants

var P_COLORS = "Price colors"

// Inputs

var resolution = input.timeframe('1D', 'Resolution', tooltip="Which resolution do you want the past prices to be pulled from.\nDaily means it will take yesterday's values.")
var hidePast = input.bool(false, "Hide past prices")
var displayRes = input.timeframe('1D', 'Display resolution', tooltip='This only matters if you checked the option above.\nDaily means it will only show the past prices for today, and hide the rest.')
var displayTomorrow = input.bool(true, "Display tomorrow values", tooltip="Remember that the prices WILL change throughout the development of the day, since we, currently, can't predict the future.")

var colorOpen = input.color(color.orange, "Open", inline=P_COLORS, group=P_COLORS)
var colorHigh = input.color(color.teal, "High", inline=P_COLORS, group=P_COLORS)
var colorLow = input.color(color.red, "Low", inline=P_COLORS, group=P_COLORS)
var colorClose = input.color(color.blue, "Close", inline=P_COLORS, group=P_COLORS)

// Getting the values

currentTicker = ticker.new(syminfo.prefix, syminfo.ticker, syminfo.session)
f_pValues() => request.security(currentTicker, resolution, [open[1], high[1], low[1], close[1]], lookahead=barmerge.lookahead_on)
f_cValues() => request.security(currentTicker, resolution, [open, high, low, close], lookahead=barmerge.lookahead_on)

[tOpen, tHigh, tLow, tClose] = f_pValues()
[cOpen, cHigh, cLow, cClose] = f_cValues()

// Adjusting for extended sessions
// Since these hours are not counted in a daily graph, I have to make a big "gambiarra" in order for it to work out.

resInSec = timeframe.in_seconds(resolution)
dailySec = timeframe.in_seconds("1D")
resClose = request.security(currentTicker, resolution, time_close, lookahead=barmerge.lookahead_on)

// If the selected resolution is not intraday
if resInSec >= dailySec
    // I can't just use session.isintraday because it would mess up the values when the resolution is greater than daily
    // If the current bar closes after the bar of the chosen resolution, i.e.,
    // we are in a pre-market session and the new session has not started
    if time_close > resClose
        // Then I need to update the values to match the current values, not the past ones.
        // I really wish PS would just let me use the walrus operator (:=) with brackets here...
        tOpen := cOpen
        tHigh := cHigh
        tLow := cLow
        tClose := cClose

// Hiding past values

// I really wish I could use the brackets here... c'mon PS, I just have to declare the na with a floating type, it's not hard.
float yOpen = na
float yHigh = na
float yLow = na
float yClose = na

isLastBar = request.security(currentTicker, displayRes, barstate.islast, lookahead=barmerge.lookahead_on)

if not hidePast or (hidePast and isLastBar)
    // Once again, you know what I wanted to do
    yOpen := tOpen
    yHigh := tHigh
    yLow := tLow
    yClose := tClose

// Display tomorrow values

// I was planning to use a fancy time calculation to set the tomorrow prices correctly,
// but PS doesn't let me use the next bar as a basis, so I will just leave this here,
// in case this ever gets implemented.

// tfInMs(tf) => int(timeframe.in_seconds(tf) * math.pow(10, 3))
var barsPerSession = int(timeframe.in_seconds(resolution) / timeframe.in_seconds(timeframe.period))

tomorrowLine(price, _color) =>
    // line.new(last_bar_time + tfInMs(timeframe.period), price, last_bar_time + tfInMs(resolution), price, xloc.bar_time, color=_color)
    line.new(last_bar_index + 1, price, last_bar_index + barsPerSession, price, xloc.bar_index, color=_color)
//    label.new(last_bar_index + 1, price, str.tostring(price), xloc.bar_index, yloc.price, _color, label.style_label_lower_right, color.black, textalign=text.align_left)
    
if isLastBar and displayTomorrow
    tomorrowLine(cOpen, colorOpen)
    tomorrowLine(cHigh, colorHigh)
    tomorrowLine(cLow, colorLow)
    tomorrowLine(cClose, colorClose)

// Plots


plot(yHigh, "Yesterday's high", colorHigh, style=plot.style_stepline)
plot(yLow, "Yesterday's low", colorLow, style=plot.style_stepline)

yesH= if (close>yHigh)
    true
else 
    false
yesL= if(close<yLow)
    true
else
    false

buy_order = buy_cci and buy_vwap and buy_tdfi and variable or yesH
sell_order = sell_cci and sell_vwap and sell_tdfi and variable2 or yesL

finally_buy=false
if(buy_order==true and long_final==true )
    finally_buy:=true
else if(buy_order==true and long_final[1]==true )
    finally_buy:=true
else if(buy_order==true and long_final[2]==true )
    finally_buy:=true
else if(buy_order==true and long_final[3]==true )
    finally_buy:=true

finally_sell=false
if(sell_order==true and short_final==true )
    finally_sell:=true
else if(sell_order==true and short_final[1]==true )
    finally_sell:=true
else if(sell_order==true and short_final[2]==true )
    finally_sell:=true
else if(sell_order==true and short_final[3]==true )
    finally_sell:=true
        

if (finally_buy[1]==true)
    finally_buy:=false
else if (finally_buy[2]==true)
    finally_buy:=false
else if (finally_buy[3]==true)
    finally_buy:=false

if(finally_sell[1]==true)
    finally_sell:=false
else if (finally_sell[2]==true)
    finally_sell:=false
else if (finally_sell[3]==true)
    finally_sell:=false




plotarrow(finally_buy?1:na,colorup=color.green,maxheight=150,minheight=100)
plotarrow(finally_sell?-1:na,colorup=color.red,maxheight=150,minheight=100)
alertcondition(finally_buy,title="buy alert s1")
alertcondition(finally_sell,title="sell alert s1")