array variables availability in ProRealTime – examples and discussions

Viewing 15 posts - 1 through 15 (of 255 total)
  • Author
    Posts
  • #118804 quote
    Nicolas
    Keymaster
    Master

    The variable arrays will arrive soon in ProRealTime, they will be available in the coming weeks in version 11, with other instructions which I will detail in this topic.
    I consider this to be a major addition to the ProBuilder language and a radical change in the way we program and for this reason, I think that a topic in the forum is much more suitable for offering examples and discuss.

    Do not hesitate to ask your questions if things do not seem clear to you at first, I will try to answer as best I can.

    I will add concrete examples to this topic for future reference.

    As soon as these new features are deployed in public, you can also come here to offer your examples and your questions in the event of necessary debugging concerning variable arrays. There are so many new possibilities now, that 

    I have been testing for 2 weeks and apart from a few small concerns that have been brought up and corrected since, I must say that I was impressed by the quality and precision of the new instructions offered, thank you IT-Finance and ProRealTime, the year 2020 will be an amazing year for coders! 🙂

    In the introduction, I would like to recall what array variable is and why it is so important to be able to use it in programming.


    An array is a special variable, which can hold more than one value at a time.

    If you have a list of values (a list of recent highest high, for example), storing them in single variables could look like this:

    period = 5
    if high[period] >= highest[(period)*2+1](high) then //new highest high
     if hh1=0 then
      hh1 = high[period]
     elsif hh2=0 then 
      hh2 = high[period]
     elsif hh3=0 then
      hh3 = high[period]
     endif
    endif 
    
    return hh1,hh2,hh3

     

    However, what if you want to loop through the highest high and find a specific one? And what if you had not 3 highest high to store, but 300?

    The solution is to create an array!

    An array can hold many values under a single name, and you can access the values by referring to an index number.

    In ProBuilder, variables array are declared with $ sign before their names, so following our previous example, our 3 variables could now be stored like this:

    period = 5
    if high[period] >= highest[(5)*2+1](high) then //new highest high
     if not isset($hh[0]) then
      $hh[0] = high[period]
     elsif not isset($hh[1]) then
      $hh[1] = high[period]
     elsif not isset($hh[2]) then
      $hh[2] = high[period]
     endif
    endif 
    
    return $hh[0],$hh[1],$hh[2]

     

     

    Note that an array always begin at 0.

    This is how you should represent how the values are stored in an array:


    Each index represent a column of the table where you put a value that you will be able to access anytime and anywhere in your code.
    An index which hasn’t received any value yet is equal to 0.

    More dynamically, we could store values in an array like this:

    period = 5
    if high[period] >= highest[(period)*2+1](high) then //new highest high
     $hh[myindex] = high[period] //store highest high
     myindex=myindex+1 //increase the index of the array for next highest high store 
    endif

     

    In this example, each time a new highest high is detected, we store its new value into a new column of the array. By using the instruction lastset(), we retrieve the last known column of the array, so by adding 1 to this number, we add dynamically a new column in order to store a new value. Don’t worry about the size of the array, it can handle 1 million index! Easy!

    Ready for our first example? See below.

    MPL, Bel, pompboss and 17 others thanked this post
    #118817 quote
    Nicolas
    Keymaster
    Master

    List of all examples available in this topic:


    Example #1: support and resistance example, based on fractals points

    This example calculate dynamically support and resistance zones based on 2 fractals found within a defined distance in percentage of price.

    Each time a new fractal is found, the code stores the value and the barindex in 2 separate arrays (2 variables for tops and 2 other variables for bottoms).

    On the last bar of the price chart, the code makes a loop through each index of the array, it selects the first index to compare it with all the other ones and if the distance in percentage is within the allowed threshold, then a new support or resistance zone is created. Then it moves to the next index and repeat and continue the loop. Note that I used nested loops in order to make the comparison.


    The code embed these new instructions:

    LastSet : Returns the last index of the array defined by the code. If no index was previously defined, the function returns -1.

    Example: LASTSET($Array)

    IsSet : Returns 1 if the index of the array has already been set in the code, otherwise returns 0.

    Example: ISSET($MyArray[Index])

    DrawPoint:Draws a point on the chart.

    Example: DRAWPOINT (x,y,size) COLOURED (R,V,B,a) BORDERCOLOR  (R,V,B,a)


    You can also notice that it is now possible to plot in the future of the chart (at the right of the curent price) by using BARINDEX+x (or with DateToBarIndex )

    // https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
    // (please do not remove the link above for future reference)
    // Example #1: support and resistance example, based on fractals points
    defparam drawonlastbaronly=true
    
    // --- settings 
    fractalP = 10
    percent = 0.5
    barlimit = 500
    // --- end of settings 
    
    //fractals
    cp=fractalP
     if high[cp] >= highest[(cp)*2+1](high) then //new fractal high found
      $TOPy[lastset($TOPy)+1] = high[cp] //store fractal value 
      $TOPx[lastset($TOPx)+1] = barindex[cp] //store fractal barindex
    endif
    
    if low[cp] <= lowest[(cp)*2+1](low)  then //new fractal low found
      $BOTy[lastset($BOTy)+1] = low[cp] //store fractal value 
      $BOTx[lastset($BOTx)+1] = barindex[cp] //stire fractal barindex
    endif
    
    if(islastbarupdate and isset($topy[0]) and isset($boty[0])) then
     //check points in a range of X percent
     for i = 0 to lastset($TOPy) do //loop through the tops
      for y = 0 to lastset($TOPy) do //check first top with other tops
       change=abs(($topy[y]-$topy[i])/$topy[i]) //percent range between the 2 tops
       if change<=percent/100 and barindex-$TOPx[y]<barlimit and $topx[i]<>$topx[y] then
        if close>min($topy[y],$topy[i]) then //define the color
         r=0
         g=255
        else
         r=255
         g=0
        endif
        //plot points at each tops
        DRAWPOINT($topx[i],$topy[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
        DRAWPOINT($topx[y],$topy[y],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
        midlevel=($topy[i]+$topy[y])/2
        //display the mid level price
        DRAWTEXT("#midlevel#", barindex+8, midlevel, monospaced, standard, 14)
        //plot the zone
        drawrectangle(min($topx[y],$topx[i]),$topy[y],barindex,$topy[i]) coloured(r,g,50,50) bordercolor(r,g,0)
       endif
      next
     next
     for i = 0 to lastset($BOTy) do //loop through the bottoms
      for y = 0 to lastset($BOTy) do //check first bottom with other bottoms
       change=abs(($boty[y]-$boty[i])/$boty[i]) //percent range between the 2 bottoms
       if change<=percent/100 and barindex-$BOTx[y]<barlimit and $BOTx[i]<>$BOTx[y] then
        if close<max($boty[y],$boty[i]) then //define the color
         r=255
         g=0
        else
         r=0
         g=255
        endif
        DRAWPOINT($botx[i],$boty[i],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
        DRAWPOINT($botx[y],$boty[y],2) COLOURED (r,g,0) BORDERCOLOR (r,g,0)
        midlevel=($boty[i]+$boty[y])/2
        DRAWTEXT("#midlevel#", barindex+8, midlevel, monospaced, standard, 14)
        drawrectangle(min($botx[y],$botx[i]),$boty[y],barindex,$boty[i]) coloured(r,g,50,50) bordercolor(r,g,0)
       endif
      next
     next
    endif
    
    return

    Feel free to fork the above code for your own needs (and please share it! 😉 ). The code is also attached below as an ITF file for convenience.

    Very nice fork of this code, by stefou102 can also be found here: Support and resistance zones with variables arrays

    Al, MPL, Bel and 11 others thanked this post
    #118818 quote
    Nicolas
    Keymaster
    Master

    Example #2: probability cone

    In this second example I created a probability cone with values stored in 2 separated variables array.

    The cone is plotted into the future of the price by using the 2 arrays. The start of the probability cone can be adjusted to start in the past (decay).

    Note that the arrays $u and $d are populated dynamically directly in a loop on the current candlestick.

    The code is continuously plotting the median high and median low values of the cone by using outside the loop the arraymin and arraymax instructions of $u and $d.

    // https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
    // (please do not remove the link above for future reference)
    // Example #2: probability cone
    
    defparam drawonlastbaronly=true
    
    // --- settings
    PriceField = 1 //1=customclose ; 0=high/low
    vol = 0.025 //Assumed Volatility (ideally based on volTyp)
    BarsFuture = 20 //No. of days ahead
    stdPer = 2 //Std. Dev. quantity
    decay = 12 //decay the cone at the previous X bar
    // --- end of settings
    
    Price = customclose
    
    hh=undefined
    ll=undefined
    if(islastbarupdate) then
     for i = 0 to BarsFuture do
    
      //cone calculation
      StdDev = StdPer *  Price * Vol * sqrt( i / 365 )
      if( PriceField ) then
       $u[i] = high[decay] + stdDev
       $d[i] = low[decay] - stdDev
      else
       $u[i] = Price[decay] + stdDev
       $d[i] = Price[decay] - stdDev
      endif
    
      if i>0 then
       //upper cone
       drawsegment(barindex[decay]+i,$u[i],barindex[decay]+i-1,$u[i-1]) coloured(0,255,0)
       drawpoint(barindex[decay]+i,$u[i],1) coloured(0,255,0)
       //lower cone
       drawsegment(barindex[decay]+i,$d[i],barindex[decay]+i-1,$d[i-1]) coloured(255,0,0)
       drawpoint(barindex[decay]+i,$d[i],1) coloured(255,0,0)
       //filling the zone
       drawtriangle(barindex[decay]+i,$u[i],barindex[decay]+i-1,$u[i-1],barindex[decay]+i,$d[i]) coloured(150,150,150,50) bordercolor(150,150,150,0)
       drawtriangle(barindex[decay]+i-1,$d[i-1],barindex[decay]+i,$d[i],barindex[decay]+i-1,$u[i-1]) coloured(150,150,150,50) bordercolor(150,150,150,0)
      endif
    
     next
     //plot the middle line
     drawsegment(barindex[decay],($u[0]+$d[0])/2,barindex[decay]+BarsFuture,($u[0]+$d[0])/2) style(dottedline)
     //plot the highest and lowest boundaries of the cone
     hh=(arraymax($u)+arraymin($u))/2
     ll=(arraymax($d)+arraymin($d))/2
    endif
    
    return hh, ll
    

     

    It was not really necessary to use arrays to plot the cone because we do it in the loop on each iteration, but the purpose of the example was to show how to populate arrays inside a loop and use their values outside of it.

    The “vol” variable should be set with implied volatility of the instrument, but you can also use either historic volatility or even average true range factorized.

    ginko, Bard, LaCrevett, Pepsmile and rrmp12 thanked this post
    #118819 quote
    Nicolas
    Keymaster
    Master

    Example #3: separate buy/sell volumes on the same candle

    With variable array, it is now possible to store values and to re-use it inside the same bar. Previously, it was not possible to refer to the last value because variables were reset on each new tick received from the market.

    IMPORTANT: arrays are not historized, they are decorrelated from the BARINDEX, it means that the arrays value from X bars ago is not saved in memory. It is not a drawback since you can save up to 1 million index which is the maximum of data history allowed now with version 11. Also, you can save your index into a normal variable and refer to it with its barindex offset (see example below, the ‘delta’ variable is used to store the difference of the 2 arrays, so that we can use it to compute an average over the last X periods!).

    In this example, we use only 1 index / value for each variable array, to benefit from their “memories” on the current bar, in order to “separate” the bullish from the bearish volumes. By making the difference between the 2 arrays, we can make a sort of accurate cumulative delta and usable in any timeframe from ticks to daily and beyond.

    // https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
    // (please do not remove the link above for future reference)
    // Example #3: separate buy/sell volumes on the same candle 
    
    if not isset($upvol[0]) then 
     $upvol[0] = 0
     $dnvol[0] = 0
    endif
    
    if islastbarupdate then
     if close>$lastclose[0]then 
      $upvol[0]=$upvol[0]+volume
     elsif close<$lastclose[0] then
      $dnvol[0]=$dnvol[0]+volume
     endif
     $lastclose[0]=close
     delta=($upvol[0]-$dnvol[0])
    endif
    avg=average[10](delta)
    
    return $upvol[0] coloured(0,255,255) style(histogram) as "up volumes", -$dnvol[0] coloured(255,0,0) style(histogram) as "down volumes", delta style(line,3), avg
    

     

    ginko thanked this post
    #118905 quote
    Rory Dryden
    Participant
    New

    In the probability cone example (Example 2) an error occurs on lines 25, 26, 28, 29 and 30.  A null error.

    It would seem like a very useful indicator if I could make it work.

    Perhaps this has not been implemented yet.

     

    Rory.

    ginko thanked this post
    #118911 quote
    Vonasi
    Moderator
    Master

    The variable arrays will arrive soon

    #118920 quote
    Nicolas
    Keymaster
    Master

    Indeed, this topic was created to prepare coders before the official public release of arrays 🙂

    I think that it is necessary to set milestones so that people take ownership of this new way of programming, which is not easy to define for those who have never programmed with variables arrays before. I may not have chosen the simplest examples, I will try to add others a little more accessible.

    #118929 quote
    Nicolas
    Keymaster
    Master

    Example #4: previous days pivot point

    In this simple example, the code is storing the daily pivot points values into an array. Each new day, the current pivot points is put into a new index of the table. That way, we can get at any time and without making new calculation, the X previous day(s) pivot points value (with DaysBack setting). It is as if we are creating our own constants that we can reuse at any time later in our code, therefore the possibilities are numerous!

    // https://www.prorealcode.com/topic/array-variables-availability-in-prorealtime/
    // (please do not remove the link above for future reference)
    // Example #4: previous days pivot point
    
    // --- settings 
    DaysBack = 3
    // --- end of settings 
    
    if dclose(1)<>lastclose then //new day begin 
     index = lastset($pp)+1 //increment the index to save the current day pivot point 
     $pp[index] = (dhigh(1)+dlow(1)+dclose(1))/3 //save the pivot point of the day in new index of the array
     lastclose = dclose(1) //save the current Dclose(1) value to future check of new day
    endif 
    
    //get the pivot point for the Y previous day before now
    mypivot = $pp[max(0,index-DaysBack)] //added MAX keyword to verify the result of the subscract is not inferior to 0
    
    RETURN mypivot style(line,3)
    
    

    In the attached picture, the “mypivot” line (thick white line) is plotting the pivot point from 2 days ago (DaysBack = 2).

    MPL and ginko thanked this post
    #118958 quote
    Rory Dryden
    Participant
    New

    Thank you, I thought so.

    #119591 quote
    MPL
    Participant
    Average

    Hello Nicolas, Thank you very much for your feedback on the upcoming arrival of the arrays and the possibilities they will offer. I’m going to read it calmly and calmly tomorrow morning but after a first scan of your messages, it really looks very promising!

    #119600 quote
    Nicolas
    Keymaster
    Master

    @MPL Thanks, I translated your post in English.

    I had not a lot of time this week to try and implement new things with arrays and I’ll certainly do next week!

    #119925 quote
    JMat45
    Participant
    Senior

    This is great news. I would like to percentile rank price moves (of say DAX Futures) from 8.00am (UK time) to 8.00am + x minutes. Until now, without arrays, it’s been virtually impossible.

    #120021 quote
    MPL
    Participant
    Average

    Hello everyone, hello Nicolas,

    1. I have read and understood your examples, very interesting, thank you very much
    2. Do you know what will be the date of the release which will allow us to have arrays
    3. Do you know if there will be instructions to facilitate the manipulation of arrays :
    – Ex1: rank array in ascending or descending order (trier un tableau)
    – Ex2: merge 2 arrays
    – …

    Point 3 is not very important since it will always be possible to do this “by hand” with a few loops …

    Thank you

    #120024 quote
    Nicolas
    Keymaster
    Master

    I think that we are closed to a release in version 11. I already asked for an “array sort” instruction which is not available, it will be added but I don’t know when.

    MPL thanked this post
    #120531 quote
    grivoire
    Participant
    New

    Hello Nicolas,

    This topic on the tables to come seems super interesting, and risks revolutionizing our indicators (support / resistance – trend lines / number of times affected … etc) do you have any news concerning this long awaited shift?

    Thanking you in advance,

     

    Vincent

Viewing 15 posts - 1 through 15 (of 255 total)
  • You must be logged in to reply to this topic.

array variables availability in ProRealTime – examples and discussions


ProBuilder support

New Reply
Author
author-avatar
Nicolas @nicolas Keymaster
Summary

This topic contains 254 replies,
has 50 voices, and was last updated by robertogozzi
1 year, 3 months ago.

Topic Details
Forum: ProBuilder support
Language: English
Started: 02/06/2020
Status: Active
Attachments: 59 files
Logo Logo
Loading...