0% found this document useful (0 votes)
10 views12 pages

Pine Code - Volumes

This Pine Script code is designed for analyzing financial options data, specifically focusing on call and put volumes and their average prices. It includes functions to calculate the average volume of calls and puts, as well as their closing prices, while also determining the daily highest high and lowest low. The script plots the difference between call and put volumes, providing insights into market sentiment based on the calculated metrics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Pine Code - Volumes

This Pine Script code is designed for analyzing financial options data, specifically focusing on call and put volumes and their average prices. It includes functions to calculate the average volume of calls and puts, as well as their closing prices, while also determining the daily highest high and lowest low. The script plots the difference between call and put volumes, providing insights into market sentiment based on the calculated metrics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.

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

// Close price function


ClosePrice(symbol, tf) =>
[C, V] = request.security(symbol, tf, [close, volume])
Numerator = 0.0
for i = 0 to 29
Numerator += C[i] * V[i]
Denominator = math.sum(V, 30)
closing_price = Numerator / Denominator
roundn(C - closing_price, 1)

// Round to nearest Str_Diff


RoundtoStr(value, Str_Diff) =>
math.round(value / Str_Diff) * Str_Diff

// Define Index and Str_Diff


Index = str.tostring(syminfo.ticker)

Index := switch Index


"CNXFINANCE" => "FINNIFTY"
"SENSEX" => "BSX"
"BANK" => "BKX"
"NIFTY_MID_SELECT" => "MIDCPNIFTY"
"BANKNIFTY" => "BANKNIFTY"
"NIFTY" => "NIFTY"
=> na

Str_Diff = switch Index


"FINNIFTY" => 50
"NIFTY" => 50
"BSX" => 100
"BKX" => 100
"BANKNIFTY" => 100
"MIDCPNIFTY" => 25
=> 0

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

//sotring calls & pus average volumes using Functions


call_vol_avg = GetCallVolume(Index,Year,Month,Expiry,ATM)
put_vol_avg= GetPutVolume(Index,Year,Month,Expiry,ATM)

//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

isPuts = (diff > avg_diff) and (p > c) and (p > puts_sma)


isCalls = diff > avg_diff and (c > p) and ( c > calls_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

var float dailyLowestLow = na


var float dailyLowestLowOpen = na
var float dailyLowestLowClose = na
var int dailyLowestLowIndex = na
var bool dailyLowestLowIsGreen = 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

// Update daily highest high and its open/close values


if (na(dailyHighestHigh) or high > dailyHighestHigh)
dailyHighestHigh := high
dailyHighestHighOpen := open
dailyHighestHighClose := close
dailyHighestHighIndex := bar_index
dailyHighestHighIsRed := close < open

// Update daily lowest low and its open/close values


if (na(dailyLowestLow) or low < dailyLowestLow)
dailyLowestLow := low
dailyLowestLowOpen := open
dailyLowestLowClose := close
dailyLowestLowIndex := bar_index
dailyLowestLowIsGreen := close > open

// Round the close values to the specified precision

h = dailyHighestHighIsRed ? dailyHighestHighOpen : dailyHighestHighClose


l = dailyLowestLowIsGreen ? dailyLowestLowOpen : dailyLowestLowClose

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

//only hilll , no volume


ifBulls_hill = Difference[1] < 0 and Difference == 0 and not (isPuts or isPuts[1])
ifBears_hill= Difference[1] > 0 and Difference == 0 and not (isCalls or
isCalls[1])

bull_sl = low[1] < low ? low[1] : low


bear_sl = high[1] > high ? high[1] : high
// plotshape(ifBulls,style=shape.triangleup,force_overlay =
true,color=color.green,size = size.small,location = location.belowbar)
// plotshape(ifBears,style=shape.triangledown,force_overlay =
true,color=color.red,size = size.small,location = location.abovebar)
plotshape(ifboom_bulls,style=shape.arrowup,force_overlay =
true,color=color.yellow,size = size.large,location = location.belowbar)
plotshape(ifboom_bears,style=shape.arrowdown,force_overlay =
true,color=color.yellow,size = size.large,location = location.abovebar)

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)

//exit positions - vulnerable - can re enter again no problem


isfake_bears =(ifBears[6] or ifBears[5] or ifBears[4] or ifBears[3] or ifBears[2]
or ifBears[1]) and Difference > 0
plotshape(isfake_bears,style=shape.diamond,force_overlay =
true,color=color.purple,size = size.small,location = location.belowbar)
isfake_bulls = (ifBulls[6] or ifBulls[5] or ifBulls[4] or ifBulls[3] or ifBulls[2]
or ifBulls[1] ) and Difference < 0
plotshape(isfake_bulls,style=shape.diamond,force_overlay =
true,color=color.purple,size = size.small,location = location.belowbar)

zero_col = if Difference > 0


color.green
else if Difference < 0
color.red

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

// Check if it's a new day


if (bool(ta.change(time('D'))))
call_volume_sum := 0.0
put_volume_sum := 0.0
// Update the sums based on candle color
if isCalls
call_volume_sum += diff
else if isPuts
put_volume_sum += diff

opt_vol = roundn(put_volume_sum - call_volume_sum,1)


opt_vol_col = opt_vol > 0 ? color.green : color.red

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

who = if netVol < 0


-1
else
1
p_netvol := if netVol < 0
-(netVol)
else
netVol

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

// Check if it's a new day


if (bool(ta.change(time('D'))))
buyers := 0.0
sellers := 0.0
// Update the sums based on candle color
if isBuyers
buyers += p_netvol
else if isSellers
sellers += p_netvol
net_guys = buyers - sellers
net_guys

//Futures

//Get Stocks & Futures Vol


calc_Stk_fut() =>

float Fut_Vol =na


float Stock_Vol = na

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)

table.cell(tbl, 1, 1,"Fut",text_size =txtsize,text_color = Fut_Vol_30M_col)


table.cell(tbl, 2, 1,str.tostring(roundn(Fut_Vol,0))+"k", text_color =
Fut_Vol_30M_col,text_size =txtsize)

//table.cell(tbl, 1, 2,"Stocks",text_size =txtsize, text_color =


Stock_Vol_30M_col)
//table.cell(tbl, 2, 2,str.tostring(roundn(Stock_Vol,0))+"k", text_color =
Stock_Vol_30M_col,text_size =txtsize)

table.cell(tbl, 1, 3,"Options",text_size =txtsize,text_color = opt_vol_col)


table.cell(tbl, 2, 3,str.tostring(opt_vol)+"k", text_color =
opt_vol_col,text_size =txtsize)

table.cell(tbl, 1, 4,"ATM",text_color =color.black,text_size =txtsize,bgcolor =


atm_change_col)
table.cell(tbl, 2, 4,str.tostring(atm_change),text_color =color.black,
text_size =txtsize,bgcolor = atm_change_col)

//Exclusively nifty volume


nifty_calcVolumes(symbol) =>

[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

buyVol := V * (C- L) / (H - L) // Calculate buy volume using the formula:


volume * (close - low) / (high - low)
sellVol := V - buyVol // Calculate sell volume by subtracting buy volume
from total volume
pcBuy := buyVol / V * 100 // Calculate the percentage of buy volume
pcSell := 100 - pcBuy // Calculate the percentage of sell volume
[buyVol ,sellVol,pcBuy ,pcSell]//Function outputs

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)

You might also like