Lex_3CR_Functions_Library2
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LexingtonStanley
//@version=5 
// testing
// @description This is a source code for a technical analysis library in Pine Script language, 
//designed to identify and mark Bullish and Bearish Three Candle Reversal (3CR) chart patterns. 
//The library provides three functions to be used in a trading algorithm. 
//The first function, Bull_3crMarker, adds a dashed line and label to a Bullish 3CR chart pattern, indicating the 3CR point. 
//The second function, Bear_3crMarker, adds a dashed line and label to a Bearish 3CR chart pattern. 
//The third function, Bull_3CRlogicals, checks for a Bullish 3CR pattern where the first candle's low is greater than the second candle's low and the second candle's low is less than the third candle's low. 
//If found, creates a line at the breakout point and a label at the fail point, 
//if specified. All functions take parameters such as the chart pattern's characteristics and output colors, labels, and markers.
library("Lex_3CR_Functions_Library2", overlay = true)
// @function Bull_3crMarker Adds a 3CR marker to a Bullish 3CR chart pattern
// @description Adds a dashed line and label to a 3CR up chart pattern, indicating the 3CR (3 Candle Reversal) point.
// @param array linearray: An array of lines to which the marker will be added
// @param int barnum: The bar number at which to add the marker
// @param float breakpoint: The price level at which to draw the marker
// @param array failpointB: An array of 3cr float pivot point price levels
// @param float failpoint: The current pivot point price
// @param color linecolorbull: The color of the marker line
// @param array labelarray: An array of labels to which the marker label will be added
// @param color labelcolor: The color of the marker label
// @param color textcolor: The color of the marker label text
// @param bool labelon: Determines whether the marker label is displayed
export Bull_3crMarker(line[] bulllinearray, int barnum, float breakpoint, float[] failpointB, float failpoint , color linecolorbull, label[] bulllabelarray, color labelcolor, color textcolor, bool labelon)=>
	colored 		= linecolorbull
	coloredlabel 	= labelcolor
	colortext 		= textcolor
	array.push(bulllinearray,line.new(barnum-3, breakpoint, barnum-1, breakpoint, extend = extend.right, style = line.style_dashed, width = 1, color = colored))
	array.push(failpointB, failpoint)
	if labelon
	    array.push(bulllabelarray,label.new(barnum-2, failpoint, "3CR Up", color = coloredlabel,style= label.style_triangleup, textcolor =  colortext, size = size.small, yloc = yloc.belowbar))
// @function Bear_3crMarker Adds a 3CR marker to a Bearish 3CR chart pattern
// @description Adds a dashed line and label to a 3CR down chart pattern, indicating the 3CR (3 Candle Reversal) point.
// @param array linearray An array of lines to which the marker will be added
// @param int barnum The bar number at which to add the marker
// @param float breakpoint The price level at which to draw the marker
// @param array failpointB An array of 3cr float pivot point price levels
// @param float failpoint The current pivot point price
// @param color linecolorbear The color of the marker line
// @param array labelarray An array of labels to which the marker label will be added
// @param color labelcolor The color of the marker label
// @param color textcolor The color of the marker label text
// @param bool labelon Determines whether the marker label is displayed
export Bear_3crMarker(line[] bearlinearray, int barnum, float breakpoint, float[] failpointB, float failpoint , color linecolorbear, label[] bearlabelarray, color labelcolor, color textcolor, bool labelon)=>
	colored 		= linecolorbear
	coloredlabel	= labelcolor
	colortext 		= textcolor
	array.push(bearlinearray,line.new(barnum-3, breakpoint, barnum-1, breakpoint, extend = extend.right, style = line.style_dashed, width = 1, color = colored))
	array.push(failpointB, failpoint)
	if labelon
	    array.push(bearlabelarray,label.new(barnum-2, failpoint, "3CR Down", color = coloredlabel,style= label.style_triangledown, textcolor =  colortext, size = size.small, yloc = yloc.abovebar))
