Pine Code - Volumes
Pine Code - Volumes
0
at https://mozilla.org/MPL/2.0/
//@version=5
indicator("F&O")
//tf=input.timeframe('5', "Resolution")
tf='5'
// Rounding function
roundn(x, n) =>
mult = 1
if n != 0
for i = 1 to math.abs(n)
mult *= 10
n >= 0 ? math.round(x * mult) / mult : math.round(x / mult) * mult
Year = "24"
Month =input.string("08")
Expiry = input.string("01")
ATM=input(50000)
//get calls volume average
GetCallVolume(Index,Year,Month,Expiry,atm)=>
string vc0 =na
string vc1 =na
string vc2 =na
string vc3 =na
string vc4 =na
string vc5 =na
string vc6 =na
string vc7 =na
string vc8 =na
float vcall_atm = na
float vcall_otm1 =na
float vcall_otm2 =na
float vcall_otm3 =na
float vcall_otm4 =na
float vcall_otm5 =na
float vcall_otm6 =na
float vcall_otm7 =na
float vcall_otm8 =na
float vcall_avg_vol= na
vc8:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(8*Str_Diff))//atm+8
vc7:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(7*Str_Diff))//atm+7
vc6:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(6*Str_Diff))//atm+6
vc5:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(5*Str_Diff))//atm+5
vc4:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(4*Str_Diff))//atm+4
vc3:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(3*Str_Diff))//atm+3
vc2:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(2*Str_Diff))//atm+2
vc1:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(1*Str_Diff))//atm+1
vc0:= Index+Year+Month+Expiry+"C"+str.tostring(atm) //atm
vcall_atm:=request.security(vc0,tf,volume)
vcall_otm1:=request.security(vc1,tf,volume)
vcall_otm2:=request.security(vc2,tf,volume)
vcall_otm3:=request.security(vc3,tf,volume)
vcall_otm4:=request.security(vc4,tf,volume)
vcall_otm5:=request.security(vc5,tf,volume)
vcall_otm6:=request.security(vc6,tf,volume)
vcall_otm7:=request.security(vc7,tf,volume)
vcall_otm8:=request.security(vc8,tf,volume)
vcall_avg_vol :=
math.avg(vcall_atm,vcall_otm1,vcall_otm2,vcall_otm3,vcall_otm4,vcall_otm5,vcall_otm
6,vcall_otm7,vcall_otm8)
vcall_avg_vol
//gets puts average volume
GetPutVolume(Index,Year,Month,Expiry,atm)=>
string vp0 =na
string vp1 =na
string vp2 =na
string vp3 =na
string vp4 =na
string vp5 =na
string vp6 =na
string vp7 =na
string vp8 =na
float vput_atm = na
float vput_otm1 =na
float vput_otm2 =na
float vput_otm3 =na
float vput_otm4 =na
float vput_otm5 =na
float vput_otm6 =na
float vput_otm7 =na
float vput_otm8 =na
float vput_avg_vol= na
vp8:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(8*Str_Diff))
vp7:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(7*Str_Diff))
vp6:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(6*Str_Diff))
vp5:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(5*Str_Diff))
vp4:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(4*Str_Diff))
vp3:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(3*Str_Diff))
vp2:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(2*Str_Diff))
vp1:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(1*Str_Diff))
vp0:= Index+Year+Month+Expiry+"P"+str.tostring(atm)
vput_atm:=request.security(vp0,tf,volume)
vput_otm1:=request.security(vp1,tf,volume)
vput_otm2:=request.security(vp2,tf,volume)
vput_otm3:=request.security(vp3,tf,volume)
vput_otm4:=request.security(vp4,tf,volume)
vput_otm5:=request.security(vp5,tf,volume)
vput_otm6:=request.security(vp6,tf,volume)
vput_otm7:=request.security(vp7,tf,volume)
vput_otm8:=request.security(vp8,tf,volume)
vput_avg_vol :=
math.avg(vput_atm,vput_otm1,vput_otm2,vput_otm3,vput_otm4,vput_otm5,vput_otm6,vput_
otm7,vput_otm8)
vput_avg_vol
//plotting purpose
r = 1000
c= call_vol_avg /r
p = put_vol_avg/r
float diff = na
diff := (p - c)/r
//if difference become negative , then make it postive . why ? to calculate avg
diff
if diff < 0
diff := -(diff)
else
na
len = 30
avg_diff = ta.sma(diff,len) //calculating avg diff
calls_sma =ta.sma(c, len) //calculating calls vol sma
puts_sma =ta.sma(p, len) // calculating puts vol sma
diff_col = if isPuts
color.green
else if isCalls
color.red
else
color.black
plot(diff,style=plot.style_histogram,color = diff_col)
// Variables to store daily highest high and lowest low along with their closes
var float dailyHighestHigh = na
var float dailyHighestHighOpen = na
var float dailyHighestHighClose = na
var int dailyHighestHighIndex = na
var bool dailyHighestHighIsRed = na
// Reset daily highest high and lowest low variables at the start of a new day
if (bool(ta.change(time('D'))))
dailyHighestHigh := na
dailyHighestHighOpen := na
dailyHighestHighClose := na
dailyHighestHighIndex := na
dailyHighestHighIsRed := na
dailyLowestLow := na
dailyLowestLowOpen := na
dailyLowestLowClose := na
dailyLowestLowIndex := na
dailyLowestLowIsGreen := na
h0 = RoundtoStr(h, Str_Diff)
l0 = RoundtoStr(l, Str_Diff)
GetCallSymbol(Index,Year,Month,Expiry,atm)=>
string c0 =na
string c1 =na
string c2 =na
string c3 =na
string c4 =na
string c5 =na
string c6 =na
string c7 =na
string c8 =na
float call_atm = na
float call_otm1 =na
float call_otm2 =na
float call_otm3 =na
float call_otm4 =na
float call_otm5 =na
float call_otm6 =na
float call_otm7 =na
float call_otm8 =na
float call_avg= na
c8:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(8*Str_Diff))//atm+8
c7:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(7*Str_Diff))//atm+7
c6:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(6*Str_Diff))//atm+6
c5:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(5*Str_Diff))//atm+5
c4:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(4*Str_Diff))//atm+4
c3:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(3*Str_Diff))//atm+3
c2:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(2*Str_Diff))//atm+2
c1:= Index+Year+Month+Expiry+"C"+str.tostring(atm+(1*Str_Diff))//atm+1
c0:= Index+Year+Month+Expiry+"C"+str.tostring(atm) //atm
call_atm:=ClosePrice(str.tostring(c0),tf)
call_otm1:=ClosePrice(c1,tf)
call_otm2:=ClosePrice(c2,tf)
call_otm3:=ClosePrice(c3,tf)
call_otm4:=ClosePrice(c4,tf)
call_otm5:=ClosePrice(c5,tf)
call_otm6:=ClosePrice(c6,tf)
call_otm7:=ClosePrice(c7,tf)
call_otm8:=ClosePrice(c8,tf)
call_avg :=
math.avg(call_atm,call_otm1,call_otm2,call_otm3,call_otm4,call_otm5,call_otm6,call_
otm7,call_otm8)
call_avg
GetPutSymbol(Index,Year,Month,Expiry,atm)=>
string p0 =na
string p1 =na
string p2 =na
string p3 =na
string p4 =na
string p5 =na
string p6 =na
string p7 =na
string p8 =na
float put_atm = na
float put_otm1 =na
float put_otm2 =na
float put_otm3 =na
float put_otm4 =na
float put_otm5 =na
float put_otm6 =na
float put_otm7 =na
float put_otm8 =na
float put_avg= na
p8:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(8*Str_Diff))
p7:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(7*Str_Diff))
p6:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(6*Str_Diff))
p5:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(5*Str_Diff))
p4:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(4*Str_Diff))
p3:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(3*Str_Diff))
p2:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(2*Str_Diff))
p1:= Index+Year+Month+Expiry+"P"+str.tostring(atm-(1*Str_Diff))
p0:= Index+Year+Month+Expiry+"P"+str.tostring(atm)
put_atm:=ClosePrice(p0,tf)
put_otm1:=ClosePrice(p1,tf)
put_otm2:=ClosePrice(p2,tf)
put_otm3:=ClosePrice(p3,tf)
put_otm4:=ClosePrice(p4,tf)
put_otm5:=ClosePrice(p5,tf)
put_otm6:=ClosePrice(p6,tf)
put_otm7:=ClosePrice(p7,tf)
put_otm8:=ClosePrice(p7,tf)
put_avg :=
math.avg(put_atm,put_otm1,put_otm2,put_otm3,put_otm4,put_otm5,put_otm6,put_otm7,put
_otm8)
put_avg
call_avg_close = GetCallSymbol(Index,Year,Month,Expiry,ATM)
put_avg_close= GetPutSymbol(Index,Year,Month,Expiry,ATM)
//price analysis
Difference = call_avg_close < 0 and put_avg_close < 0 ? 0 : call_avg_close -
put_avg_close // we are checking with prices so call refers to green
ifBulls = Difference[1] < 0 and Difference == 0 and (isPuts or isPuts[1]) and not
isCalls /// can delete not (extra filter )
ifBears = Difference[1] > 0 and Difference == 0 and (isCalls or isCalls[1]) and
not isPuts
ifboom_bulls = Difference[1] < 0 and Difference > 0 and (isPuts or isPuts[1]) and
not isCalls
ifboom_bears = Difference[1] > 0 and Difference < 0 and (isCalls or isCalls[1]) and
not isPuts
if ifBulls
label.new(x=bar_index, y=na,text="SL:"+str.tostring(roundn(bull_sl,0)) + "[" +
str.tostring(roundn((bull_sl-close),0))
+"]" ,xloc=xloc.bar_index,yloc=yloc.belowbar,
textcolor=color.yellow,size=size.small,force_overlay = true,style
=label.style_triangleup,color=color.green)
if ifBears
label.new(x=bar_index, y=na,text="SL:"+str.tostring(roundn(bear_sl,0))+ "[" +
str.tostring(roundn((close - bear_sl),0))
+"]" ,xloc=xloc.bar_index,yloc=yloc.abovebar,
textcolor=color.yellow,size=size.small,force_overlay = true,style
=label.style_triangledown,color=color.red)
if ifBulls_hill
label.new(x=bar_index, y=na,text="might go
up" ,xloc=xloc.bar_index,yloc=yloc.belowbar,
textcolor=color.green,size=size.small,force_overlay = true,style
=label.style_triangleup)
if ifBears_hill
label.new(x=bar_index, y=na,text="might go
down" ,xloc=xloc.bar_index,yloc=yloc.abovebar,
textcolor=color.red,size=size.small,force_overlay = true,style
=label.style_triangledown)
plot(0,color=zero_col,linewidth = 1)
ema = ta.ema(close,12)
//atm change
ATM_close =RoundtoStr(ema,Str_Diff)
string atm_change = na
if ATM != ATM_close
atm_change := str.tostring(ATM_close)
else
na
atm_change_col = ATM != ATM_close ? color.yellow :color.black
///cumulative vvolume
var float call_volume_sum = 0.0
var float put_volume_sum = 0.0
var int current_day = na
calcVolumes(symbol) =>
[O,H,L,C,V]=request.security(symbol,'5',[open,high,low,close,volume])
float buyVol = na
float sellVol = na
float netVol = na
float netVol_avg = na
float vol_avg = na
var float buyers = na
var float sellers = na
float p_netvol = na
vol_avg := ta.sma(V,30)
buyVol := V * (C- L) / (H - L) // Calculate buy volume using the formula:
volume * (close - low) / (high - low)
sellVol := V - buyVol
netVol := buyVol - sellVol // Calculate sell volume
by subtracting buy volume from total volume
netVol_avg := ta.sma(p_netvol,30)
isBuyers = V > vol_avg and p_netvol > netVol_avg and who > 0
isSellers = V > vol_avg and p_netvol > netVol_avg and who < 0
//Futures
A = calcVolumes('NSE:NIFTY1!')
B = calcVolumes('NSE:BANKNIFTY1!')
Fut_Vol := (A+B)/1000
Fut_Vol_30M = roundn(Fut_Vol - Fut_Vol[6],0)
Fut_Vol_30M_col = Fut_Vol_30M > 0 ? color.green:color.red
// S_A = calcVolumes('NSE:RELIANCE')
// S_B = calcVolumes('NSE:HDFCBANK')
// S_C = calcVolumes('NSE:ICICIBANK')
// Stock_Vol := (S_A+S_B+S_C)/1000
// Stock_Vol_30M = roundn(Stock_Vol - Stock_Vol[6],0)
// Stock_Vol_30M_col = Stock_Vol_30M > 0 ? color.green:color.red
[Fut_Vol,Stock_Vol,Fut_Vol_30M,Fut_Vol_30M_col]//,Stock_Vol_30M,Stock_Vol_30M_col]
[Fut_Vol,Stock_Vol,Fut_Vol_30M,Fut_Vol_30M_col]=
calc_Stk_fut()//,Stock_Vol_30M,Stock_Vol_30M_col]
txtsize=size.normal
var tbl = table.new(position.bottom_right, 20,
20,force_overlay=false,border_width=1,border_color =color.black)
if (barstate.islast)
[O,H,L,C,V]=request.security(symbol,'5',[open,high,low,close,volume])
float buyVol = na
float sellVol = na
float pcBuy = na
float pcSell = na
input_vol = 100000
input_percent = 99
[nifty_fut_buy_vol,nifty_fut_sell_vol,buy_pc,sell_pc] =
nifty_calcVolumes("NSE:NIFTY1!")
nft_buyers = nifty_fut_buy_vol > input_vol and buy_pc > input_percent // Net
buyers should be greater than one lakh , and they should occupy full candle
nft_sellers = nifty_fut_sell_vol > input_vol and sell_pc > input_percent // Net
sellers should be greater than one lakh , and they should occupy full candle
plotshape(nft_buyers,style=shape.xcross,force_overlay =
true,color=color.yellow,size = size.tiny,location = location.belowbar)
plotshape(nft_sellers,style=shape.xcross,force_overlay =
true,color=color.yellow,size = size.tiny,location =location.abovebar)
if ifBears or ifBulls or ifboom_bears or ifboom_bulls
alert("Entry opportunity",alert.freq_once_per_bar_close)
[iO,iH,iL,iC,iV]=request.security(syminfo.tickerid,'5',
[open,high,low,close,volume])
//konda
ifmountainbull = Difference[2] == 0 and Difference[1] > 0 and isCalls[1] and
Difference > 0 and iC > iH[1]
ifmountainbear = Difference[2] == 0 and Difference[1] < 0 and isPuts[1] and
Difference < 0 and iC < iL[1]
plotshape(ifmountainbull,style=shape.flag,force_overlay =
true,color=color.blue,size = size.small,location = location.belowbar)
plotshape(ifmountainbear,style=shape.flag,force_overlay =
true,color=color.orange,size = size.small,location = location.abovebar)