ChatGPT, BARD, Other AI Scripts Which Can't Be Used In ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
90% of the members attempting chatGPT scripting; do not provide good detailed specifications.

Members know what end result that they want but lack the ability to tell chatGPT, the step-by-step logic required to code a script to achieve that end result.
This results in chatGPT providing poor code.

Here is a video on how to successfully have chatGPT create functional scripts.

The above video, explains the basics of providing good specifications to AI which results in better scripts.
AOzhl05.png
 
Last edited:
I have looking an auto fib that will plot on each new range candle . I look at a lot of auto fibs on this forum but i dont know if any of them will do this.
bSHIQnZ.png
I got ChatGPT to make this one but I cant get it to remove the past Fibonacci levels or the current bar being formed any ideas on how to get it to plot only the last bar closed?

Ruby:
# Define global colors for customization
DefineGlobalColor("Color100", CreateColor(255, 0, 0)); # RED
DefineGlobalColor("Color75", CreateColor(153, 153, 153)); # Blue
DefineGlobalColor("Color66", CreateColor(0, 153, 51)); # Cyan
DefineGlobalColor("Color50", CreateColor(255, 0, 255)); # Magenta
DefineGlobalColor("Color33", CreateColor(0, 153, 51)); # Yellow
DefineGlobalColor("Color25", CreateColor(153, 153, 153)); # Orange
DefineGlobalColor("Color0", CreateColor(255, 0, 0)); # Red

# Inputs for customization
input style100 = Curve.FIRM;
input style75 = Curve.FIRM;
input style66 = Curve.FIRM;
input style50 = Curve.FIRM;
input style33 = Curve.FIRM;
input style25 = Curve.FIRM;
input style0 = Curve.FIRM;

# Define high, low, and close of the last closed range candle
def lastClosedBar = if !IsNaN(close[1]) then BarNumber() else lastClosedBar[1];
def isLastClosedCandle = BarNumber() == lastClosedBar;

# Calculate high, low, and range of the last closed range candle
def rangeHigh = if isLastClosedCandle then high[1] else Double.NaN;
def rangeLow = if isLastClosedCandle then low[1] else Double.NaN;

# Calculate Fibonacci levels
def level100 = if isLastClosedCandle then rangeHigh else Double.NaN;
def level75 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.25 else Double.NaN;
def level66 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.3334 else Double.NaN;
def level50 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.5 else Double.NaN;
def level33 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.6666 else Double.NaN;
def level25 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.75 else Double.NaN;
def level0 = if isLastClosedCandle then rangeLow else Double.NaN;

# Plot Fibonacci levels
plot P100 = level100;
plot P75 = level75;
plot P66 = level66;
plot P50 = level50;
plot P33 = level33;
plot P25 = level25;
plot P0 = level0;

# Set color and style for each Fibonacci level
P100.SetDefaultColor(GlobalColor("Color100"));
P100.SetStyle(style100);
P100.SetLineWeight(1);
P100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P75.SetDefaultColor(GlobalColor("Color75"));
P75.SetStyle(style75);
P75.SetLineWeight(1);
P75.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P66.SetDefaultColor(GlobalColor("Color66"));
P66.SetStyle(style66);
P66.SetLineWeight(1);
P66.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P50.SetDefaultColor(GlobalColor("Color50"));
P50.SetStyle(style50);
P50.SetLineWeight(1);
P50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P33.SetDefaultColor(GlobalColor("Color33"));
P33.SetStyle(style33);
P33.SetLineWeight(1);
P33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P25.SetDefaultColor(GlobalColor("Color25"));
P25.SetStyle(style25);
P25.SetLineWeight(1);
P25.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P0.SetDefaultColor(GlobalColor("Color0"));
P0.SetStyle(style0);
P0.SetLineWeight(1);
P0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#


I made this script and it seem to do what i want but the market is close atm so im not sure if it will plot on the next bar that closes.

Code:
DefineGlobalColor("Color100", CreateColor(255, 0, 0)); # RED
DefineGlobalColor("Color75", CreateColor(153, 153, 153)); # GRAY

DefineGlobalColor("Color618", CreateColor(0, 153, 51)); # BLUE
DefineGlobalColor("Color50", CreateColor(255, 0, 255)); # MAGENTA
DefineGlobalColor("Color382", CreateColor(0, 153, 51)); # ORANGE

DefineGlobalColor("Color25", CreateColor(153, 153, 153)); # GRAY
DefineGlobalColor("Color0", CreateColor(255, 0, 0)); # RED

# Input to choose how many bars back to calculate Fibonacci levels
input barsBack = 1;

# Calculate the current BarNumber
def bn = BarNumber();

# Count bars from right to left
def currentBar = HighestAll(if !IsNaN(close) then bn else Double.NaN);
def pastIndex = currentBar - bn;

# Identify the high and low of the bar "barsBack" bars ago
def targetBar = pastIndex == barsBack;
def rangeHigh = if targetBar then high else Double.NaN;
def rangeLow = if targetBar then low else Double.NaN;

# Hold the high and low values of the target bar for subsequent bars
def lastRangeHigh = if !IsNaN(rangeHigh) then rangeHigh else lastRangeHigh[1];
def lastRangeLow = if !IsNaN(rangeLow) then rangeLow else lastRangeLow[1];

# Calculate Fibonacci levels based on the target bar
def level100 = lastRangeHigh;
def level75 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.25;
def level618 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.618;
def level50 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.5;
def level382 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.382;
def level25 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.75;
def level0 = lastRangeLow;

# Plot Fibonacci levels as horizontal lines extending across the chart
plot P100 = if !IsNaN(close) then level100 else Double.NaN;
plot P75 = if !IsNaN(close) then level75 else Double.NaN;
plot P618 = if !IsNaN(close) then level618 else Double.NaN;
plot P50 = if !IsNaN(close) then level50 else Double.NaN;
plot P382 = if !IsNaN(close) then level382 else Double.NaN;
plot P25 = if !IsNaN(close) then level25 else Double.NaN;
plot P0 = if !IsNaN(close) then level0 else Double.NaN;

# Set color and style for each Fibonacci level
P100.SetDefaultColor(GlobalColor("Color100"));
P100.SetStyle(Curve.FIRM);
P100.SetLineWeight(1);
P100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P75.SetDefaultColor(GlobalColor("Color75"));
P75.SetStyle(Curve.SHORT_DASH);
P75.SetLineWeight(1);
P75.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P618.SetDefaultColor(GlobalColor("Color618"));
P618.SetStyle(Curve.SHORT_DASH);
P618.SetLineWeight(1);
P618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P50.SetDefaultColor(GlobalColor("Color50"));
P50.SetStyle(Curve.SHORT_DASH);
P50.SetLineWeight(1);
P50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P382.SetDefaultColor(GlobalColor("Color382"));
P382.SetStyle(Curve.SHORT_DASH);
P382.SetLineWeight(1);
P382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P25.SetDefaultColor(GlobalColor("Color25"));
P25.SetStyle(Curve.SHORT_DASH);
P25.SetLineWeight(1);
P25.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P0.SetDefaultColor(GlobalColor("Color0"));
P0.SetStyle(Curve.FIRM);
P0.SetLineWeight(1);
P0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Last edited by a moderator:
Hello, I created this script to monitor a watchlist to let me know when the MACD is in a upward trend. However, for some reason it's not reporting the indicator correctly. It never shows me the diff from the fast line - signal line as - or + just always positive so it only shows the one. can you take a look and tell me where I went wrong?

# Input settings to match the lower MACD study
input fastLength = 11;
input slowLength = 21;
input macdLength = 9;
input averageType = AverageType.WEIGHTED;

# Access the MACD study directly to get the exact Fast Line and Signal Line values using the chart's timeframe
def macdFastLine = MACD(fastLength, slowLength, macdLength, averageType).Value;
def macdSignalLine = MACD(fastLength, slowLength, macdLength, averageType).Avg;

# Subtract the MACD Fast Line value from the Signal Line value
def macdDiffValue = macdFastLine - macdSignalLine;

# Determine if the result is positive or negative and assign 1 or 0 accordingly
def macdDiffCondition = if macdDiffValue > 0 then 1 else 0;

# Plot the exact difference in the watchlist column: negative or positive value
plot Data = macdDiffValue;

# Set the font color based on the value: white if 0, yellow if 1
Data.AssignValueColor(if macdDiffCondition == 1 then Color.YELLOW else Color.WHITE);

# Debugging Labels for verification with 0 or 1 in parentheses
AddLabel(yes, "MACD Fast Line: " + macdFastLine, Color.WHITE);
AddLabel(yes, "MACD Signal Line: " + macdSignalLine, Color.WHITE);
AddLabel(yes, "MACD Diff Value: " + macdDiffValue + " (" + macdDiffCondition + ")",
if macdDiffCondition == 1 then Color.YELLOW else Color.RED);
 
I have looking an auto fib that will plot on each new range candle . I look at a lot of auto fibs on this forum but i dont know if any of them will do this. I got ChatGPT to make this one but I cant get it to remove the past Fibonacci levels or the current bar being formed any ideas on how to get it to plot only the last bar closed?

Ruby:
# Define global colors for customization
DefineGlobalColor("Color100", CreateColor(255, 0, 0)); # RED
DefineGlobalColor("Color75", CreateColor(153, 153, 153)); # Blue
DefineGlobalColor("Color66", CreateColor(0, 153, 51)); # Cyan
DefineGlobalColor("Color50", CreateColor(255, 0, 255)); # Magenta
DefineGlobalColor("Color33", CreateColor(0, 153, 51)); # Yellow
DefineGlobalColor("Color25", CreateColor(153, 153, 153)); # Orange
DefineGlobalColor("Color0", CreateColor(255, 0, 0)); # Red

# Inputs for customization
input style100 = Curve.FIRM;
input style75 = Curve.FIRM;
input style66 = Curve.FIRM;
input style50 = Curve.FIRM;
input style33 = Curve.FIRM;
input style25 = Curve.FIRM;
input style0 = Curve.FIRM;

# Define high, low, and close of the last closed range candle
def lastClosedBar = if !IsNaN(close[1]) then BarNumber() else lastClosedBar[1];
def isLastClosedCandle = BarNumber() == lastClosedBar;

# Calculate high, low, and range of the last closed range candle
def rangeHigh = if isLastClosedCandle then high[1] else Double.NaN;
def rangeLow = if isLastClosedCandle then low[1] else Double.NaN;

# Calculate Fibonacci levels
def level100 = if isLastClosedCandle then rangeHigh else Double.NaN;
def level75 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.25 else Double.NaN;
def level66 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.3334 else Double.NaN;
def level50 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.5 else Double.NaN;
def level33 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.6666 else Double.NaN;
def level25 = if isLastClosedCandle then rangeHigh - (rangeHigh - rangeLow) * 0.75 else Double.NaN;
def level0 = if isLastClosedCandle then rangeLow else Double.NaN;

# Plot Fibonacci levels
plot P100 = level100;
plot P75 = level75;
plot P66 = level66;
plot P50 = level50;
plot P33 = level33;
plot P25 = level25;
plot P0 = level0;

# Set color and style for each Fibonacci level
P100.SetDefaultColor(GlobalColor("Color100"));
P100.SetStyle(style100);
P100.SetLineWeight(1);
P100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P75.SetDefaultColor(GlobalColor("Color75"));
P75.SetStyle(style75);
P75.SetLineWeight(1);
P75.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P66.SetDefaultColor(GlobalColor("Color66"));
P66.SetStyle(style66);
P66.SetLineWeight(1);
P66.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P50.SetDefaultColor(GlobalColor("Color50"));
P50.SetStyle(style50);
P50.SetLineWeight(1);
P50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P33.SetDefaultColor(GlobalColor("Color33"));
P33.SetStyle(style33);
P33.SetLineWeight(1);
P33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P25.SetDefaultColor(GlobalColor("Color25"));
P25.SetStyle(style25);
P25.SetLineWeight(1);
P25.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P0.SetDefaultColor(GlobalColor("Color0"));
P0.SetStyle(style0);
P0.SetLineWeight(1);
P0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#


I made this script and it seem to do what i want but the market is close atm so im not sure if it will plot on the next bar that closes.

Code:
DefineGlobalColor("Color100", CreateColor(255, 0, 0)); # RED
DefineGlobalColor("Color75", CreateColor(153, 153, 153)); # GRAY

DefineGlobalColor("Color618", CreateColor(0, 153, 51)); # BLUE
DefineGlobalColor("Color50", CreateColor(255, 0, 255)); # MAGENTA
DefineGlobalColor("Color382", CreateColor(0, 153, 51)); # ORANGE

DefineGlobalColor("Color25", CreateColor(153, 153, 153)); # GRAY
DefineGlobalColor("Color0", CreateColor(255, 0, 0)); # RED

# Input to choose how many bars back to calculate Fibonacci levels
input barsBack = 1;

# Calculate the current BarNumber
def bn = BarNumber();

# Count bars from right to left
def currentBar = HighestAll(if !IsNaN(close) then bn else Double.NaN);
def pastIndex = currentBar - bn;

# Identify the high and low of the bar "barsBack" bars ago
def targetBar = pastIndex == barsBack;
def rangeHigh = if targetBar then high else Double.NaN;
def rangeLow = if targetBar then low else Double.NaN;

# Hold the high and low values of the target bar for subsequent bars
def lastRangeHigh = if !IsNaN(rangeHigh) then rangeHigh else lastRangeHigh[1];
def lastRangeLow = if !IsNaN(rangeLow) then rangeLow else lastRangeLow[1];

# Calculate Fibonacci levels based on the target bar
def level100 = lastRangeHigh;
def level75 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.25;
def level618 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.618;
def level50 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.5;
def level382 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.382;
def level25 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.75;
def level0 = lastRangeLow;

# Plot Fibonacci levels as horizontal lines extending across the chart
plot P100 = if !IsNaN(close) then level100 else Double.NaN;
plot P75 = if !IsNaN(close) then level75 else Double.NaN;
plot P618 = if !IsNaN(close) then level618 else Double.NaN;
plot P50 = if !IsNaN(close) then level50 else Double.NaN;
plot P382 = if !IsNaN(close) then level382 else Double.NaN;
plot P25 = if !IsNaN(close) then level25 else Double.NaN;
plot P0 = if !IsNaN(close) then level0 else Double.NaN;