// @function Checks for a bullish three candle reversal pattern and creates a line and label at the breakout point if found
// @description Checks for a bullish three candle reversal pattern where the first candle's low is greater than the second candle's low and the second candle's low is less than the third candle's low. If found, creates a line at the breakout point and a label at the fail point, if specified.
// @param float low1 The low of the first candle in the pattern
// @param float low2 The low of the second candle in the pattern
// @param float low3 The low of the third candle in the pattern
// @param line bulllinearray The array containing the bullish lines
// @param label bulllabelarray The array containing the bullish labels
// @param float failpointB An array to store the fail point value (pass by reference)
// @param color linecolorbull The color of the bullish line
// @param color labelcolor The color of the bullish label
// @param color textcolor The color of the label text
// @param bool labelon Whether to create a label at the fail point (default = false)
export Bull_3CRlogicals(float low1, float low2, float low3, line[]bulllinearray, label[]bulllabelarray, float[]failpointB, color linecolorbull, color labelcolor, color textcolor, bool labelon)=>
	if low1 > low2 and low2 < low3
		barnum 		= bar_index
		breakpoint 	= high[3]
		failpoint 	= low[2]
		boolies		= labelon
		colorline 	= linecolorbull
		colortext 	= textcolor
		colorlabel 	= labelcolor
		bullarraye  = bulllinearray
		bullarrayl  = bulllabelarray
		failbull	= failpointB
		Bull_3crMarker(bullarraye,barnum,breakpoint,failbull,failpoint,colorline,bullarrayl, colorlabel, colortext,boolies)
// @function Checks for a Bearish 3CR pattern and draws a bearish marker on the chart at the appropriate location
// @description This function checks for a Bearish 3CR (Three-Candle Reversal) pattern, which is defined as the second candle having a higher high than the first and third candles, and the third candle having a lower high than the first candle. If the pattern is detected, a bearish marker is drawn on the chart at the appropriate location, and an optional label can be added to the marker.
// @param float high1 The highest high of the first candle in the pattern
// @param float high2 The highest high of the second candle in the pattern
// @param float high3 The highest high of the third candle in the pattern
// @param line bearlinearray The array containing the bearish lines to be drawn
// @param label bearlabelarray The array containing the bearish labels to be added (if any)
// @param float failpointB The array containing the fail points for the bearish markers (if any)
// @param color linecolorbear The color of the bearish marker line
// @param color labelcolor The color of the bearish marker label (if any)
// @param color textcolor The color of the text in the bearish marker label (if any)
// @param bool labelon Whether to add a label to the bearish marker (default = true)
export Bear_3CRlogicals(float high1, float high2, float high3,line[]bearlinearray, label[]bearlabelarray, float[]failpointB, color linecolorbear, color labelcolor, color textcolor, bool labelon)=>
	if high1 < high2 and high2 > high3
		barnum 		= bar_index
		breakpoint 	= low[3]
		failpoint 	= high[2]
		boolies		= labelon
		colorline 	= linecolorbear
		colortext 	= textcolor
		colorlabel 	= labelcolor
		beararraye  = bearlinearray
		beararrayl  = bearlabelarray
		failbear	= failpointB
		Bear_3crMarker(beararraye,barnum,breakpoint,failbear,failpoint,colorline,beararrayl, colorlabel, colortext,boolies)
// @function Removes a bullish line from a specified position in a line array, and optionally removes a label associated with that line
// @description Removes a bullish line from a specified position in a line array, and optionally removes a label associated with that line.
// @param int i The index of the line to remove
// @param int barbreak The index of the bar at which the new line will begin
// @param line bulllinearray The array containing the bullish lines
// @param label bulllabelarray The array containing the bullish labels
// @param bool labelon Whether to also remove the label associated with the line (default = true)
// @param color bullcolor The color of the bullish line (default = na)
// @return a new bullish 3cr line
export bullLineDelete(int i, line[]bulllinearray, float[]failarray, label[]bulllabelarray, bool labelon)=>
	x = array.get(bulllinearray, i)
	highp = line.get_y1(x)
	barnum = line.get_x1(x)
	array.remove(failarray, i)
	if labelon
		currentlabel = array.get(bulllabelarray, i)
		array.remove(bulllabelarray,i)
		label.delete(currentlabel)
	array.remove(bulllinearray, i)
	line.delete(x)
	//p = line.new(barbreak-1, highp, barnum, highp, style = line.style_solid, width = 2, color = bullcolor )
// @function Removes a bearish line from a specified position in a line array, and optionally removes a label associated with that line
// @description Removes a bearish line from a specified position in a line array, and optionally removes a label associated with that line.
// @param int i The index of the line to remove
// @param int barbreak The index of the bar at which the new line will begin
// @param line bearlinearray The array containing the bearish lines
// @param label bearlabelarray The array containing the bearish labels
// @param bool labelon Whether to also remove the label associated with the line (default = true)
// @param color bearcolor The color of the bearish line (default = na)
// @return a new bearish 3cr line
export bearLineDelete(int i, line[] bearlinearray,float[]failarray, label[]bearlabelarray, bool labelon)=>
	x = array.get(bearlinearray, i)
	lowp = line.get_y1(x)
	barnum = line.get_x1(x)
	array.remove(failarray, i)
	if labelon
		currentlabel = array.get(bearlabelarray, i)
		array.remove(bearlabelarray,i)
		label.delete(currentlabel)
	array.remove(bearlinearray, i)
	line.delete(x)
	//p = line.new(barbreak, lowp, barnum, lowp, style = line.style_solid, width = 2, color = bearcolor )

