Distribution of returns using arrays in PRTv11

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #123957 quote
    Vonasi
    Moderator
    Master

    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.

    //Distribution of Returns 
    //PRT v11 only
    //By Vonasi
    //Date: 20200330
    
    defparam drawonlastbaronly = true
    
    //Settings
    //linechart = 0 // 0 = histogram 1 = line chart
    
    //Fill positive gain array
    if close > open[p] then
    riseperc = round((((close - open[p])/open[p])*100),1)*10
    $rise[riseperc] = $rise[riseperc] + 1
    count = count + 1
    endif
    $risepercentage[riseperc] = ($rise[riseperc]/count)*100
    
    //Fill negative loss array
    if close < open[p] then
    fallperc = round((((open[p]-close)/open[p])*100),1)*10
    $fall[fallperc] = $fall[fallperc] + 1
    count = count + 1
    endif
    $fallpercentage[fallperc] = ($fall[fallperc]/count)*100
    
    //Only do the following code on latest bar to speed things up
    if islastbarupdate then
    //Get the latest gain or loss position in arrays
    if close > open[p] then
    thisperc = round((((close - open[p])/open[p])*100),1)*10
    endif
    if close < open[p] then
    thisperc = -round((((open[p]-close)/open[p])*100),1)*10
    endif
    
    //Initiate drawing positions
    lastvalue = $risepercentage[1]
    falllastvalue = 0
    
    //Get highest filled array for auto scaling of x axis
    lastbar = max(lastset($risepercentage),lastset($fallpercentage))
    
    //draw negative side of chart
    for a = lastbar downto 1
    b = a+(lastbar)
    //Display line or histogram depending on setting
    if linechart then
    drawsegment(barindex-b-1, falllastvalue,barindex-b ,$fallpercentage[a])coloured(128,0,0)
    else
    drawrectangle(barindex-b-1, 0,barindex-b ,$fallpercentage[a])coloured(128,0,0)
    endif
    
    //Draw x scale text
    if a mod 10 = 0 then
    mytext = a/10
    drawtext("-#mytext#", barindex-a+1-lastbar,-0.05,sansserif,bold,10)
    endif
    
    //Draw current position on curve if negative
    if thisperc = -a then
    drawpoint(barindex-b ,$fallpercentage[a],3)coloured(128,0,0)
    endif
    
    //Store old value for drawing in next loop round so segment can be drawn
    falllastvalue = $fallpercentage[a]
    //Store highest array value for autoscaling of y axis
    maxval = max(falllastvalue,maxval)
    next
    
    //Connect gap between two sides of line chart
    if linechart then
    drawsegment(barindex-b, falllastvalue,barindex-b+2 ,$risepercentage[1])coloured(128,0,0)
    endif
    
    //draw positive side of chart
    for a = 1 to lastbar
    b = lastbar-a
    
    //Display line or histogram depending on setting
    if linechart then
    drawsegment(barindex-b, lastvalue,barindex-b+1 ,$risepercentage[a])coloured(0,128,0)
    else
    drawrectangle(barindex-b, 0,barindex-b+1 ,$risepercentage[a])coloured(0,128,0)
    endif
    
    //Draw x scale text
    if b mod 10 = 0 then
    mytext = b/10
    drawtext("#mytext#", barindex-a+1,-0.05,sansserif,bold,10)
    endif
    
    //Draw current position on curve if positive
    if thisperc = a then
    drawpoint(barindex-b+1 ,$risepercentage[a],3)coloured(0,128,0)
    endif
    
    //Store old value for drawing in next loop round so segment can be drawn
    lastvalue = $risepercentage[a]
    //Store highest array value for autoscaling of y axis
    maxval = max(lastvalue,maxval)
    next
    endif
    
    //draw x axis label
    drawtext("%", barindex-(lastbar*2)-5,-0.05,sansserif,bold,10)
    
    return  -0.1 coloured(0,0,0,0),maxval coloured(0,0,0,0)
    
    #156511 quote
    Gianluca
    Participant
    Master

    This 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?

    Oliviertrader2020 thanked this post
    #156528 quote
    Vonasi
    Moderator
    Master

    Thanks 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!

    #156609 quote
    Oliviertrader2020
    Participant
    Average

    That’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/)

    #156634 quote
    Vonasi
    Moderator
    Master

    Oliviertrader2020 – 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.
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Distribution of returns using arrays in PRTv11


ProBuilder support

New Reply
Author
author-avatar
Vonasi @vonasi Moderator
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by Vonasi
5 years ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 03/30/2020
Status: Active
Attachments: 2 files
Logo Logo
Loading...