# Set color and style for each Fibonacci level
P100.SetDefaultColor(GlobalColor("Color100"));
P100.SetStyle(Curve.FIRM);
P100.SetLineWeight(1);
P100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P75.SetDefaultColor(GlobalColor("Color75"));
P75.SetStyle(Curve.SHORT_DASH);
P75.SetLineWeight(1);
P75.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P618.SetDefaultColor(GlobalColor("Color618"));
P618.SetStyle(Curve.SHORT_DASH);
P618.SetLineWeight(1);
P618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P50.SetDefaultColor(GlobalColor("Color50"));
P50.SetStyle(Curve.SHORT_DASH);
P50.SetLineWeight(1);
P50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P382.SetDefaultColor(GlobalColor("Color382"));
P382.SetStyle(Curve.SHORT_DASH);
P382.SetLineWeight(1);
P382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P25.SetDefaultColor(GlobalColor("Color25"));
P25.SetStyle(Curve.SHORT_DASH);
P25.SetLineWeight(1);
P25.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P0.SetDefaultColor(GlobalColor("Color0"));
P0.SetStyle(Curve.FIRM);
P0.SetLineWeight(1);
P0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

reply to 281
i edited your post, separated the 2 codes

i have no idea what you are asking for?
what is a range candle?
no idea what i am supposed to be looking at in the image?
what does this mean, plot only the last bar closed. do you mean just use the last bar data?

use more words and explain what you want to see.

it is drawing lines between the 2nd to last bar. that isn't a normal fib, which is from a peak to a valley.
 

Attachments

  • Capture.JPG
    Capture.JPG
    32.7 KB · Views: 65
Last edited:
Working on TTM Squeeze scan the issue seems to the script may still be interpreting length as a variable rather than a literal constant.

The issue likely stems from the fact that, despite attempts to hardcode length, the script may still be interpreting length as a variable rather than a literal constant. Help would be appreciated
Code:
# TTM Squeeze Scanner
# Original code by TSL 11.13.2019
# Modified into a scanner by @MerryDay 8/2022

input price = close;
input nK = 1.5;       # Multiplier for Keltner Channels
input nBB = 2.0;      # Multiplier for Bollinger Bands
input alertLine = 1.0;
input maLength = 50;  # Moving average length
input rsiLength = 14; # RSI length for filtering
input rsiOverbought = 70; # RSI overbought level
input rsiOversold = 30;   # RSI oversold level
input volumeMultiplier = 1.5; # Multiplier for detecting volume spikes

# Directly using 20 as the literal constant where required
def squeezeHistogram = TTM_Squeeze(price, 20, nK, nBB, alertLine).Histogram;  # Hardcoded 20

# Calculate the moving average
def movingAverage = Average(price, maLength);

# Calculate the RSI
def rsi = RSI(price, rsiLength);

# Detect significant volume spikes using a literal constant for length
def volumeSpike = volume > Average(volume, 20) * volumeMultiplier;  # Hardcoded 20

# Determine squeeze stages based on histogram behavior
def squeezeStages;
if squeezeHistogram >= 0 then {
    if squeezeHistogram > squeezeHistogram[1] then {
        squeezeStages = 1;  # Cyan: Positive increasing (potential breakout)
    } else {
        squeezeStages = 2;  # Blue: Positive decreasing (potential weakening)
    }
} else {
    if squeezeHistogram < squeezeHistogram[1] then {
        squeezeStages = 3;  # Red: Negative decreasing (potential reversal)
    } else {
        squeezeStages = 4;  # Yellow: Negative increasing (potential weakening)
    }
}

# Scan for transition from Yellow to Cyan Histogram
plot scan_YellowToCyan = squeezeStages[1] == 4 and squeezeStages == 1;

# Scan for transition from Blue to Red Histogram
plot scan_BlueToRed = squeezeStages[1] == 2 and squeezeStages == 3;

### Breakout Scan
# Detect breakouts from a squeeze with price above moving average and volume spike
def priceBreakout = price > Highest(price, 20);  # Literal constant of 20 used
plot scan_Breakout =
    squeezeStages == 1 and  # Cyan stage indicates potential breakout
    priceBreakout and
    price > movingAverage and
    volumeSpike;

### Separate Reversal Signal Scans
# Detect Bullish Reversals
plot scan_BullishReversal =
    squeezeStages == 3 and rsi < rsiOversold;  # Red stage with RSI Oversold

# Detect Bearish Reversals
plot scan_BearishReversal =
    squeezeStages == 4 and rsi > rsiOverbought;  # Yellow stage with RSI Overbought
 
Last edited by a moderator:
Working on TTM Squeeze scan the issue seems to the script may still be interpreting length as a variable rather than a literal constant.

The issue likely stems from the fact that, despite attempts to hardcode length, the script may still be interpreting length as a variable rather than a literal constant. Help would be appreciated
Code:
# TTM Squeeze Scanner
# Original code by TSL 11.13.2019
# Modified into a scanner by @MerryDay 8/2022

input price = close;
input nK = 1.5;       # Multiplier for Keltner Channels
input nBB = 2.0;      # Multiplier for Bollinger Bands
input alertLine = 1.0;
input maLength = 50;  # Moving average length
input rsiLength = 14; # RSI length for filtering
input rsiOverbought = 70; # RSI overbought level
input rsiOversold = 30;   # RSI oversold level
input volumeMultiplier = 1.5; # Multiplier for detecting volume spikes

# Directly using 20 as the literal constant where required
def squeezeHistogram = TTM_Squeeze(price, 20, nK, nBB, alertLine).Histogram;  # Hardcoded 20

# Calculate the moving average
def movingAverage = Average(price, maLength);

# Calculate the RSI
def rsi = RSI(price, rsiLength);

# Detect significant volume spikes using a literal constant for length
def volumeSpike = volume > Average(volume, 20) * volumeMultiplier;  # Hardcoded 20

# Determine squeeze stages based on histogram behavior
def squeezeStages;
if squeezeHistogram >= 0 then {
    if squeezeHistogram > squeezeHistogram[1] then {
        squeezeStages = 1;  # Cyan: Positive increasing (potential breakout)
    } else {
        squeezeStages = 2;  # Blue: Positive decreasing (potential weakening)
    }
} else {
    if squeezeHistogram < squeezeHistogram[1] then {
        squeezeStages = 3;  # Red: Negative decreasing (potential reversal)
    } else {
        squeezeStages = 4;  # Yellow: Negative increasing (potential weakening)
    }
}

# Scan for transition from Yellow to Cyan Histogram
plot scan_YellowToCyan = squeezeStages[1] == 4 and squeezeStages == 1;

# Scan for transition from Blue to Red Histogram
plot scan_BlueToRed = squeezeStages[1] == 2 and squeezeStages == 3;

### Breakout Scan
# Detect breakouts from a squeeze with price above moving average and volume spike
def priceBreakout = price > Highest(price, 20);  # Literal constant of 20 used
plot scan_Breakout =
    squeezeStages == 1 and  # Cyan stage indicates potential breakout
    priceBreakout and
    price > movingAverage and
    volumeSpike;

### Separate Reversal Signal Scans
# Detect Bullish Reversals
plot scan_BullishReversal =
    squeezeStages == 3 and rsi < rsiOversold;  # Red stage with RSI Oversold

# Detect Bearish Reversals
plot scan_BearishReversal =
    squeezeStages == 4 and rsi > rsiOverbought;  # Yellow stage with RSI Overbought

reply to 286


rsi() error,
def rsi = RSI(price, rsiLength);

go look at RSI() code and see what the inputs are
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

you have the parameters in wrong sequence.
can get around that by using the parameter names.

def rsi = RSI(price = price, length = rsiLength);


i made this a lower study for you to experiment with.
before you use this as a scan, choose which plot you want to keep and disable the others.

Code:
#chat286_ttm_sqz_scan

#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-15#post-145043
#veaceslav1952

# TTM Squeeze Scanner
# Original code by TSL 11.13.2019
# Modified into a scanner by @MerryDay 8/2022

declare lower;

input price = close;
input nK = 1.5;       # Multiplier for Keltner Channels
input nBB = 2.0;      # Multiplier for Bollinger Bands
input alertLine = 1.0;
input maLength = 50;  # Moving average length
input rsiLength = 14; # RSI length for filtering
input rsiOverbought = 70; # RSI overbought level
input rsiOversold = 30;   # RSI oversold level
input volumeMultiplier = 1.5; # Multiplier for detecting volume spikes

# Directly using 20 as the literal constant where required
def squeezeHistogram = TTM_Squeeze(price, 20, nK, nBB, alertLine).Histogram;  # Hardcoded 20

# Calculate the moving average
def movingAverage = Average(price, maLength);

# Calculate the RSI
# parameters in wrong sequence
#def rsi = RSI(price, rsiLength);
# add parameter names
def rsi = RSI(price = price, length = rsiLength);

# Detect significant volume spikes using a literal constant for length
def volumeSpike = volume > Average(volume, 20) * volumeMultiplier;  # Hardcoded 20

# Determine squeeze stages based on histogram behavior
def squeezeStages;
if squeezeHistogram >= 0
then {
    if squeezeHistogram > squeezeHistogram[1]
    then {
        squeezeStages = 1;  # Cyan: Positive increasing (potential breakout)
    } else {
        squeezeStages = 2;  # Blue: Positive decreasing (potential weakening)
    }
} else {
    if squeezeHistogram < squeezeHistogram[1]
    then {
        squeezeStages = 3;  # Red: Negative decreasing (potential reversal)
    } else {
        squeezeStages = 4;  # Yellow: Negative increasing (potential weakening)
    }
}

# Scan for transition from Yellow to Cyan Histogram
plot scan_YellowToCyan = squeezeStages[1] == 4 and squeezeStages == 1;

# Scan for transition from Blue to Red Histogram
plot scan_BlueToRed = squeezeStages[1] == 2 and squeezeStages == 3;

### Breakout Scan
# Detect breakouts from a squeeze with price above moving average and volume spike
def priceBreakout = price > Highest(price, 20);  # Literal constant of 20 used
plot scan_Breakout =
    squeezeStages == 1 and  # Cyan stage indicates potential breakout
    priceBreakout and
    price > movingAverage and
    volumeSpike;

### Separate Reversal Signal Scans
# Detect Bullish Reversals
plot scan_BullishReversal =
    squeezeStages == 3 and rsi < rsiOversold;  # Red stage with RSI Oversold

# Detect Bearish Reversals
plot scan_BearishReversal =
    squeezeStages == 4 and rsi > rsiOverbought;  # Yellow stage with RSI Overbought
#
 

Attachments

  • img1.JPG
    img1.JPG
    70.3 KB · Views: 97
reply to 279

good on you for trying something, even though the aggregation didn't fix it. that's how i learned, i kept trying things.

set the chart on week,
then on the upper chart, look in the upper left. is there a small white circle with a ! in it.
click on it and it will say portfolio functions don't work with all time periods.

look up portfolio functions
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio
you have a GetOpenPL() in the last line, in a label.
disable that line and it will work.
Thank you! You fixed it.
 
I asked ChatGPT to re-create the new Big 3 Squeeze from simpler trading. It's close but not displaying properly for me. Everything is really compressed and hard to see. If anyone wants to play around with it, I think this is a good start:

# Big 3 Squeeze Indicator by Taylor Horton
# This script combines elements of the TTM Squeeze with custom buy/sell signals

declare lower;

input length = 20;
input mult = 2.0;
input momentumLength = 12;
input momentumMultiplier = 1.5;
input smoothingLength = 3;

def price = close;
def avg = SimpleMovingAvg(price, length);
def stdev = stdev(price, length);
def upperBand = avg + (mult * stdev);
def lowerBand = avg - (mult * stdev);

def K = (price - lowerBand) / (upperBand - lowerBand);
def D = ExpAverage(K, smoothingLength);
def J = 3 * K - 2 * D;

def MomentumHistogram = (price - ExpAverage(price, momentumLength)) / (momentumMultiplier * stdev);

plot Histogram = MomentumHistogram;
Histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Histogram.AssignValueColor(if MomentumHistogram >= 0 then Color.GREEN else Color.RED);

# Big 3 Buy and Sell Signals
def buySignal = K crosses above D and K < 0.2 and ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def sellSignal = K crosses below D and K > 0.8 and ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BuyArrow = if buySignal then low else Double.NaN;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(5);

plot SellArrow = if sellSignal then high else Double.NaN;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(5);

# Squeeze Signal
def squeeze = K > 0 and K < 1 and D > 0 and D < 1;
plot SqueezeDots = if squeeze then 0 else Double.NaN;
SqueezeDots.SetPaintingStrategy(PaintingStrategy.POINTS);
SqueezeDots.SetDefaultColor(Color.BLUE);
SqueezeDots.SetLineWeight(5);

# Labels for A+ Setups
AddLabel(buySignal, "A+ Buy Setup", Color.GREEN);
AddLabel(sellSignal, "A+ Sell Setup", Color.RED);

# Multi-Time Frame Labels
def weeklySqueeze = Highest(squeeze, 5) > 0;
def dailySqueeze = Highest(squeeze, 1) > 0;
def intraDaySqueeze = squeeze;

AddLabel(weeklySqueeze, "Weekly Squeeze", Color.BLUE);
AddLabel(dailySqueeze, "Daily Squeeze", Color.YELLOW);
AddLabel(intraDaySqueeze, "Intra-Day Squeeze", Color.MAGENTA);

# Additional Structure Criteria
def structureBuy = ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def structureSell = ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BullStructure = if structureBuy then low else Double.NaN;
BullStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BullStructure.SetDefaultColor(Color.GREEN);
BullStructure.SetLineWeight(5);

plot BearStructure = if structureSell then high else Double.NaN;
BearStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BearStructure.SetDefaultColor(Color.RED);
BearStructure.SetLineWeight(5);

# Trailing Stop Labels
def trailingStopValue = ExpAverage(close, 10);

AddLabel(close > trailingStopValue, "Above Trailing Stop", Color.GREEN);
AddLabel(close < trailingStopValue, "Below Trailing Stop", Color.RED);

plot TrailingStopLine = trailingStopValue;
TrailingStopLine.SetDefaultColor(Color.ORANGE);
TrailingStopLine.SetStyle(Curve.MEDIUM_DASH);
TrailingStopLine.SetLineWeight(3);

# Bull and Bear Target Labels
def bullTarget = close + 3 * stdev;
def bearTarget = close - 3 * stdev;

AddLabel(close >= bullTarget, "Bull Target Hit", Color.GREEN);
AddLabel(close <= bearTarget, "Bear Target Hit", Color.RED);
 

