TDC Market Kinematics v2.3.1
THE SOURCE FILE /code/market-kinematics.pine
Read the file, copy it, or download the exact bytes served at this address. No performance figure is claimed on this page.
//@version=6
// MFP edition: 8 inputs, confirmed prior HTF references, standard intraday bars.
indicator("TDC Market Kinematics v2.3.1 MFP",shorttitle="TDC Kinematics",overlay=true,max_bars_back=500,max_lines_count=500,max_boxes_count=8,max_labels_count=80,max_polylines_count=2)
string lw=input.string("Prediction only","What to show",options=["Prediction only","Prediction + key levels"],group="01 | Easy settings")
bool kk=input.bool(true,"Use simple words",group="01 | Easy settings")
bool lu=lw=="Prediction + key levels"
string ji=input.string("Live idea only","Prediction history",options=["Live idea only","Live idea + history"],group="01 | Easy settings")
bool bz=ji=="Live idea only"
bool cr=input.bool(true,"Show price since current idea started",group="01 | Easy settings")
bool iK=input.bool(true,"Show idea start dots",group="01 | Easy settings")
const bool fp=true
bool fH=not bz and fp
const int iy=30
const string mT="Asia/Kolkata"
string nK=mT=="Exchange"?syminfo.timezone:mT
const bool hk=true
const int hP=50
int mP=input.int(8,"Space after the candles",minval=2,maxval=40,group="02 | Chart layout")
const int cL=30
const int cc=20
const bool gY=true
const bool fR=false
const bool dL=false
const int ho=1
const int fQ=6
const int ky=35
const bool ib=true
const int hA=36
const int cp=3
const float hT=0.45
const float lZ=0.9
float oh=input.float(1.5,"Minimum planned reward/risk",minval=0.5,step=0.25,group="Advanced | Research")
const float ee=0.01
const float dH=0.85
const float ma=0.35
const float cg=2
bool iP=input.bool(true,"Trail the idea behind defended structure",group="Advanced | Entry & protection")
const bool hv=true
const int jb=3
const int hl=3
const float hW=0.45
const bool cq=false
const bool fZ=true
const int fY=3
const int eP=10
const int dv=12
const float be=0.35
const bool kc=true
const int ir=3
const int aM=8
const int kE=21
const int gG=8
const int gH=15
const int it=480
const float dF=0.35
const bool kX=true
const float jj=1.15
const int kY=10
const int mZ=15
const int nZ=400
const float es=0.55
const int gk=4500
const int eZ=7
const int fi=3
const int cm=1
const string iU="Close"
const float ja=0.55
const bool fN=true
const bool mO=true
const bool mq=true
const int kF=3
const float ly=0.10
const bool dx=true
const int cR=4
const int E=55
const int gK=80
const bool iv=true
const string eb="Confirmed"
const string eB="Swing + EQH/EQL"
const bool eA=true
const bool hp=true
const int bw=5
const int ej=1
const int hq=45
const bool ou=true
const int gQ=16
const bool hw=false
const bool iH=false
const string ca="Both"
const string gP="Full candle"
const bool fO=true
const bool hx=true
const string el="Both"
const bool ms=true
const float hN=0.08
const bool ni=true
const float jx=0.025
const int jC=165
const bool gs=true
const int hI=60
const string dS="Exchange day"
const string eV="0930-1600"
const string hj="America/New_York"
const string fP="High-Low"
const int nD=14
const float nv=2
const float jE=0.5
const bool kW=false
gO=math.pow(nv,1.1-jE)
float jF=hlc3
type dT
int kind
int G
float top
float bG
int nT
int cy
int bg
int eT
float iY
int ad
bool dG
bool gw
bool iM
bool nO
int lA
int bF
int aV
box S
int hn
float fW
int gD
type cW
float ae
int side
int cO
int born
int nN
bool jK
bool eO
int jL
int fI
line S
label tag
var array<dT>eX=array.new<dT>()
var array<cW>jA=array.new<cW>()
var array<cW>D=array.new<cW>()
type ai
float ae
int side
int born
type ab
int side
float top
float bG
float jD
int born
bool fF
box S
bool em
var array<ai>L=array.new<ai>()
var array<ab>J=array.new<ab>()
var bool ck=false
var bool cl=false
var float ak=na
var float aw=na
var float eJ=na
var float dQ=na
atr=ta.atr(14)
float jB=math.abs(close-open)
float ll=math.max(high-low,syminfo.mintick)
bool A=jB>=atr*ja and jB/ll>=0.48
bool ek=iU=="Close"or A
fD(dT z)=>
switch z.kind
1=>z.dG?"ROB":z.gw?"OOB":"OB"
2=>"FVG"
3=>"VI"
4=>"BB"
5=>"IFVG"
=>"??"
bI(int x,int nE)=>not na(x)and bar_index>=x and bar_index-x<=nE
ci(int kind,int dir,float eU,float eC,int dq,int ad,bool dG,bool kR,bool lh,bool kl,int lz)=>
if not na(eU)and not na(eC)and eU>eC and dq>=0
dT z=dT.new(kind,dir,eU,eC,dq,bar_index,0,0,0.0,ad,dG,kR,lh,kl,lz,na,na,na,na,na,0)
array.push(eX,z)
if array.size(eX)>jC
dT old=array.shift(eX)
if not na(old.S)
box.delete(old.S)
bE(float ae,int side,int cO,int hU)=>
if not na(ae)and side!=0
int lG=na
float lk=math.max(syminfo.mintick*2,nz(atr)*ly)
if array.size(D)>0
for n=array.size(D)-1 to 0
cW p=array.get(D,n)
if not p.jK and p.side==side and math.abs(p.ae-ae)<=lk
lG:=n
break
if not na(lG)
cW p=array.get(D,lG)
p.fI+=1
p.cO:=p.cO==3?3:cO
else
int kh=na(hU)?bar_index:math.max(hU,bar_index-gk)
cW p=cW.new(ae,side,cO,bar_index,kh,false,false,na,1,na,na)
array.push(jA,p)
array.push(D,p)
while array.size(jA)>gK
cW old=array.shift(jA)
if not old.jK
array.shift(D)
if not na(old.S)
line.delete(old.S)
if not na(old.tag)
label.delete(old.tag)
bi(int side,float top,float bG,float hS,bool em)=>
if top>bG and not na(hS)
ab s=ab.new(side,top,bG,hS,bar_index,false,na,em)
array.push(J,s)
while array.size(J)>hq
ab old=array.shift(J)
if not na(old.S)
box.delete(old.S)
lE(int dir,int fz)=>
int T=na
float best= -100000.0
for j=1 to fz
if not na(close[j+1])
bool jy=dir==1?close[j]<=open[j]:close[j]>=open[j]
bool kd=dir==1?low[j]<low[j+1]:high[j]>high[j+1]
float kQ=9.0*(1.0-float(j)/math.max(float(fz),1.0))
float nh=(jy?25.0:0.0)+(kd?13.0:0.0)+kQ
if jy and nh>best
best:=nh
T:=j
T
float xPH=ta.pivothigh(high,eZ,eZ)
float xPL=ta.pivotlow(low,eZ,eZ)
float iPH=ta.pivothigh(high,fi,fi)
float iPL=ta.pivotlow(low,fi,fi)
float mPH=ta.pivothigh(high,cm,cm)
float mPL=ta.pivotlow(low,cm,cm)
var float cU=na
var float xLow=na
var float lb=na
var float mh=na
var int dz=na
var int eW=na
var int dA=na
var int ei=na
var float dN=na
var float iLow=na
var int er=na
var int gg=na
var int dh=na
var int dY=na
var float kG=na
var float mLow=na
var int gJ=na
var int ix=na
var int dl=na
var int ec=na
var int X=0
var int dZ=0
var int gM=0
var float N=na
var float W=na
var float iX=na
var float kt=na
var int aA=na
var int az=na
if barstate.isconfirmed
if not na(xPH)
float dr=cU
lb:=cU
cU:=xPH
dz:=bar_index-eZ
if fN
bE(xPH,1,not na(dr)and math.abs(dr-xPH)<=nz(atr)*ly?2:1,dz)
if not na(xPL)
float dr=xLow
mh:=xLow
xLow:=xPL
eW:=bar_index-eZ
if fN
bE(xPL,-1,not na(dr)and math.abs(dr-xPL)<=nz(atr)*ly?2:1,eW)
if not na(iPH)
dN:=iPH
er:=bar_index-fi
if not na(iPL)
iLow:=iPL
gg:=bar_index-fi
if not na(mPH)
kG:=mPH
gJ:=bar_index-cm
if not na(mPL)
mLow:=mPL
ix:=bar_index-cm
float jn=ta.pivothigh(high,bw,bw)
float jo=ta.pivotlow(low,bw,bw)
if barstate.isconfirmed
if not na(jn)
array.push(L,ai.new(jn,1,bar_index))
if not na(jo)
array.push(L,ai.new(jo,-1,bar_index))
while array.size(L)>120
array.shift(L)
float pdh=request.security(syminfo.tickerid,"D",high[1],lookahead=barmerge.lookahead_on)
float pdl=request.security(syminfo.tickerid,"D",low[1],lookahead=barmerge.lookahead_on)
int iC=ta.valuewhen(high==pdh,bar_index,0)
int iD=ta.valuewhen(low==pdl,bar_index,0)
bool cK=timeframe.change("D")
if barstate.isconfirmed and mO and(cK or barstate.isfirst)
bE(pdh,1,3,iC)
bE(pdl,-1,3,iD)
float h15=request.security(syminfo.tickerid,"15",ta.pivothigh(high,kF,kF)[1],lookahead=barmerge.lookahead_on)
float l15=request.security(syminfo.tickerid,"15",ta.pivotlow(low,kF,kF)[1],lookahead=barmerge.lookahead_on)
int ik=ta.valuewhen(high==h15,bar_index,0)
int iu=ta.valuewhen(low==l15,bar_index,0)
bool nl=not na(h15)and(na(h15[1])or h15!=h15[1])
bool nn=not na(l15)and(na(l15[1])or l15!=l15[1])
if barstate.isconfirmed and mq and timeframe.in_seconds(timeframe.period)<=900
if nl
bE(h15,1,4,ik)
if nn
bE(l15,-1,4,iu)
bool dk=eB!="EQH/EQL Only"
bool jp=eB!="Swing Only"
float bk=dk?cU[1]:na
float bl=dk?xLow[1]:na
if jp and array.size(D)>0
for n=array.size(D)-1 to 0
cW lp=array.get(D,n)
if not lp.jK and lp.cO==2
if lp.side==1
bk:=na(bk)?lp.ae:math.max(bk,lp.ae)
else
bl:=na(bl)?lp.ae:math.min(bl,lp.ae)
bool iF=barstate.isconfirmed and not na(bk)and high>bk and close<bk
bool iG=barstate.isconfirmed and not na(bl)and low<bl and close>bl
bool fq=iF and(high-bk)>syminfo.mintick and(not eA or close<open)
bool fr=iG and(bl-low)>syminfo.mintick and(not eA or close>open)
bool aG=false
bool aH=false
if barstate.isconfirmed and iv
if eb=="Confirmed"
if hp
if ck
aG:=high<=ak and close<=ak
if cl
aH:=low>=aw and close>=aw
else
aG:=fq
aH:=fr
ck:=fq
cl:=fr
ak:=iF?high:na
aw:=iG?low:na
else
aG:=eb=="Strict"?fq:iF
aH:=eb=="Strict"?fr:iG
if aG
dQ:=high
if aH
eJ:=low
if barstate.isconfirmed and array.size(L)>0
for n=array.size(L)-1 to 0
ai ref=array.get(L,n)
if bar_index>ref.born
bool eO=ref.side==1?high>ref.ae and close<ref.ae:low<ref.ae and close>ref.ae
bool aR=ref.side==1?close>ref.ae:close<ref.ae
if eO
float eU=ref.side==1?high:ref.ae
float eC=ref.side==1?ref.ae:low
bi(ref.side,eU,eC,ref.ae,false)
array.remove(L,n)
else if aR
array.remove(L,n)
float aC=ta.highest(high,ej)[1]
float aW=ta.lowest(low,ej)[1]
if barstate.isconfirmed and not na(aC)and high>aC and close<aC
bi(1,high,aC,aC,true)
if barstate.isconfirmed and not na(aW)and low<aW and close>aW
bi(-1,aW,low,aW,true)
if barstate.isconfirmed and array.size(J)>0
for n=array.size(J)-1 to 0
ab sb=array.get(J,n)
if not sb.fF and bar_index>sb.born
bool nw=sb.side==1?close>sb.jD:close<sb.jD
if nw
if not na(sb.S)
box.delete(sb.S)
array.remove(J,n)
bool aS=false
bool aE=false
if barstate.isconfirmed and array.size(D)>0
for k=array.size(D)-1 to 0
cW p=array.get(D,k)
if not p.jK and bar_index>p.born
bool kg=p.side==1?high>p.ae:low<p.ae
bool bQ=p.side==1?close<p.ae:close>p.ae
if kg
array.remove(D,k)
p.jK:=true
p.jL:=bar_index
p.eO:=bQ
if bQ
if p.side==1
aS:=true
else
aE:=true
float gR=ta.highest(high,cR)[1]
float hQ=ta.lowest(low,cR)[1]
bool hs=dx and barstate.isconfirmed and high>gR and close<gR
bool gN=dx and barstate.isconfirmed and low<hQ and close>hQ
aS:=aS or hs
aE:=aE or gN
if barstate.isconfirmed
if aS
az:=bar_index
dQ:=high
if aE
aA:=bar_index
eJ:=low
bool kp=barstate.isconfirmed and not na(iLow[1])and low<iLow[1]and close>iLow[1]
bool ko=barstate.isconfirmed and not na(dN[1])and high>dN[1]and close<dN[1]
var int go=na
var int gm=na
if kp
go:=bar_index
if ko
gm:=bar_index
bool hY=barstate.isconfirmed and ek and not na(cU)and close>cU and(na(dA)or dA!=dz)
bool hX=barstate.isconfirmed and ek and not na(xLow)and close<xLow and(na(ei)or ei!=eW)
bool hE=barstate.isconfirmed and not na(dN)and close>dN and(na(dh)or dh!=er)
bool hD=barstate.isconfirmed and not na(iLow)and close<iLow and(na(dY)or dY!=gg)
bool oe=barstate.isconfirmed and not na(kG)and close>kG and(na(dl)or dl!=gJ)
bool od=barstate.isconfirmed and not na(mLow)and close<mLow and(na(ec)or ec!=ix)
if barstate.isconfirmed
if hY
dA:=dz
X:=1
W:=xLow
iX:=cU
kt:=xLow
if hX
ei:=eW
X:= -1
N:=cU
iX:=cU
kt:=xLow
if hE
dh:=er
dZ:=1
if hD
dY:=gg
dZ:= -1
if oe
dl:=gJ
gM:=1
if od
ec:=ix
gM:= -1
bool gd=barstate.isconfirmed and bar_index>=2 and low>high[2]and low-high[2]>=atr*hN
bool gc=barstate.isconfirmed and bar_index>=2 and high<low[2]and low[2]-high>=atr*hN
if ms and gd
int ad=30+(A?22:0)+(bI(aA,E)?18:0)+(X==1?15:0)
ci(2,1,low,high[2],bar_index-2,math.min(ad,100),false,false,bI(aA,E),A,X)
if ms and gc
int ad=30+(A?22:0)+(bI(az,E)?18:0)+(X== -1?15:0)
ci(2,-1,low[2],high,bar_index-2,math.min(ad,100),false,false,bI(az,E),A,X)
float cf=math.max(open[1],close[1])
float cM=math.min(open[1],close[1])
float dp=math.max(open,close)
float ed=math.min(open,close)
bool nq=barstate.isconfirmed and close>open and ed>cf and ed-cf>=atr*jx
bool np=barstate.isconfirmed and close<open and dp<cM and cM-dp>=atr*jx
if ni and nq
ci(3,1,ed,cf,bar_index-1,25+(A?20:0),false,false,false,A,X)
if ni and np
ci(3,-1,cM,dp,bar_index-1,25+(A?20:0),false,false,false,A,X)
bool fx=ca=="External"?hY:ca=="Internal"?hE:(hY or hE)
bool fw=ca=="External"?hX:ca=="Internal"?hD:(hX or hD)
if barstate.isconfirmed and ou and(fx or fw)and(not hw or A)
for pass=0 to 1
int side=pass==0?1: -1
bool bull=side==1
bool gap=bull?(gd or gd[1]or gd[2]):(gc or gc[1]or gc[2])
if(bull?fx:fw)and(not iH or gap)
int j=lE(side,gQ)
if not na(j)
bool dG=gap and(bull?low[j]<low[j+1]:high[j]>high[j+1])
float top=gP=="Full candle"?high[j]:math.max(open[j],close[j])
float bot=gP=="Full candle"?low[j]:math.min(open[j],close[j])
bool kv=bull?(not na(xLow)and bot<=xLow+atr*0.35):(not na(cU)and top>=cU-atr*0.35)
bool eO=bI(bull?aA:az,E)or bI(bull?go:gm,E)
int ad=math.min(100,28+(A?18:0)+(gap?20:0)+(eO?15:0)+(kv?12:0)+(dG?7:0))
ci(1,side,top,bot,bar_index-j,ad,dG,kv,eO,A,side)
float bq=0.0
if barstate.isconfirmed and array.size(eX)>0
for k=array.size(eX)-1 to 0
dT z=array.get(eX,k)
if bar_index>z.cy
float lI=z.top-z.bG
bool mH=high>=z.bG and low<=z.top
bool fy=z.G==1?close<z.bG:close>z.top
float jz=z.G==1?(z.top-low)/lI:(high-z.bG)/lI
bool kZ=z.kind==2 and z.bg!=3 and fy
if z.bg!=3
if fy
bq+= -z.G*(0.5+z.ad/100.0)
z.bg:=3
if z.kind==1
z.bF:=bar_index
else if mH
z.eT+=1
z.aV:=bar_index
z.iY:=math.max(z.iY,math.min(1,math.max(0,jz)))
z.bg:=z.iY>=0.999?2:1
bool bX=false
if z.kind==1 and fO and z.bg==3 and bar_index-z.bF<=E
bool hu=z.G== -1?(hY or(el=="Both"and hE)):(hX or(el=="Both"and hD))
bool eO=bI(z.G== -1?aA:az,E)
bX:=hu and(not hx or eO or z.iM)
if kZ or bX
if not na(z.S)
box.delete(z.S)
z.S:=na
z.kind:=bX?4:5
z.G:= -z.G
z.bg:=0
z.eT:=0
z.iY:=0
z.cy:=bar_index
z.hn:=na
z.fW:=na
z.gD:=0
if bX
z.bF:=na
z.ad:=math.min(100,z.ad+10)
bool iB=z.kind==1 and fO and((z.bg==3 and not na(z.bF)and bar_index-z.bF<=E)or(z.bg==2 and not na(z.aV)and bar_index-z.aV<=E))
bool ht=z.kind==2 and z.bg==2 and not na(z.aV)and bar_index-z.aV<=E
bool jM=z.bg>=2
if jM and not na(z.S)
box.delete(z.S)
z.S:=na
if gs and jM and not iB and not ht
if not na(z.S)
box.delete(z.S)
array.remove(eX,k)
// Previous confirmed HTF values; daily open is captured at the actual day boundary.
float aX=pdh
float bO=pdl
float cb=request.security(syminfo.tickerid,"D",close[1],lookahead=barmerge.lookahead_on)
float prevH2=request.security(syminfo.tickerid,"D",high[2],lookahead=barmerge.lookahead_on)
float prevL2=request.security(syminfo.tickerid,"D",low[2],lookahead=barmerge.lookahead_on)
float prevC2=request.security(syminfo.tickerid,"D",close[2],lookahead=barmerge.lookahead_on)
float nI=(aX+bO+cb)/3.0
float aI=math.max((aX+bO)*0.5,2*nI-(aX+bO)*0.5)
float aZ=math.min((aX+bO)*0.5,2*nI-(aX+bO)*0.5)
float lf=(prevH2+prevL2+prevC2)/3.0
float R=math.max((prevH2+prevL2)*0.5,2*lf-(prevH2+prevL2)*0.5)
float Y=math.min((prevH2+prevL2)*0.5,2*lf-(prevH2+prevL2)*0.5)
float eL=request.security(syminfo.tickerid,"D",ta.sma(math.abs(2*(high+low+close)/3.0-(high+low)),5)[2],lookahead=barmerge.lookahead_on)
float ow=request.security(syminfo.tickerid,"W",high[1],lookahead=barmerge.lookahead_on)
float wLow=request.security(syminfo.tickerid,"W",low[1],lookahead=barmerge.lookahead_on)
float nL=request.security(syminfo.tickerid,"W",close[1],lookahead=barmerge.lookahead_on)
float jr=request.security(syminfo.tickerid,"M",high[1],lookahead=barmerge.lookahead_on)
float ke=request.security(syminfo.tickerid,"M",low[1],lookahead=barmerge.lookahead_on)
float iw=request.security(syminfo.tickerid,"M",close[1],lookahead=barmerge.lookahead_on)
var float dJ=na
if (cK and not barstate.isfirst) or time==time("D")
dJ:=open
bool gr=cK
var int ew=na
var float fh=na
var float hH=na
var float cA=na
var float dV=na
bool I=timeframe.change("D")
if timeframe.isintraday or timeframe.isdaily
if barstate.isfirst or I
cA:=high
dV:=low
else
cA:=math.max(nz(cA,high),high)
dV:=math.min(nz(dV,low),low)
if barstate.isconfirmed and timeframe.isintraday
if I
ew:=time
fh:=high
hH:=low
else if not na(ew)and time-ew<hI*60000
fh:=math.max(nz(fh,high),high)
hH:=math.min(nz(hH,low),low)
bool Z=timeframe.isintraday and not na(aI)and not na(R)
float ha=aI-aZ
float mu=R-Y
bool jI=Z and eL>0 and ha<=eL*0.75
bool lg=Z and eL>0 and ha>=eL*1.25
int hz=aZ>R?2:aI<Y?-2:aZ>Y and aI>R?1:aZ<Y and aI<R?-1:aZ==Y and aI==R?0:aZ>=Y and aI<=R?3:aZ<=Y and aI>=R?-3:0
int bu=na(dJ)?0:dJ>aI?1:dJ<aZ?-1:0
int O=close>aI?1:close<aZ?-1:0
var bool eM=false
var int bo=0
var int bS=0
var int aq=0
var int cP=0
var int ce=0
var bool bd=false
bool cs=Z and(aX<Y or bO>R)
bool ct=cs and high>=Y and low<=R
if I
eM:=true
if Z and barstate.isconfirmed
if I or barstate.isfirst
bo:=0
bS:=0
cP:=0
aq:=bu
ce:=0
bd:=false
bo:=O==1?bo+1:0
bS:=O== -1?bS+1:0
if O!=0
if aq!=0 and aq!=O
cP+=1
aq:=O
if bo>=2 or bS>=2
ce:=bo>=2?1: -1
bd:=bd or ct
float eN=syminfo.mintick*0.5
bool jH=Z and not(I and not barstate.isconfirmed)and O==1 and bo>=2
bool hy=Z and not(I and not barstate.isconfirmed)and O== -1 and bS>=2
bool ng=Z and high>=aI-eN and low<=aI+eN
bool nf=Z and high>=aZ-eN and low<=aZ+eN
int mt=I?bu:nz(O[1],bu)
bool ba=cs and eM and(I or not bd)and not ct
bool iL=Z and not na(ew)and time-ew>=hI*60000 and not I
int ne=iL?(cA>fh and dV<hH?2:cA>fh?1:dV<hH?-1:0):0
kS(int k,int side,float p)=>
side==1?(k==1?2*p-bO:k==2?p+(aX-bO):k==3?aX+2*(p-bO):(k-1)*p+aX-(k-1)*bO):(k==1?2*p-aX:k==2?p-(aX-bO):k==3?bO-2*(aX-p):(k-1)*p-((k-1)*aX-bO))
jc(int k)=>
float mult=k==1?1.1/12.0:k==2?1.1/6.0:k==3?1.1/4.0:1.1/2.0
float mi=aX-bO
float h=k==5?(bO>0?aX/bO*cb:na):cb+mi*mult
float l=k==5?cb-(h-cb):cb-mi*mult
[h,l]
jR()=>
src=hl2
tr=math.max(high-low,math.max(math.abs(high-close[1]),math.abs(low-close[1])))
trendAtr=ta.rma(tr,nD)
float up_t=src-(gO*trendAtr)
float dn_t=src+(gO*trendAtr)
var float gi=0.0
var float ez=0.0
gi:=nz(up_t[1])
ez:=nz(dn_t[1])
up_t:=up_t>gi or gi>src[1]?up_t:gi
dn_t:=dn_t<ez or ez<src[1]?dn_t:ez
var int dir=0
var float trnd=na
dir:=nz(trnd[1])>=ez?(close>dn_t?1: -1):(close<up_t?-1:1)
trnd:=dir==1?up_t:dn_t
[trnd,dir]
[t0,d0]=jR()
fG=timeframe.change("D")
if na(jF[1])
fG:=true
float dK=na
if not(kW and timeframe.isdwm)
[_vwap,_,_]=ta.vwap(jF,fG,1)
dK:=_vwap
type bh
float vol
int bar
int oI
float o
float c
float hi
float lo
float oA
float nM
var array<bh>ag=array.new<bh>()
hm()=>
bool nk=true
bool fv=dS=="Exchange day"
string nX=dS=="New York cash"?"0930-1600":eV
bool jY=fv or not na(time(timeframe.period,nX,hj))
bool ok=fv?timeframe.change("D"):jY and not jY[1]
if nk and barstate.isconfirmed
if ok
array.clear(ag)
if jY and not na(volume)and volume>0
int at=array.size(ag)
if at>0
for i=0 to at-1
bh old=array.get(ag,i)
if volume>old.vol
at:=i
break
if at<fQ
bh lQ=bh.new(volume,bar_index,time,open,close,high,low,fP=="High-Low"?high:math.max(open,close),fP=="High-Low"?low:math.min(open,close))
array.insert(ag,at,lQ)
if array.size(ag)>fQ
array.pop(ag)
hm()
const int md=18
const int eF=8
const int bs=18
type cw
float dO
string name
int C
g(array<cw>a,float dO,string name,int C)=>
if not na(dO)
array.push(a,cw.new(dO,name,C))
ie(array<cw>a,float h,float l,float c,string name,int C)=>
float p=(h+l+c)/3.0
float b=(h+l)/2.0
float t=2*p-b
g(a,math.max(b,t),name+" TC",C)
g(a,p,name+" P",C)
g(a,math.min(b,t),name+" BC",C)
fU()=>
var array<cw>au=array.new<cw>()
if barstate.isfirst or cK or gr or ba!=ba[1]
array.clear(au)
float p=(aX+bO+cb)/3.0
if timeframe.in_seconds()<=86400
ie(au,aX,bO,cb,"Daily CPR",1)
if ba
g(au,R,"Virgin CPR TC",15)
g(au,lf,"Virgin CPR P",15)
g(au,Y,"Virgin CPR BC",15)
for j=0 to 1
int side=j==0?1: -1
for k=1 to 5
g(au,kS(k,side,p),(side==1?"R":"S")+str.tostring(k),3)
for k=1 to 5
[h,l]=jc(k)
g(au,h,"H"+str.tostring(k),4)
g(au,l,"L"+str.tostring(k),4)
g(au,aX,"PDH",5)
g(au,bO,"PDL",5)
g(au,cb,"PDC",5)
g(au,dJ,"Daily open",5)
array<cw>a=array.copy(au)
if timeframe.in_seconds()<=86400
if not cK
g(a,cA[1],"Daily high",5)
g(a,dV[1],"Daily low",5)
g(a,fh[1],"ORH",5)
g(a,hH[1],"ORL",5)
if timeframe.in_seconds()<=604800
ie(a,ow,wLow,nL,"Weekly CPR",6)
if timeframe.in_seconds()<=timeframe.in_seconds("M")
ie(a,jr,ke,iw,"Monthly CPR",7)
if not cK
g(a,dK[1],"VWAP",8)
g(a,t0[1],"Trendbase",9)
g(a,cU[1],"External high",10)
g(a,xLow[1],"External low",10)
g(a,dN[1],"Internal high",10)
g(a,iLow[1],"Internal low",10)
if not cK and array.size(ag)>0
for j=0 to array.size(ag)-1
bh v=array.get(ag,j)
if v.bar<bar_index
g(a,v.hi,"V"+str.tostring(j+1)+" H",11)
g(a,v.lo,"V"+str.tostring(j+1)+" L",11)
a
float og=math.max(nz(atr,syminfo.mintick),syminfo.mintick)
float iA=math.max(high-low,syminfo.mintick)
float mb=math.abs(close-open)
float ju=math.min(open,close)-low
float jw=high-math.max(open,close)
float jv=math.sum(math.abs(ta.change(close)),eF)
float V=jv>0?(close-close[eF])/jv:0.0
float oz=(close-close[eF])/eF
float js=(close-close[3])/3
float eE=ta.sma(volume,20)[1]
float bZ=not na(volume)and eE>0?volume/eE:na
bool hr=not na(bZ)and bZ>1.35 and mb/iA<0.35
bool mc=high<=high[1]and low>=low[1]
bool nR=high>high[1]and low<low[1]
var float an=na
var float aN=na
var int bY=na
bool aB=not na(bY)and bar_index-bY<=6
bool cV=aB and low<aN and close>aN
bool cJ=aB and high>an and close<an
bool kf=(low<low[1]and close>low[1])or cV
bool jt=(high>high[1]and close<high[1])or cJ
if barstate.isconfirmed
if mc
an:=not aB?high:math.max(an,high)
aN:=not aB?low:math.min(aN,low)
bY:=bar_index
else if not aB or close>an or close<aN
an:=na
aN:=na
bY:=na
float mD=math.sum(ju/iA>=0.33 or kf?1.0:0.0,5)
float mE=math.sum(jw/iA>=0.33 or jt?1.0:0.0,5)
float al=ta.highest(high,bs)[1]
float am=ta.lowest(low,bs)[1]
float ao=al-am
float gL=math.sum(math.abs(ta.change(close)),bs)
float hM=gL>0?math.abs(close-close[bs])/gL:0
bool iz=hM<0.30 and ao>=2*og and close<=al and close>=am and math.abs(V)<0.48
bool eD=bar_index>60 and not na(t0)and not na(V)and(timeframe.isintraday or timeframe.isdaily)
const int aQ=7
const int jO=10
float tick=syminfo.mintick
float norm=math.max(nz(atr,tick),tick)
B(float x,float lo,float hi)=>math.max(lo,math.min(hi,x))
gA(float x)=>x>0?1:x<0?-1:0
lC(int n)=>n==9?0:n==8?1:n==10?2:n==11?5:n==16?4:n==17 or n==5?6:3
f_px(float p)=>str.tostring(p,format.mintick)
fc(float p)=>math.round_to_mintick(p)
type dB
float hi
float lo
float mid
int born
bool on
int op=0
int bQ=0
int aR=0
var array<dB>M=array.new<dB>()
float dP=ta.sma(high-low,20)[1]
bool eg=not na(bZ)and bZ>=1.6
bool lm=eg and high-low<=nz(dP,norm)*0.75
bool mQ=eg and high-low>=nz(dP,norm)*1.35 and mb/iA>=0.60
bool hh=lm or hr
if barstate.isconfirmed
if cK
array.clear(M)
if array.size(M)>0
for j=0 to array.size(M)-1
dB v=array.get(M,j)
bool eT=high>=v.lo and low<=v.hi and(high[1]<v.lo or low[1]>v.hi)
if eT
v.op+=1
if(close[1]>v.hi and close>v.hi)or(close[1]<v.lo and close<v.lo)
v.bQ+=1
else if close<v.lo or close>v.hi
v.aR+=1
if eg
array.push(M,dB.new(high,low,hl2,bar_index,hh))
if array.size(M)>24
array.shift(M)
type aJ
float lo
float hi
string name
int C
int Q
float dy
gx(array<aJ>book,float lo,float hi,string name,int fam,int Q,float dy)=>
if not na(lo)and not na(hi)and hi>=lo
array.push(book,aJ.new(lo,hi,name,fam,Q,dy))
aF(array<aJ>book,float p,string name,int fam,int Q,float dy)=>
gx(book,p,p,name,fam,Q,dy)
array<cw>gl=fU()
array<aJ>book=array.new<aJ>()
if array.size(gl)>0
for lv in gl
aF(book,lv.dO,lv.name,lC(lv.C),0,lv.C==5 or lv.C==10 or lv.C==11?1.0:0.8)
if X[1]==1
aF(book,W[1],"Protected low",2,1,1.35)
if X[1]== -1
aF(book,N[1],"Protected high",2,-1,1.35)
aF(book,al,"Range high",6,-1,1.0)
aF(book,am,"Range low",6,1,1.0)
if array.size(D)>0
for lp in D
if lp.born<bar_index
aF(book,lp.ae,lp.side==1?"BSL":"SSL",6,-lp.side,lp.fI>1?1.3:1.0)
if array.size(jA)>0
for lp in jA
if lp.eO and lp.jL==bar_index
aF(book,lp.ae,lp.side==1?"BSL sweep":"SSL sweep",6,-lp.side,1.35)
if array.size(eX)>0
for z in eX
if z.cy<bar_index and z.bg<2 and bar_index-z.cy<=240
float q=B(0.75+z.ad/150.0-0.08*math.min(z.eT,5)-0.35*z.iY,0.4,1.45)
gx(book,z.bG,z.top,fD(z),4,z.G,q)
if z.bg==3 and z.bF==bar_index
aF(book,z.G==1?z.bG:z.top,"Failed "+fD(z),4,-z.G,1.1)
if array.size(J)>0
for sb in J
if sb.born<bar_index and not sb.fF and bar_index-sb.born<=md
gx(book,sb.bG,sb.top,sb.side==1?"BSL rejection":"SSL rejection",6,-sb.side,0.9)
if array.size(ag)>0
for j=0 to array.size(ag)-1
bh v=array.get(ag,j)
if v.bar<bar_index
aF(book,(v.hi+v.lo)*0.5,"V"+str.tostring(j+1)+" MID",5,0,0.75)
if array.size(M)>0
for v in M
if v.born<bar_index and v.on
float q=B(0.9+0.12*v.bQ-0.15*v.aR,0.45,1.35)
aF(book,v.hi,"Absorption? H",5,0,q)
aF(book,v.lo,"Absorption? L",5,0,q)
aF(book,v.mid,"Absorption? MID",5,0,q*0.75)
int cu=int(nz(timeframe.in_seconds(),60))
int mo=int(math.max(1,nz(time_close-time,cu*1000)/1000))
bool fM=not na(time_close[1])and time-time_close[1]>cu*1000
int iN=hour(time,"America/New_York")*60+minute(time,"America/New_York")
int cv=iN>=1080 or iN<180?0:iN<570?1:iN<960?2:3
mV(int b)=>b==0?"Overnight":b==1?"Europe / pre-open":b==2?"US cash":"US late"
var int u=0
if barstate.isconfirmed and timeframe.isintraday
u+=mo
type iQ
int side=0
int born=na
float aa=na
float ih=na
float dq=na
string cO=""
float bD=0.0
float loss=0.0
int wins=0
int nV=0
int mx=0
float hR=0.0
float hK=na
int gI=na
int dC=na
float eh=na
float de=na
int li=0
int km=0
var array<iQ>ki=array.new<iQ>()
if barstate.isfirst
for j=0 to aQ-1
array.push(ki,iQ.new())
array<float>dW=array.new_float(aQ,0.0)
array<int>hC=array.new_int(aQ,0)
array<float>fA=array.new_float(aQ,na)
array<float>eo=array.new_float(aQ,na)
array<string>hB=array.new_string(aQ,"")
if barstate.isconfirmed and eD and array.size(book)>0
for a in book
bool kO=high>=a.lo and low<=a.hi
int cX=close[1]>a.hi?1:close[1]<a.lo?-1:0
int jT=close>a.hi?1:close<a.lo?-1:0
bool bQ=kO and cX!=0 and jT==cX
bool aR=kO and cX!=0 and jT== -cX
bool mf=high[1]<a.lo or low[1]>a.hi or aR
int G=bQ?cX:aR?jT:0
bool fits=a.Q==0 or a.Q==G or aR
if G!=0 and fits and mf
float loc=G==1?a.hi:a.lo
float oj=math.abs(close-loc)/norm
float jG=a.dy*(aR?1.05:1.0)*B(oj,0.15,1.3)
if jG>array.get(dW,a.C)
array.set(dW,a.C,jG)
array.set(hC,a.C,G)
array.set(fA,a.C,loc)
array.set(eo,a.C,G==1?math.min(low,a.lo):math.max(high,a.hi))
array.set(hB,a.C,a.name)
float jJ=0.0
float gX=0.0
if barstate.isconfirmed and eD
for j=0 to aQ-1
iQ p=array.get(ki,j)
p.hR*=0.86
if p.side!=0 and bar_index>p.born
if fM
p.dC:=na
bool fail=p.side==1?low<=p.ih:high>=p.ih
bool win=p.side==1?high>=p.aa:low<=p.aa
bool mM=bar_index-p.born>=jO
if fail or win or mM
float bC=na(p.dC)?na:float(u-p.dC)/60
if fail
if not na(bC)
p.km+=1
p.de:=na(p.de)?bC:0.90*p.de+0.10*bC
p.nV+=1
p.loss:=0.94*p.loss+1
p.bD*=0.94
float kn=(p.bD+2)/(p.bD+p.loss+4)
p.hR:= -p.side*(0.65+kn)
jJ+=math.abs(p.hR)
else if win
if not na(bC)
p.li+=1
p.eh:=na(p.eh)?bC:0.90*p.eh+0.10*bC
p.wins+=1
p.bD:=0.94*p.bD+1
p.loss*=0.94
p.hR:=p.side*0.65
else
p.mx+=1
p.hR*=0.5
p.gI:=bar_index
p.hK:=p.dq
p.side:=0
float val=array.get(dW,j)
int sign=array.get(hC,j)
float dq=array.get(fA,j)
bool nW=na(p.gI)or bar_index-p.gI>=3 or math.abs(dq-p.hK)>norm*0.5
if p.side==0 and sign!=0 and nW and val>=0.18
p.side:=sign
p.born:=bar_index
p.dC:=u
p.dq:=dq
p.cO:=array.get(hB,j)
p.aa:=fc(close+sign*math.max(norm*0.8,tick*2))
p.ih:=fc(array.get(eo,j)-sign*tick)
float os=2.0*(p.bD+3)/(p.bD+p.loss+6)
gX+=B(p.hR+sign*val*os*0.55,-1.4,1.4)
var float bT=0.0
var float F=0.0
if barstate.isconfirmed
bT:=0.84*bT+jJ
F:=0.65*F+0.35*gX
cB(int C)=>
iQ p=array.get(ki,C)
2.0*(p.bD+3)/(p.bD+p.loss+6)
float kr=not Z?0.0:0.32*gA(close-nI)+0.18*bu+0.18*hz/2.0+0.17*(jH?1:hy?-1:0)+0.15*gA(nz(close-dJ,0))
float cd=(X[1]==1 and not na(W[1])and close<W[1]and close[1]>=W[1]?-1.0:0.0)+(X[1]== -1 and not na(N[1])and close>N[1]and close[1]<=N[1]?1.0:0.0)
float du=B(bq,-1.4,1.4)+cd
float cQ=B(0.45*d0*cB(0)+0.35*gA(nz(close-dK,0))*cB(1)+0.55*X*cB(2)+0.22*dZ+0.08*gM+0.4*kr*cB(3),-1.5,1.5)
float hf=math.sum(math.max(nz(ta.change(close)),0),8)
float gu=math.sum(math.max(-nz(ta.change(close)),0),8)
float he=math.sum(close>close[1]?nz(volume):0,8)
float gt=math.sum(close<close[1]?nz(volume):0,8)
float db=he>0 and gt>0?B((hf/he-gu/gt)/math.max(hf/he+gu/gt,1e-10),-1,1):0.0
float aD=ao>tick?B((close-am)/ao,0,1):0.5
bool mp=hh and(aD>0.7 or high>=nz(cA[1],high))
bool iS=hh and(aD<0.3 or low<=nz(dV[1],low))
float bp=(aE or kp or iG?1.0:0.0)-(aS or ko or iF?1.0:0.0)
float dU=(hY or hE?1.0:0.0)-(hX or hD?1.0:0.0)
float hg=(iS?0.65:0.0)-(mp?0.65:0.0)+(mQ?0.45*gA(close-open):0.0)
var float dc=0.0
if barstate.isconfirmed
dc:=0.77*dc+0.23*(bp+dU+hg+du)
float iZ=cQ+0.75*nz(V)+0.35*db+0.55*B(F,-2,2)+0.65*dc
float ap=B((0.55-nz(hM,0.55))*2.4+0.10*math.min(cP,3)+(lg?0.08:0.0),0,1)*(ao>norm*1.5?1:0)
fa(int n)=>
float dt=float(time_close-time_close[n])/60000.0
float hO=float(time_close[n]-time_close[2*n])/60000.0
float eH=float(time_close[2*n]-time_close[3*n])/60000.0
int gZ=nz(ta.barssince(fM),100000)
bool cT=gZ>=n and not na(dt)and dt>0
bool fn=gZ>=2*n and not na(hO)and hO>0
bool hc=gZ>=3*n and not na(eH)and eH>0
float A=cT?close-close[n]:na
float path=math.sum(math.abs(ta.change(close)),n)
float ch=cT?path:na
float v=cT?A/dt:na
float prev=fn?(close[n]-close[2*n])/hO:na
float oi=hc?(close[2*n]-close[3*n])/eH:na
float a=cT and fn?(v-prev)/((dt+hO)*0.5):na
float nc=fn and hc?(prev-oi)/((hO+eH)*0.5):na
float j=not na(a)and not na(nc)?(a-nc)/((dt+2*hO+eH)*0.25):na
float eff=cT and ch>0?B(A/ch,-1,1):cT?0.0:na
[A,ch,v,a,j,eff,dt]
[oF,oG,kV,kA,kJ,kEff,kDt]=fa(ir)
[ob,lU,lV,oa,oH,ip,nC]=fa(aM)
[nP,nQ,lW,oD,oE,oc,oy]=fa(kE)
float ey=math.max(tick,nz(atr[1],norm))/math.max(float(cu)/60.0,0.01)
float kVN=nz(kV)/ey
float jZ=nz(lV)/ey
float mC=nz(lW)/ey
float kAN=nz(kA)*math.max(nz(kDt,1.0),0.01)/ey
float oC=nz(oa)*math.max(nz(nC,1.0),0.01)/ey
float jk=ta.sma(math.abs(kV),60)[1]
float kb=ta.sma(high-low,ir)
float fl=ta.sma(high-low,40)[1]
float cG=not na(fl)and fl>tick?B(1-kb/fl,0,1):0.0
bool kOn=kc and timeframe.isintraday and eD and ir<aM and aM<kE and not na(lV)and not na(kA)
bool nB=kOn and not na(jk)and math.abs(kV)>=math.max(jk*1.5,ey*0.45)
id(int s)=>s==1?"Up accelerating":s==2?"Up decelerating":s==3?"Down accelerating":s==4?"Down decelerating":s==5?"Rotation":s==6?"Compression":s==7?"Up steady":s==8?"Down steady":"Unclear"
ge(int s)=>s==1 or s==2 or s==7?1:s==3 or s==4 or s==8?-1:0
bool kRot=kOn and math.abs(ip)<0.32 and math.abs(jZ)<0.22 and lU>norm*1.3
bool fj=kOn and cG>=0.28 and math.abs(jZ)<0.22 and math.abs(kVN)<0.30
int kRaw=not kOn?0:fj?6:kRot?5:kVN>0.12 and kEff>0.32?(kAN>0.12?1:kAN< -0.12?2:7):kVN< -0.12 and kEff< -0.32?(kAN< -0.12?3:kAN>0.12?4:8):0
var int cI=0
var int iq=0
var int cF=0
var int U=0
var int bK=na
if barstate.isconfirmed
if not kOn or fM
cI:=0
iq:=0
cF:=0
U:=0
bK:=na
else if kRaw==cI
cF:=kRaw
U:=0
else
U:=cF==kRaw?U+1:1
cF:=kRaw
if U>=2
iq:=cI
cI:=kRaw
bK:=u
U:=0
int kAge=na(bK)?0:math.max(0,u-bK)
var int aj=0
var float aU=na
var int aK=na
var float bj=na
var int io=na
var float ay=na
var float gE=na
var float dj=na
var float aL=na
var float ac=na
var float bL=na
var float aT=na
var int fk=0
var int cH=na
if barstate.isconfirmed
if not kOn or fM
aj:=0
aU:=na
aK:=na
bj:=na
io:=na
ay:=na
gE:=na
dj:=na
aL:=na
ac:=na
bL:=na
aT:=na
fk:=0
cH:=na
else if na(bj)
bj:=close
io:=u
aU:=close
aK:=u
ay:=math.max(tick*2,norm*0.55)
else if aj==0
if math.abs(close-bj)>=ay
aj:=close>bj?1: -1
aU:=close
aK:=u
else
if aj*(close-aU)>0
aU:=close
aK:=u
else if aj*(aU-close)>=ay
float amp=math.abs(aU-bj)
float mins=float(aK-io)/60.0
if mins>0 and amp>0
if aj==1
bL:=gE
gE:=amp
aL:=mins
else
aT:=dj
dj:=amp
ac:=mins
fk+=1
cH:=u
bj:=aU
io:=aK
aj:= -aj
aU:=close
aK:=u
ay:=math.max(tick*2,norm*0.55)
float ox=not na(aL)and not na(ac)?aL+ac:na
float gF=not na(aL)and aL>0?gE/aL:na
float ea=not na(ac)and ac>0?dj/ac:na
float lR=not na(gF)and not na(ea)?B((gF-ea)/math.max(gF+ea,1e-10),-1,1):0.0
float lS=not na(bL)and not na(aT)and bL+aT>0?B(1-(gE+dj)/(bL+aT),-1,1):0.0
bool ex=kOn and fk>=4 and not na(cH)and u-cH<=math.max(gG*120,cu*aM*2)
type Obs
int bg
int nd
int iJ
int bt
int gj
var array<Obs>cn=array.new<Obs>()
var array<Obs>bP=array.new<Obs>()
var int bR=na
var int fJ=0
var int fK=0
var int dR=0
if barstate.isconfirmed and kOn
if fM or cK
bR:=na
if not na(bR)and u-bR>=gG*60
array.push(cn,Obs.new(fJ,fK,dR,cI,u-bR))
if array.size(cn)>it
array.shift(cn)
bR:=na
if na(bR)
bR:=u
fJ:=cI
fK:=iq
dR:=cv
ig()=>
int n=0
int up=0
int dn=0
int bal=0
for o in cn
if o.bg==cI and o.nd==iq and o.iJ==cv
n+=1
up+=ge(o.bt)==1?1:0
dn+=ge(o.bt)== -1?1:0
bal+=o.bt==5 or o.bt==6?1:0
if n<gH
n:=0
up:=0
dn:=0
bal:=0
for o in cn
if o.bg==cI
n+=1
up+=ge(o.bt)==1?1:0
dn+=ge(o.bt)== -1?1:0
bal+=o.bt==5 or o.bt==6?1:0
[n,n>=gH?float(up)/n:na,n>=gH?float(dn)/n:na,n>=gH?float(bal)/n:na]
[kN,kD,hJ,ka]=ig()
bool lT=kN>=gH
float lX=kOn?B(0.45*B(kVN/0.45,-1,1)+0.35*nz(ip)+0.20*B(mC/0.35,-1,1),-1,1):0.0
float nA=kOn?B(kAN/0.40,-1,1):0.0
float di=kOn?B((1-math.abs(nz(ip)))*0.50+(kRot?0.30:0)+(fj?0.20:0),0,1):0.0
float la=lT?B(kD-hJ,-1,1):0.0
kw(int side)=>kOn and side*kVN>0.10 and side*kAN< -0.10?B(-side*kAN/0.40,0,1):0.0
nt(int side)=>kOn and side*kVN< -0.12 and side*lX< -0.25 and(side*kAN< -0.10 or side*jZ< -0.12)
jf(int side)=>ex and(kRot or fj)?math.max(0,side*lR)*math.max(0.25,lS):0.0
oB(float x)=>na(x)?"learning":str.tostring(x,"#.###")
bool tmOn=fZ and eD and timeframe.isintraday
int hd=iz or di>0.7 and ap>0.5?2:V>0.36 and(X==1 or dZ==1)?1:V< -0.36 and(X== -1 or dZ== -1)?-1:0
var int ar=0
var int fL=0
var int eR=0
var int fs=na
var int eQ=0
var bool af=false
var int bU=na
var float iO=na
var float dI=na
if barstate.isconfirmed and tmOn
if na(fs)or fM or cK
ar:=hd
fs:=u
eQ:=cv
af:=false
eR:=0
bU:=u
iO:=close
else
eR:=hd==ar?0:hd==fL?eR+1:1
fL:=hd
if eR>=fY+(hd==0?2:0)
if af and ar!=0
array.push(bP,Obs.new(ar,0,eQ,hd,u-fs))
if array.size(bP)>240
array.shift(bP)
ar:=hd
fs:=u
eQ:=cv
af:=true
eR:=0
bU:=u
iO:=close
else if math.abs(ar)==1 and ar*(close-iO)>norm*0.35
float gap=float(u-bU)/60
dI:=na(dI)?gap:0.85*dI+0.15*gap
iO:=close
bU:=u
int kM=u-nz(fs,u)
int nJ=u-nz(bU,u)
kU()=>
array<float>fo=array.new_float()
int ends=0
for o in bP
if o.bg==ar and o.iJ==eQ and o.gj>kM
float r=float(o.gj-kM)/60
array.push(fo,r)
ends+=r<=eP?1:0
if array.size(fo)<dv
array.clear(fo)
ends:=0
for o in bP
if o.bg==ar and o.gj>kM
float r=float(o.gj-kM)/60
array.push(fo,r)
ends+=r<=eP?1:0
int n=array.size(fo)
[n,n>=dv and af?array.median(fo):na,n>=dv and af?float(ends)/n:na]
[mN,jN,lj]=kU()
float eS=tmOn and math.abs(ar)==1?B((float(nJ)/60/math.max(nz(dI,5),1)-1.25)/3,0,1)*(0.7+0.3*nz(lj)):0
float ga=tmOn and ar==2?lR:0
bool mY=fM or(not na(time_close[1])and time>time_close[1])
float cD=mY?na:close-close[1]
float ah=float(mo)/60.0
float gB=ah>0?cD/ah:na
float lJ=not na(gB[1])?(gB-gB[1])/math.max((ah+ah[1])*0.5,0.01):na
float kC=ta.sma(math.abs(gB),60)[1]
float kz=ta.sma(math.abs(lJ),60)[1]
float ny=math.max(tick,nz(atr[1],norm))
int jW=nz(ta.barssince(mY),100000)
bool iaOK=kX and kOn and cu<=180 and not na(kC)and not na(kz)and jW>=2*aM
float lP=math.sum(math.max(nz(cD),0),aM)
float lM=math.sum(math.max(-nz(cD),0),aM)
float eu=math.sum(cD>0?ah:0.0,aM)
float et=math.sum(cD<0?ah:0.0,aM)
float hG=eu>0?lP/eu:na
float hF=et>0?lM/et:na
float ev=math.sum(ah,aM)
float ff=math.max((ev+ev[aM])*0.5,0.01)
float nb=not na(hG)and not na(hG[aM])?(hG-hG[aM])/ff:na
float mW=not na(hF)and not na(hF[aM])?(hF-hF[aM])/ff:na
je(int side)=>
float ownV=side==1?hG:hF
float oppV=side==1?hF:hG
float ownA=side==1?nb:mW
float oppA=side==1?mW:nb
float ownT=side==1?eu:et
float oppT=side==1?et:eu
bool cT=iaOK and not na(ownV)and not na(oppV)and ownT>=ah*2 and oppT>=ah*2
float om=cT?B((oppV-ownV)/math.max(oppV+ownV,1e-10),-1,1):0.0
float nx=cT and not na(oppA)and not na(ownA)?B((oppA-ownA)*ff/math.max(oppV+ownV,ey*0.2),-1,1):0.0
float ol=cT?B((oppT-ownT)/math.max(oppT+ownT,0.01),-1,1):0.0
float oo=cT and ex?-side*lR:0.0
cT?B(0.40*om+0.30*nx+0.20*ol+0.10*oo,-1,1):0.0
float lL=je(1)
float lK=je(-1)
fB(int side)=>side==1?lL:lK
int fg=gA(cD)
bool mz=iaOK and fg!=0 and math.abs(cD)>=ny*jj and(math.abs(gB)>=math.max(kC*1.8,ey*0.8)or fg*lJ>=math.max(kz*2.0,ey/math.max(ah,0.01)*0.4))
var int av=0
var int il=na
var float gC=na
var float mX=na
var float jV=na
var float H=na
var int lN=0
var float iaR1=na
var float iaR3=na
var float iaR5=na
var float kB=na
var int bJ=0
var array<Obs>bW=array.new<Obs>()
if barstate.isconfirmed
if fM or cK
av:=0
if av!=0
float age=float(u-il)/60
H:=(close-gC)/(mX-gC)
jV:=math.max(jV,H)
if age>=1 and na(iaR1)
iaR1:=H
if age>=3 and na(iaR3)
iaR3:=H
if age>=5 and na(iaR5)
iaR5:=H
if age>=10 and na(kB)
kB:=H
if age>=kY
int iI=H>=1.2?0:H<=0?1:di>=0.55?2:3
array.push(bW,Obs.new(av,lN,bJ,iI,u-il))
if array.size(bW)>nZ
array.shift(bW)
av:=0
if av==0 and mz
av:=fg
il:=u
gC:=close[1]
mX:=close
H:=1
jV:=1
iaR1:=na
iaR3:=na
iaR5:=na
kB:=na
lN:=av*bp<0?2:av==1 and aD>0.8 or av== -1 and aD<0.2?1:cG>0.25?3:0
bJ:=cv
bool cE=av!=0
float nY=cE?float(u-il)/60:0
float lO=cE?fB(av):0
float im=cE?B((0.85-H)/0.85,0,1):0
int mA=cE and im>0.2 and lO>0.05?2:cE?1:0
kT()=>
int n=0
int cont=0
int rev=0
for o in bW
if o.bg==av and o.nd==lN and o.iJ==bJ
n+=1
cont+=o.bt==0?1:0
rev+=o.bt==1?1:0
[n,n>=mZ?float(cont)/n:na,n>=mZ?float(rev)/n:na]
[iaN,iaPC,iaPR]=kT()
jd(int side)=>cE and av==side and mA>=2 and(side*kVN<0 or lO>0.25)
ic(int side)=>
float comp= -fB(side)
float same=cE and av==side?1.0:0.0
float opp=cE and av== -side?1.0:0.0
[B(comp*0.4+same*(0.25*B(nz(H,1)-0.7,-1,1)-im*0.6)+0.1*same*nz(iaPC-iaPR),-1,1),B(opp*im*math.max(0,lO),0,1),im*di]
jg(int side)=>
float edge=side==1?1-aD:aD
float dm=side*nz(V)
float fX= -side*cQ
float gW=0.8*ap+1.5*(edge-0.5)+0.40*side*F+0.55*side*bp-0.45*math.abs(nz(V))
float cz=side*iZ+0.35*math.max(0,dm)-0.9*ap
float gV=0.38*math.max(0,fX)+0.7*side*F+0.9*side*bp+0.7*side*hg+0.55*side*du+0.4*side*db+0.18*math.min(bT,3)*math.max(0,side*F)-0.35*math.max(0,-dm)
bool eI=side==1?aD>0.65:aD<0.35
float gn=0.55*ap+(jI?0.10:0.0)+(eI?0.55: -0.4)+0.7*dm+0.45*side*F+0.55*side*dU+0.4*side*hg
float mJ=ar==side?eS:0.0
float ld=ar== -side?eS:0.0
float P=B(math.max(0,side*F)+math.max(0,side*du)+math.max(0,side*bp),0,1)
cz-=be*mJ*(ap+math.max(0,-dm))*0.5
gV+=be*ld*P
gW+=be*eS*ap*math.max(0,edge-0.35)
gn+=be*math.max(0,side*ga)*math.max(0,dm)*(eI?1:0)
float bM=side*lX
float lq=side*nA
float hZ=B(side*cQ,0,1)
float fade=kw(side)
float ia=kw(-side)
float lr=B(0.55*bM+0.20*lq*math.max(0,bM)+0.15*side*la*math.max(0,bM)-0.55*fade,-1,1)
float revK=B(0.45*ia*P+0.35*math.max(0,lq)*P+0.20*math.max(0,bM)*P,-1,1)
float rotK=B(di*(edge-0.5)*1.6+0.15*di*P-0.25*math.max(0,-bM),-1,1)
float jQ=B((eI?1:0)*(0.45*math.max(0,bM)+0.30*math.max(0,lq)+0.25*jf(side)+0.15*cG*hZ),0,1)
if iaOK and es>0
[iaTrendAdj,iaRevAdj,iaBalanceAdj]=ic(side)
lr:=B(lr+es*iaTrendAdj,-1,1)
revK:=B(revK+es*iaRevAdj*P,-1,1)
rotK:=B(rotK+es*iaBalanceAdj*math.max(0,edge-0.35),-1,1)
jQ:=B(jQ+es*iaTrendAdj*(eI?1:0),0,1)
if kOn
cz+=dF*lr
gV+=dF*revK
gW+=dF*rotK
gn+=dF*jQ
[cz,gW,gV,gn]
[bc,br,bv,bb]=jg(1)
[sc,sr,sv,kK]=jg(-1)
lD(float a,float b,float c,float d)=>a>=b and a>=c and a>=d?0:b>=c and b>=d?1:c>=d?2:3
int bm=lD(bc,br,bv,bb)
int sm=lD(sc,sr,sv,kK)
float cY=math.max(bc,br,bv,bb)
float co=math.max(sc,sr,sv,kK)
nu(int m)=>m==0?"Continuation":m==1?"Range rotation":m==2?"Reversal":m==3?"Breakout":"Unclear"
type Plan
int side=0
int mode=0
float bH=na
float lo=na
float hi=na
float stop=na
float aa=na
string cO=""
string nS=""
string nr=""
float mn= -100.0
float rr=na
int C=0
array<aJ>hb=na
fC(int side,float gU,array<aJ>bn)=>
float aa=na
string nm=""
float nF=math.max(tick*2,norm*0.20)
for a in bn
float p=side==1?a.lo:a.hi
bool mv=a.Q!=side or a.C==6 or a.C==2
if mv and side*(p-gU)>nF and(na(aa)or side*(p-aa)<0)
aa:=p
nm:=a.name
[aa,nm]
fb(int side,int mode,float ref,float df,string ii,array<aJ>bn)=>
float best=na
string nm=""
float gap=math.max(tick*2,norm*lZ)
float edge=side==1?al:am
if mode==1 and not na(edge)and side*(edge-ref)>=tick*2 and(na(df)or side*(edge-df)>=0)
best:=edge
nm:=side==1?"Range high":"Range low"
if na(best)
for a in bn
float p=side==1?a.lo:a.hi
bool of=(a.C==2 or a.C==6)and a.Q!=side and a.name!="Daily open"and a.name!="PDC"
if of and side*(p-ref)>=gap and(na(df)or side*(p-df)>=0)and(na(best)or side*(p-best)<0)
best:=p
nm:=a.name
[fc(na(best)?df:best),na(best)?ii:nm]
fT(int side,int mode,float ref,float hL)=>
float pad=math.max(tick,math.abs(ref)*ee/100.0)
float gS=X==side?(side==1?W:N):na
float jX=dZ==side?(side==1?iLow:dN):na
float edge=side==1?am:al
float T=mode==1?edge:mode==0 or mode==3?gS:edge
if na(T)or side*(ref-T)<=0 or side*(ref-T)>norm*5
T:=jX
float iI=hL
if not na(T)and side*(ref-T)>0 and side*(ref-T)<=norm*5
float iT=side==1?math.floor((T-pad)/tick)*tick:math.ceil((T+pad)/tick)*tick
if side*(iT-hL)<0
iI:=iT
iI
dX(int side)=>
bool my=kOn and side*kVN>0.45 and side*kAN>0.10
bool lY=cE and av==side and nY<=3 and nz(H,0)>=0.85
bool no=side==1?aD>=0.80:aD<=0.20
(my or lY)and no
dd(int side,float e,bool gv)=>
float ch=side*(close-e)
bool fV=kOn and side*kVN< -0.28 and side*kAN< -0.15 and ch<norm*0.55
not na(e)and ch>=math.max(tick,gv?norm*0.65:tick)and not fV
dM(Plan p)=>
p.side!=0 and dd(p.side,p.bH,dX(p.side))
lF(int side,int mode,array<aJ>bn,float ij=na,float cN=na)=>
Plan best=Plan.new()
bool gv=dX(side)
float pad=math.max(tick,math.abs(close)*ee/100.0)
array<int>ids=array.new_int()
array<float>mj=array.new_float()
if array.size(bn)>0
for k=0 to array.size(bn)-1
aJ a=array.get(bn,k)
float e=side==1?a.hi:a.lo
float dist=side*(close-e)
bool lH=a.Q==0 or a.Q==side
bool jq=dist>=tick and dist<=norm*2.8 and(na(cN)or side*(e-cN)>tick)
bool mk=mode!=1 or(side==1?e<=am+ao*0.35:e>=al-ao*0.35)
if lH and jq and mk and dd(side,fc(e),gv)
float rank=a.dy*cB(a.C)+0.5*(a.C==4?1:0)-0.25*dist/norm
int at=array.size(ids)
if at>0
for q=0 to at-1
if rank>array.get(mj,q)
at:=q
break
if at<10
array.insert(ids,at,k)
array.insert(mj,at,rank)
if array.size(ids)>10
array.pop(ids)
array.pop(mj)
if array.size(ids)>0
for k in ids
aJ a=array.get(bn,k)
float e=fc(side==1?a.hi:a.lo)
float back=side==1?a.lo:a.hi
bool band=a.hi-a.lo>=tick
if not band
array<float>sw=array.from(side==1?mLow:kG,side==1?iLow:dN,side==1 and X==1?W:side== -1 and X== -1?N:na)
back:=na
for v in sw
if not na(v)and side*(e-v)>=tick and side*(e-v)<=norm*2.5 and(na(back)or side*(v-back)>0)
back:=v
if not na(back)
float sl=side==1?math.floor((back-pad)/tick)*tick:math.ceil((back+pad)/tick)*tick
if mode==0 and d0==side and side*(e-t0)>0
sl:=side==1?math.min(sl,math.floor((t0-pad)/tick)*tick):math.max(sl,math.ceil((t0+pad)/tick)*tick)
[tp,nm]=fC(side,close,bn)
if not na(ij)and(na(tp)or side*(tp-ij)>0)
tp:=ij
nm:="Idea goal"
float risk=side*(e-sl)+cg*tick
float ml=side*(tp-e)-cg*tick
float rr=risk>0?ml/risk:na
array<int>fd=array.new_int(aQ,-1)
array<float>bx=array.new_float(aQ,-1e9)
if array.size(bn)>0
for q=0 to array.size(bn)-1
aJ bN=array.get(bn,q)
bool near=bN.lo<=a.hi+norm*0.18 and bN.hi>=a.lo-norm*0.18
bool mm=bN.Q==0 or bN.Q==side
if near and mm and bN.C!=a.C
float mF=math.abs((bN.lo+bN.hi-a.lo-a.hi)*0.5)/norm
float r=bN.dy*cB(bN.C)-0.30*mF
if r>array.get(bx,bN.C)
array.set(bx,bN.C,r)
array.set(fd,bN.C,q)
int fz=0
for q in fd
fz+=q>=0?1:0
string me=""
array<aJ>kL=array.new<aJ>()
for pick=0 to 3
float mR=array.max(bx)
int f=array.indexof(bx,mR)
if f<0 or mR<= -1e8
break
int q=array.get(fd,f)
aJ bN=array.get(bn,q)
array.push(kL,bN)
me+=(me==""?"":" / ")+bN.name
array.set(bx,f,-1e9)
float ad=a.dy*cB(a.C)+0.20*math.min(fz,3)+0.25*math.min(nz(rr),5)-0.10*side*(close-e)/norm
if not na(tp)and side*(tp-close)>=tick and risk>0 and ml>0 and rr>=oh and(na(cN)or side*(sl-cN)>=0)and ad>best.mn
best:=Plan.new(side,mode,e,a.lo,a.hi,sl,fc(tp),a.name,nm,me,ad,rr,a.C,kL)
best
cj(int side,float K)=>
float pad=math.max(tick,math.abs(K)*ee/100.0)
side==1?math.floor((K-pad)/tick)*tick:math.ceil((K+pad)/tick)*tick
type Idea
int side=0
int mode=0
int born=na
int mS=na
float eK=na
float goal=na
float hard=na
float cS=na
float cC=na
float peak=0
int aY=na
int da=0
int gb=0
int ax=0
int mU=na
int iR=0
int kj=na
int iE=na
int fe=na
int bf=0
float stop=na
float risk=na
Plan plan=na
float ds=na
float kP=na
int dw=na
var Idea idea=Idea.new()
var array<Idea>cx=array.new<Idea>()
var array<int>aO=array.new_int()
var array<float>aP=array.new_float()
var array<label>dots=array.new<label>()
var int iV=0
var int gp=na
var int gh=na
var int jm=0
var int jl= -1
var int good=0
var int bad=0
var int kq=0
var int fE=0
var float netR=0
bool kH=false
bool cZ=false
if barstate.isconfirmed and eD and timeframe.isintraday
if idea.side!=0 and idea.ax==0 and bar_index>idea.born
int sd=idea.side
float own=sd==1?cY:co
float opp=sd==1?co:cY
float mr=sd*(idea.goal-idea.eK)
float aY=sd*(close-idea.eK)/mr
if aY>idea.peak+0.03
idea.peak:=aY
idea.aY:=bar_index
bool ks=sd*(close-idea.eK)< -norm*0.65 or(idea.peak>=0.2 and(idea.peak-aY)*mr>norm*0.9)
bool le=X== -sd or(dZ== -sd and sd*dU<0)or sd*cd<0
bool ov=(d0== -sd?1:0)+(sd*(close-dK)< -norm*0.12?1:0)+(sd*kr< -0.25?1:0)>=2
bool lc=sd*bq< -0.35 or sd*bp<0 or sd*F< -0.65
bool bM=sd*V< -0.25 and(nt(sd)or jd(sd)or sd*js< -norm*0.10)
int jU=(le?1:0)+(ov?1:0)+(lc?1:0)+(bM?1:0)
bool gq=opp>=dH and opp>own+ma+hT and ks and jU>=3
idea.da:=gq?idea.da+1:math.max(0,idea.da-1)
bool mG=sd==1?idea.goal>al:idea.goal<am
bool jP=idea.mode!=1 and iz and mG and bar_index-idea.aY>=cp+1 and math.max(br,sr)>own+hT
idea.gb:=jP?idea.gb+1:0
bool mK=ib and bar_index-idea.aY>=hA and ks and(iz or own<dH or gq)
bool jh=sd*(close-idea.hard)<=0
bool hV=sd*(close-idea.cS)<=0
bool kx=sd==1?high>=idea.goal:low<=idea.goal
int need=eS>0.65 and bM?math.max(2,cp-1):cp
if jh or hV or kx or idea.da>=need or idea.gb>=cp or mK
idea.ax:=jh?-1:hV?2:kx?1:2
idea.mU:=bar_index
gh:=bar_index
jm:=sd
jl:=idea.mode
good+=idea.ax==1?1:0
bad+=idea.ax== -1?1:0
kq+=idea.ax==2?1:0
if idea.bf==1 and bar_index>idea.iE
bool oq=sd==1?low<=idea.plan.bH:high>=idea.plan.bH
bool mL=sd==1?low<=idea.stop:high>=idea.stop
bool nG=sd==1?high>=idea.plan.aa:low<=idea.plan.aa
if oq and not(sd*(open-idea.stop)<=0)
idea.bf:=2
idea.fe:=bar_index
idea.iR+=1
fE+=1
idea.risk:=sd*(idea.plan.bH-idea.stop)+cg*tick
else if mL or nG or idea.ax!=0 or bar_index-idea.iE>=ky
idea.bf:=0
cZ:=true
if idea.bf==2
bool sl=sd==1?low<=idea.stop:high>=idea.stop
bool tp=sd==1?high>=idea.plan.aa:low<=idea.plan.aa
if sl or(tp and bar_index>idea.fe)or idea.ax!=0 or bar_index-idea.fe>=ky
float out=sl?(sd==1?math.min(open,idea.stop):math.max(open,idea.stop)):tp and bar_index>idea.fe?idea.plan.aa:bar_index==idea.fe?idea.plan.bH:close
netR+=(sd*(out-idea.plan.bH)-cg*tick)/math.max(tick,idea.risk)
idea.bf:=0
cZ:=true
if idea.ax==0 and iP
float K=na
if(sd==1?hY:hX)and(sd==1?eW:dz)>=idea.born
K:=sd==1?W:N
if(sd==1?hE:hD)and(sd==1?gg:er)>=idea.born
float v=sd==1?iLow:dN
if not na(v)and(na(K)or sd*(v-K)>0)
K:=v
if not na(idea.dw)
if sd*(close-idea.ds)<=0 or bar_index-idea.dw>8
idea.dw:=na
else if bar_index>idea.dw and sd*(close-idea.kP)>norm*0.12
if na(K)or sd*(idea.ds-K)>0
K:=idea.ds
idea.dw:=na
if na(idea.dw)
for a in book
float edge=sd==1?a.hi:a.lo
float back=sd==1?math.min(a.lo,low):math.max(a.hi,high)
bool mI=(a.Q==sd or a.Q==0)and high>=a.lo and low<=a.hi and sd*(close-edge)>norm*0.12 and sd*(close[1]-edge)>0
if mI and sd*(back-idea.cS)>tick and(na(idea.dw)or sd*(back-idea.ds)>0)
idea.dw:=bar_index
idea.ds:=back
idea.kP:=sd==1?high:low
if not na(K)and jU<=1 and aY>=0.15
float next=cj(sd,K)
if sd*(close-next)>=math.max(tick*2,norm*hW)and sd*(next-idea.cS)>=tick
idea.cS:=next
if idea.bf==2 and bar_index>idea.fe and sd*(next-idea.stop)>=tick and sd*(close-next)>=norm*hW and sd*(close-idea.plan.bH)>=idea.risk*0.5
idea.stop:=next
if idea.ax==0 and idea.bf!=2 and not cZ
if idea.bf==1 and(sd*(idea.plan.stop-idea.cS)<0 or sd*(idea.plan.aa-close)<=tick or sd*(close-idea.plan.bH)>norm*3.5 or gq)
idea.bf:=0
if hv and idea.iR<hl and bar_index-nz(idea.kj,idea.born)>=jb and jU<=1 and own>=dH and own>=opp
idea.kj:=bar_index
Plan q=lF(sd,idea.mode,book,idea.goal,idea.cS)
if dM(q)
bool mB=idea.bf!=1 or(q.rr>=idea.plan.rr+0.25 and q.mn>=idea.plan.mn-0.15)
if mB
idea.plan:=q
idea.stop:=q.stop
idea.iE:=bar_index
idea.bf:=1
int sd=cY>=co?1: -1
int mode=sd==1?bm:sm
bool lx=(idea.side==0 or idea.ax!=0)and(na(gh)or bar_index>gh and(sd!=jm or mode!=jl or bar_index-gh>=cp))
if lx and math.max(cY,co)>=dH and math.abs(cY-co)>=ma
Plan q=lF(sd,mode,book)
[aa,nm]=fC(sd,close,book)
float jq=q.side!=0?q.stop:na
if na(jq)
float K=na
for a in book
float v=sd==1?a.lo:a.hi
if(a.Q==sd or a.C==2 or a.C==0 or a.C==6)and sd*(close-v)>tick and sd*(close-v)<=norm*3 and(na(K)or sd*(v-K)>0)
K:=v
if not na(K)
jq:=cj(sd,K)
[goal,_]=fb(sd,mode,close,aa,nm,book)
float dE=fT(sd,mode,close,jq)
if not na(goal)and not na(dE)and sd*(goal-close)>0 and sd*(close-dE)>0
idea:=Idea.new()
idea.side:=sd
idea.mode:=mode
idea.born:=bar_index
idea.mS:=u
idea.eK:=close
idea.goal:=goal
idea.hard:=dE
idea.cS:=dE
idea.aY:=bar_index
idea.kj:=bar_index
idea.plan:=Plan.new()
if q.side!=0 and dM(q)and sd*(q.stop-dE)>=0 and sd*(goal-q.aa)>=0
idea.plan:=q
idea.cC:=q.bH
idea.stop:=q.stop
idea.iE:=bar_index
idea.bf:=1
array.push(cx,idea)
if array.size(cx)>250
array.shift(cx)
if iV!=sd
iV:=sd
gp:=bar_index
array.clear(aO)
array.clear(aP)
if iK
if not fH
while array.size(dots)>0
label.delete(array.pop(dots))
while array.size(dots)>=iy
label.delete(array.shift(dots))
string tip="Idea started: "+f_px(close)+"\n"+str.format_time(time_close,"yyyy-MM-dd HH:mm",nK)+"\nGoal: "+f_px(goal)+"\nWrong: "+f_px(dE)+"\nNot an entry."
array.push(dots,label.new(bar_index,close,"",style=label.style_circle,color=sd==1?color.rgb(125,204,178):color.rgb(231,142,151),size=size.tiny,tooltip=tip))
kH:=true
if iV!=0
array.push(aO,bar_index)
array.push(aP,close)
if array.size(aO)>1600
array.shift(aO)
array.shift(aP)
if not fH and idea.ax!=0
while array.size(dots)>0
label.delete(array.pop(dots))
var array<line>fu=array.new<line>()
var array<label>ef=array.new<label>()
var array<box>ft=array.new<box>()
var polyline bV=na
color ot=color.rgb(125,204,178)
color en=color.rgb(231,142,151)
color dg=color.rgb(235,235,235)
bA(int x1,float y1,int x2,float y2,color ink,string sty,int w=1)=>
if not na(y1)and not na(y2)
array.push(fu,line.new(x1,y1,x2,y2,color=ink,style=sty,width=w))
ep(int x,float y,string msg,color ink,string tip,bool nU=true)=>
array.push(ef,label.new(x,y,msg,style=nU?label.style_label_down:label.style_label_up,color=color.new(ink,100),textcolor=ink,size=size.small,tooltip=tip))
ns(int x1,int x2,float lo,float hi,color ink)=>
array.push(ft,box.new(x1,hi,x2,lo,bgcolor=color.new(ink,91),border_color=na))
gf(int b,int first,int bB,int left,int mg,int nH)=>
int bC=math.max(0,bB-first)
int gT=math.min(nH,bC)
int fm=bC-gT
int eG=math.min(mg,fm)
int lt=bB-gT
int bb=math.max(first,math.min(bB,b))
fm>0 and bb<lt?left+int(math.round(float(bb-first)*eG/fm)):left+eG+bb-lt
gz(float n)=>na(n)?"Learning":str.tostring(n,"#.0")
eq(float n)=>na(n)?"Learning":str.tostring(n*100,"#.0")+"%"
lB(int t)=>gz(float(u-t)/60)+" min"
if barstate.islast
while array.size(fu)>0
line.delete(array.pop(fu))
while array.size(ef)>0
label.delete(array.pop(ef))
while array.size(ft)>0
box.delete(array.pop(ft))
if not na(bV)
polyline.delete(bV)
bV:=na
bool kI=idea.side!=0 and idea.ax==0
int left=bar_index+mP
if array.size(aO)>0 and(kI or not bz)
int bB=array.last(aO)
int first=bz?(cr?idea.born:bB):gp
int nowX=gf(bB,first,bB,left,cL,cc)
int endX=nowX+hP
float dO=array.last(aP)
color ink=iV==1?ot:en
array<chart.point>pts=array.new<chart.point>()
if not bz or cr
if bz
array.push(pts,chart.point.from_index(left,idea.eK))
for i=0 to array.size(aO)-1
int b=array.get(aO,i)
if b>=first and(not bz or b>first)
array.push(pts,chart.point.from_index(gf(b,first,bB,left,cL,cc),array.get(aP,i)))
if array.size(pts)>1
bV:=polyline.new(pts,xloc=xloc.bar_index,line_color=ink,line_width=2)
ep(left,bz?idea.eK:array.first(aP),"Start",ink,"Price history",false)
if not bz and gY and array.size(cx)>0
for i=math.max(0,array.size(cx)-120)to array.size(cx)-1
Idea old=array.get(cx,i)
if old.ax!=0 and old.side==iV and old.born>=first
int a=gf(old.born,first,bB,left,cL,cc)
int b=gf(nz(old.mU,bB),first,bB,left,cL,cc)
if b-a>=3 or fR
b:=math.max(a+1,b)
color pc=color.new(old.ax==1?color.rgb(130,154,195):en,45)
if not na(old.cC)
int m=a+int((b-a)*0.35)
bA(a,old.eK,m,old.cC,pc,line.style_dashed)
bA(m,old.cC,b,old.goal,pc,line.style_dashed)
else
bA(a,old.eK,b,old.goal,pc,line.style_dashed)
float ku=barstate.isconfirmed?close-close[1]:close-open
string bM=math.abs(ku)<tick*0.5?"Barely moving":ku>norm*0.75?"Jumped up":ku< -norm*0.75?"Dropped sharply":ku>0?"Up":"Down"
string note="Live: "+bM
string tip=note
if kI
string ax=idea.da>0 or idea.gb>0?"Be careful":idea.peak>=0.15?"Still on track":"Watching"
string nj=idea.bf==2?(idea.side*(idea.stop-idea.plan.bH)>cg*tick?"Protect gains":"Practice entry touched"):idea.bf==1?(idea.side==1?"Wait for dip ":"Wait for bounce ")+f_px(idea.plan.bH):dX(idea.side)?(idea.side==1?"Too high to buy":"Too low to sell"):"No entry yet"
note+="\nIdea: "+(idea.side==1?"Up":"Down")+" | "+ax+" | "+lB(idea.mS)+"\n"+nj
tip:=note+"\nGoal: "+f_px(idea.goal)+"\nWrong if close "+(idea.side==1?"at/below ":"at/above ")+f_px(idea.cS)+"\nStarted: "+f_px(idea.eK)
if idea.bf==1 or idea.bf==2
tip+="\nPractice entry: "+f_px(idea.plan.bH)+" | Stop: "+f_px(idea.stop)+" | Goal: "+f_px(idea.plan.aa)
if not kk
tip+="\nPhase: "+id(cI)+" / "+lB(nz(bK,u))+"\nVelocity: "+gz(kV)+" / "+gz(lV)+" / "+gz(lW)+"\nAcceleration: "+gz(kA)+" | Jerk: "+gz(kJ)+"\nHistorical next up/down/range: "+eq(kD)+" / "+eq(hJ)+" / "+eq(ka)+" n="+str.tostring(kN)+"\nRegime age: "+lB(nz(fs,u))+" | Completed episodes remaining median: "+gz(jN)+" min n="+str.tostring(mN)+"\nImpulse retention 1/3/5/10m: "+eq(iaR1)+" / "+eq(iaR3)+" / "+eq(iaR5)+" / "+eq(kB)+"\nImpulse continuation/reversal: "+eq(iaPC)+" / "+eq(iaPR)+" n="+str.tostring(iaN)
bool pull=idea.bf==1 and idea.side*(dO-idea.plan.bH)>tick and(not hk or idea.peak<0.40)
int mid=nowX+int(hP*0.35)
if pull
bA(nowX,dO,mid,idea.plan.bH,dg,line.style_dashed,2)
bA(mid,idea.plan.bH,endX,idea.goal,dg,line.style_dashed,2)
else
bA(nowX,dO,endX,idea.goal,dg,line.style_dashed,2)
bA(nowX,idea.goal,endX,idea.goal,ink,line.style_dotted)
bA(nowX,idea.cS,endX,idea.cS,en,line.style_dotted)
ep(endX,idea.goal,"Goal "+f_px(idea.goal),ink,tip)
ep(endX,idea.cS,"Wrong "+(idea.side==1?"at/below ":"at/above ")+f_px(idea.cS),en,tip,false)
if lu and(idea.bf==1 or idea.bf==2)
if idea.plan.hi>idea.plan.lo
ns(nowX,mid,idea.plan.lo,idea.plan.hi,ink)
if not na(idea.plan.hb)
int fz=0
for a in idea.plan.hb
if fz<ho and(a.hi<idea.plan.lo or a.lo>idea.plan.hi)
bA(nowX,(a.lo+a.hi)/2,mid,(a.lo+a.hi)/2,color.new(ink,55),line.style_dotted)
fz+=1
if dL
bA(nowX,idea.stop,mid,idea.stop,en,line.style_dotted)
bA(nowX,idea.plan.aa,mid,idea.plan.aa,ink,line.style_dotted)
else
note+="\nWait for a new idea"
if not barstate.isconfirmed and bB<bar_index
bA(nowX,dO,nowX+1,close,color.new(ink,25),line.style_dotted)
ep(nowX,dO,note,ink,tip)
else
ep(left,close,"Wait for a new idea",color.gray,"No active prediction")
plot(cq?good:na,"Forecast goals",display=display.data_window)
plot(cq?bad:na,"Forecast failures",display=display.data_window)
plot(cq?kq:na,"Forecast changes",display=display.data_window)
plot(cq?fE:na,"Practice fills",display=display.data_window)
plot(cq?netR:na,"Practice net R",display=display.data_window)
alertcondition(kH,"New idea","TDC: new market idea")
#THE CLAIM
The original indicator was a market-structure descriptor — no orders, no auditable P&L.
#SOURCE
Pine v6 · 1933 lines · /code/market-kinematics.pine · category Price action.
#NOTES FROM THE REPO
The algo lives in `bot/DAAVILE_Kinematics_Grid.strategy.pine`.
Scripts are provided for research; publication is not a performance claim. No backtest result is implied by this file. Not financial advice.
Source is provided for research, not as a performance claim. Paste into a Pine editor before use. Not financial advice.