#// Indicator for TOS
#// Author: Sean Lee Greer
#indicator("Greer BuyZone", "Greer BuyZone", overlay = true)
# Converted by Sam4Cok@Samer800 - 10/2024
input length = 100; #, minval=1)
input ShowBuyZoneBox = yes; #(true, title = "Show BuyZone Box", group = "UI Options")
input showLabel = yes; #(true, title = "Show Text", group = "UI Options")
def na = Double.NaN;
def last = IsNaN(close);
#// Calculate the upper and lower bounds based on the Aroon indicator and Fibonacci numbers
def upper = 100 * (GetMaxValueOffset(high, length + 1) + length) / length;
def lower = 100 * (GetMinValueOffset(low, length + 1) + length) / length;
def ahigh = Highest(high, length + 1); # // Calculate the highest price over the lookback period + 1 bar
def alow = Lowest(low, length + 1); # // Calculate the lowest price over the lookback period + 1 bar
def amidpoint = (ahigh + alow) / 2; # // Calculate the midpoint between the highest and lowest prices
#// Logic to detect the beginning of the BuyZone
def newbox;
def bzs;
if upper == 100 {
newbox = 1; # // Mark the start of a new BuyZone
bzs = 0; # // Reset the BuyZone start flag
} else if newbox[1] == 1 {
if lower == 100 {
bzs = 1; # // Confirm BuyZone start
newbox = 0; # // Reset the newbox flag
} else {
newbox = newbox[1];
bzs = 0; # // No BuyZone yet
}
} else {
newbox = newbox[1];
bzs = 0; # // Ensure BuyZone start flag is reset if not in a BuyZone
}
#// Logic to detect the end of the BuyZone
def endbox;
def bze;
if lower == 100 {
endbox = 1; #// Mark the end of a BuyZone
bze = 0; #// Reset the BuyZone end flag
} else if endbox[1] == 1 {
if upper == 100 {
bze = 1; # // Confirm BuyZone end
endbox = 0; #// Reset the endbox flag
} else {
endbox = endbox[1];
bze = 0; # // No BuyZone end yet
}
} else {
endbox = endbox[1];
bze = 0; # // Ensure BuyZone end flag is reset if not ending a BuyZone
}
def in_buyzone = if ShowBuyZoneBox then (newbox == 0 and endbox != 0) else no;
plot hh = if in_buyzone then ahigh else na;
def mid = if in_buyzone then amidpoint else na;
plot ll = if in_buyzone then alow else na;
hh.SetDefaultColor(Color.GREEN);
ll.SetDefaultColor(Color.RED);
AddCloud(hh, mid, Color.DARK_GREEN);
AddCloud(mid, ll, Color.DARK_RED);
AddChartBubble(showLabel and bzs == 1, low, "Start", Color.RED, no);
AddChartBubble(showLabel and bze == 1, high, "End", Color.GREEN);
#-- end of code