TradingView
informanerd
16 sept. 2021 18:44

Auto Fibonacci and Gann Fan/Retracements Combo 

Bitcoin / U.S. dollarBitstamp

Description

Introduction

This is a combination of Fibonacci and Gann fan/retracements.
The script can automatically draw as many:
  • Fibonacci Retracements
  • Fibonacci Fan
  • Gann Retracements
  • Gann Fan

as the user requires on the chart. Each level set or fan consists of 7 lines based on the most important ratios of Fibonacci/Gann.

Basics

What are Fibonacci retracements?
Fibonacci retracement levels are horizontal lines that indicate where support and resistance are likely to occur. They stem from Fibonacci’s sequence. Each level is associated with a percentage which is how much of a prior move the price has retraced. The Fibonacci retracement levels are 23.6%, 38.2%, 61.8%, and 78.6%. While not officially a Fibonacci ratio, 50% is also used. The indicator is useful because it can be drawn between any two significant price points, such as a high and a low. The indicator will then create the levels between those two points.

What are Gann retracements?
A developer of technical analysis and trading was W.D. Gann. Gann theory expects a normal retracement of 50 percent. This means that under normal selling pressure, the stock price will decline half the amount of its most recent rise, and vice versa. It also suggests that retracements occur at the halfway point of a move, such as 25 percent (half of 50 percent), 12.5 percent (half of 25 percent), and so on.

What is Fibonacci fan?
Fibonacci fan is a set of sequential trend lines drawn from a trough or peak through a set of points dictated by Fibonacci retracements. The first step to create it is to draw a trend line covering the local lowest and highest prices of a security. To reach retracement levels, the trader divides the difference in price at the low and high end by ratios determined by the Fibonacci series. The lines formed by connecting the starting point for the base trend line and each retracement level create the Fibonacci fan.

What is Gann fan?
A Gann fan consists of a series of lines called Gann angles. These angles are superimposed over a price chart to show potential support and resistance levels. The resulting image is supposed to help technical analysts predict price changes. Gann believed the 45-degree angle to be most important, but the Gann fan also draws angles at degrees like 75, 63.75, 26.25 and 15. The Gann fan originates at a low or high point. The resulting lines show areas of potential future support and resistance. The 45-degree line is known as the 1:1 line because the price will rise or fall at a 45-degree angle when the price moves up/down one unit for each unit of time. All other lines in the Gann fan are drawn above and below the 1:1 line. The other angles are associated with 2:1, 3:1, 4:1, 8:1 and 1:8, 1:4, 1:3, and 1:2 time-to-price moves.

Challenges

The most of the time I dedicated to writing this script has been spent on handling these problems:

1. Finding Local Highest/Lowest Prices
In order to draw Fibonacci and Gann fan/retracements, it's necessary to find local highest and lowest price points (Extrema) on the chart. As this could be so challenging, most traders and coders draw the lines covering the low and high prices over a given period of time or a limited number of bars back instead. I already wrote an indicator using this approach (Auto Fibonacci Combo).
In this new script I tried to find the exact highest and lowest prices based on this idea that: if a high point is formed lower than previous high which was after a lowest point, then that previous one was the local highest point, and vice versa if a low point is formed higher than previous low which was after a highest point, then that previous one was the local lowest point. So logically an extremum price on the chart won't be found until the next high/low point is formed.

2. Finding Proper Chart Scale for Gann Fan
Based on the theory, Gann angles are sensitive to the chart price scale and in order to have the right angles, the chart must be made with the proper scale. J.A. Hyerczyk in his book "Pattern, Price & Time - Using Gann Theory in Technical Analysis" suggests that the easiest way to determine the scale of a market is by taking the difference between top-to-top and bottom-to-bottom and dividing it by the time it took the market to move from top to top and bottom to bottom.
Thus on a properly constructed chart, the basic equation for calculating Gann angles is: Price * Time.

3. Drawing Fans and Relocating Fan Labels at Each New Bar in Pine (A Programming-Related Subject)
To do this, I used linear equations and line slopes. Of course it was so complicated and exhausting, but finally I overcame that thanks to my genius cousin.

Settings and Usage

By default, the script shows detected extremum points plus 1 Fibonacci fan, 1 Gann fan, 1 set of Fibonacci retracements and no Gann retracements on the chart. All of these could be changed in the indicator settings beside the color and transparency of each line.

Feel free to use this and send me your thoughts!
Commentaires
Trendoscope
Well done. Hope to see more interesting scripts in future :)
informanerd
@HeWhoMustNotBeNamed, Thank you for the encouragement! It's a pleasure to have the experienced ones support.
z28betz
Informanerd, big hugs)
informanerd
@europabetz, Thanks!
PineCoders
Vangravino
Hi, I loved the script, but to use it I would like to make a modification to it: I would like the pivots to be longer, which takes into account 15 or more bars for each side before modifying the pivot. I tried to do it myself but couldn't. Please could you tell me which line I would have to modify the depth? Thanks!!!
informanerd
@Vangravino, Hey! I didn't think of the capability while I was writing this. But if you have some programming skills, you could do that by changing isHigh() and isLow() functions. Or you could wait till I make a new version within coming weeks.
Vangravino
@informanerd, Thank you!!! my pine skills are almost nil, but I'm trying. If I get it, I will send it to you for you to review. If I don't get it, I'll wait for your update! :)
Vangravino
Dear @informanerd, from what I saw in another script (auto fibo) the options would be something like what is in part 1 (which I send below); in that case, the pivot would have to be found as it appears written in part 2. But, obviously, that changes the functions and I try to modify your script and it gives me different errors that I don't know how to solve hehe. Actually the only thing I'm looking for is an automatic time speed fibo that is adjustable in its depth ... your script is great, much more complex than what I'm looking for. I really appreciate your input and I look forward to your update.

Part 1:
devTooltip = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
depthTooltip = "The minimum number of bars that will be taken into account when calculating the indicator."
// pivots threshold
threshold_multiplier = input(title="Deviation", type=input.float, defval=3, minval=0, tooltip=devTooltip)
dev_threshold = atr(10) / close * 100 * threshold_multiplier
depth = input(title="Depth", type=input.integer, defval=10, minval=1, tooltip=depthTooltip)

part 2:
var line lineLast = na
var int iLast = 0
var int iPrev = 0
var float pLast = 0
var isHighLast = false // otherwise the last pivot is a low pivot

pivots(src, length, isHigh) =>
l2 = length * 2
c = nz(src[length])
ok = true
for i = 0 to l2
if isHigh and src > c
ok := false

if not isHigh and src < c
ok := false
if ok
[bar_index[length], c]
else
[int(na), float(na)]
[iH, pH] = pivots(high, depth / 2, true)
[iL, pL] = pivots(low, depth / 2, false)
[src, lenth, isHigh]
informanerd
@Vangravino, Actually my method is so different and it's better you wait! Also you could check this script:
tradingview.com/script/pKhv9SoH-Auto-Fib-Speed-Resistance-Fans-by-DGT/
Plus