Attachments

  • Big3.docx
    118.4 KB · Views: 70
I asked ChatGPT to re-create the new Big 3 Squeeze from simpler trading. It's close but not displaying properly for me. Everything is really compressed and hard to see. If anyone wants to play around with it, I think this is a good start:

# Big 3 Squeeze Indicator by Taylor Horton
# This script combines elements of the TTM Squeeze with custom buy/sell signals

declare lower;

input length = 20;
input mult = 2.0;
input momentumLength = 12;
input momentumMultiplier = 1.5;
input smoothingLength = 3;

def price = close;
def avg = SimpleMovingAvg(price, length);
def stdev = stdev(price, length);
def upperBand = avg + (mult * stdev);
def lowerBand = avg - (mult * stdev);

def K = (price - lowerBand) / (upperBand - lowerBand);
def D = ExpAverage(K, smoothingLength);
def J = 3 * K - 2 * D;

def MomentumHistogram = (price - ExpAverage(price, momentumLength)) / (momentumMultiplier * stdev);

plot Histogram = MomentumHistogram;
Histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Histogram.AssignValueColor(if MomentumHistogram >= 0 then Color.GREEN else Color.RED);

# Big 3 Buy and Sell Signals
def buySignal = K crosses above D and K < 0.2 and ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def sellSignal = K crosses below D and K > 0.8 and ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BuyArrow = if buySignal then low else Double.NaN;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(5);

plot SellArrow = if sellSignal then high else Double.NaN;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(5);

# Squeeze Signal
def squeeze = K > 0 and K < 1 and D > 0 and D < 1;
plot SqueezeDots = if squeeze then 0 else Double.NaN;
SqueezeDots.SetPaintingStrategy(PaintingStrategy.POINTS);
SqueezeDots.SetDefaultColor(Color.BLUE);
SqueezeDots.SetLineWeight(5);

# Labels for A+ Setups
AddLabel(buySignal, "A+ Buy Setup", Color.GREEN);
AddLabel(sellSignal, "A+ Sell Setup", Color.RED);

# Multi-Time Frame Labels
def weeklySqueeze = Highest(squeeze, 5) > 0;
def dailySqueeze = Highest(squeeze, 1) > 0;
def intraDaySqueeze = squeeze;

AddLabel(weeklySqueeze, "Weekly Squeeze", Color.BLUE);
AddLabel(dailySqueeze, "Daily Squeeze", Color.YELLOW);
AddLabel(intraDaySqueeze, "Intra-Day Squeeze", Color.MAGENTA);

# Additional Structure Criteria
def structureBuy = ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def structureSell = ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BullStructure = if structureBuy then low else Double.NaN;
BullStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BullStructure.SetDefaultColor(Color.GREEN);
BullStructure.SetLineWeight(5);

plot BearStructure = if structureSell then high else Double.NaN;
BearStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BearStructure.SetDefaultColor(Color.RED);
BearStructure.SetLineWeight(5);

# Trailing Stop Labels
def trailingStopValue = ExpAverage(close, 10);

AddLabel(close > trailingStopValue, "Above Trailing Stop", Color.GREEN);
AddLabel(close < trailingStopValue, "Below Trailing Stop", Color.RED);

plot TrailingStopLine = trailingStopValue;
TrailingStopLine.SetDefaultColor(Color.ORANGE);
TrailingStopLine.SetStyle(Curve.MEDIUM_DASH);
TrailingStopLine.SetLineWeight(3);

# Bull and Bear Target Labels
def bullTarget = close + 3 * stdev;
def bearTarget = close - 3 * stdev;

AddLabel(close >= bullTarget, "Bull Target Hit", Color.GREEN);
AddLabel(close <= bearTarget, "Bear Target Hit", Color.RED);
This is dirty. I separated them into two and overlay them on to each other and it's doesn't compress. But, either the histogram and squeeze need to be moved up to match the moving averages, or the moving averages need to be moved down to oscillate around the histogram.
1st-
# Big 3 Squeeze Indicator by Taylor Horton
# This script combines elements of the TTM Squeeze with custom buy/sell signals

declare lower;

input length = 20;
input mult = 2.0;
input momentumLength = 12;
input momentumMultiplier = 1.5;
input smoothingLength = 3;

def price = close;
def avg = SimpleMovingAvg(price, length);
def stdev = stdev(price, length);
def upperBand = avg + (mult * stdev);
def lowerBand = avg - (mult * stdev);

def K = (price - lowerBand) / (upperBand - lowerBand);
def D = ExpAverage(K, smoothingLength);
def J = 3 * K - 2 * D;

def MomentumHistogram = (price - ExpAverage(price, momentumLength)) / (momentumMultiplier * stdev);


# Big 3 Buy and Sell Signals
def buySignal = K crosses above D and K < 0.2 and ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def sellSignal = K crosses below D and K > 0.8 and ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BuyArrow = if buySignal then low else Double.NaN;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(5);

plot SellArrow = if sellSignal then high else Double.NaN;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(5);

# Squeeze Signal
def squeeze = K > 0 and K < 1 and D > 0 and D < 1;


# Labels for A+ Setups
AddLabel(buySignal, "A+ Buy Setup", Color.GREEN);
AddLabel(sellSignal, "A+ Sell Setup", Color.RED);

# Multi-Time Frame Labels
def weeklySqueeze = Highest(squeeze, 5) > 0;
def dailySqueeze = Highest(squeeze, 1) > 0;
def intraDaySqueeze = squeeze;

AddLabel(weeklySqueeze, "Weekly Squeeze", Color.BLUE);
AddLabel(dailySqueeze, "Daily Squeeze", Color.YELLOW);
AddLabel(intraDaySqueeze, "Intra-Day Squeeze", Color.MAGENTA);

# Additional Structure Criteria
def structureBuy = ExpAverage(close, 21) > SimpleMovingAvg(close, 50);
def structureSell = ExpAverage(close, 21) < SimpleMovingAvg(close, 50);

plot BullStructure = if structureBuy then low else Double.NaN;
BullStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BullStructure.SetDefaultColor(Color.GREEN);
BullStructure.SetLineWeight(5);

plot BearStructure = if structureSell then high else Double.NaN;
BearStructure.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
BearStructure.SetDefaultColor(Color.RED);
BearStructure.SetLineWeight(5);

# Trailing Stop Labels
def trailingStopValue = ExpAverage(close, 10);

AddLabel(close > trailingStopValue, "Above Trailing Stop", Color.GREEN);
AddLabel(close < trailingStopValue, "Below Trailing Stop", Color.RED);

plot TrailingStopLine = trailingStopValue;
TrailingStopLine.SetDefaultColor(Color.ORANGE);
TrailingStopLine.SetStyle(Curve.MEDIUM_DASH);
TrailingStopLine.SetLineWeight(3);

# Bull and Bear Target Labels
def bullTarget = close + 3 * stdev;
def bearTarget = close - 3 * stdev;

AddLabel(close >= bullTarget, "Bull Target Hit", Color.GREEN);
AddLabel(close <= bearTarget, "Bear Target Hit", Color.RED);
2nd-

# Big 3 Squeeze Indicator by Taylor Horton
# This script combines elements of the TTM Squeeze with custom buy/sell signals

declare lower;

input length = 20;
input mult = 2.0;
input momentumLength = 12;
input momentumMultiplier = 1.5;
input smoothingLength = 3;


def price = close;
def avg = SimpleMovingAvg(price, length);
def stdev = stdev(price, length);
def upperBand = avg + (mult * stdev);
def lowerBand = avg - (mult * stdev);

def K = (price - lowerBand) / (upperBand - lowerBand);
def D = ExpAverage(K, smoothingLength);
def J = 3 * K - 2 * D;

def MomentumHistogram = (price - ExpAverage(price, momentumLength)) / (momentumMultiplier * stdev);

plot Histogram = MomentumHistogram;
Histogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Histogram.AssignValueColor(if MomentumHistogram >= 0 then Color.GREEN else Color.RED);

# Squeeze Signal
def squeeze = K > 0 and K < 1 and D > 0 and D < 1;
plot SqueezeDots = if squeeze then 0 else Double.NaN;
SqueezeDots.SetPaintingStrategy(PaintingStrategy.POINTS);
SqueezeDots.SetDefaultColor(Color.BLUE);
SqueezeDots.SetLineWeight(5);
Edit - Fixed the second one. It still plotted the trailing stop value which compressed the histogram. Now they should overlay properly.
 
Last edited:
I am building a study that uses some sort of rolling context window to measure local mins and maxs to determine trends. I have not figured out how to implement a context window correctly. ToS isn't throwing any errors, it just doesn't print anything on the indicator. The code below does print certain conditions, just without the context window. Does anyone know how to implement this?


Code:
declare lower;

input overBought = 80;
input overSold = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = yes;
input lookbackWindow = 50;  # Lookback window for trend detection

# Calculate FastK, SlowK, and SlowD
def highK = Highest(priceH, KPeriod);
def lowK = Lowest(priceL, KPeriod);
def FastK = 100 * (priceC - lowK) / (highK - lowK);
def SlowK = MovingAverage(averageType, FastK, 3);  # 3-period moving average of FastK
def SlowD = MovingAverage(averageType, SlowK, DPeriod);  # DPeriod moving average of SlowK

# Plot SlowK and SlowD
plot SlowKPlot = SlowK;
SlowKPlot.SetDefaultColor(GetColor(1));
plot SlowDPlot = SlowD;
SlowDPlot.SetDefaultColor(GetColor(5));

# Overbought and Oversold lines
plot OverBoughtLine = overBought;
OverBoughtLine.SetDefaultColor(Color.RED);
OverBoughtLine.SetStyle(Curve.FIRM);
OverBoughtLine.SetLineWeight(1);
plot OverSoldLine = overSold;
OverSoldLine.SetDefaultColor(Color.GREEN);
OverSoldLine.SetStyle(Curve.FIRM);
OverSoldLine.SetLineWeight(1);

# Local highs and lows for SlowD
def isLocalHigh = SlowD > SlowD[1] and SlowD > SlowD[-1];
def isLocalLow = SlowD < SlowD[1] and SlowD < SlowD[-1];

# Filtered local highs and lows based on SlowD value
def filteredLocalHigh = isLocalHigh and SlowD >= 60;
def filteredLocalLow = isLocalLow and SlowD <= 30;

# Track the last three filtered local highs and lows
def lastFilteredHigh = if filteredLocalHigh then SlowD else lastFilteredHigh[1];
def secondLastFilteredHigh = if filteredLocalHigh then lastFilteredHigh[1] else secondLastFilteredHigh[1];
def thirdLastFilteredHigh = if filteredLocalHigh then secondLastFilteredHigh[1] else thirdLastFilteredHigh[1];

def lastFilteredLow = if filteredLocalLow then SlowD else lastFilteredLow[1];
def secondLastFilteredLow = if filteredLocalLow then lastFilteredLow[1] else secondLastFilteredLow[1];
def thirdLastFilteredLow = if filteredLocalLow then secondLastFilteredLow[1] else thirdLastFilteredLow[1];

# Breakout conditions
def upBreakout = SlowK crosses above overSold;
def downBreakout = SlowK crosses below overBought;

# Conditions for white dots (improved logic)
def downBreakoutDotCondition = downBreakout and SlowD < Min(lastFilteredHigh, secondLastFilteredHigh) and !IsNaN(lastFilteredHigh) and !IsNaN(secondLastFilteredHigh);
def upBreakoutDotCondition = upBreakout and SlowD > Max(lastFilteredLow, secondLastFilteredLow) and !IsNaN(lastFilteredLow) and !IsNaN(secondLastFilteredLow);

# Check for presence of any colored dot
def hasColoredDot = filteredLocalHigh or filteredLocalLow or upBreakoutDotCondition or downBreakoutDotCondition;

# Plot breakout signals only when there's a colored dot
plot UpSignal = if showBreakoutSignals and upBreakout and hasColoredDot then overSold else Double.NaN;
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetLineWeight(2);

plot DownSignal = if showBreakoutSignals and downBreakout and hasColoredDot then overBought else Double.NaN;
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetLineWeight(2);

# Plot white dots
plot UpBreakoutDot = if upBreakoutDotCondition then SlowD else Double.NaN;
UpBreakoutDot.SetPaintingStrategy(PaintingStrategy.POINTS);
UpBreakoutDot.SetDefaultColor(Color.WHITE);
UpBreakoutDot.SetLineWeight(3);

plot DownBreakoutDot = if downBreakoutDotCondition then SlowD else Double.NaN;
DownBreakoutDot.SetPaintingStrategy(PaintingStrategy.POINTS);
DownBreakoutDot.SetDefaultColor(Color.WHITE);
DownBreakoutDot.SetLineWeight(3);

# Plot filtered local highs and lows (for reference)
plot LocalHigh = if filteredLocalHigh then SlowD else Double.NaN;
LocalHigh.SetPaintingStrategy(PaintingStrategy.POINTS);
LocalHigh.SetDefaultColor(Color.BLUE);
LocalHigh.SetLineWeight(3);

plot LocalLow = if filteredLocalLow then SlowD else Double.NaN;
LocalLow.SetPaintingStrategy(PaintingStrategy.POINTS);
LocalLow.SetDefaultColor(Color.YELLOW);
LocalLow.SetLineWeight(3);
 
Hello Everyone,

I got this code through AI however it requires to be tweaked to only show entries and exit arrows once it retest the EMAs:

Code:
# Define the moving averages
input length13 = 13;
input length48 = 48;
input length200 = 200;

def ema13 = ExpAverage(close, length13);
def ema48 = ExpAverage(close, length48);
def ema200 = ExpAverage(close, length200);

# Identify the trend
def higherHigh = high > high[1];
def higherLow = low > low[1];
def lowerHigh = high < high[1];
def lowerLow = low < low[1];

def uptrend = higherHigh and higherLow;
def downtrend = lowerHigh and lowerLow;

# Entry signals
def closeAbove13EMA = close > ema13;
def closeBelow13EMA = close < ema13;
def closeAbove48EMA = close > ema48;
def closeBelow48EMA = close < ema48;

# Track previous trend and signals
def wasInUptrend = uptrend[1];
def wasInDowntrend = downtrend[1];
def longEntrySignal = uptrend and !wasInUptrend and closeAbove13EMA and close[1] <= ema13[1] and close > ema13;
def longEntrySignal48 = uptrend and !wasInUptrend and closeAbove48EMA and close[1] <= ema48[1] and close > ema48;
def shortEntrySignal = downtrend and !wasInDowntrend and closeBelow13EMA and close[1] >= ema13[1] and close < ema13;
def shortEntrySignal48 = downtrend and !wasInDowntrend and closeBelow48EMA and close[1] >= ema48[1] and close < ema48;