// @function Removes a bullish line from a specified position in a line array, and optionally removes a label associated with that line
// @description Removes a bullish line from a specified position in a line array, and optionally removes a label associated with that line.
// @param int i The index of the line to remove
// @param line bulllinearray The array containing the bullish lines
// @param label0 bulllabelarray The array containing the bullish labels
// @param bool labelon Whether to also remove the label associated with the line (default = true)
// @return void
export bulloffsetdelete(int i, line[]bulllinearray, float[]failarray, label[]bulllabelarray, bool labelon)=>
	x = array.get(bulllinearray, i)
	array.remove(failarray, i)
	if labelon
		currentlabel = array.get(bulllabelarray, i)
		array.remove(bulllabelarray,i)
		label.delete(currentlabel)
	array.remove(bulllinearray, i)
	line.delete(x)
// @function Removes a bearish line from a specified position in a line array, and optionally removes a label associated with that line
// @description Removes a bearish line from a specified position in a line array, and optionally removes a label associated with that line.
// @param int i The index of the line to remove
// @param line bearlinearray The array containing the bearish lines
// @param label bearlabelarray The array containing the bearish labels
// @param bool labelon Whether to also remove the label associated with the line (default = true)
// @return void
export bearoffsetdelete(int i, line[]bearlinearray, float[]failarray, label[]bearlabelarray, bool labelon) =>
	x = array.get(bearlinearray, i)
	array.remove(failarray, i)
	if labelon
		currentlabel = array.get(bearlabelarray, i)
		array.remove(bearlabelarray,i)
		label.delete(currentlabel)
	array.remove(bearlinearray, i)
	line.delete(x)
// @function Checks if the specified value is greater than the break point of any bullish line in an array, and removes that line if true
// @description Checks if the s pecified value is greater than the break point of any bullish line in an array, and removes that line if true.
// @param float close1 The value to compare against the bullish lines' break points
// @param line bulllinearray The array containing the bullish lines
// @param label bulllabelarray The array containing the bullish labels
// @param bool labelt Whether to remove the label associated with the line (default = true)
// @param color bullcolored The color of the bullish line (default = na)
//float riskperc = input.float(1,"Risk %",1,100)
//float tp       = input.float(1, "RR", 0.1, 100,0.5)
//float RRperc   = input.float(1,"Perc to close all")
//bool RRbool    = input.bool(true, "Use RR for Close or not for perc")
//line a = na
//float b = na
//float c = na 
//RRperc := RRperc / 100
//equity = strategy.equity 
//riskperc := riskperc/100
//if Q and (RSI < rsihigh) and array.size(bullLinet) > 0
//    a    := array.get(bullLinet, array.size(bullLinet) - 1)
//    b    := line.get_y1(a)
//    c    := array.get(failarraybull, array.size(failarraybull) - 1)
//    lotss = (equity * riskperc) / (b - c)
//    lots  = math.round(lotss,00)
//    entry = b - ((b - c) / 2)
//    stop  = c// - ATR
//    RR    = entry - stop
//    strategy.entry("3CR Long", strategy.long, lots,limit = entry)
//    if RRbool
//        strategy.exit("r", "3CR Long" ,qty_percent = 100, limit = entry + tp * RR,stop = stop)

export BullEntry_setter(int i, line[]bulllinearray, float[]failpointB, float[]entrystopB, float[]entryB, bool[]entryboolB)=>
	bool QB = false
	a = array.get(bulllinearray, i)			
	b = line.get_y1(a)
	c = array.get(failpointB   , i)
	array.push(entrystopB,c)
	array.push(entryB, b)
	array.push(entryboolB, QB)
	//strategy.entry("3CR Bull", strategy.long, lots, entry, stop)
	//if RRbool
	//	strategy.exit("EXIT", "3CR Bull",qty_percent = 100, limit = entry + (tp * RR))

