//@version=2
strategy(title='Strat', shorttitle='Strat', overlay=true, pyramiding=0, currency=currency.USD)
// Inputs
useAltTF = input(true, title='Use Alt Timeframe')
tf = input('60', 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=true)
showFib0382 = input(title='Display Fibonacci 0.382:', type=bool, defval=true)
showFib0500 = input(title='Display Fibonacci 0.500:', type=bool, defval=true)
showFib0618 = input(title='Display Fibonacci 0.618:', type=bool, defval=true)
showFib0764 = input(title='Display Fibonacci 0.764:', type=bool, defval=true)
showFib1000 = input(title='Display Fibonacci 1.000:', type=bool, defval=true)
// ZigZag function
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
// Calculate ZigZag values
_ticker = tickerid
sz = useAltTF ? (change(time(tf)) != 0 ? security(_ticker, tf, zigzag()) : na) : zigzag()
// 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))
// Patterns
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)
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)
// Labels
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 : isAntiBat(-1) and not isAntiBat(-1)[1], text="Anti Bat", title='Bear Anti Bat', style=shape.labeldown, color=maroon, textcolor=white, location=location.abovebar, transp=0, offset=-2)
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 : isAntiBat(1) and not isAntiBat(1)[1], text="Anti Bat", title='Bull Anti Bat', style=shape.triangleup, color=green, textcolor=white, location=location.belowbar, transp=0)
// Calculate Fibonacci levels
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)
// Function to calculate Fibonacci levels based on rate
f_last_fib(_rate) =>
d > c ? d - (fib_range * _rate) : d + (fib_range * _rate)
// Input parameters
target01_trade_size = input(title='Target 1 - Trade size:', type=float, defval=10000.00)
target01_ew_rate = input(title='Target 1 - Fib. Rate for Entry Window:', type=float, defval=0.236)
target01_tp_rate = input(title='Target 1 - Fib. Rate for TP:', type=float, defval=0.618)
target01_sl_rate = input(title='Target 1 - Fib. Rate for SL:', type=float, defval=-0.236)
// Define chart patterns
buy_patterns_00 = isAltBat(1)
buy_patterns_01 = isAntiBat(1)
sel_patterns_00 = isAltBat(-1)
sel_patterns_01 = isAntiBat(-1)
// Define entry and close conditions for Target 1
target01_buy_entry = (buy_patterns_00 or buy_patterns_01) and close <= f_last_fib(target01_ew_rate)
target01_buy_close = high >= f_last_fib(target01_tp_rate) or low <= f_last_fib(target01_sl_rate)
target01_sel_entry = (sel_patterns_00 or sel_patterns_01) and close >= f_last_fib(target01_ew_rate)
target01_sel_close = low <= f_last_fib(target01_tp_rate) or high >= f_last_fib(target01_sl_rate)
// Execute Target 1 trades
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)