Distribution of returns using arrays in PRTv11
Forums › ProRealTime English forum › ProBuilder support › Distribution of returns using arrays in PRTv11
- This topic has 4 replies, 3 voices, and was last updated 3 years ago by Vonasi.
Tagged: distribution of returns
-
-
03/30/2020 at 1:51 PM #123957
I coded a distribution of returns indicator for v10.3 a while back and it was pretty slow and very clunky and I never really did get the auto scaling to work how I wanted it to plus the number of bars in the chart were limited by my patience for repetitive coding. Now that we have arrays in PRTv11 I decided to have another go. The results are much better!
The indicator will analyse all of the history on the price chart and every time price is above or below price p bars ago it adds one to an array bucket for the exact percentage of price difference. The array buckets are 0.1% in size. Using LASTSET we are able to now easily auto scale the x axis and using ISLASTBARUPDATE the indicator is super fast to draw.
The distribution of returns curve can be drawn as either a line chart or a histogram with the ‘LineChart’ setting.
The current position on the curve that price is compared to price p bars ago is also displayed by a drawn point.
I’ve put comments in the code to help everyone understand how the code works a little better. I will submit the indicator to the library.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108//Distribution of Returns//PRT v11 only//By Vonasi//Date: 20200330defparam drawonlastbaronly = true//Settings//linechart = 0 // 0 = histogram 1 = line chart//Fill positive gain arrayif close > open[p] thenriseperc = round((((close - open[p])/open[p])*100),1)*10$rise[riseperc] = $rise[riseperc] + 1count = count + 1endif$risepercentage[riseperc] = ($rise[riseperc]/count)*100//Fill negative loss arrayif close < open[p] thenfallperc = round((((open[p]-close)/open[p])*100),1)*10$fall[fallperc] = $fall[fallperc] + 1count = count + 1endif$fallpercentage[fallperc] = ($fall[fallperc]/count)*100//Only do the following code on latest bar to speed things upif islastbarupdate then//Get the latest gain or loss position in arraysif close > open[p] thenthisperc = round((((close - open[p])/open[p])*100),1)*10endifif close < open[p] thenthisperc = -round((((open[p]-close)/open[p])*100),1)*10endif//Initiate drawing positionslastvalue = $risepercentage[1]falllastvalue = 0//Get highest filled array for auto scaling of x axislastbar = max(lastset($risepercentage),lastset($fallpercentage))//draw negative side of chartfor a = lastbar downto 1b = a+(lastbar)//Display line or histogram depending on settingif linechart thendrawsegment(barindex-b-1, falllastvalue,barindex-b ,$fallpercentage[a])coloured(128,0,0)elsedrawrectangle(barindex-b-1, 0,barindex-b ,$fallpercentage[a])coloured(128,0,0)endif//Draw x scale textif a mod 10 = 0 thenmytext = a/10drawtext("-#mytext#", barindex-a+1-lastbar,-0.05,sansserif,bold,10)endif//Draw current position on curve if negativeif thisperc = -a thendrawpoint(barindex-b ,$fallpercentage[a],3)coloured(128,0,0)endif//Store old value for drawing in next loop round so segment can be drawnfalllastvalue = $fallpercentage[a]//Store highest array value for autoscaling of y axismaxval = max(falllastvalue,maxval)next//Connect gap between two sides of line chartif linechart thendrawsegment(barindex-b, falllastvalue,barindex-b+2 ,$risepercentage[1])coloured(128,0,0)endif//draw positive side of chartfor a = 1 to lastbarb = lastbar-a//Display line or histogram depending on settingif linechart thendrawsegment(barindex-b, lastvalue,barindex-b+1 ,$risepercentage[a])coloured(0,128,0)elsedrawrectangle(barindex-b, 0,barindex-b+1 ,$risepercentage[a])coloured(0,128,0)endif//Draw x scale textif b mod 10 = 0 thenmytext = b/10drawtext("#mytext#", barindex-a+1,-0.05,sansserif,bold,10)endif//Draw current position on curve if positiveif thisperc = a thendrawpoint(barindex-b+1 ,$risepercentage[a],3)coloured(0,128,0)endif//Store old value for drawing in next loop round so segment can be drawnlastvalue = $risepercentage[a]//Store highest array value for autoscaling of y axismaxval = max(lastvalue,maxval)nextendif//draw x axis labeldrawtext("%", barindex-(lastbar*2)-5,-0.05,sansserif,bold,10)return -0.1 coloured(0,0,0,0),maxval coloured(0,0,0,0)5 users thanked author for this post.
01/05/2021 at 8:24 PM #156511This is one of the most usefull instrument of the forum!
Can i ask if is possible to do this distribution of returns in Standard Deviation?
1 user thanked author for this post.
01/05/2021 at 10:11 PM #156528Thanks for the compliments. I just replied to your comment on the library post saying that it didn’t work! I assume all is good now and it was user error?
As for basing the curves on standard deviations I will have to consider how to do this. I coded this almost ten months ago so I will have to remind myself of how the code works first! Strangely I found myself coding something similar yesterday before realising that I had already created an indicator to do almost the same thing. I must be losing some brain cells!
01/06/2021 at 3:23 PM #156609That’s exactly what I’m looking for, an indicator with lines (like the pivot point indicator) that would indicate 1, 2 and 3 standard deviations (https://www.prorealcode.com/topic/movement-indicator/)
01/06/2021 at 5:01 PM #156634Oliviertrader2020 – Yes you have already asked for that in the link that you have given. Asking twice is just double posting!
- Do not double post. Ask your question only once and only in one forum. All double posts will be deleted anyway so posting the same question multiple times will just be wasting your own time and will not get you an answer any quicker. Double posting just creates confusion in the forums.
-
AuthorPosts
Find exclusive trading pro-tools on