If you want to perform the calculation from the beginning, then you need to add the boolean variable showbeg and change it depending on whether we want to select 2 dates or take all historical data.
Regarding selecting dates from the chart by clicking on the candlestick, this is not possible in ProRealTime.
//-------------------------------------------------------//
//PRC_Calmar Ratio
//version = 0
//23.04.25
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//-------------------------------------------------------//
defparam drawonlastbaronly=true
//-----------------------------------------------//
// Inputs
//-----------------------------------------------//
startDate=20250207
startTime=100000
endDate=20250407
endTime=150000
showbeg=1
//-----------------------------------------------//
// Calculate x1,y1 and x2,y2
//-----------------------------------------------//
tm = gettimeframe
if showbeg then
idxStart=0
ValueStart=close[barindex]
idxEnd=barindex
ValueEnd=close
else
if tm >= 86400 then
if opendate=startdate then
idxStart=barindex
ValueStart=close
endif
if opendate=endDate then
idxEnd=barindex
ValueEnd=close
endif
else
if opendate=startdate and opentime=startTime then
idxStart=barindex
ValueStart=close
endif
if opendate=endDate and opentime=endTime then
idxEnd=barindex
ValueEnd=close
endif
endif
endif
if ValueEnd>ValueStart then
r=0
g=255
b=0
else
r=255
g=0
b=0
endif
//-----------------------------------------------//
// Draw and calculations
//-----------------------------------------------//
if startDate>=EndDate then
drawtext("You need more than 1 day between 2 points",0,-100)anchor(top,xshift,yshift)
elsif ValueStart=0 or ValueEnd=0 then
drawtext("Check the start date you entered. It might be that you don’t have enough bars loaded.Or maybe you entered a day/time when the market was closed",0,-100)anchor(top,xshift,yshift)
else
if islastbarupdate then
n1=barindex-idxStart
n2=barindex-idxEnd
//-----------------------------------------------//
drawpoint(idxStart,ValueStart,5)coloured(r,g,b,30)
drawpoint(idxEnd,ValueEnd,5)coloured(r,g,b,30)
drawpoint(idxStart,ValueStart,2)coloured(r,g,b)
drawpoint(idxEnd,ValueEnd,2)coloured(r,g,b)
drawsegment(idxStart,ValueStart,idxEnd,ValueEnd)coloured(r,g,b)
//-----------------------------------------------//
drawvline(barindex[n1])style(dottedline)
drawvline(barindex[n2])style(dottedline)
//-----------------------------------------------//
peak=ValueStart
maxDD=0
bars=0
for i=n1 downto n2 do
if tm>=86400 then
bars=bars+1
else
if openday[i]<>openday[i+1] then//intradaybarindex[i]=0 then
bars=bars+1
else
bars=bars
endif
endif
if close[i]>peak then
peak=close[i]
endif
dd=(peak-close[i])/peak*100
if dd>maxDD then
maxDD=dd
endif
next
//-----------------------------------------------//
//---Check timeframe for CAGR calculation--------//
if tm = 2592000 then //monthly timeframe
n = 12
elsif tm = 604800 then //weekly timeframe
n = 52
elsif tm <=86400 then //Other timeframes (daily or less)
n = 252
endif
iYears=bars/n
cagr = round((pow(ValueEnd/ValueStart,(1/iyears))-1)*100,2)
//-----------------------------------------------//
MaxDrawDown=round(-maxDD,2)
Calmar=round(cagr/maxDD,2)
drawtext("CAGR=#cagr#%",-100,-100)anchor(topright,xshift,yshift)
drawtext("MaxDD=#MaxDrawDown#%",-100,-120)anchor(topright,xshift,yshift)
drawtext("Calmar=#Calmar#",-100,-140)anchor(topright,xshift,yshift)
endif
endif
return