Hello,
The common way to code fibonacci level is to take the high/low over a past period, lets say 1000 units, or 10 days, or whatever. The issue with this approch is that
- You never know if the relevant high/low is within your period
- The fibo that you have drawn has maybe already play its role (a retracement has occured) and news levels should be used.
So I’m looking for a way to remove this fixed period, by using a rule. The rule is “if there has been a retracement greater than 50%, and then afterwards the market turns again and break the 50% level again, then a new fibo should be drawn”. I have attached an exemple on the Dax TF4H. Three fibos can be drawn successfully, first the blue, then the red, and finally the white. It means that the fibo which is now relevant is the white one, the two other can be deleted. This is in opposition with the common way to drawn it (11265-11990 vs 11572-11990).
I have reused a code of Nicolas, but I struggle to
- Add a loop which will takes first the highest high /lowest low, then move to the next high/low if there has been a retracement
- I need to add a way via zigzag or anything else to check if we break again the 50%.
Sorry I’m a bit in a rush, but looking forward your advice
defparam drawonlastbaronly=true
ONCE startbar=0
ONCE hh=0
ONCE ll=1000000000
ONCE shifthighest=0
ONCE shiftlowest=0
//shiftlowest=barindex
for i = 1000-startbar downto 1 do
if high[i]>=hh then
hh=high[i]
shifthighest=barindex[i]
endif
if low[i]<=ll then
ll=low[i]
shiftlowest=barindex[i]
endif
next
//
isSwingDown = shiftHighest < shiftLowest
if isSwingDown then
fullrange = abs(hh-ll)
fibo100 = hh
fibo0 = ll
fibo236 = ll+(fullrange*0.236)
fibo382 = ll+(fullrange*0.382)
fibo50 = ll+fullrange/2
fibo618 = ll+(fullrange*0.618)
startbar1 = min(shifthighest,shiftlowest)
endbar=max(shifthighest,shiftlowest)
r=255
g=0
b=0
else
fullrange = abs(hh-ll)
fibo100 = ll
fibo0 = hh
fibo236 = hh-(fullrange*0.236)
fibo382 = hh-(fullrange*0.382)
fibo50 = hh-fullrange/2
fibo618 = hh-(fullrange*0.618)
startbar1 = min(shifthighest,shiftlowest)
endbar=max(shifthighest,shiftlowest)
r=0
g=255
b=0
endif
if not isSwingDown then
blockprice=hh
startbar=barindex-shifthighest
for i = startbar downto 1 do
if low[i]<blockprice then
blockprice=low[i]
//blockbar=barindex[i]
endif
next
if blockprice<fibo50 then
startbar=shifthighest
endif
else
blockprice=ll
startbar=barindex-shiftlowest
for i = startbar downto 1 do
if high[i]>blockprice then
blockprice=high[i]
endif
next
if blockprice>fibo50 then
startbar=shiftlowest
endif
endif
//
drawsegment(startbar1,fibo100,endbar,fibo100) coloured(r,g,b)
drawtext(" 100%",endbar,fibo100,Dialog,Standard,20) coloured(r,g,b)
drawsegment(startbar1,fibo0,endbar,fibo0) coloured(r,g,b)
drawtext(" 0%",endbar,fibo0,Dialog,Standard,20) coloured(r,g,b)
drawsegment(startbar1,fibo236,endbar,fibo236) coloured(r,g,b)
drawtext(" 23.6%",endbar,fibo236,Dialog,Standard,20) coloured(r,g,b)
drawsegment(startbar1,fibo382,endbar,fibo382) coloured(r,g,b)
drawtext(" 38.2%",endbar,fibo382,Dialog,Standard,20) coloured(r,g,b)
drawsegment(startbar1,fibo50,endbar,fibo50) coloured(r,g,b)
drawtext(" 50%",endbar,fibo50,Dialog,Standard,20) coloured(r,g,b)
drawsegment(startbar1,fibo618,endbar,fibo618) coloured(r,g,b)
drawtext(" 61.8%",endbar,fibo618,Dialog,Standard,20) coloured(r,g,b)
return