semki
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: August 14th, 2022
Contact: TradingView Profile

Convert Pine script 2 to 5

Hi, ive been trying to improve this old script in version 2 but its very hard due to the old version, i need to mainly fix the repainting issue but i need to get the script to version 5 and i dont have a clue to how i can do it. I get a lot of errors when trying.

Here is my code: Any help would be appreciated

Code: Select all

//@version=2
strategy(title='Semki v4.1', shorttitle='Semki', overlay=true, pyramiding=0, initial_capital=100, currency=currency.USD)
useHA = input(false, title='Use Heikken Ashi Candles')
useAltTF = input(true, title='Use Alt Timeframe')
tf = input('40', title='Alt Timeframe')
showPatterns = input(true, title='Show Patterns')
showFib0000 = input(title='Display Fibonacci 0.000:', type=bool, defval=true)
showFib0236 = input(title='Display Fibonacci 0.236:', type=bool, defval=false)
showFib0382 = input(title='Display Fibonacci 0.382:', type=bool, defval=false)
showFib0500 = input(title='Display Fibonacci 0.500:', type=bool, defval=true)
showFib0618 = input(title='Display Fibonacci 0.618:', type=bool, defval=false)
showFib0764 = input(title='Display Fibonacci 0.764:', type=bool, defval=false)
showFib1000 = input(title='Display Fibonacci 1.000:', type=bool, defval=true)
zigzag() =>
    _isUp = close >= open
    _isDown = close <= open
    _direction = _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1])
    _zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? lowest(2) : na

_ticker = useHA ? heikenashi(tickerid) : tickerid
sz = useAltTF ? (change(time(tf)) != 0 ? security(_ticker, tf, zigzag()) : na) : zigzag()

plot(sz, title='zigzag', color=white, linewidth=1)

//  ||---   Pattern Recognition:

x = valuewhen(sz, sz, 4) 
a = valuewhen(sz, sz, 3) 
b = valuewhen(sz, sz, 2) 
c = valuewhen(sz, sz, 1) 
d = valuewhen(sz, sz, 0)

xab = (abs(b-a)/abs(x-a))
xad = (abs(a-d)/abs(x-a))
abc = (abs(b-c)/abs(a-b))
bcd = (abs(c-d)/abs(b-c))

//  ||-->   Functions:
isBat(_mode)=>
    _xab = xab >= 0.382 and xab <= 0.5
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad <= 0.618 and xad <= 1.000    // 0.886
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiBat(_mode)=>
    _xab = xab >= 0.500 and xab <= 0.886    // 0.618
    _abc = abc >= 1.000 and abc <= 2.618    // 1.13 --> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618    // 2.0  --> 2.618
    _xad = xad >= 0.886 and xad <= 1.000    // 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAltBat(_mode)=>
    _xab = xab <= 0.382
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.0 and bcd <= 3.618
    _xad = xad <= 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isButterfly(_mode)=>
    _xab = xab <= 0.786
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad >= 1.27 and xad <= 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiButterfly(_mode)=>
    _xab = xab >= 0.236 and xab <= 0.886    // 0.382 - 0.618
    _abc = abc >= 1.130 and abc <= 2.618    // 1.130 - 2.618
    _bcd = bcd >= 1.000 and bcd <= 1.382    // 1.27
    _xad = xad >= 0.500 and xad <= 0.886    // 0.618 - 0.786
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isABCD(_mode)=>
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _abc and _bcd and (_mode == 1 ? d < c : d > c)

isGartley(_mode)=>
    _xab = xab >= 0.5 and xab <= 0.618 // 0.618
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _xad = xad >= 0.75 and xad <= 0.875 // 0.786
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiGartley(_mode)=>
    _xab = xab >= 0.500 and xab <= 0.886    // 0.618 -> 0.786
    _abc = abc >= 1.000 and abc <= 2.618    // 1.130 -> 2.618
    _bcd = bcd >= 1.500 and bcd <= 5.000    // 1.618
    _xad = xad >= 1.000 and xad <= 5.000    // 1.272
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isCrab(_mode)=>
    _xab = xab >= 0.500 and xab <= 0.875    // 0.886
    _abc = abc >= 0.382 and abc <= 0.886    
    _bcd = bcd >= 2.000 and bcd <= 5.000    // 3.618
    _xad = xad >= 1.382 and xad <= 5.000    // 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiCrab(_mode)=>
    _xab = xab >= 0.250 and xab <= 0.500    // 0.276 -> 0.446
    _abc = abc >= 1.130 and abc <= 2.618    // 1.130 -> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618    // 1.618 -> 2.618
    _xad = xad >= 0.500 and xad <= 0.750    // 0.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isShark(_mode)=>
    _xab = xab >= 0.500 and xab <= 0.875    // 0.5 --> 0.886
    _abc = abc >= 1.130 and abc <= 1.618    //
    _bcd = bcd >= 1.270 and bcd <= 2.240    //
    _xad = xad >= 0.886 and xad <= 1.130    // 0.886 --> 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiShark(_mode)=>
    _xab = xab >= 0.382 and xab <= 0.875    // 0.446 --> 0.618
    _abc = abc >= 0.500 and abc <= 1.000    // 0.618 --> 0.886
    _bcd = bcd >= 1.250 and bcd <= 2.618    // 1.618 --> 2.618
    _xad = xad >= 0.500 and xad <= 1.250    // 1.130 --> 1.130
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