# Exit signals
def exitLongSignal = close < ema13 or close < ema48;
def exitShortSignal = close > ema13 or close > ema48;

# Track the signals
def longEntryPlotted = longEntrySignal or longEntrySignal48;
def shortEntryPlotted = shortEntrySignal or shortEntrySignal48;
def exitLongPlotted = exitLongSignal;
def exitShortPlotted = exitShortSignal;

# Plot arrows
plot longEntryArrow = if longEntryPlotted and !longEntryPlotted[1] then low else Double.NaN;
plot shortEntryArrow = if shortEntryPlotted and !shortEntryPlotted[1] then high else Double.NaN;
plot exitLongArrow = if exitLongPlotted and !exitLongPlotted[1] then low else Double.NaN;
plot exitShortArrow = if exitShortPlotted and !exitShortPlotted[1] then high else Double.NaN;

# Styling
longEntryArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
longEntryArrow.SetDefaultColor(Color.GREEN);
shortEntryArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
shortEntryArrow.SetDefaultColor(Color.RED);
exitLongArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
exitLongArrow.SetDefaultColor(Color.RED);
exitShortArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
exitShortArrow.SetDefaultColor(Color.GREEN);

Is it possible to have it tweaked?

Thank you! :)
 
Hello Everyone,

I got this code through AI however it requires to be tweaked to only show entries and exit arrows once it retest the EMAs:

Code:
# Define the moving averages
input length13 = 13;
input length48 = 48;
input length200 = 200;

def ema13 = ExpAverage(close, length13);
def ema48 = ExpAverage(close, length48);
def ema200 = ExpAverage(close, length200);

# Identify the trend
def higherHigh = high > high[1];
def higherLow = low > low[1];
def lowerHigh = high < high[1];
def lowerLow = low < low[1];

def uptrend = higherHigh and higherLow;
def downtrend = lowerHigh and lowerLow;

# Entry signals
def closeAbove13EMA = close > ema13;
def closeBelow13EMA = close < ema13;
def closeAbove48EMA = close > ema48;
def closeBelow48EMA = close < ema48;

# Track previous trend and signals
def wasInUptrend = uptrend[1];
def wasInDowntrend = downtrend[1];
def longEntrySignal = uptrend and !wasInUptrend and closeAbove13EMA and close[1] <= ema13[1] and close > ema13;
def longEntrySignal48 = uptrend and !wasInUptrend and closeAbove48EMA and close[1] <= ema48[1] and close > ema48;
def shortEntrySignal = downtrend and !wasInDowntrend and closeBelow13EMA and close[1] >= ema13[1] and close < ema13;
def shortEntrySignal48 = downtrend and !wasInDowntrend and closeBelow48EMA and close[1] >= ema48[1] and close < ema48;

# Exit signals
def exitLongSignal = close < ema13 or close < ema48;
def exitShortSignal = close > ema13 or close > ema48;

# Track the signals
def longEntryPlotted = longEntrySignal or longEntrySignal48;
def shortEntryPlotted = shortEntrySignal or shortEntrySignal48;
def exitLongPlotted = exitLongSignal;
def exitShortPlotted = exitShortSignal;

# Plot arrows
plot longEntryArrow = if longEntryPlotted and !longEntryPlotted[1] then low else Double.NaN;
plot shortEntryArrow = if shortEntryPlotted and !shortEntryPlotted[1] then high else Double.NaN;
plot exitLongArrow = if exitLongPlotted and !exitLongPlotted[1] then low else Double.NaN;
plot exitShortArrow = if exitShortPlotted and !exitShortPlotted[1] then high else Double.NaN;

# Styling
longEntryArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
longEntryArrow.SetDefaultColor(Color.GREEN);
shortEntryArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
shortEntryArrow.SetDefaultColor(Color.RED);
exitLongArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
exitLongArrow.SetDefaultColor(Color.RED);
exitShortArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
exitShortArrow.SetDefaultColor(Color.GREEN);

Is it possible to have it tweaked?

Thank you! :)

maybe, with 3 averages, looking for restests on all 3 would be too many signals.
write up some rules to do what you really want.
 
I am building a study that uses some sort of rolling context window to measure local mins and maxs to determine trends. I have not figured out how to implement a context window correctly. ToS isn't throwing any errors, it just doesn't print anything on the indicator. The code below does print certain conditions, just without the context window. Does anyone know how to implement this?


Code:
declare lower;

input overBought = 80;
input overSold = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = yes;
input lookbackWindow = 50;  # Lookback window for trend detection

# Calculate FastK, SlowK, and SlowD
def highK = Highest(priceH, KPeriod);
def lowK = Lowest(priceL, KPeriod);
def FastK = 100 * (priceC - lowK) / (highK - lowK);
def SlowK = MovingAverage(averageType, FastK, 3);  # 3-period moving average of FastK
def SlowD = MovingAverage(averageType, SlowK, DPeriod);  # DPeriod moving average of SlowK

# Plot SlowK and SlowD
plot SlowKPlot = SlowK;
SlowKPlot.SetDefaultColor(GetColor(1));
plot SlowDPlot = SlowD;
SlowDPlot.SetDefaultColor(GetColor(5));

# Overbought and Oversold lines
plot OverBoughtLine = overBought;
OverBoughtLine.SetDefaultColor(Color.RED);
OverBoughtLine.SetStyle(Curve.FIRM);
OverBoughtLine.SetLineWeight(1);
plot OverSoldLine = overSold;
OverSoldLine.SetDefaultColor(Color.GREEN);
OverSoldLine.SetStyle(Curve.FIRM);
OverSoldLine.SetLineWeight(1);

# Local highs and lows for SlowD
def isLocalHigh = SlowD > SlowD[1] and SlowD > SlowD[-1];
def isLocalLow = SlowD < SlowD[1] and SlowD < SlowD[-1];

# Filtered local highs and lows based on SlowD value
def filteredLocalHigh = isLocalHigh and SlowD >= 60;
def filteredLocalLow = isLocalLow and SlowD <= 30;

# Track the last three filtered local highs and lows
def lastFilteredHigh = if filteredLocalHigh then SlowD else lastFilteredHigh[1];
def secondLastFilteredHigh = if filteredLocalHigh then lastFilteredHigh[1] else secondLastFilteredHigh[1];
def thirdLastFilteredHigh = if filteredLocalHigh then secondLastFilteredHigh[1] else thirdLastFilteredHigh[1];

def lastFilteredLow = if filteredLocalLow then SlowD else lastFilteredLow[1];
def secondLastFilteredLow = if filteredLocalLow then lastFilteredLow[1] else secondLastFilteredLow[1];
def thirdLastFilteredLow = if filteredLocalLow then secondLastFilteredLow[1] else thirdLastFilteredLow[1];

# Breakout conditions
def upBreakout = SlowK crosses above overSold;
def downBreakout = SlowK crosses below overBought;

# Conditions for white dots (improved logic)
def downBreakoutDotCondition = downBreakout and SlowD < Min(lastFilteredHigh, secondLastFilteredHigh) and !IsNaN(lastFilteredHigh) and !IsNaN(secondLastFilteredHigh);
def upBreakoutDotCondition = upBreakout and SlowD > Max(lastFilteredLow, secondLastFilteredLow) and !IsNaN(lastFilteredLow) and !IsNaN(secondLastFilteredLow);

# Check for presence of any colored dot
def hasColoredDot = filteredLocalHigh or filteredLocalLow or upBreakoutDotCondition or downBreakoutDotCondition;

# Plot breakout signals only when there's a colored dot
plot UpSignal = if showBreakoutSignals and upBreakout and hasColoredDot then overSold else Double.NaN;
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetLineWeight(2);

plot DownSignal = if showBreakoutSignals and downBreakout and hasColoredDot then overBought else Double.NaN;
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetLineWeight(2);

# Plot white dots
plot UpBreakoutDot = if upBreakoutDotCondition then SlowD else Double.NaN;
UpBreakoutDot.SetPaintingStrategy(PaintingStrategy.POINTS);
UpBreakoutDot.SetDefaultColor(Color.WHITE);
UpBreakoutDot.SetLineWeight(3);

plot DownBreakoutDot = if downBreakoutDotCondition then SlowD else Double.NaN;
DownBreakoutDot.SetPaintingStrategy(PaintingStrategy.POINTS);
DownBreakoutDot.SetDefaultColor(Color.WHITE);
DownBreakoutDot.SetLineWeight(3);

# Plot filtered local highs and lows (for reference)
plot LocalHigh = if filteredLocalHigh then SlowD else Double.NaN;
LocalHigh.SetPaintingStrategy(PaintingStrategy.POINTS);
LocalHigh.SetDefaultColor(Color.BLUE);
LocalHigh.SetLineWeight(3);

plot LocalLow = if filteredLocalLow then SlowD else Double.NaN;
LocalLow.SetPaintingStrategy(PaintingStrategy.POINTS);
LocalLow.SetDefaultColor(Color.YELLOW);
LocalLow.SetLineWeight(3);
sorry, no idea what you are asking for.
you are using words that have no meaning.
what is a context window? what do you want to see and where? could put a bubble somewhere.
what is local min and max? what do you really mean?
 
reply to 286


rsi() error,
def rsi = RSI(price, rsiLength);

go look at RSI() code and see what the inputs are
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

you have the parameters in wrong sequence.
can get around that by using the parameter names.

def rsi = RSI(price = price, length = rsiLength);


i made this a lower study for you to experiment with.
before you use this as a scan, choose which plot you want to keep and disable the others.

Code:
#chat286_ttm_sqz_scan

#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-15#post-145043
#veaceslav1952

# TTM Squeeze Scanner
# Original code by TSL 11.13.2019
# Modified into a scanner by @MerryDay 8/2022

declare lower;

input price = close;
input nK = 1.5;       # Multiplier for Keltner Channels
input nBB = 2.0;      # Multiplier for Bollinger Bands
input alertLine = 1.0;
input maLength = 50;  # Moving average length
input rsiLength = 14; # RSI length for filtering
input rsiOverbought = 70; # RSI overbought level
input rsiOversold = 30;   # RSI oversold level
input volumeMultiplier = 1.5; # Multiplier for detecting volume spikes

# Directly using 20 as the literal constant where required
def squeezeHistogram = TTM_Squeeze(price, 20, nK, nBB, alertLine).Histogram;  # Hardcoded 20

# Calculate the moving average
def movingAverage = Average(price, maLength);

# Calculate the RSI
# parameters in wrong sequence
#def rsi = RSI(price, rsiLength);
# add parameter names
def rsi = RSI(price = price, length = rsiLength);

# Detect significant volume spikes using a literal constant for length
def volumeSpike = volume > Average(volume, 20) * volumeMultiplier;  # Hardcoded 20

# Determine squeeze stages based on histogram behavior
def squeezeStages;
if squeezeHistogram >= 0
then {
    if squeezeHistogram > squeezeHistogram[1]
    then {
        squeezeStages = 1;  # Cyan: Positive increasing (potential breakout)
    } else {
        squeezeStages = 2;  # Blue: Positive decreasing (potential weakening)
    }
} else {
    if squeezeHistogram < squeezeHistogram[1]
    then {
        squeezeStages = 3;  # Red: Negative decreasing (potential reversal)
    } else {
        squeezeStages = 4;  # Yellow: Negative increasing (potential weakening)
    }
}

# Scan for transition from Yellow to Cyan Histogram
plot scan_YellowToCyan = squeezeStages[1] == 4 and squeezeStages == 1;

# Scan for transition from Blue to Red Histogram
plot scan_BlueToRed = squeezeStages[1] == 2 and squeezeStages == 3;

### Breakout Scan
# Detect breakouts from a squeeze with price above moving average and volume spike
def priceBreakout = price > Highest(price, 20);  # Literal constant of 20 used
plot scan_Breakout =
    squeezeStages == 1 and  # Cyan stage indicates potential breakout
    priceBreakout and
    price > movingAverage and
    volumeSpike;

### Separate Reversal Signal Scans
# Detect Bullish Reversals
plot scan_BullishReversal =
    squeezeStages == 3 and rsi < rsiOversold;  # Red stage with RSI Oversold

# Detect Bearish Reversals
plot scan_BearishReversal =
    squeezeStages == 4 and rsi > rsiOverbought;  # Yellow stage with RSI Overbought
#
@halcyonguy thank you .
 
Last edited by a moderator:
Hi - I need the help of someone smarter than me. I am trying to add an audible alert on my watchlist anytime this indicator is triggered. Can someone help?

# Inputs

def CumulativePeriod = 1; # Input for changing cumulative sum period
def AveragePeriod = 20; # Input for the period over which to calculate the average volume
def length = 34; # Length of the moving average
def pullbackLength = 5; # Length of the pullback

# Day calculation
def day = GetDay();

# Calculate buying and selling volume
def Denominator = high - low;
def Buying = if Denominator != 0 then volume * (close - low) / Denominator else 0;
def Selling = if Denominator != 0 then volume * (high - close) / Denominator else 0;

# Calculate cumulative buying and selling volumes with adjustable period
def CumulativeBuying = sum(Buying, CumulativePeriod);
def CumulativeSelling = sum(Selling, CumulativePeriod);

# Calculate the total volume
def TotalVolume = CumulativeBuying + CumulativeSelling;

# Calculate the percentage of buying and selling
def BuyingPercentage = if TotalVolume != 0 then CumulativeBuying / TotalVolume else 0;
def SellingPercentage = if TotalVolume != 0 then CumulativeSelling / TotalVolume else 0;

# Calculate the average volume
def avgVolume = Average(volume, AveragePeriod);

# Moving average
def ma = Average(close, length);

# Calculate the 34-period exponential moving average
def ema = MovAvgExponential("length" = 34)."AvgExp";

# Close is Above or Below 34 EMA
def AboveEma = close >= ema;
def BelowEma = close <= ema;

# Define conditions for uptrend and pullback
def isUptrend = close > ma;
def highestHigh = Highest(high, pullbackLength);
def isPullback = close < highestHigh[pullbackLength];

# Detect pullback ending in an uptrend
def pullbackEnd = isUptrend and isPullback[1] and !isPullback and BuyingPercentage > SellingPercentage and CumulativeBuying > avgVolume and AboveEma;

