Prompt for AI
AlgoMind LabPROMPT 1
You are a professional Pine Script 6 programmer.
You have been developing indicators for the TradingView platform for a long time.
You know how to code both indicators and strategies, and you understand the differences between them. Do not use any code that Pine Script 6 does not support.
I would like to create a new indicator that helps me detect the “Double Top” or “Double Bottom” pattern. It seems reasonable to use Williams Fractals for this task; I will send you that indicator’s code.
To identify a Double Top we must, when a new fractal appears, look at the previous fractal and compare two parameters:
- It must be no farther than 20 candles from the newly formed fractal (make this a user-set variable).
- The HIGH of the candle where the fractal formed must be approximately equal to the HIGH of the previous fractal’s candle; the divergence must not exceed 0.8 % (also a variable).
Remember that Williams Fractals use a 2-candle offset (a fractal spans 5 candles), so we have to compare the candles on which the fractal arrows are plotted—that is, with an offset.
If both conditions are met, we have found a Double Top and should place a SELL label above the candle that became the latest fractal, because this is a reversal pattern.
For a Double Bottom we do the same, but mirrored.
Here is the Williams Fractals indicator code:
//@version=6
indicator("Williams Fractals", shorttitle="Fractals", format=format.price,
precision=0, overlay=true)
// Define "n" as the number of periods and keep a minimum value of 2 for error handling.
n = input.int(title="Periods", defval=2, minval=2)
// ---------------- Up Fractal ----------------
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true
for i = 1 to n
upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4
upFractal = (upflagDownFrontier and flagUpFrontier)
// ---------------- Down Fractal ----------------
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true
for i = 1 to n
downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4
downFractal = (downflagDownFrontier and flagDownFrontier)
plotshape(downFractal, style=shape.triangledown, location=location.belowbar,
offset=-n, color=#F44336, size=size.small)
plotshape(upFractal, style=shape.triangleup, location=location.abovebar,
offset=-n, color=#009688, size=size.small)
PROMPT 2
The script works great—thanks!
However, on higher timeframes small additional fractals sometimes appear between the double tops. Let’s check not only the last fractal but also several previous ones—say, up to 10 fractals back.
If we find any of those with the specified HIGH or LOW divergence, we should still issue a signal.
Don’t forget to add an array-length check, because the array will be empty at first, which would trigger an error.
PROMPT 3
Excellent! Now let’s make the indicator a bit more aggressive and profitable.
Right now our fractals are confirmed only after at least two candles to the left and two to the right, which is accurate but slow.
Let’s try to fire the signal earlier:
- Keep the fractal drawing rules exactly as they are.
- But create the signal with a one-candle offset on the right (leave the left offset at 2). Make both offsets user-adjustable variables.
In other words, we split the “left” and “right” offsets into two separate variables:
- Left offset = 2 (default)
- Right offset = 1 (default test value)
If the candle’s HIGH is the highest within the left-offset window and meets all our other conditions, we issue the signal without waiting for the fractal arrow to plot.
The older fractals remain formed under the strict rules—we are merely decoupling fractal formation from signal generation.
Finally, add a checkbox so the user can enable or disable each of these two signal systems independently.