Code: Select all
//@version=4
study("Channel","CT",true)
delay_plot_til_closed = true
repaint = (not(delay_plot_til_closed) or barstate.ishistory or barstate.isconfirmed)
fleftBars = input(title="Look Back Period", type=input.integer, defval=20, minval=1, maxval=50, step=1, group="Fibo Channel Settings")
frightBars=input(title="Look Forward Period", type=input.integer, defval=5, minval=1, maxval=30, step=1, group="Fibo Channel Settings")
widt = input(defval = 1, title = "Line Width",inline ="FiboSet1", group="Fibo Channel Settings")
extendit = input(defval = true, title = "Extend Lines to Right",inline ="FiboSet1", group="Fibo Channel Settings")
PeakPointsConfirmation = input(title="Confirm Peak on Move %", type=input.float, defval=0.15, minval=0.05, maxval=0.25, step=0.01, group="Fibo Channel Settings")
UpChannelOption = input(title="Up Channel", type=input.string,
options=["solid (─)", "dotted (┈)", "dashed (╌)"],
defval="dashed (╌)", group="Fibo Channel Settings")
UpChanlineStyle = (UpChannelOption == "dotted (┈)") ? line.style_dotted :
(UpChannelOption == "dashed (╌)") ? line.style_dashed :
line.style_solid
DownChannelOption = input(title="Down Channel", type=input.string,
options=["solid (─)", "dotted (┈)", "dashed (╌)"],
defval="dashed (╌)", group="Fibo Channel Settings")
DownChanlineStyle = (DownChannelOption == "dotted (┈)") ? line.style_dotted :
(UpChannelOption == "dashed (╌)") ? line.style_dashed :
line.style_solid
fb1 = input(title="Fibo Channel 1", type=input.float, defval=0.236, group="Fibo Channel Percentage")
fb2 = input(title="Fibo Channel 2", type=input.float, defval=0.618, group="Fibo Channel Percentage")
fb3 = input(title="Fibo Channel 3", type=input.float, defval=1.618, group="Fibo Channel Percentage")
fb4 = input(title="Fibo Channel 4", type=input.float, defval=2.618, group="Fibo Channel Percentage")
//========================== Variables ===============================
var float TenPctOfPivotLow = 0,float TenPctOfPivotHigh = 0
var float CurrentPivotLow = 0,float CurrentPivotHigh = 0
var float TempPivotLow = 0,float TempPivotHigh = 0
var int TempCandlePivotLow = 0,int TempCandlePivotHigh = 0
var int HasclosedAboveTenPct = 0,int HasclosedBelowTenPct = 0
var float LastKnownPivotLow = 1e99,float LastKnownPivotHigh = 1e99
var int CandleOfLatestLow = 0,int CandleOfLatestHigh = 0
var int CandleOfLastKnownPivotLow = 0,int CandleOfLastKnownPivotHigh = 0
var float Slope = 0, int Trend = 0
var int DistanceBwTwoZeroPoints = 0, int MidToLeftDistance = 0, int MidToRightDistance = 0
var float ExpValueOf100FromMidToLeft = 0, float ExpValueOf100FromMidToRight = 0
var fibo_ratios = array.new_float(0)
var bool AlreadyDownChannelExists = false, bool AlreadyUpChannelExists = false
var line DrawUpTrendLine = line.new(bar_index,low,bar_index,low)
var line DrawUpHighTrendLine = line.new(bar_index,low,bar_index,low)
var Downfibolines = array.new_line(4)
var Upfibolines = array.new_line(4)
var line DrawDownTrendLine = line.new(bar_index,high,bar_index,high)
var line DrawUpLowTrendLine = line.new(bar_index,high,bar_index,high)
if barstate.isfirst
array.push(fibo_ratios, fb1)
array.push(fibo_ratios, fb2)
array.push(fibo_ratios, fb3)
array.push(fibo_ratios, fb4)
//========================== BASIC CALCULATION ===============================
PivotHigh=0.00, PivotHigh := nz(pivothigh(high,fleftBars, frightBars),0.00)
PivotLow=0.00, PivotLow := nz(pivotlow(low,fleftBars, frightBars),0.00)
//========================== PIVOT HIGH CALCULATION ===============================
if PivotHigh != 0.00 // Candles where Pivot High is found
TempPivotHigh:= PivotHigh
TempCandlePivotHigh := valuewhen(TempPivotHigh, bar_index[frightBars], 0)
TenPctOfPivotHigh := (TempPivotHigh - ((TempPivotHigh * PeakPointsConfirmation)/100))
else
TempPivotHigh:= TempPivotHigh[1]
TempCandlePivotHigh := TempCandlePivotHigh[1]
TenPctOfPivotHigh := TenPctOfPivotHigh[1]
HasclosedBelowTenPct := 0
for i = 0 to 30
if close[i] <= TenPctOfPivotHigh and bar_index[i] > TempCandlePivotHigh
HasclosedBelowTenPct:= HasclosedBelowTenPct + 1
if HasclosedBelowTenPct == 1
LastKnownPivotHigh:= CurrentPivotHigh[1]
CandleOfLastKnownPivotHigh:=CandleOfLatestHigh[1]
CurrentPivotHigh := TempPivotHigh
CandleOfLatestHigh := TempCandlePivotHigh
else
LastKnownPivotHigh:= LastKnownPivotHigh[1]
CandleOfLastKnownPivotHigh:=CandleOfLastKnownPivotHigh[1]
CurrentPivotHigh := CurrentPivotHigh[1]
CandleOfLatestHigh := CandleOfLatestHigh[1]
//========================== PIVOT LOW CALCULATION ===============================
if PivotLow != 0.00 // Candles where Pivot Low is found
TempPivotLow:= PivotLow
TempCandlePivotLow := valuewhen(TempPivotLow, bar_index[frightBars], 0)
TenPctOfPivotLow := (TempPivotLow + ((TempPivotLow * PeakPointsConfirmation)/100))
else
TempPivotLow:= TempPivotLow[1]
TempCandlePivotLow := TempCandlePivotLow[1]
TenPctOfPivotLow := TenPctOfPivotLow[1]
HasclosedAboveTenPct := 0
for i = 0 to 30
if close[i] >= TenPctOfPivotLow and bar_index[i] > TempCandlePivotLow
HasclosedAboveTenPct:= HasclosedAboveTenPct + 1
if HasclosedAboveTenPct == 1
LastKnownPivotLow:= CurrentPivotLow[1]
CandleOfLastKnownPivotLow:=CandleOfLatestLow[1]
CurrentPivotLow := TempPivotLow
CandleOfLatestLow := TempCandlePivotLow
else
LastKnownPivotLow:= LastKnownPivotLow[1]
CandleOfLastKnownPivotLow:=CandleOfLastKnownPivotLow[1]
CurrentPivotLow := CurrentPivotLow[1]
CandleOfLatestLow := CandleOfLatestLow[1]
//===================== Determination of Trend ========================
DistanceBwTwoZeroPoints:= 0
Slope:= 0
MidToLeftDistance:= 0
MidToRightDistance:= 0
ExpValueOf100FromMidToLeft := 0
ExpValueOf100FromMidToRight := 0
if CurrentPivotLow > LastKnownPivotLow and repaint and AlreadyUpChannelExists == false
AlreadyUpChannelExists := true
DistanceBwTwoZeroPoints:= CandleOfLatestLow - CandleOfLastKnownPivotLow
Slope:= (CurrentPivotLow - LastKnownPivotLow) / DistanceBwTwoZeroPoints
MidToRightDistance:= CandleOfLatestLow - CandleOfLatestHigh
MidToLeftDistance:= CandleOfLatestHigh - CandleOfLastKnownPivotLow
ExpValueOf100FromMidToLeft := CurrentPivotHigh - (Slope * MidToLeftDistance)
ExpValueOf100FromMidToRight := CurrentPivotHigh + (Slope * MidToRightDistance)
Trend:= 1
else if CurrentPivotHigh < LastKnownPivotHigh and repaint and AlreadyDownChannelExists == false
AlreadyDownChannelExists := true
DistanceBwTwoZeroPoints:= CandleOfLatestHigh - CandleOfLastKnownPivotHigh
Slope:= (LastKnownPivotHigh - CurrentPivotHigh) / DistanceBwTwoZeroPoints
MidToLeftDistance:= CandleOfLatestLow - CandleOfLastKnownPivotHigh
MidToRightDistance:= CandleOfLatestHigh - CandleOfLatestLow
ExpValueOf100FromMidToLeft := CurrentPivotLow + (Slope * MidToLeftDistance)
ExpValueOf100FromMidToRight := CurrentPivotLow - (Slope * MidToRightDistance)
Trend:= -1
if (CurrentPivotLow < LastKnownPivotLow) or (MidToRightDistance <= 0) or (CurrentPivotHigh < LastKnownPivotHigh) or dayofmonth(time[bar_index - CandleOfLatestLow]) != dayofmonth(time[bar_index - CandleOfLastKnownPivotLow])
AlreadyUpChannelExists := false
if (CurrentPivotHigh < LastKnownPivotHigh) or (MidToRightDistance <= 0) or (CurrentPivotLow < LastKnownPivotLow) or dayofmonth(time[bar_index - CandleOfLatestHigh]) != dayofmonth(time[bar_index - CandleOfLastKnownPivotHigh])
AlreadyDownChannelExists := false
if Trend== 1
line.delete(DrawDownTrendLine)
line.delete(DrawUpLowTrendLine)
for ab = 0 to 3
line.delete(array.get(Downfibolines, ab))
DrawUpTrendLine := line.new(CandleOfLastKnownPivotLow, LastKnownPivotLow, CandleOfLatestLow,CurrentPivotLow, style=UpChanlineStyle, color=color.lime,width = widt,extend = extendit ? extend.right : extend.none)
line.delete(DrawUpTrendLine[1])
DrawUpHighTrendLine := line.new(CandleOfLastKnownPivotLow, ExpValueOf100FromMidToLeft , CandleOfLatestLow, ExpValueOf100FromMidToRight, style=UpChanlineStyle, color=color.lime,width = widt,extend = extendit ? extend.right : extend.none)
line.delete(DrawUpHighTrendLine[1])
for x = 0 to 3
line.delete(array.get(Upfibolines, x))
array.set(Upfibolines, x,
line.new(x1 = CandleOfLastKnownPivotLow,
y1 = LastKnownPivotLow + (( ExpValueOf100FromMidToLeft - LastKnownPivotLow ) * array.get(fibo_ratios, x)),
x2 = CandleOfLatestLow,
y2 = CurrentPivotLow + (( ExpValueOf100FromMidToRight - CurrentPivotLow ) * array.get(fibo_ratios, x)),
color = x == 0? color.aqua : x == 1? color.lime : color.yellow,
style = UpChanlineStyle,
width = widt,
extend = extendit ? extend.right : extend.none))
else if Trend== -1
line.delete(DrawUpTrendLine)
line.delete(DrawUpHighTrendLine)
for aa = 0 to 3
line.delete(array.get(Upfibolines, aa))
DrawDownTrendLine := line.new(CandleOfLastKnownPivotHigh, LastKnownPivotHigh, CandleOfLatestHigh,CurrentPivotHigh, style=DownChanlineStyle, color=color.red,width = widt,extend = extendit ? extend.right : extend.none)
line.delete(DrawDownTrendLine[1])
DrawUpLowTrendLine := line.new(CandleOfLastKnownPivotHigh, ExpValueOf100FromMidToLeft , CandleOfLatestHigh, ExpValueOf100FromMidToRight, style=DownChanlineStyle, color=color.red,width = widt,extend = extendit ? extend.right : extend.none)
line.delete(DrawUpLowTrendLine[1])
for x = 0 to 3
line.delete(array.get(Downfibolines, x))
array.set(Downfibolines, x,
line.new(x1 = CandleOfLastKnownPivotHigh,
y1 = LastKnownPivotHigh + (( ExpValueOf100FromMidToLeft - LastKnownPivotHigh ) * array.get(fibo_ratios, x)),
x2 = CandleOfLatestHigh,
y2 = CurrentPivotHigh + (( ExpValueOf100FromMidToRight - CurrentPivotHigh ) * array.get(fibo_ratios, x)),
color = x == 0? color.aqua : x == 1? color.red : color.yellow,
style = DownChanlineStyle,
width = widt,
extend = extendit ? extend.right : extend.none))
The above script first identifies 3 main points. if in case to plot a channel for an uptrend, it requires first low point and next high point and finally higher low point. vise versa for downtrend which requires high, low, and lower high points.
based on these points channel is being plotted on charts. at any point in time, I need only one channel to show on the chart.
The current problem is:
let's say it has found those 3 points initially for an uptrend. in few candles, if a new high is found it re-adjusts the cannel by deleting the old one. can you help me put a condition that either zero-line is broken or price (high) has reached "fb4" no new channel should be plotted?
Found an uptrend while the channel is down and the price is within the zero-line and fb4 line. Currently, it deletes the down channel and plots a new uptrend channel. can you help me put a condition that either zero-line is broken or price (high) has reached "fb4" no new channel should be plotted?