# Define conditions for downtrend and pullback
def isDowntrend = close < ma;
def lowestLow = Lowest(low, pullbackLength);
def isPullbackDown = close > lowestLow[pullbackLength];

# Detect pullback ending in a downtrend
def pullbackEndDown = isDowntrend and isPullbackDown[1] and !isPullbackDown and SellingPercentage > BuyingPercentage and CumulativeSelling > avgVolume and BelowEma;

# Define the label for the watchlist column with color
AddLabel(yes,
if pullbackEnd then "UP"
else if pullbackEndDown then "Down"
else " ",
if pullbackEnd then Color.GREEN
else if pullbackEndDown then Color.RED
else Color.BLACK);
 
"H/L Top 3 Price Ranges" with # of closes per range, to find long term support and resistance zones.

Hello, I've been trying to get this simple excel set of formulas to become a thinkscript for charting. Most of this script is derived from Chatgpt and the ai cannot fix the incorrect counts either. The problem is all about # of closing counts per range, that are not counted correctly.

Test version listed below: with only 3 ranges, (actually 10 ranges is the goal to further achieve a "top 3 ranges" from the ranges with the most closes.
# this study: 3 Ranges & #of bars that closed within 90 days.
Dividing the timeframes highest close & lowest close, into 3 ranges,
then "count" t/number of bars that closed within each.
ie., range 110-149: 20 bars , 150-159: 40 bars, 160-180: 30 bars .
(Then adding horizontal lines, when it works).

The result provides an indication of chop/congestion price zones, w/support & resistance.
Eventually, when top3 can be added back into it (after the #counting is fixed) :
each range ranked w/t/highest number of closes, becomes a top 3, then only those ranges are plotted into price bubbles (which does work w/chatgpt's provided code, yet not great because t/horizontal lines are not static.)
As a Top 3 range out of 10, this creates a series of 3 strong long term price levels
& t/horizontal range bars update automatically (no drawing required) when the closing counts change.
# Error: the range divisions are always correct whether 1 or 10 ranges.
So the error itself is w/the # of bars within each range. (tested w/fold, nan, compound value, sum and always the wrong # of bars are counted). Either syntax error or a variable is missing.

Oddly, :) when only 2 ranges are tested over #of days, ie 90.,
then the counts of closing bars per range are correct, and match the candle count: ie., range 1: 30, range 2: 60.
Only when adding the 3rd+ range or using shorter timeframes does the code fall apart.

Hope you can help it might be fun for some of you to see ChatGPT's code, it's easy to read and hopefully the countrange1-countrange3 section can stay that way so other newbies just like me can use it. Yet, a condensed code fold version is fine also. Thanks in advance for your help!!

This beta version below works for chart tests.
The chart image displays the 3 ranges as labels, & the price level lines on the chart were drawn, yet the 2nd goal is to have them automatically drawn, from this script.

# Define the lookback period (90 days)
input lookbackPeriod = 90; # Lookback period in days
input levels = 3; # Number of price ranges to divide into
input length = 90; #possibly not needed

# Force the use of daily data for calculations
def aggClose = close(Period = AggregationPeriod.DAY);

# Get the high and low over the lookback period
def highestClose = Highest(aggClose, lookbackPeriod);
def lowestClose = Lowest(aggClose, lookbackPeriod);

# Calculate the range increment
def range = (highestClose - lowestClose);
def increment = range / levels;

#---

# Define Range 1 (lowest range)
def range1Low = lowestClose;
def range1High = range1Low + increment;

# Define Range 2 (second range)
def range2Low = range1High + 0.01; #.01 for boundries between ranges
def range2High = range2Low + increment;

# Define Range 3 (second range)
def range3Low = range2High + 0.01;
def range3High = range3Low + increment;

#---

# Count closing prices within Range 1
def closeInRange1 = if aggClose >= range1Low and aggClose <= range1High then 1 else 0;
def countRange1 = Sum(closeInRange1, lookbackPeriod);

# Count closing prices within Range 2
def closeInRange2 = if aggClose >= range2Low and aggClose <= range2High then 1 else 0;
def countRange2 = Sum(closeInRange2, lookbackPeriod);

# Count closing prices within Range 3
def closeInRange3 = if aggClose >= range3Low and aggClose <= range3High then 1 else 0;
def countRange3 = Sum(closeInRange3, lookbackPeriod);

#---

# Add labels to display the counts
AddLabel(yes, " 1: " + AsText(range1Low) + " - " + AsText(range1High) + " 1: " + countRange1, CreateColor(175, 143, 14));
AddLabel(yes, " 2: " + AsText(range2Low) + " - " + AsText(range2High) + " 2: " + countRange2, CreateColor(127, 196, 203));
AddLabel(yes, " 3: " + AsText(range3Low) + " - " + AsText(range3High) + " 3: " + countRange2, CreateColor(89, 186, 131));

#-------------------------------------------------------------------------------------------
#-------------------------------------------------------------------to add into it------
#--- After the counting is repaired: This section details Ranking for top 3 ranges--- beginning Ranking: with Max defines for the counts (& that code does appear to work, with price bubbles):
# Find the top 3 ranked ranges
#def topRank1 = Max(count1, Max(count2, Max(count3, Max(count4, Max(count5, Max(count6, Max(count7, Max(count8, Max(count9, count10)))))))));
#def topRank2 = Max(If(count1 != topRank1, count1, 0), Max(If(count2 != topRank1, count2, 0), Max(If(count3 != topRank1, count3, 0), Max(If(count4 != topRank1, count4, 0), Max(If(count5 != topRank1, count5, 0), Max(If(count6 != topRank1, count6, 0), Max(If(count7 != topRank1, count7, 0), Max(If(count8 != topRank1, count8, 0), Max(If(count9 != topRank1, count9, 0), If(count10 != topRank1, count10, 0))))))))));
#def topRank3 = Max(If(count1 != topRank1 and count1 != topRank2, count1, 0), Max(If(count2 != topRank1 and count2 != topRank2, count2, 0), Max(If(count3 != topRank1 and count3 != topRank2, count3, 0), Max(If(count4 != topRank1 and count4 != topRank2, count4, 0),
Max(If(count5 != topRank1 and count5 != topRank2, count5, 0), Max(If(count6 != topRank1 and count6 != topRank2, count6, 0), Max(If(count7 != topRank1 and count7 != topRank2, count7, 0), Max(If(count8 != topRank1 and count8 != topRank2, count8, 0), Max(If(count9 != topRank1 and count9 != topRank2, count9, 0), If(count10 != topRank1 and count10 != topRank2, count10, 0))))))))));

#Define the low and high for the top 3 ranked ranges
#def top1Low = if topRank1 == count1 then range1Low else if topRank1 == count2 then range2Low else if topRank1 == count3 then range3Low else if topRank1 == count4 then range4Low else if topRank1 == count5 then range5Low else if topRank1 == count6 then range6Low else if topRank1 == count7 then range7Low else if topRank1 == count8 then range8Low else if topRank1 == count9 then range9Low else range10Low;
#def top1High = if topRank1 == count1 then range1High else if topRank1 == count2 then range2High else if topRank1 == count3 then range3High else if topRank1 == count4 then range4High else if topRank1 == count5 then range5High else if topRank1 == count6 then range6High else if topRank1 == count7 then range7High else if topRank1 == count8 then range8High else if topRank1 == count9 then range9High else range10High;

# Display the top 3 ranked ranges
#AddLabel(yes, "Top Rank 1: " + AsText(top1Low) + " to " + AsText(top1High) + " # " + AsText(topRank1), CreateColor(89, 186, 131));
#AddLabel(yes, "Top Rank 2: Low: " + AsText(top2Low) + " High: " + AsText(top2High) + " # " + AsText(topRank2), CreateColor(175, 143, 14));
#AddLabel(yes, "Top Rank 3: Low: " + AsText(top3Low) + " High: " + AsText(top3High) + " # " + AsText(topRank3), CreateColor(127, 196, 203));


# Plot top 3 ranges as support/resistance lines
#plot Pivot1 = highestClose;
plot Support1 = top1Low;
plot Resistance1 = top1High;

#plot Pivot2 = lowestClose; # Horizontal line
plot Support2 = top2Low;
plot Resistance2 = top2High;

plot Support3 = top3Low;
plot Resistance3 = top3High;

# Formatting lines
#Pivot1.SetDefaultColor(Color.GREEN);
#Pivot1.DefineColor("highestClose", CreateColor(217, 199, 40));
Support1.SetDefaultColor(Color.DARK_GREEN);
Resistance1.SetDefaultColor(Color.DARK_GREEN);

#Pivot2.SetDefaultColor(Color.RED);
Support2.SetDefaultColor(Color.DARK_RED);
Resistance2.SetDefaultColor(Color.DARK_RED);

#Pivot2.SetDefaultColor(Color.BLUE);
#Pivot3.DefineColor("lowestClose", CreateColor(255, 255, 255));
Support3.SetDefaultColor(Color.BLUE);
Resistance3.SetDefaultColor(Color.BLUE);
 

Attachments

  • multiple-ranges.png
    multiple-ranges.png
    253.5 KB · Views: 51
Last edited:
"H/L Top 3 Price Ranges" with # of closes per range, to find long term support and resistance zones.

Hello, I've been trying to get this simple excel set of formulas to become a thinkscript for charting. Most of this script is derived from Chatgpt and the ai cannot fix the incorrect counts either. The problem is all about # of closing counts per range, that are not counted correctly.

Test version listed below: with only 3 ranges, (actually 10 ranges is the goal to further achieve a "top 3 ranges" from the ranges with the most closes.
# this study: 3 Ranges & #of bars that closed within 90 days.
Dividing the timeframes highest close & lowest close, into 3 ranges,
then "count" t/number of bars that closed within each.
ie., range 110-149: 20 bars , 150-159: 40 bars, 160-180: 30 bars .
(Then adding horizontal lines, when it works).

The result provides an indication of chop/congestion price zones, w/support & resistance.
Eventually, when top3 can be added back into it (after the #counting is fixed) :
each range ranked w/t/highest number of closes, becomes a top 3, then only those ranges are plotted into price bubbles (which does work w/chatgpt's provided code, yet not great because t/horizontal lines are not static.)
As a Top 3 range out of 10, this creates a series of 3 strong long term price levels
& t/horizontal range bars update automatically (no drawing required) when the closing counts change.
# Error: the range divisions are always correct whether 1 or 10 ranges.
So the error itself is w/the # of bars within each range. (tested w/fold, nan, compound value, sum and always the wrong # of bars are counted). Either syntax error or a variable is missing.

Oddly, :) when only 2 ranges are tested over #of days, ie 90.,
then the counts of closing bars per range are correct, and match the candle count: ie., range 1: 30, range 2: 60.
Only when adding the 3rd+ range or using shorter timeframes does the code fall apart.

Hope you can help it might be fun for some of you to see ChatGPT's code, it's easy to read and hopefully the countrange1-countrange3 section can stay that way so other newbies just like me can use it. Yet, a condensed code fold version is fine also. Thanks in advance for your help!!

This beta version below works for chart tests.
The chart image displays the 3 ranges as labels, & the price level lines on the chart were drawn, yet the 2nd goal is to have them automatically drawn, from this script.

# Define the lookback period (90 days)
input lookbackPeriod = 90; # Lookback period in days
input levels = 3; # Number of price ranges to divide into
input length = 90; #possibly not needed

# Force the use of daily data for calculations
def aggClose = close(Period = AggregationPeriod.DAY);

# Get the high and low over the lookback period
def highestClose = Highest(aggClose, lookbackPeriod);
def lowestClose = Lowest(aggClose, lookbackPeriod);

# Calculate the range increment
def range = (highestClose - lowestClose);
def increment = range / levels;

#---

# Define Range 1 (lowest range)
def range1Low = lowestClose;
def range1High = range1Low + increment;

# Define Range 2 (second range)
def range2Low = range1High + 0.01; #.01 for boundries between ranges
def range2High = range2Low + increment;

# Define Range 3 (second range)
def range3Low = range2High + 0.01;
def range3High = range3Low + increment;

#---

# Count closing prices within Range 1
def closeInRange1 = if aggClose >= range1Low and aggClose <= range1High then 1 else 0;
def countRange1 = Sum(closeInRange1, lookbackPeriod);

# Count closing prices within Range 2
def closeInRange2 = if aggClose >= range2Low and aggClose <= range2High then 1 else 0;
def countRange2 = Sum(closeInRange2, lookbackPeriod);

# Count closing prices within Range 3
def closeInRange3 = if aggClose >= range3Low and aggClose <= range3High then 1 else 0;
def countRange3 = Sum(closeInRange3, lookbackPeriod);

#---

# Add labels to display the counts
AddLabel(yes, " 1: " + AsText(range1Low) + " - " + AsText(range1High) + " 1: " + countRange1, CreateColor(175, 143, 14));
AddLabel(yes, " 2: " + AsText(range2Low) + " - " + AsText(range2High) + " 2: " + countRange2, CreateColor(127, 196, 203));
AddLabel(yes, " 3: " + AsText(range3Low) + " - " + AsText(range3High) + " 3: " + countRange2, CreateColor(89, 186, 131));

#-------------------------------------------------------------------------------------------
#-------------------------------------------------------------------to add into it------
#--- After the counting is repaired: This section details Ranking for top 3 ranges--- beginning Ranking: with Max defines for the counts (& that code does appear to work, with price bubbles):
# Find the top 3 ranked ranges
#def topRank1 = Max(count1, Max(count2, Max(count3, Max(count4, Max(count5, Max(count6, Max(count7, Max(count8, Max(count9, count10)))))))));
#def topRank2 = Max(If(count1 != topRank1, count1, 0), Max(If(count2 != topRank1, count2, 0), Max(If(count3 != topRank1, count3, 0), Max(If(count4 != topRank1, count4, 0), Max(If(count5 != topRank1, count5, 0), Max(If(count6 != topRank1, count6, 0), Max(If(count7 != topRank1, count7, 0), Max(If(count8 != topRank1, count8, 0), Max(If(count9 != topRank1, count9, 0), If(count10 != topRank1, count10, 0))))))))));
#def topRank3 = Max(If(count1 != topRank1 and count1 != topRank2, count1, 0), Max(If(count2 != topRank1 and count2 != topRank2, count2, 0), Max(If(count3 != topRank1 and count3 != topRank2, count3, 0), Max(If(count4 != topRank1 and count4 != topRank2, count4, 0),
Max(If(count5 != topRank1 and count5 != topRank2, count5, 0), Max(If(count6 != topRank1 and count6 != topRank2, count6, 0), Max(If(count7 != topRank1 and count7 != topRank2, count7, 0), Max(If(count8 != topRank1 and count8 != topRank2, count8, 0), Max(If(count9 != topRank1 and count9 != topRank2, count9, 0), If(count10 != topRank1 and count10 != topRank2, count10, 0))))))))));