export Bull3CRchecker(float close1, line[]bulllinearray,float[]FailpointB,float[]rsiB, label[]bulllabelarray, bool labelt, color bullcolored, label[]directionarray, float rsi, line[]secondbullline)=>//, float[]entrystopB, float[]entryB, bool[]entryboolB) =>
	labelon = labelt
	bullybline = bulllinearray
	bullyblabel = bulllabelarray
	bullcolor = bullcolored
	failarray = FailpointB
	rizza = rsi
	bool Q = false 
	if array.size(bulllinearray) > 0 
		for i = array.size(bulllinearray) - 1 to 0
			r3c = array.get(bulllinearray, i)
			breakp = line.get_y1(r3c)
			l = line.get_x1(r3c)
			if close1 > breakp //and rsi < rsiinput
				barindex = bar_index
				array.push(directionarray,label.new(bar_index,close,labelt ? "Up" : na,text_font_family =  font.family_monospace,yloc = yloc.abovebar, color = bullcolor, style = label.style_arrowup, textcolor = bullcolor, size = size.small, textalign = text.align_right))
				if array.size(directionarray) >= 2	
					x = array.remove(directionarray,array.size(directionarray) - 2)
					label.delete(x)
				array.push(secondbullline,line.new(bar_index[1], breakp, l, breakp, style = line.style_solid, width = 2, color = bullcolor ))
				array.push(rsiB, rsi)
				//BullEntry_setter(i,bullybline,failarray,entrystopB,entryB,entryboolB)
				bullLineDelete(i,bullybline,failarray,bullyblabel,labelon)
				Q := true
	Q			
// @function Checks if the specified value is less than the break point of any bearish line in an array, and removes that line if true
// @description Checks if the specified value is less than the break point of any bearish line in an array, and removes that line if true.
// @param float close1 The value to compare against the bearish lines' break points
// @param line bearlinearray The array containing the bearish lines
// @param label bearlabelarray The array containing the bearish labels
// @param bool labelt Whether to remove the label associated with the line
// @param color bearcolored The color of the bearish line		
export Bear3CRchecker(float close1, line[]bearlinearray,float[]FailpointB, label[]bearlabelarray, bool labelt, color bearcolored, label[]directionarray, float rsi, line[]secondbearline, float[]rsiB) =>
	labelon = labelt
	bearybline 	= bearlinearray
	bearyblabel = bearlabelarray
	bearcolor 	= bearcolored
	failarray   = FailpointB
	rizza = rsi
	bool Q = false
	if array.size(bearlinearray) > 0 
		for i = array.size( bearlinearray) - 1 to 0
			r3c = array.get(bearlinearray, i)
			breakp = line.get_y1(r3c)
			l = line.get_x1(r3c)
			if close1 < breakp //and rsi > rsiinput
				barindex = bar_index
				array.push(directionarray,label.new(bar_index,close,labelt ? "Down" : na, text_font_family =  font.family_monospace,yloc = yloc.belowbar, color = bearcolor, style = label.style_arrowdown, textcolor = bearcolor, size = size.small, textalign = text.align_right))
				if array.size(directionarray) >= 2
					x = array.remove(directionarray,array.size(directionarray) - 2)
					label.delete(x) 
				array.push(secondbearline,line.new(bar_index[1], breakp, l, breakp, style = line.style_solid, width = 2, color = bearcolor ))
				array.push(rsiB, rsi)
				bearLineDelete(i,bearybline,failarray,bearyblabel,labelon)
				Q := true
	Q
// @function Checks the offset of bullish lines and deletes them if they are beyond a certain offset from the current bar index
// @description Checks the offset of bullish lines and deletes them if they are beyond a certain offset from the current bar index
// @param float FailpointB The array of fail points to check against
// @param label bulllabelarray The array containing the bullish labels
// @param line linearray The array containing the bullish lines
// @param bool labelt Whether to remove the label associated with the line (default = true)
// @param int offset The maximum allowed offset from the current bar index
export Bulloffsetcheck(float[]FailpointB, label[]bulllabelarray, line[]linearray, bool labelt, int offset)=>
	labelon = labelt
	failarray = FailpointB
	bulllabel = bulllabelarray
	lineyface = linearray
	if array.size(lineyface) > 0 
		for i = array.size(linearray) - 1 to 0
			barting = array.get(linearray, i )
			bartingb = line.get_x1(barting)
			if bar_index - bartingb > offset
				bulloffsetdelete(i,lineyface,failarray,bulllabel,labelon)
