array variables availability in ProRealTime – examples and discussions

Viewing 15 posts - 181 through 195 (of 255 total)
  • Author
    Posts
  • #189500 quote
    PeterSt
    Participant
    Master

    Sorry, I dont get it, I just made a test wit the code you provided and the result is 99:

    Haha, it well could be a language issue (my English). Your code :

    $Arr[0] = 89
    $Arr[1] = 200
     
    MyVar = 10
    MyVar = MyVar + $Arr[0]
     
    a=1
    if a=0 then 
     buy at market 
    endif 
     
    graph myvar

    does not comply to my expression about “across bars” (I said it differently before). Thus, try this (I did not test it today) :

    Once ArraySet = 0
    
    If not ArraySet then
      $Arr[0] = 89
      $Arr[1] = 200
      ArraySet = 1
    endif
     
    MyVar = 10
    MyVar = MyVar + $Arr[0]
     
    a=1
    if a=0 then 
     buy at market 
    endif 
     
    graph myvar   // Should graph 99 one time. All of the next times 10.

    And thus the issue :

    • ArraySet is sustained for its value
    • $Arr is “new” at the next bar-call (call of the code).

     

    If I am right (after all) I should show you a most useful code/application with arrays, which I made in vain because the arrays are not sustained. I made it in vain because natural thinking makes me do it, even after I knew and made that topic about it (~ 2 months back – “What arrays can *not* be used for”).

    davidguerreir thanked this post
    #189504 quote
    robertogozzi
    Moderator
    Master

    @PeterSt
    Your comment is “// Should graph 99 one time. All of the next times 10
    No, array elements keep any retained value until you change it purposely, like any variable.
    The sole difference is that if you change a variable, you can still access its previous value with and index, such as MyVar[1], MyVar[2], etc…, while array elements CANNOT, you can’t access a prior value of an array element by using, say, $Arr[0][1] to retrieve the value that element $array[0] retained the prior bar!

    #189511 quote
    Nicolas
    Keymaster
    Master

    Can you post a description of an example of when you want an array to be totally erased at any new bar? It could be done, but you have to code it!

    You mentioned that ProBuilder is not handling arrays like any other programming languages, because values are persistent other time. I do not know any language where arrays are automatically erased without any explicit instruction to do it?

    #189513 quote
    PeterSt
    Participant
    Master

    No, array elements keep any retained value until you change it purposely, like any variable.

    Even if I read that a 100 times I don’t understand the message in it. Maybe rephrase because the semantics seem so-so to begin with (I will try to learn Italian).
    But apparently you guys don’t understand my messages either. 🙂

    No array elements retain their value, unlike normal variables do.
    (from bar to bar)

    That should be correct (correct semantics maybe not – haha)

    #189514 quote
    Nicolas
    Keymaster
    Master

    ARRAY ELEMENTS RETAIN THEIR VALUES LIKE ANY OTHER VARIABLE DO.

    (no italian or french words here) 😆

    $var[0] = 1000
    //

    $var[0] will always keep 1000 as its value until you want to replace it with another one. From bar to bar or even in the same bar.

    #189527 quote
    PeterSt
    Participant
    Master
    OK, you are right. It works. I guess there are just too many pitfalls and our mutual examples keep on being incorrect (mine too !).
    With a larger array-application, always something goes wrong (not working) because of the strange way how to deal with them. This is for that other topic, so never mind that for now (I just try to find excuses 🙂 🙂 ).
    Anyway, it requires 100% literal examples, or else something differently happens. Thus your last example
    $var[0] = 1000

    will always show 1000 indeed, but not because it is going across bars. You just assign it newly each time in this example (it requires an If not to do that).
    I repeat : my own examples also work out differently. But a lot is based on mistakes, where you don’t correct me on. Example (implied in my earlier example) :

    // Suppose these are THE ONLY lines in a Strategy :
    
    $Arr[2] = $Arr[2] + 1000   // This errors-out internally.
    aaa = aaa + 1              // But this is fine (which I personally hate, but alas).
    
    Graph $Arr[2]   // Graphs nothing because it errored out earlier.
    Graph aaa       // Graphs 1, 2, 3, 4, ...

    As you can see this is not consistent behaviour and I never saw that (I never recognised it).

    This code would be correctly working (tested this time) :

    Once ArraySet = 0
    
    If not ArraySet then   // THIS ONE !
      $Arr[0] = 89   // So Yes, this value is retained.
      $Arr[1] = 200
      ArraySet = 1
    endif
    
    MyVar = 10
    MyVar = MyVar + $Arr[0]
    
    Graph MyVar   // Always graphs 99.

    I like to bring across that this is enormously ambiguous coding because it requires nothing much to let the line denoted “THIS ONE !” to go wrong on the coding side (I forget line one, for example or anything else that makes the If work out to False always) and the code at this line :

    MyVar = MyVar + $Arr[0]

    will just not do its job. And, if I use arrays like your examples (everywhere I’d say, including the last posts) then you use them locally (within the one bar) and by nature that would not fail. This is because I won’t need any If Not ArraySet stuff.

    So …

    @fhorus, I don’t understand what your issue is, because all just works (LOL). But I hope that Nicolas sees that his last reply to fhorus is again with an example where the array is used “locally”. And this way we will never understand.
    With my way of throwing confusement at you, also not.


    The ONCE should be used to define a variable such that its scope is always under the definition. This is ambiguous :

    If not ArraySet then
      $Arr[0] = 89 
      $Arr[1] = 200 
      ArraySet = 1 
    endif
    
    // What if the code in the If is never executed ?
    // We would have unexpected results in :
    
    bb = $Arr[0]
    
    // and all what further uses bb (or $Arr). And no error message anywhere.

    Now this :

    Once $Arr[]   // This initialises all array elements at 0. But which ? so there's more to it (Dim command).
    
    If not ArraySet then
      $Arr[0] = 89 
      $Arr[1] = 200 
      ArraySet = 1 
    endif
    
    // Now nothing is ambiguous.

    So the above (last code) can not be done because PRT does not allow for it.

    Lastly :

    Once A = 0   // If I would leave this out, I'd kill myself on the next occasion.
    
    A = A + 1   // Count across bars.

    So you guys come up with examples leaving out the first line and deem it fine.
    It is of course PRT providing the option (not for arrays !) so we can wonder who is “wrong” to begin with.

    Thus @fhorus, with the correct examples everybody turns out to be correct. But the main thing is : it just works as you (and me) want it to. Not with the ONCE but with some kind of decent If.

    Apologies for all the confusing stuff.  Luckily I was somewhat cautious. 🙁
    This was the longest post possible to state that I was wrong after all.

    #189530 quote
    Nicolas
    Keymaster
    Master

    Ok, to conclude for everyone, you can just assign a value in a column of an array, just one time, and it will last forever.

    For example, use ISSET:

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

    if not isset($var[0]) then 
     $var[0] = 1000
    endif
    #189532 quote
    fhornus
    Participant
    Junior

    Thank you Nicolas, Roberto and PeterSt for your time and effort to bring us to an understanding of those points. It appears it was much needed ! So basic, however previously unclear.

    So many years of using PRT and the point you’ve stressed here several times, Nicolas  -“ variables retain their value”-, was simply not in my mind or fairly unclear, probably because most of the variables in my indicators just change their value on each new bar. Not surprising that I’ve sometimes got some trouble with boolean variables, from one bar to the next ! 

    Ok, we’ve got it now ; hope it will also be useful to others.

    #189537 quote
    robertogozzi
    Moderator
    Master

    No array elements retain their value, unlike normal variables do

    NO, that’s incorrect. As I wrote, “No, array elements keep any retained value until you change it purposely, like any variable“. I can’t see how semantics could be of any help, just once you have assigned a value to an array element that value will ALWAYS be retained, bar after bar, untill you purposely change it, just like any variable. That’s all.

    #189549 quote
    PeterSt
    Participant
    Master

    NO, that’s incorrect.

    OK, you want to continue. Yes Roberto, I know that by now. Why repeat something which was put behind us ? please delete your own last post and this one from me. You are additionally confusing, IMHO.

    Yes, you are right too. Happy ? sure you are.
    Now you can delete your post. 🙂

    #196458 quote
    Khaled
    Participant
    Veteran

    Hello Nicolas , do you know why the above indicator from post #118817 (Example #1: support and resistance example, based on fractals points) displays as follows on PRT v11? (SP500 in 500 ticks, history 500 UT) Thanks

    Capture-décran-2022-06-30-à-12.44.40.png Capture-décran-2022-06-30-à-12.44.40.png
    #196477 quote
    Nicolas
    Keymaster
    Master

    To vary the "percent" variable, 0.5% of the price in 500 ticks is enormous.

    #196482 quote
    Khaled
    Participant
    Veteran

    Thanks Nicholas!

    #205126 quote
    partyhead
    Participant
    Average

    Hello . Is it possible to assign array from another ?

    For example i have day array with some values , week and  month  –  $Dx , $Wx , $Mx

    After  islastbarupdate  in  i want to populate array with  another

    IF sDay THEN
    $Final = $Dx
    ELSIF sWeek THEN
    $Final =$Wx
    ELSIF sMonth THEN  etc .

    Later draw lines   using  $Final  array ?

    Or only for .. next   loop ?

    Thank you

    #205129 quote
    Nicolas
    Keymaster
    Master

    Sorry there is no array duplicate, you have to make a loop to copy each row of an array into another one, like you suggested.

    partyhead and davidguerreir thanked this post
Viewing 15 posts - 181 through 195 (of 255 total)
  • You must be logged in to reply to this topic.

array variables availability in ProRealTime – examples and discussions


ProBuilder: Indicators & Custom Tools

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, 4 months ago.

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