#Define the low and high for the top 3 ranked ranges
#def top1Low = if topRank1 == count1 then range1Low else if topRank1 == count2 then range2Low else if topRank1 == count3 then range3Low else if topRank1 == count4 then range4Low else if topRank1 == count5 then range5Low else if topRank1 == count6 then range6Low else if topRank1 == count7 then range7Low else if topRank1 == count8 then range8Low else if topRank1 == count9 then range9Low else range10Low;
#def top1High = if topRank1 == count1 then range1High else if topRank1 == count2 then range2High else if topRank1 == count3 then range3High else if topRank1 == count4 then range4High else if topRank1 == count5 then range5High else if topRank1 == count6 then range6High else if topRank1 == count7 then range7High else if topRank1 == count8 then range8High else if topRank1 == count9 then range9High else range10High;

# Display the top 3 ranked ranges
#AddLabel(yes, "Top Rank 1: " + AsText(top1Low) + " to " + AsText(top1High) + " # " + AsText(topRank1), CreateColor(89, 186, 131));
#AddLabel(yes, "Top Rank 2: Low: " + AsText(top2Low) + " High: " + AsText(top2High) + " # " + AsText(topRank2), CreateColor(175, 143, 14));
#AddLabel(yes, "Top Rank 3: Low: " + AsText(top3Low) + " High: " + AsText(top3High) + " # " + AsText(topRank3), CreateColor(127, 196, 203));


# Plot top 3 ranges as support/resistance lines
#plot Pivot1 = highestClose;
plot Support1 = top1Low;
plot Resistance1 = top1High;

#plot Pivot2 = lowestClose; # Horizontal line
plot Support2 = top2Low;
plot Resistance2 = top2High;

plot Support3 = top3Low;
plot Resistance3 = top3High;

# Formatting lines
#Pivot1.SetDefaultColor(Color.GREEN);
#Pivot1.DefineColor("highestClose", CreateColor(217, 199, 40));
Support1.SetDefaultColor(Color.DARK_GREEN);
Resistance1.SetDefaultColor(Color.DARK_GREEN);

#Pivot2.SetDefaultColor(Color.RED);
Support2.SetDefaultColor(Color.DARK_RED);
Resistance2.SetDefaultColor(Color.DARK_RED);

#Pivot2.SetDefaultColor(Color.BLUE);
#Pivot3.DefineColor("lowestClose", CreateColor(255, 255, 255));
Support3.SetDefaultColor(Color.BLUE);
Resistance3.SetDefaultColor(Color.BLUE);

EDIT - fixed the ranking numbers

------------------------------------

are you trying to count when close is within some price ranges?

why not use a day chart and not mess with 2nd aggregation. the agg may be causing problems with this many calculations on the data.


--------------------

here is my version , it finds quantities of close, within , several prices ranges

chart has to be day ( data is bars , not day agg)
pick up to 10 rows (levels) , default is 7
. input levels = 7
pick bars for lookback , default is 90
for finding the highest and lowest, can change price types , from close to high and low

