Pine Script_Calmar Ratio Custom Range
Forums › ProRealTime English forum › ProBuilder support › Pine Script_Calmar Ratio Custom Range
-
-
04/22/2025 at 12:49 PM #246216
This indicator aims to provide a risk-adjusted return statistic for a security over a selected period. Return is measured as CAGR and risk as Max Drawdown. The ratio of these two gives the Calmar Ratio. It works on any timeframe or range.
Calmar Ratio Custom Range on TradingView (open-source script)
It would be great if someone could convert it to ProBuilder.
04/24/2025 at 10:50 AM #246329Here you have:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091//-------------------------------------------------------////PRC_Calmar Ratio//version = 0//23.04.25//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledgedefparam drawonlastbaronly=true//-----------------------------------------------//// Inputs//-----------------------------------------------//startDate=20250207startTime=100000endDate=20250407endTime=150000//-----------------------------------------------//// Calculate x1,y1 and x2,y2//-----------------------------------------------//tm = gettimeframeif tm >= 86400 thenif opendate=startdate thenidxStart=barindexValueStart=closeendifif opendate=endDate thenidxEnd=barindexValueEnd=closeendifelseif opendate=startdate and opentime=startTime thenidxStart=barindexValueStart=closeendifif opendate=endDate and opentime=endTime thenidxEnd=barindexValueEnd=closeendifendifif ValueEnd>ValueStart thenr=0g=255b=0elser=255g=0b=0endif//-----------------------------------------------//// Draw and calculations//-----------------------------------------------//if startDate>=EndDate thendrawtext("You need more than 1 day between 2 points",0,-100)anchor(top,xshift,yshift)elsif ValueStart=0 or ValueEnd=0 thendrawtext("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)elseif islastbarupdate thenn1=barindex-idxStartn2=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)//-----------------------------------------------//daysBetween=endDate-startDateiYears=daysBetween/365cagr = round((pow(ValueEnd/ValueStart,(1/iyears))-1)*100,2)//-----------------------------------------------//peak=ValueStartmaxDD=0for i=n1 downto n2 doif close[i]>peak thenpeak=close[i]endifdd=(peak-close[i])/peak*100if dd>maxDD thenmaxDD=ddendifnext//-----------------------------------------------//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)endifendifreturn04/24/2025 at 3:09 PM #246347Now it’s better than the previous one.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109//-------------------------------------------------------////PRC_Calmar Ratio//version = 0//23.04.25//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//-------------------------------------------------------//defparam drawonlastbaronly=true//-----------------------------------------------//// Inputs//-----------------------------------------------//startDate=20250207startTime=100000endDate=20250407endTime=150000//-----------------------------------------------//// Calculate x1,y1 and x2,y2//-----------------------------------------------//tm = gettimeframeif tm >= 86400 thenif opendate=startdate thenidxStart=barindexValueStart=closeendifif opendate=endDate thenidxEnd=barindexValueEnd=closeendifelseif opendate=startdate and opentime=startTime thenidxStart=barindexValueStart=closeendifif opendate=endDate and opentime=endTime thenidxEnd=barindexValueEnd=closeendifendifif ValueEnd>ValueStart thenr=0g=255b=0elser=255g=0b=0endif//-----------------------------------------------//// Draw and calculations//-----------------------------------------------//if startDate>=EndDate thendrawtext("You need more than 1 day between 2 points",0,-100)anchor(top,xshift,yshift)elsif ValueStart=0 or ValueEnd=0 thendrawtext("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)elseif islastbarupdate thenn1=barindex-idxStartn2=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=ValueStartmaxDD=0bars=0for i=n1 downto n2 doif tm>=86400 thenbars=bars+1elseif openday[i]<>openday[i+1] then//intradaybarindex[i]=0 thenbars=bars+1elsebars=barsendifendifif close[i]>peak thenpeak=close[i]endifdd=(peak-close[i])/peak*100if dd>maxDD thenmaxDD=ddendifnext//-----------------------------------------------////---Check timeframe for CAGR calculation--------//if tm = 2592000 then //monthly timeframen = 12elsif tm = 604800 then //weekly timeframen = 52elsif tm <=86400 then //Other timeframes (daily or less)n = 252endifiYears=bars/ncagr = 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)endifendifreturn04/27/2025 at 12:51 PM #246468Thank you Iván for the conversion — I have a few suggestions:
-
The indicator currently has 4 manual inputs, while the TradingView version uses just 2 and allows easy selection of dates by directly clicking on the candles.
-
It only works on the daily timeframe; on others, it shows ‘n/d’ or this message: “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.”
In the Pine Script version, date selection is straightforward on any timeframe. It also offers the ability to divide the CAGR by the Max DD since inception.
I recommend checking how it works on TradingView to see it in action.
04/28/2025 at 9:25 AM #246477If 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.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117//-------------------------------------------------------////PRC_Calmar Ratio//version = 0//23.04.25//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//-------------------------------------------------------//defparam drawonlastbaronly=true//-----------------------------------------------//// Inputs//-----------------------------------------------//startDate=20250207startTime=100000endDate=20250407endTime=150000showbeg=1//-----------------------------------------------//// Calculate x1,y1 and x2,y2//-----------------------------------------------//tm = gettimeframeif showbeg thenidxStart=0ValueStart=close[barindex]idxEnd=barindexValueEnd=closeelseif tm >= 86400 thenif opendate=startdate thenidxStart=barindexValueStart=closeendifif opendate=endDate thenidxEnd=barindexValueEnd=closeendifelseif opendate=startdate and opentime=startTime thenidxStart=barindexValueStart=closeendifif opendate=endDate and opentime=endTime thenidxEnd=barindexValueEnd=closeendifendifendifif ValueEnd>ValueStart thenr=0g=255b=0elser=255g=0b=0endif//-----------------------------------------------//// Draw and calculations//-----------------------------------------------//if startDate>=EndDate thendrawtext("You need more than 1 day between 2 points",0,-100)anchor(top,xshift,yshift)elsif ValueStart=0 or ValueEnd=0 thendrawtext("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)elseif islastbarupdate thenn1=barindex-idxStartn2=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=ValueStartmaxDD=0bars=0for i=n1 downto n2 doif tm>=86400 thenbars=bars+1elseif openday[i]<>openday[i+1] then//intradaybarindex[i]=0 thenbars=bars+1elsebars=barsendifendifif close[i]>peak thenpeak=close[i]endifdd=(peak-close[i])/peak*100if dd>maxDD thenmaxDD=ddendifnext//-----------------------------------------------////---Check timeframe for CAGR calculation--------//if tm = 2592000 then //monthly timeframen = 12elsif tm = 604800 then //weekly timeframen = 52elsif tm <=86400 then //Other timeframes (daily or less)n = 252endifiYears=bars/ncagr = 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)endifendifreturn04/28/2025 at 8:42 PM #246495I understand that choosing start and end dates by candle is not feasible in ProRealTime. I also believe there might be a quicker way than using 4 inputs — maybe just 2 would be enough (with hours either predefined or detected based on the timeframe?).
The next step would be to screen for it (calculating a rolling Calmar Ratio with a custom lookback period in ProScreener).
This could help identify the best-performing stocks over the last 1M, 3M, or 6M periods (for example) on a risk-adjusted basis — similar to a momentum strategy, but adjusting raw returns by risk (where risk is = Max DD here).
Let me know what you think about it.
Note: Yesterday, when I was changing inputs on the daily timeframe, it was working fine. Now, it no longer works (even when trying with the previous code). I change the date, but nothing happens. I’m new to this platform, and I don’t really understand how it could work yesterday but not today…
04/29/2025 at 8:17 AM #246507Of course, you can set a predefined time and only modify the dates.
You can adapt it however you want.
Regarding the screener, when you start working on it, keep in mind that depending on whether you have the complete version or the premium version, you will be able to reference back 255 bars or 1024 bars, respectively. -
-
AuthorPosts
Find exclusive trading pro-tools on