VCP pivot buy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stockone1231

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

//@version=4
strategy("My Strategy", overlay=true)

len50 = input(50, minval=1, title="Length")
src = input(close, title="Source")
offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)
out50 = sma(src, len50)
plot(out50, color=color.blue, title="MA", offset=offset)

len200 = input(200, minval=1, title="Length")
out200 = sma(src, len200)
plot(out200, color=color.red, title="MA", offset=offset)

len150 = input(150, minval=1, title="Length")
out150 = sma(src, len150)
plot(out150, color=color.green, title="MA", offset=offset)


slow = input(50, minval=1, title="TREND") // 10 days a
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 500, defval = 10)

// Calculations
slowline = sma(volume, slow)
trend = sma(slowline, signal_length)


tema(s, p) =>
    3 * ema(s, p) - 3 * ema(ema(s, p), p) + ema(ema(ema(s, p), p), p)
ma(t, s, p) =>
    ema_1 = ema(s, p)
    rma_1 = rma(s, p)
    vwma_1 = vwma(s, p)
    wma_1 = wma(s, p)
    tema_1 = tema(s, p)
    sma_1 = sma(s, p)
    t == "ema" ? ema_1 : t == "rma" ? rma_1 : 
       t == "vwma" ? vwma_1 : t == "wma" ? wma_1 : t == "tema" ? tema_1 : sma_1
off(s, o) =>
    offset_1 = offset(s, o == 0 ? 1 : o)
    o > 0 ? offset_1 : s
mat11 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 1 Type")
len11 = input(10, minval=1, title="Moving Average 1 Length")
src11 = input(close, title="Moving Average 1 Source")
off11 = input(0, minval=0, title="Moving Average 1 Offset")
mat22 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 2 Type")
len22 = input(20, minval=1, title="Moving Average 2 Length")
src22 = input(close, title="Moving Average 2 Source")
off22 = input(0, minval=0, title="Moving Average 2 Offset")


out11 = ma(mat11, off(src11, off11), len11)
out22 = ma(mat22, off(src22, off22), len22)

plot(out11, color=#1155CC, transp=0, title="MA 1")
plot(out22, color=#FF0000, transp=50, linewidth=2, title="MA 2")





//trade to hold trend indicator//
bullishtrenddef = (close>out50) and (out50>out150) and (out150>out200) and (out200>out200[1]) and (out200>out200[20]) and (out50>=out50[1])

//pivot


atr_length = input(defval=3, title="ATR Length", type=input.integer)
lookback = input(defval=150, title="Look Back Period", type=input.integer)
closerange = input(defval=4, title="Close range", type=input.integer)
percentile1 = input(defval=30, title="Smaller Percentile", type=input.integer)
percentile2 = input(defval=40, title="Largerer Percentile", type=input.integer)



//range2
atr_value1 = 0.0
stan1 = 0.0
range1 = 0.0
l1=0.0
h1=0.0
for i = 1 to atr_length
    h1:=highest(high,atr_length)
    l1:=lowest(low,atr_length)
    range1 := h1-l1
atrp1 = (range1 / h1) * 100
//range2
atr_value2 = 0.0
stan2 = 0.0
range2 = 0.0
l2=0.0
h2=0.0
for i = 1 to atr_length
    h2:=highest(close,atr_length)
    l2:=lowest(close,atr_length)
    range2 := h2-l2
atrp2 = (range2 / h2) * 100

per1 = percentrank( atrp1, lookback)
per2 = percentrank( atrp2, lookback)

setting1 = (per1 <= percentile2) and (per1 > percentile1)
rangepivot = (per1 <= percentile1)  and (atrp1<=10)
closepivot = (atrp2<=closerange)

pivot = rangepivot or closepivot

sessioncolor2 = setting1 and bullishtrenddef? color.yellow :na
sessioncolor3 = rangepivot and bullishtrenddef? color.blue :na
sessioncolor4 = closepivot and bullishtrenddef? color.red :na


bgcolor(sessioncolor2 , transp=70)
bgcolor(sessioncolor3 , transp=70)
bgcolor(sessioncolor4 , transp=70)

high_loopback = input(3, "High Lookback Length")
h3 = float (highest(high, high_loopback))

takePer = input(10.0, title='Take Profit %', type=input.float) / 100
shortPer = input(10.0, title='Stoploss %', type=input.float) / 100

stoploss = strategy.position_avg_price * (1 - shortPer)
longTake1 = strategy.position_avg_price * (1 + takePer)
longTake2 = strategy.position_avg_price * (1 + takePer*2)
longTake3 = strategy.position_avg_price * (1 + takePer*3)

long1 =  bullishtrenddef and pivot


if (long1)
    strategy.entry("Long", strategy.long, stop = h3[1] )

    
if strategy.position_size > 0 
    strategy.exit(id="Close Long1", qty_percent = 25, stop=stoploss, limit=longTake1)
    
if strategy.position_size > 0 
    strategy.exit(id="Close Long2", qty_percent = 50,  stop=stoploss, limit=longTake2)
    
if strategy.position_size > 0 
    strategy.exit(id="Close Long3", qty_percent = 100, stop=stoploss, limit=longTake3)