// @function Checks the offset of bearish lines and deletes them if they are beyond a certain offset from the current bar index
// @description Checks the offset of bearish lines and deletes them if they are beyond a certain offset from the current bar index
// @param float FailpointB The array of fail points to check against
// @param label bearlabelarray The array containing the bearish labels
// @param line linearray The array containing the bearish lines
// @param bool labelt Whether to remove the label associated with the line (default = true)
// @param int offset The maximum allowed offset from the current bar index
export Bearoffsetcheck(float[]FailpointB, label[]bearlabelarray, line[]linearray, bool labelt, int offset)=>
	labelon = labelt
	failarray = FailpointB
	bearlabel = bearlabelarray
	lineyface = linearray
	if array.size(lineyface) > 0 
		for i = array.size(linearray) - 1 to 0
			barting = array.get(linearray, i )
			bartingb = line.get_x1(barting)
			if bar_index - bartingb > offset
				bearoffsetdelete(i,lineyface,failarray,bearlabel,labelon)
// @function Checks if the current price has crossed above a bullish fail point and deletes the corresponding line and label
// @description Checks if the current price has crossed above a bullish fail point and deletes the corresponding line and label
// @param float close1 The current price
// @param float FailpointB The array of bullish fail points
// @param label bulllabelarray The array containing the bullish labels
// @param line linearray The array containing the lines to check for deletion
// @param bool labelt Whether to delete the label associated with the line (default = true)
export Bullfailchecker(float close1, float[]FailpointB, label[]bulllabelarray, line[]linearray, bool labelt)=>
	labelon = labelt
	failarray = FailpointB
	bulllabel = bulllabelarray
	lineyface = linearray
	if array.size(lineyface) > 0 
		for i = array.size(failarray) - 1 to 0
			fail = array.get(failarray, i)
			if close1 < fail
				bulloffsetdelete(i,lineyface,failarray,bulllabel,labelon)
// @function Checks for bearish lines that have failed to trigger and removes them from the chart
// @description This function checks for bearish lines that have failed to trigger (i.e., where the current price is above the fail point) and removes them from the chart along with any associated label.
// @param float close1 The current closing price
// @param float FailpointB An array containing the fail points of the bearish lines
// @param label bearlabelarray The array containing the bearish labels
// @param line linearray The array containing the bearish lines
// @param bool labelt Whether to also remove the label associated with the line (default = true)
// @return void			
export Bearfailchecker(float close1, float[]FailpointB, label[]bearlabelarray, line[]linearray, bool labelt)=>
	labelon = labelt
	failarray = FailpointB
	bearlabel = bearlabelarray
	lineyface = linearray
	if array.size(lineyface) > 0 
		for i = array.size(failarray) - 1 to 0
			fail = array.get(failarray, i)
			if close1 > fail
				bearoffsetdelete(i,lineyface,failarray,bearlabel,labelon)
// @function Checks for bullish RSI lines that have failed to trigger and removes them from the chart
// @description This function checks for bullish RSI lines that have failed to trigger (i.e., where the current RSI value is below the line's trigger level) and removes them from the chart along with any associated line.
// @param float rsiinput The current RSI value
// @param float rsiBull An array containing the trigger levels for bullish RSI lines
// @param line secondbullline The array containing the bullish RSI lines
// @return void
export rsibullchecker(float rsiinput, float[]rsiBull, line[]secondbullline)=>
	bool Q = true
	if array.size(rsiBull) > 0
		for i = array.size(rsiBull) -1 to 0 
			rizza = array.get(rsiBull,i)
			u = array.get(secondbullline, i)
			if rizza > rsiinput
				array.remove(rsiBull, i)
				array.remove(secondbullline , i)
				line.delete(u)
				Q := false
	Q
// @function Checks for bearish RSI lines that have failed to trigger and removes them from the chart
// @description This function checks for bearish RSI lines that have failed to trigger (i.e., where the current RSI value is above the line's trigger level) and removes them from the chart along with any associated line.
// @param float rsiinput The current RSI value
// @param float rsiBear An array containing the trigger levels for bearish RSI lines
// @param line secondbearline The array containing the bearish RSI lines
// @return void
export rsibearchecker(float rsiinput, float[]rsiBear,  line[]secondbearline)=>
	bool Q = true
	if array.size(rsiBear) > 0
		for i = array.size(rsiBear) -1 to 0 
			rizza = array.get(rsiBear,i)
			u = array.get(secondbearline, i)
			if rizza < rsiinput
				array.remove(rsiBear, i)
				array.remove(secondbearline , i)
				line.delete(u)
				Q := false
	Q