Hello
I am leaving this link to seek helps to convert Pine script at Tradingview.
https://www.tradingview.com/script/m03F3fQ7-Daily-Historical-Volatility-StdDev-Levels/
--------------------- Code below ______________________________________
//@version=3
//
study(title="Daily Historical Volatility StdDev Levels R2 by JustUncleL", shorttitle="HVSD", overlay = true)
//
// Author : JustUncleL
//
// Description:
// Plots Daily Standard deviation levels on price chart based on Historical Volatility (HV).
// The following uses the most common approach for calculating historical volatility as
// standard deviation of logarithmic returns, based on daily closing/settlement prices.
// Assets: Any Currency, Commodities also on stocks, some indices.
// Time Frames: 5min to 60min.
// This will also work on Daily Chart, by setting "DaystoExpire" to 21
//
// Options:
// - Use Daily Data to Calculate StdDev HV (default), otherwise use the charts Time Frame
// - Lookback = number of days/periods to calculate stddev of HV (21 by default)
// - Annual = number of trading days in a calender year (252 by default)
// - Days to Expiry = number of days for the life of this option ( for auto calculation
// this is 1 for intraday, 21 for dayly and annual when chart TF used)
// - Settlement Source = close price by default, can use another source.
// - Settlement Volume Weighted Average Length = by setting this to >1 then an average
// is used for settlement.
// - Display ### Standard Deviation Levels = select what levels are to be displayed.
//
// References:
// - How To Use Standard Deviation In Your Trading Day
//
// - Deviation Levels Indicator
//
// -
http://www.macroption.com/historical-volatility-calculation/
// - Historical Volatility based Standard Deviation_V2 by ucsgears
// - Historical Volatility Strategy by Hpotter
//
// Revisions:
//
// 30-July-2017 : Original
//
// 01-Aug-2017 : - Corrected Change of day detection for Intraday TFs
// - Corrected -0.75 Label.
//
// 14-Aug-2017 : - Chnge to only display background fill for upto +/- 1 Stddev.
// - Added option to Display Only Todays Levels.
//
// 16-Aug-2017 : - Made changes so that it also work with Renko Charts, But
// only works well with short TF (<=5min) and brick (<=6pip).
// - Added Option to display Previous Day's High Low Levels.
// - Added Option to display Current Day's High Low Levels.
// - Changed 0.75 level to 0.7 level.
//
// 9-Sep-2017 : - Modified the way displaying "Only Today" levels, now only
// show up within the current days price action.
//
// 5-Jun-2018 : - Improved efficiency by reducing the number of plot lines
// required by adding "style=2" to the plot options.
// - Added fill colour for 2nd and 3rd Stddevs.
//
// 8-Jun-2018 : - Corrected no history "nohist" flag.
//
//
// -----------------------------------------------------------------------------
// Copyright 2017,2018 @JustUncleL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <
http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// === INPUTS
useDaily = input(true,title="Use Daily Data to Calculate HV (default), otherwise chart TF")
LookBack = input(21, minval=1,type=integer)
annual = input(252,minval=1,type=integer)
DaystoExpire_ = input (defval=0, minval=0,type=integer, title="Calender Days to Expiry (0=Auto, default)")
src = input(close,title="Settlement Source (close=default)")
sLength_ = input(1,minval=1,type=integer,title="Settlement Volume Weighted Average Length (1=Use end of day)")
//
stddev1 =input(true,title="Display 1x Standard Deviation Levels")
stddev2 =input(false,title="Display 2x Standard Deviation Levels")
stddev3 =input(false,title="Display 3x Standard Deviation Levels")
pivotNow =input(false,title="Display Only Todays Deviation Levels")
sHiLo =input(false,title="Show Previous Day's High Low Levels")
sHiLoNow =input(false,title="Show Current Day's Running High Low Levels")
//
// === /INPUTs
dodgerblue = #1E90FF
//
// Test for new Daily Session or start of new month for Daily.
sLength = isintraday ? sLength_ : 1
nstart = security(tickerid,"D", n, barmerge.gaps_off, barmerge.lookahead_on)
start = security(tickerid,"D", time, barmerge.gaps_off, barmerge.lookahead_on)
first = security(tickerid,"D",valuewhen(barstate.islast,time,0), barmerge.gaps_off, barmerge.lookahead_on)
nohist = nstart<=max(sLength,LookBack)+1
newDay = nohist? false : isintraday ? change(start) : dayofmonth(time)<dayofmonth(time[1])
// Calculate Annualised Volatility
hv = 0.0
hv_ = useDaily? security(tickerid,"D", stdev(log(src / src[1]), LookBack) * sqrt(annual), barmerge.gaps_off, barmerge.lookahead_on) : stdev(log(src / src[1]), LookBack) * sqrt(annual)
hv := newDay ? hv_ : nz(hv[1],hv_)
hinow = high
hinow := newDay ? high : high>hinow[1] ? high : hinow[1]
lonow = low
lonow := newDay ? low : low<lonow[1] ? low : lonow[1]
prevhi = valuewhen(newDay,hinow[1],0)
prevlo = valuewhen(newDay,lonow[1],0)
// get the Daily Settlement
settlement = sLength==1? valuewhen(start[1]<=time, src, 0) : vwma(src[1],sLength)
settlement := newDay ? settlement : nz(settlement[1],open[1])
//--- debug
//plotchar(n,title="n",location=location.bottom,color=na)
//plotchar(nstart,title="nstart",location=location.bottom,color=na)
//plotchar(nohist,title="noHist",location=location.bottom,color=na)
//plotchar(dayofmonth(start),title="startDay",location=location.bottom,color=na)
//plotchar(hour(start),title="startMins",location=location.bottom,color=na)
//plotchar(dayofmonth(first),title="firstDay",location=location.bottom,color=na)
//plotchar(hour(first),title="firstMins",location=location.bottom,color=na)
//plotchar(newDay,title="newDay",location=location.bottom,color=na)
//--- /debug
firstDay = dayofmonth(start)==dayofmonth(first) and month(start)==month(first) and year(start)==year(first)
//
// Calculate STDdev over life of the option (generally this is one day for 5min to 60min TFs,
// and 21 days for Daily TF)
stdhv = 0.0
DaystoExpire = DaystoExpire_==0? isintraday? useDaily? 1 : min(annual,1440/interval) : LookBack : DaystoExpire_
stdhv := newDay? settlement*hv*sqrt(DaystoExpire/annual) : nz(stdhv[1])
// calculate StdDev lines ratios.
stdhv05 = stdhv * 0.5
stdhv07 = stdhv * 0.7
stdhv1 = stdhv
stdhv15= stdhv*1.5
stdhv2 = stdhv*2.0
stdhv25 = stdhv*2.5
stdhv3 = stdhv*3.0
// Plot the StdDev Levels for all Days.
SettleM = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? settlement:na, color = purple, style=2,title = "Settlement",linewidth=3, transp=40)
Stdhv05u = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv05):na, color = orange, style=2,title = "+0.5 SD",linewidth=1, transp=20)
Stdhv05d = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv05):na, color = orange, style=2,title = "-0.5 SD",linewidth=1, transp=20)
Stdhv07u = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv07):na, color = red, style=2,title = "+0.7 SD",linewidth=1, transp=20)
Stdhv07d = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv07):na, color = red, style=2,title = "-0.7 SD",linewidth=1, transp=20)
Stdhv1u = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv1):na, color = lime, style=2,title = "+1 SD",linewidth=3, transp=20)
Stdhv1d = plot(not nohist and stddev1 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv1):na, color = lime, style=2,title = "-1 SD",linewidth=3, transp=20)
Stdhv15u = plot(not nohist and stddev2 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv15):na, color = green, style=2,title = "+1.5 SD",linewidth=1, transp=20)
Stdhv15d = plot(not nohist and stddev2 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv15):na, color = green, style=2, title = "-1.5 SD",linewidth=1, transp=20)
Stdhv2u = plot(not nohist and stddev2 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv2):na, color = dodgerblue, style=2,title = "+2 SD",linewidth=3, transp=20)
Stdhv2d = plot(not nohist and stddev2 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv2):na, color = dodgerblue, style=2,title = "-2 SD",linewidth=3, transp=20)
Stdhv25u = plot(not nohist and stddev3 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv25):na, color = dodgerblue, style=2,title = "+2.5 SD",linewidth=1, transp=20)
Stdhv25d = plot(not nohist and stddev3 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv25):na, color = dodgerblue, style=2,title = "-2.5 SD",linewidth=1, transp=20)
Stdhv3u = plot(not nohist and stddev3 and (not pivotNow or firstDay) and not newDay? (settlement+stdhv3):na, color = gray, style=2,title = "+3 SD",linewidth=3, transp=20)
Stdhv3d = plot(not nohist and stddev3 and (not pivotNow or firstDay) and not newDay? (settlement-stdhv3):na, color = gray,style=2, title = "-3 SD",linewidth=3, transp=20)
//
plot( (not pivotNow or firstDay) and not nohist and sHiLo and not newDay? prevhi:na, color = navy,style=2, title = "PREV HI",linewidth=2, transp=20,style=cross,join=true)
plot( (not pivotNow or firstDay) and not nohist and sHiLo and not newDay? prevlo:na, color = black,style=2, title = "PREV LO",linewidth=2, transp=20,style=cross,join=true)
plot( (not pivotNow or firstDay) and not nohist and sHiLoNow and not newDay? lonow:na, color = fuchsia,style=2, title = "D Lo",linewidth=2, transp=20,trackprice = false,offset=0,style=circles,join=true)
plot( (not pivotNow or firstDay) and not nohist and sHiLoNow and not newDay? hinow:na, color = teal,style=2, title = "D Hi",linewidth=2, transp=20,trackprice = false,offset=0,style=circles,join=true)
// Colour in between levels.
// Removed causes issues with next option.
fill(Stdhv3d,Stdhv2d,color=gray,transp=95,title="-3 Stdev Fill")
fill(Stdhv3u,Stdhv2u,color=gray,transp=95,title="+3 Stdev Fill")
fill(Stdhv2d,Stdhv15d,color=blue,transp=95,title="-2 Stdev Fill")
fill(Stdhv2u,Stdhv15u,color=blue,transp=95,title="+2 Stdev Fill")
fill(Stdhv15d,Stdhv1d,color=green,transp=95,title="-1.5 Stdev Fill")
fill(Stdhv15u,Stdhv1u,color=green,transp=95,title="+1.5 Stdev Fill")
fill(Stdhv1d,Stdhv07d,color=lime,transp=95,title="-1 Stdev Fill")
fill(Stdhv1u,Stdhv07u,color=lime,transp=95,title="+1 Stdev Fill")
fill(Stdhv07d,Stdhv05d,color=red,transp=95,title="-0.7 Stdev Fill")
fill(Stdhv07u,Stdhv05u,color=red,transp=95,title="+0.7 Stdev Fill")
fill(Stdhv05d,SettleM,color=purple,transp=92,title="-0.5 Stdev Fill")
fill(Stdhv05u,SettleM,color=purple,transp=92,title="+0.5 Stdev Fill")
//