is5o(_mode)=>
    _xab = xab >= 1.13 and xab <= 1.618
    _abc = abc >= 1.618 and abc <= 2.24
    _bcd = bcd >= 0.5 and bcd <= 0.625 // 0.5
    _xad = xad >= 0.0 and xad <= 0.236 // negative?
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isWolf(_mode)=>
    _xab = xab >= 1.27 and xab <= 1.618
    _abc = abc >= 0 and abc <= 5
    _bcd = bcd >= 1.27 and bcd <= 1.618
    _xad = xad >= 0.0 and xad <= 5
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isHnS(_mode)=>
    _xab = xab >= 2.0 and xab <= 10
    _abc = abc >= 0.90 and abc <= 1.1
    _bcd = bcd >= 0.236 and bcd <= 0.88
    _xad = xad >= 0.90 and xad <= 1.1
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isConTria(_mode)=>
    _xab = xab >= 0.382 and xab <= 0.618
    _abc = abc >= 0.382 and abc <= 0.618
    _bcd = bcd >= 0.382 and bcd <= 0.618
    _xad = xad >= 0.236 and xad <= 0.764
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isExpTria(_mode)=>
    _xab = xab >= 1.236 and xab <= 1.618
    _abc = abc >= 1.000 and abc <= 1.618
    _bcd = bcd >= 1.236 and bcd <= 2.000
    _xad = xad >= 2.000 and xad <= 2.236
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

plotshape(not showPatterns ? na : isABCD(-1) and not isABCD(-1)[1], text="\nAB=CD", title='Bear ABCD', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0, offset=-2)
plotshape(not showPatterns ? na : isBat(-1) and not isBat(-1)[1], text="Bat", title='Bear Bat', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0, offset=-2)
plotshape(not showPatterns ? na : isAntiBat(-1) and not isAntiBat(-1)[1], text="Anti Bat", title='Bear Anti Bat', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0, offset=-2)
plotshape(not showPatterns ? na : isAltBat(-1) and not isAltBat(-1)[1], text="Alt Bat", title='Bear Alt Bat', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isButterfly(-1) and not isButterfly(-1)[1], text="Butterfly", title='Bear Butterfly', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isAntiButterfly(-1) and not isAntiButterfly(-1)[1], text="Anti Butterfly", title='Bear Anti Butterfly', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isGartley(-1) and not isGartley(-1)[1], text="Gartley", title='Bear Gartley', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isAntiGartley(-1) and not isAntiGartley(-1)[1], text="Anti Gartley", title='Bear Anti Gartley', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isCrab(-1) and not isCrab(-1)[1], text="Crab", title='Bear Crab', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isAntiCrab(-1) and not isAntiCrab(-1)[1], text="Anti Crab", title='Bear Anti Crab', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isShark(-1) and not isShark(-1)[1], text="Shark", title='Bear Shark', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isAntiShark(-1) and not isAntiShark(-1)[1], text="Anti Shark", title='Bear Anti Shark', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : is5o(-1) and not is5o(-1)[1], text="5-O", title='Bear 5-O', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isWolf(-1) and not isWolf(-1)[1], text="Wolf Wave", title='Bear Wolf Wave', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isHnS(-1) and not isHnS(-1)[1], text="Head and Shoulders", title='Bear Head and Shoulders', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isConTria(-1) and not isConTria(-1)[1], text="Contracting Triangle", title='Bear Contracting triangle', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)
plotshape(not showPatterns ? na : isExpTria(-1) and not isExpTria(-1)[1], text="Expanding Triangle", title='Bear Expanding Triangle', style=shape.labeldown, color=maroon, textcolor=white, location=location.top, transp=0)

plotshape(not showPatterns ? na : isABCD(1) and not isABCD(1)[1], text="AB=CD\n", title='Bull ABCD', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isBat(1) and not isBat(1)[1], text="Bat", title='Bull Bat', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAntiBat(1) and not isAntiBat(1)[1], text="Anti Bat", title='Bull Anti Bat', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAltBat(1) and not isAltBat(1)[1], text="Alt Bat", title='Bull Alt Bat', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isButterfly(1) and not isButterfly(1)[1], text="Butterfly", title='Bull Butterfly', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAntiButterfly(1) and not isAntiButterfly(1)[1], text="Anti Butterfly", title='Bull Anti Butterfly', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isGartley(1) and not isGartley(1)[1], text="Gartley", title='Bull Gartley', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAntiGartley(1) and not isAntiGartley(1)[1], text="Anti Gartley", title='Bull Anti Gartley', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isCrab(1) and not isCrab(1)[1], text="Crab", title='Bull Crab', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAntiCrab(1) and not isAntiCrab(1)[1], text="Anti Crab", title='Bull Anti Crab', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isShark(1) and not isShark(1)[1], text="Shark", title='Bull Shark', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isAntiShark(1) and not isAntiShark(1)[1], text="Anti Shark", title='Bull Anti Shark', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : is5o(1) and not is5o(1)[1], text="5-O", title='Bull 5-O', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isWolf(1) and not isWolf(1)[1], text="Wolf Wave", title='Bull Wolf Wave', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isHnS(1) and not isHnS(1)[1], text="Head and Shoulders", title='Bull Head and Shoulders', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isConTria(1) and not isConTria(1)[1], text="Contracting Triangle", title='Bull Contracting Triangle', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)
plotshape(not showPatterns ? na : isExpTria(1) and not isExpTria(1)[1], text="Expanding Triangle", title='Bull Expanding Triangle', style=shape.labelup, color=green, textcolor=white, location=location.bottom, transp=0)

