alexeix
alexeix

Reputation: 77

How can I edit the Williams Fractal indicator in Pine Script to display a 3-bar fractal?

The Williams Fractal paints a fractal by finding the low/high of 5 bars, lagging by 2 bars.

I would like to set up an indicator that finds the low/high of 3 bars.

I've examined TradingView's built-in Williams Fractal, which shows the following code:

//@version=4
study("Williams Fractal", shorttitle="Fractal", 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(title="Periods", defval=2, minval=2, type=input.integer)

My first question is about defval: why is the default value 2? Does this refer to the lag? Also, I thought setting an input allows the user to manipulate this number on TradingView for indicator preferences (like setting the lookback period for an MA), but I don't see this input on the built-in Williams Fractal.

Then we get to these parameters:

upFractal = (                                                                                                          (high[n+2]  < high[n]) and (high[n+1]  < high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
         or (                                                                               (high[n+3]  < high[n]) and (high[n+2]  < high[n]) and (high[n+1] == high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
         or (                                                    (high[n+4]  < high[n]) and (high[n+3]  < high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
         or (                          (high[n+5] < high[n]) and (high[n+4]  < high[n]) and (high[n+3] == high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
         or ((high[n+6] < high[n]) and (high[n+5] < high[n]) and (high[n+4] == high[n]) and (high[n+3] <= high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))

dnFractal = (                                                                                                  (low[n+2]  > low[n]) and (low[n+1]  > low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
         or (                                                                         (low[n+3]  > low[n]) and (low[n+2]  > low[n]) and (low[n+1] == low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
         or (                                                (low[n+4]  > low[n]) and (low[n+3]  > low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
         or (                        (low[n+5] > low[n]) and (low[n+4]  > low[n]) and (low[n+3] == low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
         or ((low[n+6] > low[n]) and (low[n+5] > low[n]) and (low[n+4] == low[n]) and (low[n+3] >= low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))

I was able to figure out that high and low are the current high and low prices, and that the the bracketed value gives access to previous high/low values. But does high[n+3] (to use one example) mean the high price 3 candles forward, and high[n-1] mean the high 1 candle back?

I'm having a hard time following the logic of the code, regardless. I get upFractal is the variable that will be painted on the screen, but let me try to put the following line into words:

upFractal = (                                                                                                          (high[n+2]  < high[n]) and (high[n+1]  < high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))

This is what I think it means:

"Paint an upFractal if the high of the bar two bars ahead is less than the current high of the current bar and the high of the bar one bar ahead is less than the high of the current bar and the high of the bar one bar behind is less than the high of the current bar and the high of the bar two bars behind is less than the high of the current bar."

Am I getting this right? Can I simply follow this logic for a 3 bar pattern?

Thank you.

Upvotes: 1

Views: 2512

Answers (2)

kambiz hosseinzadeh
kambiz hosseinzadeh

Reputation: 11

Try to change defval and minval from 2 to 3 . Maybe it is your answer.

Upvotes: 0

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

When using the history referencing [] operator on time series in Pine, the behavior is the opposite of your understanding; greater indices are further away in the past and zero (or the variable name without the operator) references the current bar.

Upvotes: 1

Related Questions