draws vertical line at start of period
draws horizontal lines to divide the levels
a column of yellow bubbles after last bar, show the quantity of bars that closed in the range.
a 2nd column of bubbles show the ranking of the quantities. if rank is 1,2,3 then it is purple.
. if there are 2 quantity bubbles with same number, there will be 2 rank bubbles with the same numbers.
labels show the range levels and the quantity ( i didn't make them have unique colors)


Code:
#ranges_of_bars_aaa

#https://usethinkscript.com/threads/multiple-ranges-with-number-of-closing-bars-each.19721/
#Multiple Ranges with number of closing bars each
#zayvra  9/23

# I've been trying to get this simple excel set of formulas to become a thinkscript for charting. Most of this is derived from Chatgpt and the ai cannot fix it either.
#The problem is all about the counts of closes, per range, and that they are not being counted correctly.

#Test version listed below: with only 3 ranges, actually 10 ranges is the goal to achieve a top 3 ranges from the count results.

# this study: Multiple Ranges with #of bars that closed within each over a time of approx. 90 days. Splitting the highest close and lowest close over a period of 90 days into 3 ranges, then "count" the number of bars that closed within each. ie., 110-149: 20 bars , 150-159: 40 bars, 160-180: 30 bars . (Eventually adding horizontal lines, if it worked) The result provides an indication of possible chop/congestion areas, if price return to them. This data was provided by ChatGPT. & constructed by my design from my simple excel formulas, For many days chatgpt nor I can convert this to obtain the correct range counts of closes each. Eventually each range is range with the highest number of closes is turned into a top 3, (which does work via chatgpt's provided code, not listed here) creating series of price levels over a long term, w/horizontal range bars.
# Error: the ranges are correct, yet the count of bars within each range is always wrong. (whether, fold, nan or as shown below).

#So the original goal of finding the correct top 3 ranges out of 10 ranges (ie) has become near impossible for me.
#Oddly :) if only 2 ranges are used, over a long term #of days, ie 90., then the counts of closing bars per range are correct,
#Only when adding the 3rd range did it all fall apart.

#Hope you can help it might be fun for some of you to see chatgpt's code, it's easy to read and hopefully the countrange1-countrange3 section can stay that way so other newbies like me can use it. Thanks in advance for your help!!

#This beta version does work to test on charts. Just the total sum of "closes per range" is incorrect.

def na = double.nan;
def bn = barnumber();
def lastbn = highestall( if !isnan(close) then bn else 0);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

def big = 99999;
# Define the lookback period (90 days)
input lookbackPeriod = 90; # Lookback period in days

# Number of price ranges to divide into
input levels = 7;
def levels2 = if levels < 1 then 1 else if levels > 10 then 10 else levels;

def startx = (!isnan(close[-(lookbackPeriod-1)]) and isnan(close[-(lookbackPeriod+0)]));
addverticalline(startx, "-", color.cyan);
def startxbn = if startx then bn else startxbn[1];

input rng_hi = close;
input rng_lo = close;
input top_qty = 3;
def cls = close;

# Get the high and low over the lookback period
# get data on last bar
def hi;
def lo;
def rng;
if bn == 1 then {
 hi = 0;
 lo = 0;
 rng = 0;
} else if lastbar then {
 hi = Highest(rng_hi, lookbackPeriod);
 lo = Lowest(rng_lo, lookbackPeriod);
 rng = hi - lo;
} else {
 hi = hi[1];
 lo = lo[1];
 rng = rng[1];
}


# Calculate the range increment
def increment = rng / levels2;


def l0 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 0 and bn == startxbn then lo[-lookbackPeriod]
 else l0[1];

def l1 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 1 and bn == startxbn then lo[-lookbackPeriod] + (1*increment[-lookbackPeriod])
 else l1[1];

def l2 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 2 and bn == startxbn then lo[-lookbackPeriod] + (2*increment[-lookbackPeriod])
 else l2[1];

def l3 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 3 and bn == startxbn then lo[-lookbackPeriod] + (3*increment[-lookbackPeriod])
 else l3[1];

def l4 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 4 and bn == startxbn then lo[-lookbackPeriod] + (4*increment[-lookbackPeriod])
 else l4[1];

def l5 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 5 and bn == startxbn then lo[-lookbackPeriod] + (5*increment[-lookbackPeriod])
 else l5[1];

def l6 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 6 and bn == startxbn then lo[-lookbackPeriod] + (6*increment[-lookbackPeriod])
 else l6[1];

def l7 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 7 and bn == startxbn then lo[-lookbackPeriod] + (7*increment[-lookbackPeriod])
 else l7[1];

def l8 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 8 and bn == startxbn then lo[-lookbackPeriod] + (8*increment[-lookbackPeriod])
 else l8[1];

def l9 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 9 and bn == startxbn then lo[-lookbackPeriod] + (9*increment[-lookbackPeriod])
 else l9[1];

def l10 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 10 and bn == startxbn then lo[-lookbackPeriod] + (10*increment[-lookbackPeriod])
 else l10[1];


input show_lines = yes;
plot z0 = if l0 > 0 then l0 else na;
z0.SetDefaultColor(Color.light_gray);
z0.SetHiding(!show_lines);

plot z1 = if l1 > 0 then l1 else na;
z1.SetDefaultColor(Color.light_gray);
z1.SetHiding(!show_lines);

plot z2 = if l2 > 0 then l2 else na;
z2.SetDefaultColor(Color.light_gray);
z2.SetHiding(!show_lines);

plot z3 = if l3 > 0 then l3 else na;
z3.SetDefaultColor(Color.light_gray);
z3.SetHiding(!show_lines);

plot z4 = if l4 > 0 then l4 else na;
z4.SetDefaultColor(Color.light_gray);
z4.SetHiding(!show_lines);

plot z5 = if l5 > 0 then l5 else na;
z5.SetDefaultColor(Color.light_gray);
z5.SetHiding(!show_lines);

plot z6 = if l6 > 0 then l6 else na;
z6.SetDefaultColor(Color.light_gray);
z6.SetHiding(!show_lines);

plot z7 = if l7 > 0 then l7 else na;
z7.SetDefaultColor(Color.light_gray);
z7.SetHiding(!show_lines);

plot z8 = if l8 > 0 then l8 else na;
z8.SetDefaultColor(Color.light_gray);
z8.SetHiding(!show_lines);

plot z9 = if l9 > 0 then l9 else na;
z9.SetDefaultColor(Color.light_gray);
z9.SetHiding(!show_lines);

plot z10 = if l10 > 0 then l10 else na;
z10.SetDefaultColor(Color.light_gray);
z10.SetHiding(!show_lines);


# Count closing prices within Range
# chg levels2 >= 0  to  levels2 > 0
def inrng1 = if lastbar and levels2 > 0 then sum( (cls >= l0 and cls < l1), lookbackPeriod) else big;
def inrng2 = if lastbar and levels2 > 1 then sum( (cls >= l1 and cls < l2), lookbackPeriod) else big;
def inrng3 = if lastbar and levels2 > 2 then sum( (cls >= l2 and cls < l3), lookbackPeriod) else big;
def inrng4 = if lastbar and levels2 > 3 then sum( (cls >= l3 and cls < l4), lookbackPeriod) else big;
def inrng5 = if lastbar and levels2 > 4 then sum( (cls >= l4 and cls < l5), lookbackPeriod) else big;
def inrng6 = if lastbar and levels2 > 5 then sum( (cls >= l5 and cls < l6), lookbackPeriod) else big;
def inrng7 = if lastbar and levels2 > 6 then sum( (cls >= l6 and cls < l7), lookbackPeriod) else big;
def inrng8 = if lastbar and levels2 > 7 then sum( (cls >= l7 and cls < l8), lookbackPeriod) else big;
def inrng9 = if lastbar and levels2 > 8 then sum( (cls >= l8 and cls < l9), lookbackPeriod) else big;
def inrng10 = if lastbar and levels2 > 9 then sum( (cls >= l9 and cls < l10), lookbackPeriod) else big;


input test_within_bubbles = no;
def test_in_off = -7;
addchartbubble(test_within_bubbles and lastbar[test_in_off], lo[test_in_off],
inrng10[test_in_off] + "\n" + 
inrng9[test_in_off] + "\n" + 
inrng8[test_in_off] + "\n" +
inrng7[test_in_off] + "\n" +
inrng6[test_in_off] + "\n" +
inrng5[test_in_off] + "\n" +
inrng4[test_in_off] + "\n" +
inrng3[test_in_off] + "\n" +
inrng2[test_in_off] + "\n" +
inrng1[test_in_off] + "\n" 
, color.yellow, yes);


def rank1b = (inrng1 > inrng2) + (inrng1 > inrng3) + (inrng1 > inrng4) + (inrng1 > inrng5) + (inrng1 > inrng6) + (inrng1 > inrng7) +  (inrng1 > inrng8) + (inrng1 > inrng9) + (inrng1 > inrng10);
def rank2b = (inrng2 > inrng1) + (inrng2 > inrng3) + (inrng2 > inrng4) + (inrng2 > inrng5) + (inrng2 > inrng6) + (inrng2 > inrng7) + (inrng2 > inrng8) + (inrng2 > inrng9) + (inrng2 > inrng10);
def rank3b = (inrng3 > inrng1) + (inrng3 > inrng2) + (inrng3 > inrng4) + (inrng3 > inrng5) + (inrng3 > inrng6) + (inrng3 > inrng7) + (inrng3 > inrng8) + (inrng3 > inrng9) + (inrng3 > inrng10);
def rank4b = (inrng4 > inrng1) + (inrng4 > inrng2) + (inrng4 > inrng3) + (inrng4 > inrng5) + (inrng4 > inrng6) + (inrng4 > inrng7) + (inrng4 > inrng8) + (inrng4 > inrng9) + (inrng4 > inrng10);
def rank5b = (inrng5 > inrng1) + (inrng5 > inrng2) + (inrng5 > inrng3) + (inrng5 > inrng4) + (inrng5 > inrng6) + (inrng5 > inrng7) + (inrng5 > inrng8) + (inrng5 > inrng9) + (inrng5 > inrng10);
def rank6b = (inrng6 > inrng1) + (inrng6 > inrng2) + (inrng6 > inrng3) + (inrng6 > inrng4) + (inrng6 > inrng5) + (inrng6 > inrng7) + (inrng6 > inrng8) + (inrng6 > inrng9) + (inrng6 > inrng10);
def rank7b = (inrng7 > inrng1) + (inrng7 > inrng2) + (inrng7 > inrng3) + (inrng7 > inrng4) + (inrng7 > inrng5) + (inrng7 >  inrng6) + (inrng7 > inrng8) + (inrng7 > inrng9) + (inrng7 > inrng10);
def rank8b = (inrng8 > inrng1) + (inrng8 > inrng2) + (inrng8 > inrng3) + (inrng8 > inrng4) + (inrng8 > inrng5) + (inrng8 > inrng6) + (inrng8 > inrng7) + (inrng8 > inrng9) + (inrng8 > inrng10);
def rank9b = (inrng9 > inrng1) + (inrng9 > inrng2) + (inrng9 > inrng3) + (inrng9 > inrng4) + (inrng9 > inrng5) + (inrng9 >  inrng6) + (inrng9 > inrng7) + (inrng9 > inrng8) + (inrng9 > inrng10);
def rank10b = (inrng10 > inrng1) + (inrng10 > inrng2) + (inrng10 > inrng3) + (inrng10 > inrng4) + (inrng10 > inrng5) + (inrng10 > inrng6) + (inrng10 > inrng7) + (inrng10 > inrng8) + (inrng10 > inrng9);


def rank1 = levels2 - rank1b;
def rank2 = levels2 - rank2b;
def rank3 = levels2 - rank3b;
def rank4 = levels2 - rank4b;
def rank5 = levels2 - rank5b;
def rank6 = levels2 - rank6b;
def rank7 = levels2 - rank7b;
def rank8 = levels2 - rank8b;
def rank9 = levels2 - rank9b;
def rank10 = levels2 - rank10b;


input test_rank_bubbles = no;
def test_rk_off = -2;
addchartbubble(test_rank_bubbles and lastbar[test_rk_off], lo[test_rk_off],
 rank10[test_rk_off] + "\n" +
 rank9[test_rk_off] + "\n" +
 rank8[test_rk_off] + "\n" +
 rank7[test_rk_off] + "\n" +
 rank6[test_rk_off] + "\n" +
 rank5[test_rk_off] + "\n" +
 rank4[test_rk_off] + "\n" +
 rank3[test_rk_off] + "\n" +
 rank2[test_rk_off] + "\n" +
 rank1[test_rk_off] + "\n" 
, color.magenta, yes);


# counts
def cntoff = 4;
addchartbubble(lastbar[cntoff] and levels2 > 0, l0[cntoff], inrng1[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 1, l1[cntoff], inrng2[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 2, l2[cntoff], inrng3[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 3, l3[cntoff], inrng4[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 4, l4[cntoff], inrng5[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 5, l5[cntoff], inrng6[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 6, l6[cntoff], inrng7[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 7, l7[cntoff], inrng8[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 8, l8[cntoff], inrng9[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 9, l9[cntoff], inrng10[cntoff], color.yellow, yes);


# rank
def rankoff = 15;
addchartbubble(lastbar[rankoff] and levels2 > 0, l0[rankoff], rank1[rankoff], (if rank1[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 1, l1[rankoff], rank2[rankoff], (if rank2[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 2, l2[rankoff], rank3[rankoff], (if rank3[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 3, l3[rankoff], rank4[rankoff], (if rank4[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 4, l4[rankoff], rank5[rankoff], (if rank5[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 5, l5[rankoff], rank6[rankoff], (if rank6[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 6, l6[rankoff], rank7[rankoff], (if rank7[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 7, l7[rankoff], rank8[rankoff], (if rank8[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 8, l8[rankoff], rank9[rankoff], (if rank9[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 9, l9[rankoff], rank10[rankoff], (if rank10[rankoff] <= top_qty then color.magenta else color.gray), yes);


addlabel(1, " ", color.black);
# Add labels to display the counts
AddLabel(levels2 > 0, " 1: " + l0 + " - " + l1 + " - " + inrng1, CreateColor(175, 143, 14));
AddLabel(levels2 > 1, " 2: " + l1 + " - " + hi + " - " + inrng2, CreateColor(127, 196, 203));
AddLabel(levels2 > 2, " 3: " + l2 + " - " + hi + " - " + inrng3, CreateColor(89, 186, 131));
AddLabel(levels2 > 3, " 4: " + l3 + " - " + l4 + " - " + inrng4, CreateColor(89, 186, 131));
AddLabel(levels2 > 4, " 5: " + l4 + " - " + l5 + " - " + inrng5, CreateColor(89, 186, 131));
AddLabel(levels2 > 5, " 6: " + l5 + " - " + l6 + " - " + inrng6, CreateColor(89, 186, 131));
AddLabel(levels2 > 6, " 7: " + l6 + " - " + l7 + " - " + inrng7, CreateColor(89, 186, 131));
AddLabel(levels2 > 7, " 8: " + l7 + " - " + l8 + " - " + inrng8, CreateColor(89, 186, 131));
addLabel(levels2 > 8, " 9: " + l8 + " - " + l9 + " - " + inrng9, CreateColor(89, 186, 131));
AddLabel(levels2 > 9, " 10: " + l9 + " - " + l10 + " - " + inrng10, CreateColor(89, 186, 131));
addlabel(1, " ", color.black);
addlabel(1, " ", color.black);
#


--------------------------------



take a look at post13
a homemade version of close profile
it has 40 ranges
https://usethinkscript.com/threads/tradingview-poor-mans-volume-profile.14460/#post-119999

change this line, to change quantity of lines
def row_qty = 40;
to this
input row_qty = 10;
 

Attachments

  • img1.JPG
    img1.JPG
    98.3 KB · Views: 56
Last edited:
EDIT - fixed the ranking numbers

------------------------------------

are you trying to count when close is within some price ranges?

why not use a day chart and not mess with 2nd aggregation. the agg may be causing problems with this many calculations on the data.


--------------------

here is my version , it finds quantities of close, within , several prices ranges

chart has to be day ( data is bars , not day agg)
pick up to 10 rows (levels) , default is 7
. input levels = 7
pick bars for lookback , default is 90
for finding the highest and lowest, can change price types , from close to high and low

draws vertical line at start of period
draws horizontal lines to divide the levels
a column of yellow bubbles after last bar, show the quantity of bars that closed in the range.
a 2nd column of bubbles show the ranking of the quantities. if rank is 1,2,3 then it is purple.
. if there are 2 quantity bubbles with same number, there will be 2 rank bubbles with the same numbers.
labels show the range levels and the quantity ( i didn't make them have unique colors)


Code:
#ranges_of_bars_aaa

#https://usethinkscript.com/threads/multiple-ranges-with-number-of-closing-bars-each.19721/
#Multiple Ranges with number of closing bars each
#zayvra  9/23

# I've been trying to get this simple excel set of formulas to become a thinkscript for charting. Most of this is derived from Chatgpt and the ai cannot fix it either.
#The problem is all about the counts of closes, per range, and that they are not being counted correctly.
[/QUOTE]

[QUOTE="halcyonguy, post: 146470, member: 10697"]
#This is the new better version from Halcyonguy. 

def na = double.nan;
def bn = barnumber();
def lastbn = highestall( if !isnan(close) then bn else 0);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

def big = 99999;
# Define the lookback period (90 days)
input lookbackPeriod = 90; # Lookback period in days

# Number of price ranges to divide into
input levels = 7;
def levels2 = if levels < 1 then 1 else if levels > 10 then 10 else levels;

def startx = (!isnan(close[-(lookbackPeriod-1)]) and isnan(close[-(lookbackPeriod+0)]));
addverticalline(startx, "-", color.cyan);
def startxbn = if startx then bn else startxbn[1];

input rng_hi = close;
input rng_lo = close;
input top_qty = 3;
def cls = close;

# Get the high and low over the lookback period
# get data on last bar
def hi;
def lo;
def rng;
if bn == 1 then {
 hi = 0;
 lo = 0;
 rng = 0;
} else if lastbar then {
 hi = Highest(rng_hi, lookbackPeriod);
 lo = Lowest(rng_lo, lookbackPeriod);
 rng = hi - lo;
} else {
 hi = hi[1];
 lo = lo[1];
 rng = rng[1];
}


# Calculate the range increment
def increment = rng / levels2;


def l0 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 0 and bn == startxbn then lo[-lookbackPeriod]
 else l0[1];

def l1 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 1 and bn == startxbn then lo[-lookbackPeriod] + (1*increment[-lookbackPeriod])
 else l1[1];

def l2 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 2 and bn == startxbn then lo[-lookbackPeriod] + (2*increment[-lookbackPeriod])
 else l2[1];

def l3 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 3 and bn == startxbn then lo[-lookbackPeriod] + (3*increment[-lookbackPeriod])
 else l3[1];

def l4 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 4 and bn == startxbn then lo[-lookbackPeriod] + (4*increment[-lookbackPeriod])
 else l4[1];

def l5 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 5 and bn == startxbn then lo[-lookbackPeriod] + (5*increment[-lookbackPeriod])
 else l5[1];

def l6 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 6 and bn == startxbn then lo[-lookbackPeriod] + (6*increment[-lookbackPeriod])
 else l6[1];

def l7 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 7 and bn == startxbn then lo[-lookbackPeriod] + (7*increment[-lookbackPeriod])
 else l7[1];

def l8 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 8 and bn == startxbn then lo[-lookbackPeriod] + (8*increment[-lookbackPeriod])
 else l8[1];

def l9 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 9 and bn == startxbn then lo[-lookbackPeriod] + (9*increment[-lookbackPeriod])
 else l9[1];

def l10 = if bn == 1 or isnan(close) then 0
 else if levels2 >= 10 and bn == startxbn then lo[-lookbackPeriod] + (10*increment[-lookbackPeriod])
 else l10[1];


input show_lines = yes;
plot z0 = if l0 > 0 then l0 else na;
z0.SetDefaultColor(Color.light_gray);
z0.SetHiding(!show_lines);

plot z1 = if l1 > 0 then l1 else na;
z1.SetDefaultColor(Color.light_gray);
z1.SetHiding(!show_lines);

plot z2 = if l2 > 0 then l2 else na;
z2.SetDefaultColor(Color.light_gray);
z2.SetHiding(!show_lines);

plot z3 = if l3 > 0 then l3 else na;
z3.SetDefaultColor(Color.light_gray);
z3.SetHiding(!show_lines);

plot z4 = if l4 > 0 then l4 else na;
z4.SetDefaultColor(Color.light_gray);
z4.SetHiding(!show_lines);

plot z5 = if l5 > 0 then l5 else na;
z5.SetDefaultColor(Color.light_gray);
z5.SetHiding(!show_lines);

plot z6 = if l6 > 0 then l6 else na;
z6.SetDefaultColor(Color.light_gray);
z6.SetHiding(!show_lines);

plot z7 = if l7 > 0 then l7 else na;
z7.SetDefaultColor(Color.light_gray);
z7.SetHiding(!show_lines);

plot z8 = if l8 > 0 then l8 else na;
z8.SetDefaultColor(Color.light_gray);
z8.SetHiding(!show_lines);

plot z9 = if l9 > 0 then l9 else na;
z9.SetDefaultColor(Color.light_gray);
z9.SetHiding(!show_lines);

plot z10 = if l10 > 0 then l10 else na;
z10.SetDefaultColor(Color.light_gray);
z10.SetHiding(!show_lines);


# Count closing prices within Range
# chg levels2 >= 0  to  levels2 > 0
def inrng1 = if lastbar and levels2 > 0 then sum( (cls >= l0 and cls < l1), lookbackPeriod) else big;
def inrng2 = if lastbar and levels2 > 1 then sum( (cls >= l1 and cls < l2), lookbackPeriod) else big;
def inrng3 = if lastbar and levels2 > 2 then sum( (cls >= l2 and cls < l3), lookbackPeriod) else big;
def inrng4 = if lastbar and levels2 > 3 then sum( (cls >= l3 and cls < l4), lookbackPeriod) else big;
def inrng5 = if lastbar and levels2 > 4 then sum( (cls >= l4 and cls < l5), lookbackPeriod) else big;
def inrng6 = if lastbar and levels2 > 5 then sum( (cls >= l5 and cls < l6), lookbackPeriod) else big;
def inrng7 = if lastbar and levels2 > 6 then sum( (cls >= l6 and cls < l7), lookbackPeriod) else big;
def inrng8 = if lastbar and levels2 > 7 then sum( (cls >= l7 and cls < l8), lookbackPeriod) else big;
def inrng9 = if lastbar and levels2 > 8 then sum( (cls >= l8 and cls < l9), lookbackPeriod) else big;
def inrng10 = if lastbar and levels2 > 9 then sum( (cls >= l9 and cls < l10), lookbackPeriod) else big;


input test_within_bubbles = no;
def test_in_off = -7;
addchartbubble(test_within_bubbles and lastbar[test_in_off], lo[test_in_off],
inrng10[test_in_off] + "\n" +
inrng9[test_in_off] + "\n" +
inrng8[test_in_off] + "\n" +
inrng7[test_in_off] + "\n" +
inrng6[test_in_off] + "\n" +
inrng5[test_in_off] + "\n" +
inrng4[test_in_off] + "\n" +
inrng3[test_in_off] + "\n" +
inrng2[test_in_off] + "\n" +
inrng1[test_in_off] + "\n"
, color.yellow, yes);


def rank1b = (inrng1 > inrng2) + (inrng1 > inrng3) + (inrng1 > inrng4) + (inrng1 > inrng5) + (inrng1 > inrng6) + (inrng1 > inrng7) +  (inrng1 > inrng8) + (inrng1 > inrng9) + (inrng1 > inrng10);
def rank2b = (inrng2 > inrng1) + (inrng2 > inrng3) + (inrng2 > inrng4) + (inrng2 > inrng5) + (inrng2 > inrng6) + (inrng2 > inrng7) + (inrng2 > inrng8) + (inrng2 > inrng9) + (inrng2 > inrng10);
def rank3b = (inrng3 > inrng1) + (inrng3 > inrng2) + (inrng3 > inrng4) + (inrng3 > inrng5) + (inrng3 > inrng6) + (inrng3 > inrng7) + (inrng3 > inrng8) + (inrng3 > inrng9) + (inrng3 > inrng10);
def rank4b = (inrng4 > inrng1) + (inrng4 > inrng2) + (inrng4 > inrng3) + (inrng4 > inrng5) + (inrng4 > inrng6) + (inrng4 > inrng7) + (inrng4 > inrng8) + (inrng4 > inrng9) + (inrng4 > inrng10);
def rank5b = (inrng5 > inrng1) + (inrng5 > inrng2) + (inrng5 > inrng3) + (inrng5 > inrng4) + (inrng5 > inrng6) + (inrng5 > inrng7) + (inrng5 > inrng8) + (inrng5 > inrng9) + (inrng5 > inrng10);
def rank6b = (inrng6 > inrng1) + (inrng6 > inrng2) + (inrng6 > inrng3) + (inrng6 > inrng4) + (inrng6 > inrng5) + (inrng6 > inrng7) + (inrng6 > inrng8) + (inrng6 > inrng9) + (inrng6 > inrng10);
def rank7b = (inrng7 > inrng1) + (inrng7 > inrng2) + (inrng7 > inrng3) + (inrng7 > inrng4) + (inrng7 > inrng5) + (inrng7 >  inrng6) + (inrng7 > inrng8) + (inrng7 > inrng9) + (inrng7 > inrng10);
def rank8b = (inrng8 > inrng1) + (inrng8 > inrng2) + (inrng8 > inrng3) + (inrng8 > inrng4) + (inrng8 > inrng5) + (inrng8 > inrng6) + (inrng8 > inrng7) + (inrng8 > inrng9) + (inrng8 > inrng10);
def rank9b = (inrng9 > inrng1) + (inrng9 > inrng2) + (inrng9 > inrng3) + (inrng9 > inrng4) + (inrng9 > inrng5) + (inrng9 >  inrng6) + (inrng9 > inrng7) + (inrng9 > inrng8) + (inrng9 > inrng10);
def rank10b = (inrng10 > inrng1) + (inrng10 > inrng2) + (inrng10 > inrng3) + (inrng10 > inrng4) + (inrng10 > inrng5) + (inrng10 > inrng6) + (inrng10 > inrng7) + (inrng10 > inrng8) + (inrng10 > inrng9);


def rank1 = levels2 - rank1b;
def rank2 = levels2 - rank2b;
def rank3 = levels2 - rank3b;
def rank4 = levels2 - rank4b;
def rank5 = levels2 - rank5b;
def rank6 = levels2 - rank6b;
def rank7 = levels2 - rank7b;
def rank8 = levels2 - rank8b;
def rank9 = levels2 - rank9b;
def rank10 = levels2 - rank10b;


input test_rank_bubbles = no;
def test_rk_off = -2;
addchartbubble(test_rank_bubbles and lastbar[test_rk_off], lo[test_rk_off],
 rank10[test_rk_off] + "\n" +
 rank9[test_rk_off] + "\n" +
 rank8[test_rk_off] + "\n" +
 rank7[test_rk_off] + "\n" +
 rank6[test_rk_off] + "\n" +
 rank5[test_rk_off] + "\n" +
 rank4[test_rk_off] + "\n" +
 rank3[test_rk_off] + "\n" +
 rank2[test_rk_off] + "\n" +
 rank1[test_rk_off] + "\n"
, color.magenta, yes);


# counts
def cntoff = 4;
addchartbubble(lastbar[cntoff] and levels2 > 0, l0[cntoff], inrng1[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 1, l1[cntoff], inrng2[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 2, l2[cntoff], inrng3[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 3, l3[cntoff], inrng4[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 4, l4[cntoff], inrng5[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 5, l5[cntoff], inrng6[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 6, l6[cntoff], inrng7[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 7, l7[cntoff], inrng8[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 8, l8[cntoff], inrng9[cntoff], color.yellow, yes);
addchartbubble(lastbar[cntoff] and levels2 > 9, l9[cntoff], inrng10[cntoff], color.yellow, yes);


# rank
def rankoff = 15;
addchartbubble(lastbar[rankoff] and levels2 > 0, l0[rankoff], rank1[rankoff], (if rank1[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 1, l1[rankoff], rank2[rankoff], (if rank2[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 2, l2[rankoff], rank3[rankoff], (if rank3[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 3, l3[rankoff], rank4[rankoff], (if rank4[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 4, l4[rankoff], rank5[rankoff], (if rank5[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 5, l5[rankoff], rank6[rankoff], (if rank6[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 6, l6[rankoff], rank7[rankoff], (if rank7[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 7, l7[rankoff], rank8[rankoff], (if rank8[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 8, l8[rankoff], rank9[rankoff], (if rank9[rankoff] <= top_qty then color.magenta else color.gray), yes);
addchartbubble(lastbar[rankoff] and levels2 > 9, l9[rankoff], rank10[rankoff], (if rank10[rankoff] <= top_qty then color.magenta else color.gray), yes);


addlabel(1, " ", color.black);
# Add labels to display the counts
AddLabel(levels2 > 0, " 1: " + l0 + " - " + l1 + " - " + inrng1, CreateColor(175, 143, 14));
AddLabel(levels2 > 1, " 2: " + l1 + " - " + hi + " - " + inrng2, CreateColor(127, 196, 203));
AddLabel(levels2 > 2, " 3: " + l2 + " - " + hi + " - " + inrng3, CreateColor(89, 186, 131));
AddLabel(levels2 > 3, " 4: " + l3 + " - " + l4 + " - " + inrng4, CreateColor(89, 186, 131));
AddLabel(levels2 > 4, " 5: " + l4 + " - " + l5 + " - " + inrng5, CreateColor(89, 186, 131));
AddLabel(levels2 > 5, " 6: " + l5 + " - " + l6 + " - " + inrng6, CreateColor(89, 186, 131));
AddLabel(levels2 > 6, " 7: " + l6 + " - " + l7 + " - " + inrng7, CreateColor(89, 186, 131));
AddLabel(levels2 > 7, " 8: " + l7 + " - " + l8 + " - " + inrng8, CreateColor(89, 186, 131));
addLabel(levels2 > 8, " 9: " + l8 + " - " + l9 + " - " + inrng9, CreateColor(89, 186, 131));
AddLabel(levels2 > 9, " 10: " + l9 + " - " + l10 + " - " + inrng10, CreateColor(89, 186, 131));
addlabel(1, " ", color.black);
addlabel(1, " ", color.black);
#


--------------------------------



take a look at post13
a homemade version of close profile
it has 40 ranges
https://usethinkscript.com/threads/tradingview-poor-mans-volume-profile.14460/#post-119999

change this line, to change quantity of lines
def row_qty = 40;
to this
input row_qty = 10;
That's Incredible! Thank you so much, the price level counts match mine exactly!!
It's going to take me some time to review this script but its showing all levels 7 or 10.
The magenta Top ranking bubble is there for reference, but going with the top 3 levels only view, my original plan, not sure if turning off the other weaker levels is so wise, because exiting from a 15 when the next level has a high count of 14, meaning I could enter or exit too early.

Very useful tool! Absolutely. To find stronger and weaker price zones.
I included the original excel view for price ranking, although it's not required anymore.

You're Great halcyonguy !!
 

Attachments

  • ranking-image.png
    ranking-image.png
    36.1 KB · Views: 49
  • excel forumlas.png
    excel forumlas.png
    83.9 KB · Views: 47
  • Halycons-version1.png
    Halycons-version1.png
    219.3 KB · Views: 49
Last edited:
reply to 281
i edited your post, separated the 2 codes

i have no idea what you are asking for?
what is a range candle?
no idea what i am supposed to be looking at in the image?
what does this mean, plot only the last bar closed. do you mean just use the last bar data?

use more words and explain what you want to see.

it is drawing lines between the 2nd to last bar. that isn't a normal fib, which is from a peak to a valley.
I made one that works and I posted it if anyone wants to use it. The fibonacci lines are automatically drawn on each new range candle. It automatically detects the range candle size and plots the lines. I know its not being used the way a fibonacci would normally be used.

here is what i am using now.


Code:
DefineGlobalColor("Color100", CreateColor(255, 0, 0));      # RED
DefineGlobalColor("Color75", CreateColor(153, 153, 153));   # GRAY

DefineGlobalColor("Color618", CreateColor(0, 153, 51));      # BLUE
DefineGlobalColor("Color50", CreateColor(255, 0, 255));     # MAGENTA
DefineGlobalColor("Color382", CreateColor(0, 153, 51));    # ORANGE

DefineGlobalColor("Color25", CreateColor(153, 153, 153));   # GRAY
DefineGlobalColor("Color0", CreateColor(255, 0, 0));        # RED

# Input to choose how many bars back to calculate Fibonacci levels
input barsBack = 1;

# Calculate the current BarNumber
def bn = BarNumber();

# Count bars from right to left
def currentBar = HighestAll(if !IsNaN(close) then bn else Double.NaN);
def pastIndex = currentBar - bn;

# Identify the high and low of the bar "barsBack" bars ago
def targetBar = pastIndex == barsBack;
def rangeHigh = if targetBar then high else Double.NaN;
def rangeLow = if targetBar then low else Double.NaN;

# Hold the high and low values of the target bar for subsequent bars
def lastRangeHigh = if !IsNaN(rangeHigh) then rangeHigh else lastRangeHigh[1];
def lastRangeLow = if !IsNaN(rangeLow) then rangeLow else lastRangeLow[1];

# Calculate Fibonacci levels based on the target bar
def level100 = lastRangeHigh;
def level75 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.25;
def level618 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.618;
def level50 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.5;
def level382 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.382;
def level25 = lastRangeHigh - (lastRangeHigh - lastRangeLow) * 0.75;
def level0 = lastRangeLow;

# Plot Fibonacci levels as horizontal lines extending across the chart
plot P100 = if !IsNaN(close) then level100 else Double.NaN;
plot P75 = if !IsNaN(close) then level75 else Double.NaN;
plot P618 = if !IsNaN(close) then level618 else Double.NaN;
plot P50 = if !IsNaN(close) then level50 else Double.NaN;
plot P382 = if !IsNaN(close) then level382 else Double.NaN;
plot P25 = if !IsNaN(close) then level25 else Double.NaN;
plot P0 = if !IsNaN(close) then level0 else Double.NaN;

# Set color and style for each Fibonacci level
P100.SetDefaultColor(GlobalColor("Color100"));
P100.SetStyle(Curve.FIRM);
P100.SetLineWeight(1);
P100.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P75.SetDefaultColor(GlobalColor("Color75"));
P75.SetStyle(Curve.SHORT_DASH);
P75.SetLineWeight(1);
P75.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P618.SetDefaultColor(GlobalColor("Color618"));
P618.SetStyle(Curve.SHORT_DASH);
P618.SetLineWeight(1);
P618.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P50.SetDefaultColor(GlobalColor("Color50"));
P50.SetStyle(Curve.SHORT_DASH);
P50.SetLineWeight(1);
P50.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P382.SetDefaultColor(GlobalColor("Color382"));
P382.SetStyle(Curve.SHORT_DASH);
P382.SetLineWeight(1);
P382.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P25.SetDefaultColor(GlobalColor("Color25"));
P25.SetStyle(Curve.SHORT_DASH);
P25.SetLineWeight(1);
P25.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

P0.SetDefaultColor(GlobalColor("Color0"));
P0.SetStyle(Curve.FIRM);
P0.SetLineWeight(1);
P0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#


I added this to so i can see how many ticks there are till the close of the range candle.


Code:
# Input the dollar range for your range candles
input dollarRange = 50.0;  # The target dollar range for the bar

# Define the tick size in dollars (assuming 1 tick = $0.25)
def tickSize = 0.25;

# Calculate the current range of the bar in dollars
def currentRange = (high - low) / tickSize;

# Calculate the accumulated dollar amount as the bar progresses
def accumulatedDollars = currentRange * tickSize;

# Display the accumulated dollar amount as a label on the chart
AddLabel(yes, "$" + Round(accumulatedDollars, 2), if accumulatedDollars < dollarRange then Color.GREEN else Color.GREEN);
#
 
Last edited by a moderator:
That's Incredible! Thank you so much, the price level counts match mine exactly!!
It's going to take me some time to review this script but its showing all levels 7 or 10.
The magenta Top ranking bubble is there for reference, but going with the top 3 levels only view, my original plan, not sure if turning off the other weaker levels is so wise, because exiting from a 15 when the next level has a high count of 14, meaning I could enter or exit too early.

Very useful tool! Absolutely. To find stronger and weaker price zones.
I included the original excel view for price ranking, although it's not required anymore.

You're Great halcyonguy !!
To strengthen these price ranges, perhaps the entire candle must be present,
both open & close of a candle within the range.
However, this returns to the issue of most important prices ever (highest ranked & without ranges) vs the static levels with candle counts per a range.

I'll use this simplistic idea of candle counting within a range for a while, but better if the ranges aren't divided exactly but based upon other values, like long term pivots. Never ending!!
 
Last edited:
To strengthen these price ranges, perhaps the entire candle must be present,
both open & close of a candle within the range.
However, this returns to the issue of most important prices ever (highest ranked & without ranges) vs the static levels with candle counts per a range.

I'll use this simplistic idea of candle counting within a range for a while, but better if the ranges aren't divided exactly but based upon other values, like long term pivots. Never ending!!
I created a Watchlist column to sort thru symbols faster,
for current number of closes within 1 range to the last close. Yet STILL T/wrong counts but closer.

It is like the devil wrote this thinkscript coding language,
because the most basic feature of "counting bars" has been obscured on purpose. Oh yeah to stop the enemy "me", the one who can't program, to stop the enemy "me", from succeeding.
Like I said the devil wrote this thinkscript coding language.
-----------------------------
Only the total # of closes near the last close (can be edited to last price), with a small buffer range of .0035 higher or lower. (for testing)
(the very tiny range threshold can be edited,)

I asked both ChatGpt & Bard, & very much the same result, both were fails, so I actually wrote this piecemeal from the chart version.

My piecemeal code... still wrong counts but closer.
# Define a Range around LastClose !!
input lookbackPeriod = 10; #?!! I used to a long time ago but I'm too busy,
input threshold = 0.0035; # ranges + or - percent

def lastClose = if !isnan(close[-1]) then close[-1] else 0;
def increment = lastClose * threshold;

def range1Low = lastClose - increment;
def range1High = lastClose + increment;
def range = range1High - range1Low;

def candleCount = sum(if !isnan(close) and close >= range1Low and close <= range1High then 1 else 0, lookbackPeriod);

# Plot the result in the watchlist column
plot result = candleCount;
------------------------------------------------
:) Someday maybe adding an RSI:
Rsi w/3 different font colors upon conditions:
1) if the total #of bars near the last close were <6 with a low RSI (30% or less), "green font (buy)"
2) and if the total #of bars near the last close were >15 or <6, with a high RSI (60% or more). "red font (sell)"

Where a low bar count w/a low rsi would indicate an entry,
and a high bar count or extremely low bar count w/a high rsi would indicate an exit.
On some charts it looks like nice entry & exit.

My pretty font colored RSI for a Watchlist Column:
input VLow =35;
input Low = 42;
input High = 58;
input VHigh = 68;

plot RSI = Round(RSI(),0);

RSI.AssignValueColor(
if RSI < VLow then Color.GREEN
else if RSI >= VLow and RSI < Low then CreateColor(97, 155, 22)
else if RSI >= Low and RSI < High then CreateColor(209, 229, 182)
else if RSI >= High and RSI < VHigh then CreateColor(178, 109, 218)
else CreateColor(208, 43, 98)
);
----------------------------
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
290 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top