//-------------------------------------------------------------------------------------------------------------------------------------------------------------
fib_range = abs(d-c)
fib_0000 = not showFib0000 ? na : d > c ? d-(fib_range*0.000):d+(fib_range*0.000)
fib_0236 = not showFib0236 ? na : d > c ? d-(fib_range*0.236):d+(fib_range*0.236)
fib_0382 = not showFib0382 ? na : d > c ? d-(fib_range*0.382):d+(fib_range*0.382)
fib_0500 = not showFib0500 ? na : d > c ? d-(fib_range*0.500):d+(fib_range*0.500)
fib_0618 = not showFib0618 ? na : d > c ? d-(fib_range*0.618):d+(fib_range*0.618)
fib_0764 = not showFib0764 ? na : d > c ? d-(fib_range*0.764):d+(fib_range*0.764)
fib_1000 = not showFib1000 ? na : d > c ? d-(fib_range*1.000):d+(fib_range*1.000)
plot(title='Fib 0.000', series=fib_0000, color=fib_0000 != fib_0000[1] ? na : red)
plot(title='Fib 0.236', series=fib_0236, color=fib_0236 != fib_0236[1] ? na : red)
plot(title='Fib 0.382', series=fib_0382, color=fib_0382 != fib_0382[1] ? na : olive)
plot(title='Fib 0.500', series=fib_0500, color=fib_0500 != fib_0500[1] ? na : lime)
plot(title='Fib 0.618', series=fib_0618, color=fib_0618 != fib_0618[1] ? na : teal)
plot(title='Fib 0.764', series=fib_0764, color=fib_0764 != fib_0764[1] ? na : blue)
plot(title='Fib 1.000', series=fib_1000, color=fib_1000 != fib_1000[1] ? na : fuchsia)

bgcolor(not useAltTF ? na : change(time(tf))!=0?black:na)
f_last_fib(_rate)=>d > c ? d-(fib_range*_rate):d+(fib_range*_rate)

target01_trade_size = input(title='Target 1 - Trade size:', type=float, defval=1)
target01_ew_rate = input(title='Target 1 - Fib. Rate to use for Entry Window:', type=float, defval=0.236)
target01_tp_rate = input(title='Target 1 - Fib. Rate to use for TP:', type=float, defval=1.618)
target01_sl_rate = input(title='Target 1 - Fib. Rate to use for SL:', type=float, defval=-0.436)
target02_active = input(title='Target 2 - Active?', type=bool, defval=false)
target02_trade_size = input(title='Target 2 - Trade size:', type=float, defval=1)
target02_ew_rate = input(title='Target 2 - Fib. Rate to use for Entry Window:', type=float, defval=0.236)
target02_tp_rate = input(title='Target 2 - Fib. Rate to use for TP:', type=float, defval=1.618)
target02_sl_rate = input(title='Target 2 - Fib. Rate to use for SL:', type=float, defval=-0.236)

disableRepaint = input(title="Disable Repainting?", defval=false)

buy_patterns_00 = isABCD(1) or isBat(1) or isAltBat(1) or isButterfly(1) or isGartley(1) or isCrab(1) or isShark(1) or is5o(1) or isWolf(1) or isHnS(1) or isConTria(1) or isExpTria(1) and (not disableRepaint)
buy_patterns_01 = isAntiBat(1) or isAntiButterfly(1) or isAntiGartley(1) or isAntiCrab(1) or isAntiShark(1) and (not disableRepaint)
sel_patterns_00 = isABCD(-1) or isBat(-1) or isAltBat(-1) or isButterfly(-1) or isGartley(-1) or isCrab(-1) or isShark(-1) or is5o(-1) or isWolf(-1) or isHnS(-1) or isConTria(-1) or isExpTria(-1) and (not disableRepaint)
sel_patterns_01 = isAntiBat(-1) or isAntiButterfly(-1) or isAntiGartley(-1) or isAntiCrab(-1) or isAntiShark(-1) and (not disableRepaint)

