Price breakout and reversal [TCS] | PA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zendrer

//@version=5
indicator(title='TCS•BREAKS AND REVERSALS', shorttitle='TCS•BREAKS AND REVERSALS', overlay=true, max_bars_back=5000)

//••••••••••••••
//INPUT
//••••••••••••••
//----------------------------------------------------------------
breaks = input(true, title='SHOW BREAKS')
reversals = input(true, title='SHOW REVERSALS')
fairbreak = input(true, title='SHOW FAIR VALUE BREAKS')
leftBars = input(8, title='LOOKBACK')
//----------------------------------------------------------------

//----------------------------------------------------------------
//CRYPTO RISK CALC
//----------------------------------------------------------------
labels = input(true, "SHOW RISK", group = "ACCOUNT RISK - CRYPTO")
risk = input.float(3, "ACCOUNT RISK % ", step = 0.1, group = "ACCOUNT RISK - CRYPTO")
account = input(10000,"ACCOUNT AMOUNT ", group = "ACCOUNT RISK - CRYPTO")
fees = input(0.07,"BROKER FEES ENTRY %", group = "ACCOUNT RISK - CRYPTO")
feesex = input(0.07,"BROKER FEES EXIT %", group = "ACCOUNT RISK - CRYPTO")
//----------------------------------------------------------------

//••••••••••••••
//FUNCTION
//••••••••••••••
//----------------------------------------------------------------
highPivot = fixnan(ta.pivothigh(leftBars, 0)[1])
lowPivot = fixnan(ta.pivotlow(leftBars, 0)[1])
fairvalue = fixnan((highPivot+lowPivot)/2)
//----------------------------------------------------------------

//BB TO FILTER REVERSALS
//----------------------------------------------------------------
length = 20
src = close
mult = 2
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
//----------------------------------------------------------------

//BULLISH CONDITIONS
//----------------------------------------------------------------
bullishbreaks = ta.crossover(close, highPivot) and not (open - low > close - open) and breaks
bullishfairbreaks = ta.crossover(close, fairvalue) and not (open - low > close - open) and fairbreak
bullishreversals = ((ta.crossover(close, lowPivot[1]) and not (open - low > close - open)) or (low < lowPivot[1] and close > lowPivot[1] and open > lowPivot[1] and close > open))  and reversals and ta.crossover(close,lower)
//----------------------------------------------------------------

//BEARISH CONDITIONS
//----------------------------------------------------------------
bearishbreaks = ta.crossunder(close, lowPivot) and not (open - close < high - open) and breaks
bearishfairbreaks = ta.crossunder(close, fairvalue) and not (open - close < high - open) and fairbreak
bearishreversals = ((ta.crossunder(close, highPivot[1]) and not (open - close < high - open)) or (high > highPivot[1] and close < highPivot[1] and open < highPivot[1] and close < open)) and reversals and ta.crossunder(close,upper)
//----------------------------------------------------------------

//SIZE CALC LONG
//----------------------------------------------------------------
changesl = (((close/lowPivot)-1)*100)+fees 
accval = risk*account
relative = (changesl*accval)/risk
accquant = accval/relative
//----------------------------------------------------------------
//SIZE CALC SHORT
//----------------------------------------------------------------
changesl1 = (((highPivot/close)-1)*100)+fees 
accval1 = risk*account
relative1 = (changesl1*accval1)/risk
accquant1 = accval1/relative1
//----------------------------------------------------------------
//SIZE CALC LONG FAIR VALUE
//----------------------------------------------------------------
changesl2 = (((close/fairvalue)-1)*100)+fees 
accval2 = risk*account
relative2 = (changesl2*accval2)/risk
accquant2 = accval2/relative2
//----------------------------------------------------------------
//SIZE CALC SHORT FAIR VALUE
//----------------------------------------------------------------
changesl3 = (((fairvalue/close)-1)*100)+fees 
accval3 = risk*account
relative3 = (changesl3*accval3)/risk
accquant3 = accval3/relative3
//----------------------------------------------------------------

//••••••••••••••
//PLOT
//••••••••••••••
//----------------------------------------------------------------
plot(highPivot, color = ta.change(highPivot) ? na : color.new(color.lime,0), linewidth = 2, title ='HIGH PIVOT')
plot(lowPivot, color = ta.change(lowPivot) ? na : color.new(color.purple, 0), linewidth = 2, title ='LOW PIVOT')
plot(fairvalue, color = color.new(color.gray, 0), linewidth = 1, style = plot.style_circles, title = 'FAIR VALUE')

plotshape(bullishbreaks, title = 'BULLISH BREAK', text = 'B', color = color.new(color.lime, 0), style = shape.triangleup, location = location.belowbar, textcolor = color.new(color.white, 0), size = size.tiny)
plotshape(bearishbreaks, title = 'BEARISH BREAK', text = 'S', color = color.new(color.purple, 0), style = shape.triangledown, location = location.abovebar, textcolor = color.new(color.white, 0), size = size.tiny)
plotshape(bullishfairbreaks, title ='FAIR BULLISH BREAK', color = color.new(color.lime, 0), style = shape.circle, location = location.belowbar, size = size.tiny)
plotshape(bearishfairbreaks, title = 'FAIR BEARISH BREAK', color = color.new(color.purple, 0), style = shape.circle, location = location.abovebar, size = size.tiny)
plotshape(bullishreversals, title = 'BULLISH REVERSAL', text = 'R+', color = color.new(color.lime, 0), style = shape.diamond, location = location.belowbar, textcolor = color.new(color.white, 0), size = size.tiny)
plotshape(bearishreversals, title = 'BULLISH REVERSAL', text = 'R-', color = color.new(color.purple, 0), style = shape.diamond, location = location.abovebar, textcolor = color.new(color.white, 0), size = size.tiny)
//----------------------------------------------------------------

//----------------------------------------------------------------
accqty =  (labels ? label.new(bar_index+9, close[1], color =color.new(color.white,100), style = label.style_label_left, text="QTY x "+ "|LONG ON LOW| x" + str.tostring(accquant,"#.##") + " • |SHORT ON HIGH| x" + str.tostring(accquant1,"#.##") + " \n • |LONG ON FAIR| x" + str.tostring(accquant2,"#.##") + " • |SHORT ON FAIR| x" + str.tostring(accquant3,"#.##"), textcolor = color.new(color.white, 20), textalign = text.align_left) : na)
label.delete(accqty[1])
//----------------------------------------------------------------

//••••••••••••••
//ALERTS
//••••••••••••••
//----------------------------------------------------------------
alertcondition(bullishbreaks, title = 'Bullish breakout', message = 'Bullish breakout')
alertcondition(bearishbreaks, title = 'Bearish breakout', message = 'Bearish breakout')
alertcondition(bullishfairbreaks, title = 'Bullish fair value breakout', message = 'Bullish fair value breakout')
alertcondition(bearishfairbreaks, title = 'Bearish fair value breakout', message = 'Bearish fair value breakout')
alertcondition(bullishreversals, title = 'Bullish reversal', message = 'Bullish reversal')
alertcondition(bearishreversals, title = 'Bearish reversal', message = 'Bearish reversal')
//----------------------------------------------------------------
//END