target01_buy_entry = (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(target01_ew_rate) and (not disableRepaint)
target01_buy_close = high >= f_last_fib(target01_tp_rate) or low <= f_last_fib(target01_sl_rate) and (not disableRepaint)
target01_sel_entry = (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(target01_ew_rate) and (not disableRepaint)
target01_sel_close = low <= f_last_fib(target01_tp_rate) or high >= f_last_fib(target01_sl_rate) and (not disableRepaint)

strategy.entry('target01_buy', long=strategy.long, qty=target01_trade_size, comment='buy 01', when=target01_buy_entry) 
strategy.close('target01_buy', when=target01_buy_close) 
strategy.entry('target01_sell', long=strategy.short, qty=target01_trade_size, comment='sell 01', when=target01_sel_entry)
strategy.close('target01_sell', when=target01_sel_close)

target02_buy_entry = target02_active and (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(target02_ew_rate) and (not disableRepaint)
target02_buy_close = target02_active and high >= f_last_fib(target02_tp_rate) or low <= f_last_fib(target02_sl_rate) and (not disableRepaint)
target02_sel_entry = target02_active and (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(target02_ew_rate) and (not disableRepaint)
target02_sel_close = target02_active and low <= f_last_fib(target02_tp_rate) or high >= f_last_fib(target02_sl_rate) and (not disableRepaint)

strategy.entry('target02_buy', long=strategy.long, qty=target02_trade_size, comment='buy 02', when=target02_buy_entry) 
strategy.close('target02_buy', when=target02_buy_close) 
strategy.entry('target02_sell', long=strategy.short, qty=target02_trade_size, comment='sell 02', when=target02_sel_entry)
strategy.close('target02_sell', when=target02_sel_close)

titles = input(title="EMA/SMA:", type=bool, defval=true)

//EMA
len = input(13, minval=1, title="Length")
src = input(close, title="Source")
out = ema(src, len)
plot(out, color=#28B463, transp=0, linewidth=1, title="EMA")
//SMA 1
len1 = input(21, minval=1, title="Length")
src1 = input(close, title="Source")
out1 = sma(src1, len1)
plot(out1, color=#D4AC0D, transp=0, linewidth=1, title="SMA")
//SMA 2
len2 = input(55, minval=1, title="Length")
src2 = input(close, title="Source")
out2 = sma(src2, len2)
plot(out2, color=#FF0033, transp=0, linewidth=1, title="SMA")
//SMA 3
len3 = input(8, minval=1, title="Length")
src3 = input(close, title="Source")
out3 = sma(src3, len3)
plot(out3, color=#0033FF, transp=0, linewidth=1, title="SMA")


processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Convert Pine script 2 to 5

Hey.

Here is the conversion from v2 to v5

Code: Select all

// © processingclouds
// processingclouds For bugs, and to show your support you can contact me anytime here and @gmail

//@version=5
strategy(title='Semki v4.1', shorttitle='Semki', overlay=true, pyramiding=0, initial_capital=100, currency=currency.USD)
useHA = input(false, title='Use Heikken Ashi Candles')
useAltTF = input(true, title='Use Alt Timeframe')
tf = input('40', title='Alt Timeframe')
showPatterns = input(true, title='Show Patterns')
showFib0000 = input(title='Display Fibonacci 0.000:', defval=true)
showFib0236 = input(title='Display Fibonacci 0.236:', defval=false)
showFib0382 = input(title='Display Fibonacci 0.382:', defval=false)
showFib0500 = input(title='Display Fibonacci 0.500:', defval=true)
showFib0618 = input(title='Display Fibonacci 0.618:', defval=false)
showFib0764 = input(title='Display Fibonacci 0.764:', defval=false)
showFib1000 = input(title='Display Fibonacci 1.000:', defval=true)
zigzag() =>
    _direction = 0
    _zigzag = float(na)
    _isUp = close >= open
    _isDown = close <= open
    _direction := _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1])
    highest_1 = ta.highest(2)
    lowest_1 = ta.lowest(2)
    _zigzag := _isUp[1] and _isDown and _direction[1] != -1 ? highest_1 : _isDown[1] and _isUp and _direction[1] != 1 ? lowest_1 : na
    _zigzag

heikenashi_1 = ticker.heikinashi(syminfo.tickerid)
_ticker = useHA ? heikenashi_1 : syminfo.tickerid
change_1 = ta.change(time(tf))
zigzag_1 = zigzag()
security_1 = request.security(_ticker, tf, zigzag_1)
zigzag_2 = zigzag()
sz = useAltTF ? change_1 != 0 ? security_1 : na : zigzag_2

plot(sz, title='zigzag', color=color.new(color.white, 0), linewidth=1)

//  ||---   Pattern Recognition:

x = ta.valuewhen(sz, sz, 4)
a = ta.valuewhen(sz, sz, 3)
b = ta.valuewhen(sz, sz, 2)
c = ta.valuewhen(sz, sz, 1)
d = ta.valuewhen(sz, sz, 0)

xab = math.abs(b - a) / math.abs(x - a)
xad = math.abs(a - d) / math.abs(x - a)
abc = math.abs(b - c) / math.abs(a - b)
bcd = math.abs(c - d) / math.abs(b - c)

//  ||-->   Functions:
isBat(_mode) =>
    _xab = xab >= 0.382 and xab <= 0.5
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad <= 0.618 and xad <= 1.000  // 0.886
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiBat(_mode) =>
    _xab = xab >= 0.500 and xab <= 0.886  // 0.618
    _abc = abc >= 1.000 and abc <= 2.618  // 1.13 --> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618  // 2.0  --> 2.618
    _xad = xad >= 0.886 and xad <= 1.000  // 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAltBat(_mode) =>
    _xab = xab <= 0.382
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.0 and bcd <= 3.618
    _xad = xad <= 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isButterfly(_mode) =>
    _xab = xab <= 0.786
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.618 and bcd <= 2.618
    _xad = xad >= 1.27 and xad <= 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiButterfly(_mode) =>
    _xab = xab >= 0.236 and xab <= 0.886  // 0.382 - 0.618
    _abc = abc >= 1.130 and abc <= 2.618  // 1.130 - 2.618
    _bcd = bcd >= 1.000 and bcd <= 1.382  // 1.27
    _xad = xad >= 0.500 and xad <= 0.886  // 0.618 - 0.786
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isABCD(_mode) =>
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _abc and _bcd and (_mode == 1 ? d < c : d > c)

isGartley(_mode) =>
    _xab = xab >= 0.5 and xab <= 0.618  // 0.618
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 1.13 and bcd <= 2.618
    _xad = xad >= 0.75 and xad <= 0.875  // 0.786
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiGartley(_mode) =>
    _xab = xab >= 0.500 and xab <= 0.886  // 0.618 -> 0.786
    _abc = abc >= 1.000 and abc <= 2.618  // 1.130 -> 2.618
    _bcd = bcd >= 1.500 and bcd <= 5.000  // 1.618
    _xad = xad >= 1.000 and xad <= 5.000  // 1.272
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isCrab(_mode) =>
    _xab = xab >= 0.500 and xab <= 0.875  // 0.886
    _abc = abc >= 0.382 and abc <= 0.886
    _bcd = bcd >= 2.000 and bcd <= 5.000  // 3.618
    _xad = xad >= 1.382 and xad <= 5.000  // 1.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiCrab(_mode) =>
    _xab = xab >= 0.250 and xab <= 0.500  // 0.276 -> 0.446
    _abc = abc >= 1.130 and abc <= 2.618  // 1.130 -> 2.618
    _bcd = bcd >= 1.618 and bcd <= 2.618  // 1.618 -> 2.618
    _xad = xad >= 0.500 and xad <= 0.750  // 0.618
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isShark(_mode) =>
    _xab = xab >= 0.500 and xab <= 0.875  // 0.5 --> 0.886
    _abc = abc >= 1.130 and abc <= 1.618  //
    _bcd = bcd >= 1.270 and bcd <= 2.240  //
    _xad = xad >= 0.886 and xad <= 1.130  // 0.886 --> 1.13
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isAntiShark(_mode) =>
    _xab = xab >= 0.382 and xab <= 0.875  // 0.446 --> 0.618
    _abc = abc >= 0.500 and abc <= 1.000  // 0.618 --> 0.886
    _bcd = bcd >= 1.250 and bcd <= 2.618  // 1.618 --> 2.618
    _xad = xad >= 0.500 and xad <= 1.250  // 1.130 --> 1.130
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

is5o(_mode) =>
    _xab = xab >= 1.13 and xab <= 1.618
    _abc = abc >= 1.618 and abc <= 2.24
    _bcd = bcd >= 0.5 and bcd <= 0.625  // 0.5
    _xad = xad >= 0.0 and xad <= 0.236  // negative?
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isWolf(_mode) =>
    _xab = xab >= 1.27 and xab <= 1.618
    _abc = abc >= 0 and abc <= 5
    _bcd = bcd >= 1.27 and bcd <= 1.618
    _xad = xad >= 0.0 and xad <= 5
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isHnS(_mode) =>
    _xab = xab >= 2.0 and xab <= 10
    _abc = abc >= 0.90 and abc <= 1.1
    _bcd = bcd >= 0.236 and bcd <= 0.88
    _xad = xad >= 0.90 and xad <= 1.1
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isConTria(_mode) =>
    _xab = xab >= 0.382 and xab <= 0.618
    _abc = abc >= 0.382 and abc <= 0.618
    _bcd = bcd >= 0.382 and bcd <= 0.618
    _xad = xad >= 0.236 and xad <= 0.764
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isExpTria(_mode) =>
    _xab = xab >= 1.236 and xab <= 1.618
    _abc = abc >= 1.000 and abc <= 1.618
    _bcd = bcd >= 1.236 and bcd <= 2.000
    _xad = xad >= 2.000 and xad <= 2.236
    _xab and _abc and _bcd and _xad and (_mode == 1 ? d < c : d > c)

isABCD__1 = isABCD(-1)
isABCD__2 = isABCD(-1)
plotshape(not showPatterns ? na : isABCD__1 and not isABCD__2[1], text='\nAB=CD', title='Bear ABCD', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top, offset=-2)
isBat__1 = isBat(-1)
isBat__2 = isBat(-1)
plotshape(not showPatterns ? na : isBat__1 and not isBat__2[1], text='Bat', title='Bear Bat', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top, offset=-2)
isAntiBat__1 = isAntiBat(-1)
isAntiBat__2 = isAntiBat(-1)
plotshape(not showPatterns ? na : isAntiBat__1 and not isAntiBat__2[1], text='Anti Bat', title='Bear Anti Bat', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top, offset=-2)
isAltBat__1 = isAltBat(-1)
isAltBat__2 = isAltBat(-1)
plotshape(not showPatterns ? na : isAltBat__1 and not isAltBat__2[1], text='Alt Bat', title='Bear Alt Bat', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isButterfly__1 = isButterfly(-1)
isButterfly__2 = isButterfly(-1)
plotshape(not showPatterns ? na : isButterfly__1 and not isButterfly__2[1], text='Butterfly', title='Bear Butterfly', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isAntiButterfly__1 = isAntiButterfly(-1)
isAntiButterfly__2 = isAntiButterfly(-1)
plotshape(not showPatterns ? na : isAntiButterfly__1 and not isAntiButterfly__2[1], text='Anti Butterfly', title='Bear Anti Butterfly', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isGartley__1 = isGartley(-1)
isGartley__2 = isGartley(-1)
plotshape(not showPatterns ? na : isGartley__1 and not isGartley__2[1], text='Gartley', title='Bear Gartley', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isAntiGartley__1 = isAntiGartley(-1)
isAntiGartley__2 = isAntiGartley(-1)
plotshape(not showPatterns ? na : isAntiGartley__1 and not isAntiGartley__2[1], text='Anti Gartley', title='Bear Anti Gartley', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isCrab__1 = isCrab(-1)
isCrab__2 = isCrab(-1)
plotshape(not showPatterns ? na : isCrab__1 and not isCrab__2[1], text='Crab', title='Bear Crab', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isAntiCrab__1 = isAntiCrab(-1)
isAntiCrab__2 = isAntiCrab(-1)
plotshape(not showPatterns ? na : isAntiCrab__1 and not isAntiCrab__2[1], text='Anti Crab', title='Bear Anti Crab', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isShark__1 = isShark(-1)
isShark__2 = isShark(-1)
plotshape(not showPatterns ? na : isShark__1 and not isShark__2[1], text='Shark', title='Bear Shark', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isAntiShark__1 = isAntiShark(-1)
isAntiShark__2 = isAntiShark(-1)
plotshape(not showPatterns ? na : isAntiShark__1 and not isAntiShark__2[1], text='Anti Shark', title='Bear Anti Shark', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
is5o__1 = is5o(-1)
is5o__2 = is5o(-1)
plotshape(not showPatterns ? na : is5o__1 and not is5o__2[1], text='5-O', title='Bear 5-O', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isWolf__1 = isWolf(-1)
isWolf__2 = isWolf(-1)
plotshape(not showPatterns ? na : isWolf__1 and not isWolf__2[1], text='Wolf Wave', title='Bear Wolf Wave', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isHnS__1 = isHnS(-1)
isHnS__2 = isHnS(-1)
plotshape(not showPatterns ? na : isHnS__1 and not isHnS__2[1], text='Head and Shoulders', title='Bear Head and Shoulders', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isConTria__1 = isConTria(-1)
isConTria__2 = isConTria(-1)
plotshape(not showPatterns ? na : isConTria__1 and not isConTria__2[1], text='Contracting Triangle', title='Bear Contracting triangle', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)
isExpTria__1 = isExpTria(-1)
isExpTria__2 = isExpTria(-1)
plotshape(not showPatterns ? na : isExpTria__1 and not isExpTria__2[1], text='Expanding Triangle', title='Bear Expanding Triangle', style=shape.labeldown, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0), location=location.top)

isABCD__3 = isABCD(1)
isABCD__4 = isABCD(1)
plotshape(not showPatterns ? na : isABCD__3 and not isABCD__4[1], text='AB=CD\n', title='Bull ABCD', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isBat__3 = isBat(1)
isBat__4 = isBat(1)
plotshape(not showPatterns ? na : isBat__3 and not isBat__4[1], text='Bat', title='Bull Bat', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAntiBat__3 = isAntiBat(1)
isAntiBat__4 = isAntiBat(1)
plotshape(not showPatterns ? na : isAntiBat__3 and not isAntiBat__4[1], text='Anti Bat', title='Bull Anti Bat', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAltBat__3 = isAltBat(1)
isAltBat__4 = isAltBat(1)
plotshape(not showPatterns ? na : isAltBat__3 and not isAltBat__4[1], text='Alt Bat', title='Bull Alt Bat', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isButterfly__3 = isButterfly(1)
isButterfly__4 = isButterfly(1)
plotshape(not showPatterns ? na : isButterfly__3 and not isButterfly__4[1], text='Butterfly', title='Bull Butterfly', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAntiButterfly__3 = isAntiButterfly(1)
isAntiButterfly__4 = isAntiButterfly(1)
plotshape(not showPatterns ? na : isAntiButterfly__3 and not isAntiButterfly__4[1], text='Anti Butterfly', title='Bull Anti Butterfly', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isGartley__3 = isGartley(1)
isGartley__4 = isGartley(1)
plotshape(not showPatterns ? na : isGartley__3 and not isGartley__4[1], text='Gartley', title='Bull Gartley', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAntiGartley__3 = isAntiGartley(1)
isAntiGartley__4 = isAntiGartley(1)
plotshape(not showPatterns ? na : isAntiGartley__3 and not isAntiGartley__4[1], text='Anti Gartley', title='Bull Anti Gartley', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isCrab__3 = isCrab(1)
isCrab__4 = isCrab(1)
plotshape(not showPatterns ? na : isCrab__3 and not isCrab__4[1], text='Crab', title='Bull Crab', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAntiCrab__3 = isAntiCrab(1)
isAntiCrab__4 = isAntiCrab(1)
plotshape(not showPatterns ? na : isAntiCrab__3 and not isAntiCrab__4[1], text='Anti Crab', title='Bull Anti Crab', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isShark__3 = isShark(1)
isShark__4 = isShark(1)
plotshape(not showPatterns ? na : isShark__3 and not isShark__4[1], text='Shark', title='Bull Shark', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isAntiShark__3 = isAntiShark(1)
isAntiShark__4 = isAntiShark(1)
plotshape(not showPatterns ? na : isAntiShark__3 and not isAntiShark__4[1], text='Anti Shark', title='Bull Anti Shark', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
is5o__3 = is5o(1)
is5o__4 = is5o(1)
plotshape(not showPatterns ? na : is5o__3 and not is5o__4[1], text='5-O', title='Bull 5-O', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isWolf__3 = isWolf(1)
isWolf__4 = isWolf(1)
plotshape(not showPatterns ? na : isWolf__3 and not isWolf__4[1], text='Wolf Wave', title='Bull Wolf Wave', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isHnS__3 = isHnS(1)
isHnS__4 = isHnS(1)
plotshape(not showPatterns ? na : isHnS__3 and not isHnS__4[1], text='Head and Shoulders', title='Bull Head and Shoulders', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isConTria__3 = isConTria(1)
isConTria__4 = isConTria(1)
plotshape(not showPatterns ? na : isConTria__3 and not isConTria__4[1], text='Contracting Triangle', title='Bull Contracting Triangle', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)
isExpTria__3 = isExpTria(1)
isExpTria__4 = isExpTria(1)
plotshape(not showPatterns ? na : isExpTria__3 and not isExpTria__4[1], text='Expanding Triangle', title='Bull Expanding Triangle', style=shape.labelup, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), location=location.bottom)

//-------------------------------------------------------------------------------------------------------------------------------------------------------------
fib_range = math.abs(d - c)
fib_0000 = not showFib0000 ? na : d > c ? d - fib_range * 0.000 : d + fib_range * 0.000
fib_0236 = not showFib0236 ? na : d > c ? d - fib_range * 0.236 : d + fib_range * 0.236
fib_0382 = not showFib0382 ? na : d > c ? d - fib_range * 0.382 : d + fib_range * 0.382
fib_0500 = not showFib0500 ? na : d > c ? d - fib_range * 0.500 : d + fib_range * 0.500
fib_0618 = not showFib0618 ? na : d > c ? d - fib_range * 0.618 : d + fib_range * 0.618
fib_0764 = not showFib0764 ? na : d > c ? d - fib_range * 0.764 : d + fib_range * 0.764
fib_1000 = not showFib1000 ? na : d > c ? d - fib_range * 1.000 : d + fib_range * 1.000
plot(title='Fib 0.000', series=fib_0000, color=fib_0000 != fib_0000[1] ? na : color.red)
plot(title='Fib 0.236', series=fib_0236, color=fib_0236 != fib_0236[1] ? na : color.red)
plot(title='Fib 0.382', series=fib_0382, color=fib_0382 != fib_0382[1] ? na : color.olive)
plot(title='Fib 0.500', series=fib_0500, color=fib_0500 != fib_0500[1] ? na : color.lime)
plot(title='Fib 0.618', series=fib_0618, color=fib_0618 != fib_0618[1] ? na : color.teal)
plot(title='Fib 0.764', series=fib_0764, color=fib_0764 != fib_0764[1] ? na : color.blue)
plot(title='Fib 1.000', series=fib_1000, color=fib_1000 != fib_1000[1] ? na : color.fuchsia)

change_2 = ta.change(time(tf))
bgcolor(not useAltTF ? na : change_2 != 0 ? color.black : na, transp=90)
f_last_fib(_rate) =>
    d > c ? d - fib_range * _rate : d + fib_range * _rate

target01_trade_size = input.float(title='Target 1 - Trade size:', defval=1)
target01_ew_rate = input(title='Target 1 - Fib. Rate to use for Entry Window:', defval=0.236)
target01_tp_rate = input(title='Target 1 - Fib. Rate to use for TP:', defval=1.618)
target01_sl_rate = input(title='Target 1 - Fib. Rate to use for SL:', defval=-0.436)
target02_active = input(title='Target 2 - Active?', defval=false)
target02_trade_size = input.float(title='Target 2 - Trade size:', defval=1)
target02_ew_rate = input(title='Target 2 - Fib. Rate to use for Entry Window:', defval=0.236)
target02_tp_rate = input(title='Target 2 - Fib. Rate to use for TP:', defval=1.618)
target02_sl_rate = input(title='Target 2 - Fib. Rate to use for SL:', defval=-0.236)

disableRepaint = input(title='Disable Repainting?', defval=false)

buy_patterns_00 = isABCD(1) or isBat(1) or isAltBat(1) or isButterfly(1) or isGartley(1) or isCrab(1) or isShark(1) or is5o(1) or isWolf(1) or isHnS(1) or isConTria(1) or isExpTria(1) and not disableRepaint
buy_patterns_01 = isAntiBat(1) or isAntiButterfly(1) or isAntiGartley(1) or isAntiCrab(1) or isAntiShark(1) and not disableRepaint
sel_patterns_00 = isABCD(-1) or isBat(-1) or isAltBat(-1) or isButterfly(-1) or isGartley(-1) or isCrab(-1) or isShark(-1) or is5o(-1) or isWolf(-1) or isHnS(-1) or isConTria(-1) or isExpTria(-1) and not disableRepaint
sel_patterns_01 = isAntiBat(-1) or isAntiButterfly(-1) or isAntiGartley(-1) or isAntiCrab(-1) or isAntiShark(-1) and not disableRepaint

target01_buy_entry = (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(target01_ew_rate) and not disableRepaint
target01_buy_close = high >= f_last_fib(target01_tp_rate) or low <= f_last_fib(target01_sl_rate) and not disableRepaint
target01_sel_entry = (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(target01_ew_rate) and not disableRepaint
target01_sel_close = low <= f_last_fib(target01_tp_rate) or high >= f_last_fib(target01_sl_rate) and not disableRepaint

strategy.entry('target01_buy', direction=strategy.long, qty=target01_trade_size, comment='buy 01', when=target01_buy_entry)
strategy.close('target01_buy', when=target01_buy_close)
strategy.entry('target01_sell', direction=strategy.short, qty=target01_trade_size, comment='sell 01', when=target01_sel_entry)
strategy.close('target01_sell', when=target01_sel_close)

target02_buy_entry = target02_active and (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(target02_ew_rate) and not disableRepaint
target02_buy_close = target02_active and high >= f_last_fib(target02_tp_rate) or low <= f_last_fib(target02_sl_rate) and not disableRepaint
target02_sel_entry = target02_active and (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(target02_ew_rate) and not disableRepaint
target02_sel_close = target02_active and low <= f_last_fib(target02_tp_rate) or high >= f_last_fib(target02_sl_rate) and not disableRepaint

strategy.entry('target02_buy', direction=strategy.long, qty=target02_trade_size, comment='buy 02', when=target02_buy_entry)
strategy.close('target02_buy', when=target02_buy_close)
strategy.entry('target02_sell', direction=strategy.short, qty=target02_trade_size, comment='sell 02', when=target02_sel_entry)
strategy.close('target02_sell', when=target02_sel_close)

titles = input(title='EMA/SMA:', defval=true)

//EMA
len = input.int(13, minval=1, title='Length')
src = input(close, title='Source')
out = ta.ema(src, len)
plot(out, color=color.new(#28B463, 0), linewidth=1, title='EMA')
//SMA 1
len1 = input.int(21, minval=1, title='Length')
src1 = input(close, title='Source')
out1 = ta.sma(src1, len1)
plot(out1, color=color.new(#D4AC0D, 0), linewidth=1, title='SMA')
//SMA 2
len2 = input.int(55, minval=1, title='Length')
src2 = input(close, title='Source')
out2 = ta.sma(src2, len2)
plot(out2, color=color.new(#FF0033, 0), linewidth=1, title='SMA')
//SMA 3
len3 = input.int(8, minval=1, title='Length')
src3 = input(close, title='Source')
out3 = ta.sma(src3, len3)
plot(out3, color=color.new(#0033FF, 0), linewidth=1, title='SMA')

jostar
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: October 31st, 2023

Re: Convert Pine script 2 to 5

Thank you very much for the conversion to V5!
In case I do use alttimeframe (e.g. 10 minutes) the zigzag of the v5 seems to have a 10 minutes delay versus the v2.
This means the plot has the same shape but 10 minutes later... which is a pitty because all opportunities are gone :-(

I've checked the script and I don't see any error.
Please could you investigate, I am still a Rookie in Pinescript and couldn't find the issue.

Thanks!

diazluise
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: November 29th, 2023
Contact: TradingView Profile

Re: Convert Pine script 2 to 5

Can any member please help me convert this script from V2 to V5?:

===============================================================================
//@version=2
//Original author: Rajandran R from www.marketcalls.in

study("Supertrend Alerts 1.1", overlay = true)

cand_col = input(title="Color Candles", type=bool, defval=true)
show_line = input(title="Show Supertrend Line", type=bool, defval=true)
Factor = input(3, type=float)
PD = input(7, minval = 1, maxval = 100)


Up=hl2-(Factor*atr(PD))
Dn=hl2+(Factor*atr(PD))

TrendUp = close[1] > TrendUp[1] ? max(Up, TrendUp[1]) : Up
TrendDown = close[1] < TrendDown[1] ? min(Dn, TrendDown[1]) : Dn

Trend = close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1], 1)
TSL = Trend == 1 ? TrendUp : TrendDown

linecolor = Trend == 1 ? color(green, 0) : color(red, 0)
barcolor((cand_col and Trend == 1) ? color(green, 0) : na)
barcolor((cand_col and Trend == -1) ? color(red, 0) : na)

plot(show_line ? TSL : na, color = linecolor , style = line , linewidth = 2, title = "SuperTrend Line", transp=65)

newup = cross(close,TSL) and close>TSL
newdn = cross(TSL,close) and close<TSL

plotshape(newup, "Long", shape.triangleup, location.belowbar, color(green, 0), size=size.normal)
plotshape(newdn, "Short", shape.triangledown , location.abovebar, color(red, 0), size=size.normal)

alertcondition(newup, title='Supertrend Long', message='Supertrend: Long ')
alertcondition(newdn, title='Supertrend Short', message='Supertrend: Short ')
alertcondition(newup or newdn, title='Supertrend Signal', message='Supertrend: Long or Short ')

funasoft
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: December 9th, 2023

Re: Convert Pine script 2 to 5

@jostar
I have the same problem.
It seem the time shift 1 hour later.
Maybe we have to adjust something in request.security(_ticker, tf, zigzag_1) ?

funasoft
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: December 9th, 2023

Re: Convert Pine script 2 to 5

@jostar
i should be request.security(_ticker, tf, zigzag_1, lookahead = barmerge.lookahead_on)

Return to “Pine Script Q&A”